Skip to content

Commit f381768

Browse files
committed
CPP: Create HeaderFile.noTopLevelCode from existing logic.
1 parent 9e6b178 commit f381768

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

cpp/ql/src/jsf/4.07 Header Files/AV Rule 35.ql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ where not hf instanceof IncludeGuardedHeader
133133
// Exclude files which contain no declaration entries or top level
134134
// declarations (e.g. just preprocessor directives; or non-top level
135135
// code).
136-
and (
137-
exists(DeclarationEntry de | de.getFile() = hf) or
138-
exists(Declaration d | d.getFile() = hf and d.isTopLevel()) or
139-
exists(UsingEntry ue | ue.getFile() = hf)
140-
)
136+
and not hf.noTopLevelCode()
141137
// Exclude files which look like they contain 'x-macros'
142138
and not hasXMacro(hf)
143139
// Exclude files which are always #imported.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,20 @@ class HeaderFile extends File {
453453
exists(Include i | i.getIncludedFile() = this)
454454
}
455455

456+
/**
457+
* Holds if this header file does not contain any declaration entries or top level
458+
* declarations. For example it might be:
459+
* - a file containing only preprocessor directives and/or comments
460+
* - an empty file
461+
* - a file that contains non-top level code or data that's included in an
462+
* unusual way
463+
*/
464+
predicate noTopLevelCode()
465+
{
466+
not exists(DeclarationEntry de | de.getFile() = this) and
467+
not exists(Declaration d | d.getFile() = this and d.isTopLevel()) and
468+
not exists(UsingEntry ue | ue.getFile() = this)
469+
}
456470
}
457471

458472
/**

0 commit comments

Comments
 (0)