|
| 1 | +import cpp |
| 2 | +import Diagnostics |
| 3 | + |
| 4 | +/** |
| 5 | + * A metric is a string with a value. |
| 6 | + */ |
| 7 | +abstract class Metric extends string { |
| 8 | + bindingset[this] |
| 9 | + Metric() { any() } |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * A metric that we want to report in cpp/telemetry/extraction-metrics |
| 14 | + */ |
| 15 | +abstract class ExtractionMetric extends Metric { |
| 16 | + bindingset[this] |
| 17 | + ExtractionMetric() { any() } |
| 18 | + |
| 19 | + /** Gets the value of this metric. */ |
| 20 | + abstract int getValue(); |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * A metric that provides a baseline for a SuccessMetric. |
| 25 | + */ |
| 26 | +abstract class BaseMetric extends ExtractionMetric { |
| 27 | + bindingset[this] |
| 28 | + BaseMetric() { any() } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * A metric that is relative to another metric, |
| 33 | + * so can be used to calculate percentages. |
| 34 | + * |
| 35 | + * For clarity, metrics should express success, |
| 36 | + * so higher values means better. |
| 37 | + */ |
| 38 | +abstract class SuccessMetric extends ExtractionMetric { |
| 39 | + bindingset[this] |
| 40 | + SuccessMetric() { any() } |
| 41 | + |
| 42 | + /** Gets the metric this is relative to. */ |
| 43 | + abstract BaseMetric getBaseline(); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * A metric used to report database quality. |
| 48 | + */ |
| 49 | +class QualityMetric extends Metric { |
| 50 | + BaseMetric baseMetric; |
| 51 | + SuccessMetric relativeMetric; |
| 52 | + |
| 53 | + QualityMetric() { |
| 54 | + baseMetric = relativeMetric.getBaseline() and this = "Percentage of " + relativeMetric |
| 55 | + } |
| 56 | + |
| 57 | + float getValue() { |
| 58 | + baseMetric.getValue() > 0 and |
| 59 | + result = 100.0 * relativeMetric.getValue() / baseMetric.getValue() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +signature class RankedMetric extends Metric { |
| 64 | + int getValue(); |
| 65 | +} |
| 66 | + |
| 67 | +module RankMetric<RankedMetric M> { |
| 68 | + int getRank(M s) { s = rank[result](M m | | m order by m.getValue() desc) } |
| 69 | +} |
| 70 | + |
| 71 | +/** Various metrics we want to report. */ |
| 72 | +module CppMetrics { |
| 73 | + class Compilations extends BaseMetric { |
| 74 | + Compilations() { this = "compilations" } |
| 75 | + |
| 76 | + override int getValue() { result = count(Compilation c) } |
| 77 | + } |
| 78 | + |
| 79 | + class SourceAndHeaderFiles extends BaseMetric { |
| 80 | + SourceAndHeaderFiles() { this = "source/header files" } |
| 81 | + |
| 82 | + override int getValue() { result = count(File f | f.fromSource()) } |
| 83 | + } |
| 84 | + |
| 85 | + class SourceAndHeaderFilesWithoutErrors extends SuccessMetric { |
| 86 | + SourceAndHeaderFilesWithoutErrors() { this = "source/header files without errors" } |
| 87 | + |
| 88 | + override int getValue() { |
| 89 | + result = count(File f | f.fromSource() and not exists(CompilerError e | f = e.getFile())) |
| 90 | + } |
| 91 | + |
| 92 | + override SourceAndHeaderFiles getBaseline() { any() } |
| 93 | + } |
| 94 | + |
| 95 | + class CompilationsWithoutErrors extends SuccessMetric { |
| 96 | + CompilationsWithoutErrors() { this = "compilations without errors" } |
| 97 | + |
| 98 | + override int getValue() { |
| 99 | + result = count(Compilation c | not exists(Diagnostic d | d.getFile() = c.getAFileCompiled())) |
| 100 | + } |
| 101 | + |
| 102 | + override Compilations getBaseline() { any() } |
| 103 | + } |
| 104 | + |
| 105 | + class Expressions extends BaseMetric { |
| 106 | + Expressions() { this = "expressions" } |
| 107 | + |
| 108 | + override int getValue() { result = count(Expr e) } |
| 109 | + } |
| 110 | + |
| 111 | + class SucceededExpressions extends SuccessMetric { |
| 112 | + SucceededExpressions() { this = "non-error expressions" } |
| 113 | + |
| 114 | + override int getValue() { result = count(Expr e) - count(ErrorExpr e) } |
| 115 | + |
| 116 | + override Expressions getBaseline() { any() } |
| 117 | + } |
| 118 | + |
| 119 | + class TypedExpressions extends SuccessMetric { |
| 120 | + TypedExpressions() { this = "expressions with a known type" } |
| 121 | + |
| 122 | + override int getValue() { result = count(Expr e | not e.getType() instanceof ErroneousType) } |
| 123 | + |
| 124 | + override Expressions getBaseline() { any() } |
| 125 | + } |
| 126 | + |
| 127 | + class Calls extends BaseMetric { |
| 128 | + Calls() { this = "calls" } |
| 129 | + |
| 130 | + override int getValue() { result = count(Call c) } |
| 131 | + } |
| 132 | + |
| 133 | + class CallsWithExplicitTarget extends SuccessMetric { |
| 134 | + CallsWithExplicitTarget() { this = "calls with an explicit target" } |
| 135 | + |
| 136 | + override int getValue() { |
| 137 | + result = count(Call c | not c.getTarget().getADeclarationEntry().isImplicit()) |
| 138 | + } |
| 139 | + |
| 140 | + override Calls getBaseline() { any() } |
| 141 | + } |
| 142 | + |
| 143 | + class Variables extends BaseMetric { |
| 144 | + Variables() { this = "variables" } |
| 145 | + |
| 146 | + override int getValue() { result = count(Variable v) } |
| 147 | + } |
| 148 | + |
| 149 | + class VariablesKnownType extends SuccessMetric { |
| 150 | + VariablesKnownType() { this = "variables with a known type" } |
| 151 | + |
| 152 | + override int getValue() { |
| 153 | + result = count(Variable v | not v.getType() instanceof ErroneousType) |
| 154 | + } |
| 155 | + |
| 156 | + override Variables getBaseline() { any() } |
| 157 | + } |
| 158 | + |
| 159 | + class LinesOfText extends BaseMetric { |
| 160 | + LinesOfText() { this = "lines of text" } |
| 161 | + |
| 162 | + override int getValue() { result = sum(File f | | f.getMetrics().getNumberOfLines()) } |
| 163 | + } |
| 164 | + |
| 165 | + class LinesOfCode extends BaseMetric { |
| 166 | + LinesOfCode() { this = "lines of code" } |
| 167 | + |
| 168 | + override int getValue() { result = sum(File f | | f.getMetrics().getNumberOfLinesOfCode()) } |
| 169 | + } |
| 170 | + |
| 171 | + private predicate errorLine(File file, int line) { |
| 172 | + exists(Locatable l, Location loc | |
| 173 | + loc = l.getLocation() and |
| 174 | + loc.getFile() = file and |
| 175 | + line in [loc.getStartLine() .. loc.getEndLine()] |
| 176 | + | |
| 177 | + l instanceof Diagnostic |
| 178 | + or |
| 179 | + l instanceof ErrorExpr |
| 180 | + ) |
| 181 | + } |
| 182 | + |
| 183 | + class SucceededLines extends SuccessMetric { |
| 184 | + SucceededLines() { this = "lines of code without errors" } |
| 185 | + |
| 186 | + override int getValue() { |
| 187 | + result = |
| 188 | + sum(File f | | f.getMetrics().getNumberOfLinesOfCode()) - |
| 189 | + count(File f, int line | errorLine(f, line)) |
| 190 | + } |
| 191 | + |
| 192 | + override LinesOfCode getBaseline() { any() } |
| 193 | + } |
| 194 | + |
| 195 | + class Functions extends BaseMetric { |
| 196 | + Functions() { this = "functions" } |
| 197 | + |
| 198 | + override int getValue() { result = count(Function f) } |
| 199 | + } |
| 200 | + |
| 201 | + class SucceededFunctions extends SuccessMetric { |
| 202 | + SucceededFunctions() { this = "functions without errors" } |
| 203 | + |
| 204 | + override int getValue() { result = count(Function f | not f.hasErrors()) } |
| 205 | + |
| 206 | + override Functions getBaseline() { any() } |
| 207 | + } |
| 208 | + |
| 209 | + class Includes extends BaseMetric { |
| 210 | + Includes() { this = "#include directives" } |
| 211 | + |
| 212 | + override int getValue() { result = count(Include i) + count(CannotOpenFileError e) } |
| 213 | + } |
| 214 | + |
| 215 | + class SucceededIncludes extends SuccessMetric { |
| 216 | + SucceededIncludes() { this = "successfully resolved #include directives" } |
| 217 | + |
| 218 | + override int getValue() { result = count(Include i) } |
| 219 | + |
| 220 | + override Includes getBaseline() { any() } |
| 221 | + } |
| 222 | + |
| 223 | + class SucceededIncludeCount extends Metric { |
| 224 | + string includeText; |
| 225 | + |
| 226 | + SucceededIncludeCount() { |
| 227 | + exists(Include i | |
| 228 | + i.getIncludeText() = includeText and |
| 229 | + exists(i.getFile().getRelativePath()) // Only report includes from the repo |
| 230 | + ) and |
| 231 | + this = "Successfully included " + includeText |
| 232 | + } |
| 233 | + |
| 234 | + int getValue() { result = count(Include i | i.getIncludeText() = includeText) } |
| 235 | + |
| 236 | + string getIncludeText() { result = includeText } |
| 237 | + } |
| 238 | + |
| 239 | + class MissingIncludeCount extends Metric { |
| 240 | + string includeText; |
| 241 | + |
| 242 | + MissingIncludeCount() { |
| 243 | + exists(CannotOpenFileError e | e.getIncludedFile() = includeText) and |
| 244 | + this = "Failed to include '" + includeText + "'" |
| 245 | + } |
| 246 | + |
| 247 | + int getValue() { result = count(CannotOpenFileError e | e.getIncludedFile() = includeText) } |
| 248 | + |
| 249 | + string getIncludeText() { result = includeText } |
| 250 | + } |
| 251 | + |
| 252 | + class CompilerErrors extends ExtractionMetric { |
| 253 | + CompilerErrors() { this = "compiler errors" } |
| 254 | + |
| 255 | + override int getValue() { result = count(CompilerError e) } |
| 256 | + } |
| 257 | + |
| 258 | + class ErrorCount extends Metric { |
| 259 | + ErrorCount() { exists(CompilerError e | e.getMessage() = this) } |
| 260 | + |
| 261 | + int getValue() { result = count(CompilerError e | e.getMessage() = this) } |
| 262 | + } |
| 263 | + |
| 264 | + class SyntaxErrorCount extends ExtractionMetric { |
| 265 | + SyntaxErrorCount() { this = "syntax errors" } |
| 266 | + |
| 267 | + override int getValue() { result = count(SyntaxError e) } |
| 268 | + } |
| 269 | +} |
0 commit comments