Skip to content

Commit f5437d2

Browse files
committed
Add an "Includes" library for detecting conditional includes
1 parent 2a805c0 commit f5437d2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** A library which supports analysis of includes. */
2+
3+
import cpp
4+
import semmle.code.cpp.headers.MultipleInclusion
5+
6+
/**
7+
* Holds if `include` is included conditionally based on the branch directive `b1`.
8+
*/
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
18+
)
19+
}
20+
21+
/**
22+
* Gets a file which is directly included from `fromFile` unconditionally.
23+
*/
24+
File getAnUnconditionallyIncludedFile(File fromFile) {
25+
// Find an include which isn't conditional
26+
exists(Include i |
27+
i.getFile() = fromFile and
28+
not conditionallyIncluded(_, i) and
29+
result = i.getIncludedFile()
30+
)
31+
}

0 commit comments

Comments
 (0)