Skip to content

Commit 1543019

Browse files
committed
Includes: Improve performance
1 parent 650842b commit 1543019

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
/** A library which supports analysis of includes. */
22

33
import cpp
4+
import codingstandards.cpp.PreprocessorDirective
45
import semmle.code.cpp.headers.MultipleInclusion
56

7+
pragma[noinline]
8+
private predicate hasIncludeLocation(Include include, string filepath, int startline) {
9+
include.getLocation().hasLocationInfo(filepath, startline, _, _, _)
10+
}
11+
612
/**
713
* Holds if `include` is included conditionally based on the branch directive `b1`.
814
*/
9-
predicate conditionallyIncluded(PreprocessorBranchDirective b1, Include include) {
10-
exists(File f, int include1StartLine |
11-
not b1 = any(CorrectIncludeGuard c).getIfndef() and
12-
not b1.getHead().regexpMatch(".*_H(_.*)?") and
13-
include.getLocation().hasLocationInfo(f.getAbsolutePath(), include1StartLine, _, _, _) and
14-
f.getAbsolutePath() = b1.getFile().getAbsolutePath()
15-
|
16-
b1.getLocation().getStartLine() < include1StartLine and
17-
b1.getNext().getLocation().getStartLine() > include1StartLine
15+
pragma[noinline]
16+
predicate isConditionallyIncluded(PreprocessorBranchDirective bd, Include include) {
17+
not bd = any(CorrectIncludeGuard c).getIfndef() and
18+
not bd.getHead().regexpMatch(".*_H(_.*)?") and
19+
exists(string filepath, int startline, int endline, int includeline |
20+
isBranchDirectiveRange(bd, filepath, startline, endline) and
21+
hasIncludeLocation(include, filepath, includeline) and
22+
startline < includeline and
23+
endline > includeline
1824
)
1925
}
2026

@@ -25,7 +31,7 @@ File getAnUnconditionallyIncludedFile(File fromFile) {
2531
// Find an include which isn't conditional
2632
exists(Include i |
2733
i.getFile() = fromFile and
28-
not conditionallyIncluded(_, i) and
34+
not isConditionallyIncluded(_, i) and
2935
result = i.getIncludedFile()
3036
)
3137
}

cpp/common/src/codingstandards/cpp/PreprocessorDirective.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class PreprocessorIfOrElif extends PreprocessorBranch {
4444
/**
4545
* Holds if the preprocessor directive `m` is located at `filepath` and `startline`.
4646
*/
47-
private predicate hasPreprocessorLocation(PreprocessorDirective m, string filepath, int startline) {
47+
pragma[noinline]
48+
predicate hasPreprocessorLocation(PreprocessorDirective m, string filepath, int startline) {
4849
m.getLocation().hasLocationInfo(filepath, startline, _, _, _)
4950
}
5051

0 commit comments

Comments
 (0)