11/** A library which supports analysis of includes. */
22
33import cpp
4+ import codingstandards.cpp.PreprocessorDirective
45import 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}
0 commit comments