Skip to content

Commit 9a81ce8

Browse files
committed
C++: Separate int and float metrics
1 parent 8d2cef6 commit 9a81ce8

File tree

4 files changed

+59
-61
lines changed

4 files changed

+59
-61
lines changed

cpp/ql/src/Telemetry/Metrics.qll

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import Diagnostics
77
abstract class Metric extends string {
88
bindingset[this]
99
Metric() { any() }
10-
11-
/** Gets the value of this metric. */
12-
abstract float getValue();
1310
}
1411

1512
/**
@@ -18,6 +15,9 @@ abstract class Metric extends string {
1815
abstract class ExtractionMetric extends Metric {
1916
bindingset[this]
2017
ExtractionMetric() { any() }
18+
19+
/** Gets the value of this metric. */
20+
abstract int getValue();
2121
}
2222

2323
/**
@@ -54,9 +54,9 @@ class QualityMetric extends Metric {
5454
base_metric = relative_metric.getBaseline() and this = "Percentage of " + relative_metric
5555
}
5656

57-
override float getValue() {
57+
float getValue() {
5858
base_metric.getValue() > 0 and
59-
result = 100 * relative_metric.getValue() / base_metric.getValue()
59+
result = 100.0 * relative_metric.getValue() / base_metric.getValue()
6060
}
6161
}
6262

@@ -65,19 +65,19 @@ module CppMetrics {
6565
class CompilationUnits extends BaseMetric {
6666
CompilationUnits() { this = "compilation units" }
6767

68-
override float getValue() { result = count(Compilation c) }
68+
override int getValue() { result = count(Compilation c) }
6969
}
7070

7171
class SourceFiles extends BaseMetric {
7272
SourceFiles() { this = "source files" }
7373

74-
override float getValue() { result = count(File f | f.fromSource()) }
74+
override int getValue() { result = count(File f | f.fromSource()) }
7575
}
7676

7777
class SourceFilesWithoutErrors extends SuccessMetric {
7878
SourceFilesWithoutErrors() { this = "source files without errors" }
7979

80-
override float getValue() {
80+
override int getValue() {
8181
result = count(File f | f.fromSource() and not exists(CompilerError e | f = e.getFile()))
8282
}
8383

@@ -87,7 +87,7 @@ module CppMetrics {
8787
class CompilationUnitsWithoutErrors extends SuccessMetric {
8888
CompilationUnitsWithoutErrors() { this = "compilation units without errors" }
8989

90-
override float getValue() {
90+
override int getValue() {
9191
result = count(Compilation c | not exists(Diagnostic d | d.getFile() = c.getAFileCompiled()))
9292
}
9393

@@ -97,35 +97,35 @@ module CppMetrics {
9797
class Expressions extends BaseMetric {
9898
Expressions() { this = "expressions" }
9999

100-
override float getValue() { result = count(Expr e) }
100+
override int getValue() { result = count(Expr e) }
101101
}
102102

103103
class SucceededExpressions extends SuccessMetric {
104104
SucceededExpressions() { this = "non-error expressions" }
105105

106-
override float getValue() { result = count(Expr e) - count(ErrorExpr e) }
106+
override int getValue() { result = count(Expr e) - count(ErrorExpr e) }
107107

108108
override Expressions getBaseline() { any() }
109109
}
110110

111111
class TypedExpressions extends SuccessMetric {
112112
TypedExpressions() { this = "expressions with a known type" }
113113

114-
override float getValue() { result = count(Expr e | not e.getType() instanceof ErroneousType) }
114+
override int getValue() { result = count(Expr e | not e.getType() instanceof ErroneousType) }
115115

116116
override Expressions getBaseline() { any() }
117117
}
118118

119119
class Calls extends BaseMetric {
120120
Calls() { this = "calls" }
121121

122-
override float getValue() { result = count(Call c) }
122+
override int getValue() { result = count(Call c) }
123123
}
124124

125125
class SucceededCalls extends SuccessMetric {
126126
SucceededCalls() { this = "calls with a target" }
127127

128-
override float getValue() {
128+
override int getValue() {
129129
result = count(Call c | not c.getTarget().getADeclarationEntry().isImplicit())
130130
}
131131

@@ -135,13 +135,13 @@ module CppMetrics {
135135
class Variables extends BaseMetric {
136136
Variables() { this = "variables" }
137137

138-
override float getValue() { result = count(Variable v) }
138+
override int getValue() { result = count(Variable v) }
139139
}
140140

141141
class VariablesKnownType extends SuccessMetric {
142142
VariablesKnownType() { this = "variables with a known type" }
143143

144-
override float getValue() {
144+
override int getValue() {
145145
result = count(Variable v | not v.getType() instanceof ErroneousType)
146146
}
147147

@@ -151,13 +151,13 @@ module CppMetrics {
151151
class LinesOfText extends BaseMetric {
152152
LinesOfText() { this = "lines of text" }
153153

154-
override float getValue() { result = sum(File f | | f.getMetrics().getNumberOfLines()) }
154+
override int getValue() { result = sum(File f | | f.getMetrics().getNumberOfLines()) }
155155
}
156156

157157
class LinesOfCode extends BaseMetric {
158158
LinesOfCode() { this = "lines of code" }
159159

160-
override float getValue() { result = sum(File f | | f.getMetrics().getNumberOfLinesOfCode()) }
160+
override int getValue() { result = sum(File f | | f.getMetrics().getNumberOfLinesOfCode()) }
161161
}
162162

163163
private predicate errorLine(File file, int line) {
@@ -175,7 +175,7 @@ module CppMetrics {
175175
class SucceededLines extends SuccessMetric {
176176
SucceededLines() { this = "lines of code without errors" }
177177

178-
override float getValue() {
178+
override int getValue() {
179179
result =
180180
sum(File f | | f.getMetrics().getNumberOfLinesOfCode()) -
181181
count(File file, int line | errorLine(file, line))
@@ -187,27 +187,27 @@ module CppMetrics {
187187
class Functions extends BaseMetric {
188188
Functions() { this = "functions" }
189189

190-
override float getValue() { result = count(Function f) }
190+
override int getValue() { result = count(Function f) }
191191
}
192192

193193
class SucceededFunctions extends SuccessMetric {
194194
SucceededFunctions() { this = "functions without errors" }
195195

196-
override float getValue() { result = count(Function f | not f.hasErrors()) }
196+
override int getValue() { result = count(Function f | not f.hasErrors()) }
197197

198198
override Functions getBaseline() { any() }
199199
}
200200

201201
class Includes extends BaseMetric {
202202
Includes() { this = "#include directives" }
203203

204-
override float getValue() { result = count(Include i) + count(CannotOpenFile e) }
204+
override int getValue() { result = count(Include i) + count(CannotOpenFile e) }
205205
}
206206

207207
class SucceededIncludes extends SuccessMetric {
208208
SucceededIncludes() { this = "successfully resolved #include directives" }
209209

210-
override float getValue() { result = count(Include i) }
210+
override int getValue() { result = count(Include i) }
211211

212212
override Includes getBaseline() { any() }
213213
}
@@ -223,7 +223,7 @@ module CppMetrics {
223223
this = "Successfully included " + include_text
224224
}
225225

226-
override float getValue() { result = count(Include i | i.getIncludeText() = include_text) }
226+
int getValue() { result = count(Include i | i.getIncludeText() = include_text) }
227227

228228
string getIncludeText() { result = include_text }
229229
}
@@ -236,28 +236,26 @@ module CppMetrics {
236236
this = "Failed to include '" + include_text + "'"
237237
}
238238

239-
override float getValue() {
240-
result = count(CannotOpenFile e | e.getIncludedFile() = include_text)
241-
}
239+
int getValue() { result = count(CannotOpenFile e | e.getIncludedFile() = include_text) }
242240

243241
string getIncludeText() { result = include_text }
244242
}
245243

246244
class CompilerErrors extends ExtractionMetric {
247245
CompilerErrors() { this = "compiler errors" }
248246

249-
override float getValue() { result = count(CompilerError e) }
247+
override int getValue() { result = count(CompilerError e) }
250248
}
251249

252250
class ErrorCount extends Metric {
253251
ErrorCount() { exists(CompilerError e | e.getMessage() = this) }
254252

255-
override float getValue() { result = count(CompilerError e | e.getMessage() = this) }
253+
int getValue() { result = count(CompilerError e | e.getMessage() = this) }
256254
}
257255

258256
class SyntaxErrorCount extends ExtractionMetric {
259257
SyntaxErrorCount() { this = "syntax errors" }
260258

261-
override float getValue() { result = count(SyntaxError e) }
259+
override int getValue() { result = count(SyntaxError e) }
262260
}
263261
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
| 'this' may only be used inside a nonstatic member function | 1.0 |
2-
| There was an error during this compilation | 1.0 |
3-
| expected a ')' | 1.0 |
4-
| expected a ';' | 1.0 |
5-
| expected an expression | 1.0 |
6-
| identifier 'no_such_function' is undefined | 1.0 |
7-
| identifier 'nsf2' is undefined | 1.0 |
8-
| identifier 'so_is_this' is undefined | 1.0 |
9-
| identifier 'uint32_t' is undefined | 1.0 |
10-
| too few arguments in function call | 1.0 |
1+
| 'this' may only be used inside a nonstatic member function | 1 |
2+
| There was an error during this compilation | 1 |
3+
| expected a ')' | 1 |
4+
| expected a ';' | 1 |
5+
| expected an expression | 1 |
6+
| identifier 'no_such_function' is undefined | 1 |
7+
| identifier 'nsf2' is undefined | 1 |
8+
| identifier 'so_is_this' is undefined | 1 |
9+
| identifier 'uint32_t' is undefined | 1 |
10+
| too few arguments in function call | 1 |
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
| #include directives | 2.0 |
2-
| calls | 2.0 |
3-
| calls with a target | 1.0 |
4-
| compilation units | 2.0 |
5-
| compilation units without errors | 1.0 |
6-
| compiler errors | 10.0 |
7-
| expressions | 10.0 |
8-
| expressions with a known type | 3.0 |
9-
| functions | 8.0 |
10-
| functions without errors | 6.0 |
11-
| lines of code | 19.0 |
12-
| lines of code without errors | 12.0 |
13-
| lines of text | 24.0 |
14-
| non-error expressions | 3.0 |
15-
| source files | 3.0 |
16-
| source files without errors | 2.0 |
17-
| successfully resolved #include directives | 2.0 |
18-
| syntax errors | 3.0 |
19-
| variables | 10.0 |
20-
| variables with a known type | 9.0 |
1+
| #include directives | 2 |
2+
| calls | 2 |
3+
| calls with a target | 1 |
4+
| compilation units | 2 |
5+
| compilation units without errors | 1 |
6+
| compiler errors | 10 |
7+
| expressions | 10 |
8+
| expressions with a known type | 3 |
9+
| functions | 8 |
10+
| functions without errors | 6 |
11+
| lines of code | 19 |
12+
| lines of code without errors | 12 |
13+
| lines of text | 24 |
14+
| non-error expressions | 3 |
15+
| source files | 3 |
16+
| source files without errors | 2 |
17+
| successfully resolved #include directives | 2 |
18+
| syntax errors | 3 |
19+
| variables | 10 |
20+
| variables with a known type | 9 |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| "test.h" | 2.0 |
1+
| "test.h" | 2 |

0 commit comments

Comments
 (0)