-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathIncludesFirst.ql
More file actions
40 lines (36 loc) · 969 Bytes
/
IncludesFirst.ql
File metadata and controls
40 lines (36 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @name Misplaced include
* @description #include directives in a file shall only be preceded by other preprocessor directives or comments.
* @kind problem
* @id cpp/jpl-c/includes-first
* @problem.severity recommendation
* @tags maintainability
* readability
* external/jpl
*/
import cpp
int firstCodeLine(File f) {
result =
min(Declaration d, Location l, int toMin |
(
l = d.getLocation() and
l.getFile() = f and
not d.isInMacroExpansion()
) and
toMin = l.getStartLine()
|
toMin
)
}
int badIncludeLine(File f, Include i) {
result = i.getLocation().getStartLine() and
result > firstCodeLine(f) and
f = i.getFile()
}
from File f, Include i, int line
where
line = badIncludeLine(f, i) and
line = min(badIncludeLine(f, _))
select i,
"'" + i.toString() + "' is preceded by code -- it should be moved above line " + firstCodeLine(f) +
" in " + f.getBaseName() + "."