-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathvariables.ql
64 lines (50 loc) · 2.18 KB
/
variables.ql
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
import rust
import utils.test.InlineExpectationsTest
import codeql.rust.elements.internal.VariableImpl::Impl as VariableImpl
query predicate variable(Variable v) { any() }
query predicate variableAccess(VariableAccess va, Variable v) { v = va.getVariable() }
query predicate variableWriteAccess(VariableWriteAccess va, Variable v) { v = va.getVariable() }
query predicate variableReadAccess(VariableReadAccess va, Variable v) { v = va.getVariable() }
query predicate variableInitializer(Variable v, Expr e) { e = v.getInitializer() }
query predicate capturedVariable(Variable v) { v.isCaptured() }
query predicate capturedAccess(VariableAccess va) { va.isCapture() }
query predicate nestedFunctionAccess(VariableImpl::NestedFunctionAccess nfa, Function f) {
f = nfa.getFunction()
}
module VariableAccessTest implements TestSig {
string getARelevantTag() { result = ["", "write_", "read_"] + "access" }
private predicate declAt(Variable v, string filepath, int line, boolean inMacro) {
v.getLocation().hasLocationInfo(filepath, _, _, line, _) and
if v.getPat().isInMacroExpansion() then inMacro = true else inMacro = false
}
private predicate commmentAt(string text, string filepath, int line) {
exists(Comment c |
c.getLocation().hasLocationInfo(filepath, line, _, _, _) and
c.getCommentText().trim() = text
)
}
private predicate decl(Variable v, string value) {
exists(string filepath, int line, boolean inMacro | declAt(v, filepath, line, inMacro) |
commmentAt(value, filepath, line) and inMacro = false
or
not (commmentAt(_, filepath, line) and inMacro = false) and
value = v.getText()
)
}
predicate hasActualResult(Location location, string element, string tag, string value) {
exists(VariableAccess va |
location = va.getLocation() and
element = va.toString() and
decl(va.getVariable(), value)
|
va instanceof VariableWriteAccess and tag = "write_access"
or
va instanceof VariableReadAccess and tag = "read_access"
or
not va instanceof VariableWriteAccess and
not va instanceof VariableReadAccess and
tag = "access"
)
}
}
import MakeTest<VariableAccessTest>