-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathPreprocBlock.qll
More file actions
161 lines (144 loc) · 5.07 KB
/
PreprocBlock.qll
File metadata and controls
161 lines (144 loc) · 5.07 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* This library offers a view of preprocessor branches (`#if`, `#ifdef`,
* `#ifndef`, `#elif`, `#elifdef`, `#elifndef`, and `#else`) as blocks of
* code between the opening and closing directives, with navigable
* parent-child relationships to other blocks. The main class is
* `PreprocessorBlock`.
*/
import cpp
/**
* Gets the line of the `ix`th `PreprocessorBranchDirective` in file `f`.
*/
private int getPreprocLineFromIndex(File f, int ix) {
result =
rank[ix](PreprocessorBranchDirective g | g.getFile() = f | g.getLocation().getStartLine())
}
/**
* Gets the `ix`th `PreprocessorBranchDirective` in file `f`.
*/
private PreprocessorBranchDirective getPreprocFromIndex(File f, int ix) {
result.getFile() = f and
result.getLocation().getStartLine() = getPreprocLineFromIndex(f, ix)
}
/**
* Get the index of a `PreprocessorBranchDirective` in its `file`.
*/
private int getPreprocIndex(PreprocessorBranchDirective directive) {
directive = getPreprocFromIndex(directive.getFile(), result)
}
/**
* A chunk of code from one preprocessor branch (`#if`, `#ifdef`,
* `#ifndef`, `#elif`, `#elifdef`, `#elifndef`, or `#else`) to the
* directive that closes it (`#elif`, `#elifdef`, `#elifndef`, `#else`,
* or `#endif`). The `getParent()` method allows these blocks to be
* navigated as a tree, with the root being the entire file.
*/
class PreprocessorBlock extends @element {
PreprocessorBlock() {
mkElement(this) instanceof File or
mkElement(this) instanceof PreprocessorBranch or
mkElement(this) instanceof PreprocessorElse
}
/**
* Holds if this element is at the specified location.
* The location spans column `startcolumn` of line `startline` to
* column `endcolumn` of line `endline` in file `filepath`.
* For more information, see
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
*/
predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
filepath = this.getFile().toString() and
startline = this.getStartLine() and
startcolumn = 0 and
endline = this.getEndLine() and
endcolumn = 0
}
/**
* Gets a textual representation of this element.
*/
string toString() { result = mkElement(this).toString() }
/**
* Gets the file this `PreprocessorBlock` is located in.
*/
File getFile() { result = mkElement(this).getFile() }
/**
* Gets the start line number of this `PreprocessorBlock`.
*/
int getStartLine() { result = mkElement(this).getLocation().getStartLine() }
/**
* Gets the end line number of this `PreprocessorBlock`.
*/
int getEndLine() {
result = mkElement(this).(File).getMetrics().getNumberOfLines() or
result =
mkElement(this).(PreprocessorBranchDirective).getNext().getLocation().getStartLine() - 1
}
private PreprocessorBlock getParentInternal() {
// find the `#ifdef` corresponding to this block and the
// PreprocessorBranchDirective `prev` that came directly
// before it in the source.
exists(int ix, PreprocessorBranchDirective prev |
ix = getPreprocIndex(mkElement(this).(PreprocessorBranchDirective).getIf()) and
prev = getPreprocFromIndex(this.getFile(), ix - 1)
|
if prev instanceof PreprocessorEndif
then
// if we follow an #endif, we have the same parent
// as its corresponding `#if` has.
result = unresolveElement(prev.getIf()).(PreprocessorBlock).getParentInternal()
else
// otherwise we directly follow an #if / #ifdef / #ifndef /
// #elif / #else that must be a level above and our parent
// block.
mkElement(result) = prev
)
}
/**
* Gets the `PreprocessorBlock` that's directly surrounding this one.
* Has no result if this is a file.
*/
PreprocessorBlock getParent() {
not mkElement(this) instanceof File and
(
if exists(this.getParentInternal())
then
// found parent directive
result = this.getParentInternal()
else
// top level directive
mkElement(result) = this.getFile()
)
}
/**
* Gets a `PreprocessorBlock` that's directly inside this one.
*/
PreprocessorBlock getAChild() { result.getParent() = this }
private Include getAnEnclosedInclude() {
result.getFile() = this.getFile() and
result.getLocation().getStartLine() > this.getStartLine() and
result.getLocation().getStartLine() <= this.getEndLine()
}
/**
* Gets an include directive that is directly in this
* `PreprocessorBlock`.
*/
Include getAnInclude() {
result = this.getAnEnclosedInclude() and
not result = this.getAChild().getAnEnclosedInclude()
}
private Macro getAnEnclosedMacro() {
result.getFile() = this.getFile() and
result.getLocation().getStartLine() > this.getStartLine() and
result.getLocation().getStartLine() <= this.getEndLine()
}
/**
* Gets a macro definition that is directly in this
* `PreprocessorBlock`.
*/
Macro getAMacro() {
result = this.getAnEnclosedMacro() and
not result = this.getAChild().getAnEnclosedMacro()
}
}