1
1
/** A library which supports analysis of includes. */
2
2
3
3
import cpp
4
+ import codingstandards.cpp.PreprocessorDirective
4
5
import semmle.code.cpp.headers.MultipleInclusion
5
6
7
+ pragma [ noinline]
8
+ private predicate hasIncludeLocation ( Include include , string filepath , int startline ) {
9
+ include .getLocation ( ) .hasLocationInfo ( filepath , startline , _, _, _)
10
+ }
11
+
6
12
/**
7
13
* Holds if `include` is included conditionally based on the branch directive `b1`.
8
14
*/
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
18
24
)
19
25
}
20
26
@@ -25,7 +31,7 @@ File getAnUnconditionallyIncludedFile(File fromFile) {
25
31
// Find an include which isn't conditional
26
32
exists ( Include i |
27
33
i .getFile ( ) = fromFile and
28
- not conditionallyIncluded ( _, i ) and
34
+ not isConditionallyIncluded ( _, i ) and
29
35
result = i .getIncludedFile ( )
30
36
)
31
37
}
0 commit comments