C++: Add test for asDefinition.

This commit is contained in:
Mathias Vorreiter Pedersen
2026-01-23 16:19:06 +00:00
parent 949fc3745a
commit e58a8330cd
3 changed files with 40 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
struct S {
int x;
};
void use(int);
void test() {
int y = 43; // $ asDefinition=43
use(y);
y = 44; // $ asDefinition="... = ..."
use(y);
int x = 43; // $ MISSING: asDefinition=43
x = 44; // $ MISSING: asDefinition="... = ..."
S s;
s.x = 42; // $ MISSING: asDefinition="... = ..."
}

View File

@@ -0,0 +1,22 @@
import cpp
import utils.test.InlineExpectationsTest
import semmle.code.cpp.dataflow.new.DataFlow::DataFlow
bindingset[s]
string quote(string s) { if s.matches("% %") then result = "\"" + s + "\"" else result = s }
module AsDefinitionTest implements TestSig {
string getARelevantTag() { result = "asDefinition" }
predicate hasActualResult(Location location, string element, string tag, string value) {
exists(Node n, Expr e |
e = n.asDefinition() and
location = e.getLocation() and
element = n.toString() and
tag = "asDefinition" and
value = quote(e.toString())
)
}
}
import MakeTest<AsDefinitionTest>