Skip to content

Commit a40719b

Browse files
committed
C/C++: Add test for const folding of global vars
1 parent fedb946 commit a40719b

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#define NULL ((void*)0)
2+
3+
int global_var;
4+
5+
void test_address_null_comparison(int param_var) {
6+
int local_var;
7+
8+
if (&global_var == NULL) {} // $ MISSING: VariableAddress=global_var
9+
if (&global_var != NULL) {} // $ MISSING: VariableAddress=global_var
10+
if (&global_var) {} // $ VariableAddress=global_var
11+
if (!&global_var) {} // $ MISSING: VariableAddress=global_var
12+
13+
if (&local_var == NULL) {} // $ VariableAddress=local_var
14+
if (&local_var != NULL) {} // $ VariableAddress=local_var
15+
if (&local_var) {} // $ VariableAddress=local_var
16+
if (!&local_var) {} // $ VariableAddress=local_var
17+
18+
if (&param_var == NULL) {} // $ VariableAddress=param_var
19+
if (&param_var != NULL) {} // $ VariableAddress=param_var
20+
if (&param_var) {} // $ VariableAddress=param_var
21+
if (!&param_var) {} // $ VariableAddress=param_var
22+
}

cpp/ql/test/library-tests/ir/address_constant_folding/variable_addresses.expected

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import cpp
2+
private import utils.test.InlineExpectationsTest
3+
private import semmle.code.cpp.ir.IR
4+
5+
module VariableAddressTest implements TestSig {
6+
string getARelevantTag() { result = "VariableAddress" }
7+
8+
predicate hasActualResult(Location location, string element, string tag, string value) {
9+
exists(VariableAddressInstruction vai |
10+
not vai.getAst() instanceof DeclarationEntry and
11+
not vai.getAst() instanceof Parameter and
12+
tag = "VariableAddress" and
13+
value = vai.getAstVariable().getName() and
14+
element = vai.toString() and
15+
location = vai.getLocation()
16+
)
17+
}
18+
}
19+
20+
import MakeTest<VariableAddressTest>

0 commit comments

Comments
 (0)