Skip to content

Commit 127b759

Browse files
C++: Move a couple predicates into Exclusions.qll
1 parent f13fc42 commit 127b759

File tree

4 files changed

+62
-55
lines changed

4 files changed

+62
-55
lines changed

cpp/ql/src/Likely Bugs/Arithmetic/PointlessComparison.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* readability
1313
*/
1414
import cpp
15+
private import semmle.code.cpp.commons.Exclusions
1516
private import semmle.code.cpp.rangeanalysis.PointlessComparison
1617
private import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
1718
import UnsignedGEZero

cpp/ql/src/Likely Bugs/Likely Typos/ExprHasNoEffect.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* external/cwe/cwe-561
1212
*/
1313
import cpp
14+
private import semmle.code.cpp.commons.Exclusions
1415

1516
class PureExprInVoidContext extends ExprInVoidContext {
1617
PureExprInVoidContext() { this.isPure() }

cpp/ql/src/semmle/code/cpp/Preprocessor.qll

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -221,58 +221,3 @@ class PreprocessorPragma extends PreprocessorDirective, @ppd_pragma {
221221
class PreprocessorLine extends PreprocessorDirective, @ppd_line {
222222
override string toString() { result = "#line " + this.getHead() }
223223
}
224-
225-
/**
226-
* Holds if the preprocessor branch `pbd` is on line `pbdStartLine` in file `file`.
227-
*/
228-
private predicate pbdLocation(PreprocessorBranchDirective pbd, string file, int pbdStartLine) {
229-
pbd.getLocation().hasLocationInfo(file, pbdStartLine, _, _, _)
230-
}
231-
232-
/**
233-
* Holds if the body of the function `f` is on lines `fBlockStartLine` to `fBlockEndLine` in file `file`.
234-
*/
235-
private predicate functionLocation(Function f, string file, int fBlockStartLine, int fBlockEndLine) {
236-
f.getBlock().getLocation().hasLocationInfo(file, fBlockStartLine, _, fBlockEndLine, _)
237-
}
238-
239-
/**
240-
* Holds if the function `f` is inside a preprocessor branch that may have code in another arm.
241-
*/
242-
predicate functionDefinedInIfDef(Function f) {
243-
exists(PreprocessorBranchDirective pbd, string file, int pbdStartLine, int pbdEndLine, int fBlockStartLine,
244-
int fBlockEndLine |
245-
functionLocation(f, file, fBlockStartLine, fBlockEndLine) and
246-
pbdLocation(pbd, file, pbdStartLine) and
247-
pbdLocation(pbd.getNext(), file, pbdEndLine) and
248-
pbdStartLine <= fBlockStartLine and
249-
pbdEndLine >= fBlockEndLine and
250-
// pbd is a preprocessor branch where multiple branches exist
251-
(
252-
pbd.getNext() instanceof PreprocessorElse or
253-
pbd instanceof PreprocessorElse or
254-
pbd.getNext() instanceof PreprocessorElif or
255-
pbd instanceof PreprocessorElif
256-
)
257-
)
258-
}
259-
260-
/**
261-
* Holds if the function `f` contains code excluded by the preprocessor.
262-
*/
263-
predicate functionContainsDisabledCode(Function f) {
264-
// `f` contains a preprocessor branch that was not taken
265-
exists(PreprocessorBranchDirective pbd, string file, int pbdStartLine, int fBlockStartLine, int fBlockEndLine |
266-
functionLocation(f, file, fBlockStartLine, fBlockEndLine) and
267-
pbdLocation(pbd, file, pbdStartLine) and
268-
pbdStartLine <= fBlockEndLine and
269-
pbdStartLine >= fBlockStartLine and
270-
(
271-
pbd.(PreprocessorBranch).wasNotTaken() or
272-
273-
// an else either was not taken, or it's corresponding branch
274-
// was not taken.
275-
pbd instanceof PreprocessorElse
276-
)
277-
)
278-
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Common predicates used to exclude results from a query based on heuristics.
3+
*/
4+
5+
import cpp
6+
7+
/**
8+
* Holds if the preprocessor branch `pbd` is on line `pbdStartLine` in file `file`.
9+
*/
10+
private predicate pbdLocation(PreprocessorBranchDirective pbd, string file, int pbdStartLine) {
11+
pbd.getLocation().hasLocationInfo(file, pbdStartLine, _, _, _)
12+
}
13+
14+
/**
15+
* Holds if the body of the function `f` is on lines `fBlockStartLine` to `fBlockEndLine` in file `file`.
16+
*/
17+
private predicate functionLocation(Function f, string file, int fBlockStartLine, int fBlockEndLine) {
18+
f.getBlock().getLocation().hasLocationInfo(file, fBlockStartLine, _, fBlockEndLine, _)
19+
}
20+
21+
/**
22+
* Holds if the function `f` is inside a preprocessor branch that may have code in another arm.
23+
*/
24+
predicate functionDefinedInIfDef(Function f) {
25+
exists(PreprocessorBranchDirective pbd, string file, int pbdStartLine, int pbdEndLine, int fBlockStartLine,
26+
int fBlockEndLine |
27+
functionLocation(f, file, fBlockStartLine, fBlockEndLine) and
28+
pbdLocation(pbd, file, pbdStartLine) and
29+
pbdLocation(pbd.getNext(), file, pbdEndLine) and
30+
pbdStartLine <= fBlockStartLine and
31+
pbdEndLine >= fBlockEndLine and
32+
// pbd is a preprocessor branch where multiple branches exist
33+
(
34+
pbd.getNext() instanceof PreprocessorElse or
35+
pbd instanceof PreprocessorElse or
36+
pbd.getNext() instanceof PreprocessorElif or
37+
pbd instanceof PreprocessorElif
38+
)
39+
)
40+
}
41+
42+
/**
43+
* Holds if the function `f` contains code excluded by the preprocessor.
44+
*/
45+
predicate functionContainsDisabledCode(Function f) {
46+
// `f` contains a preprocessor branch that was not taken
47+
exists(PreprocessorBranchDirective pbd, string file, int pbdStartLine, int fBlockStartLine, int fBlockEndLine |
48+
functionLocation(f, file, fBlockStartLine, fBlockEndLine) and
49+
pbdLocation(pbd, file, pbdStartLine) and
50+
pbdStartLine <= fBlockEndLine and
51+
pbdStartLine >= fBlockStartLine and
52+
(
53+
pbd.(PreprocessorBranch).wasNotTaken() or
54+
55+
// an else either was not taken, or it's corresponding branch
56+
// was not taken.
57+
pbd instanceof PreprocessorElse
58+
)
59+
)
60+
}

0 commit comments

Comments
 (0)