C/C++: Add test for const folding of global vars

This commit is contained in:
idrissrio
2026-02-02 16:26:03 +01:00
parent fedb9464af
commit a40719b660
3 changed files with 42 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
#define NULL ((void*)0)
int global_var;
void test_address_null_comparison(int param_var) {
int local_var;
if (&global_var == NULL) {} // $ MISSING: VariableAddress=global_var
if (&global_var != NULL) {} // $ MISSING: VariableAddress=global_var
if (&global_var) {} // $ VariableAddress=global_var
if (!&global_var) {} // $ MISSING: VariableAddress=global_var
if (&local_var == NULL) {} // $ VariableAddress=local_var
if (&local_var != NULL) {} // $ VariableAddress=local_var
if (&local_var) {} // $ VariableAddress=local_var
if (!&local_var) {} // $ VariableAddress=local_var
if (&param_var == NULL) {} // $ VariableAddress=param_var
if (&param_var != NULL) {} // $ VariableAddress=param_var
if (&param_var) {} // $ VariableAddress=param_var
if (!&param_var) {} // $ VariableAddress=param_var
}

View File

@@ -0,0 +1,20 @@
import cpp
private import utils.test.InlineExpectationsTest
private import semmle.code.cpp.ir.IR
module VariableAddressTest implements TestSig {
string getARelevantTag() { result = "VariableAddress" }
predicate hasActualResult(Location location, string element, string tag, string value) {
exists(VariableAddressInstruction vai |
not vai.getAst() instanceof DeclarationEntry and
not vai.getAst() instanceof Parameter and
tag = "VariableAddress" and
value = vai.getAstVariable().getName() and
element = vai.toString() and
location = vai.getLocation()
)
}
}
import MakeTest<VariableAddressTest>