From e58a8330cd263ef0053f8f4fe58180fcdf2ffde2 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Fri, 23 Jan 2026 16:19:06 +0000 Subject: [PATCH] C++: Add test for asDefinition. --- .../dataflow/asDefinition/test.cpp | 18 +++++++++++++++ .../dataflow/asDefinition/test.expected | 0 .../dataflow/asDefinition/test.ql | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 cpp/ql/test/library-tests/dataflow/asDefinition/test.cpp create mode 100644 cpp/ql/test/library-tests/dataflow/asDefinition/test.expected create mode 100644 cpp/ql/test/library-tests/dataflow/asDefinition/test.ql diff --git a/cpp/ql/test/library-tests/dataflow/asDefinition/test.cpp b/cpp/ql/test/library-tests/dataflow/asDefinition/test.cpp new file mode 100644 index 00000000000..6af97c18b15 --- /dev/null +++ b/cpp/ql/test/library-tests/dataflow/asDefinition/test.cpp @@ -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="... = ..." +} \ No newline at end of file diff --git a/cpp/ql/test/library-tests/dataflow/asDefinition/test.expected b/cpp/ql/test/library-tests/dataflow/asDefinition/test.expected new file mode 100644 index 00000000000..e69de29bb2d diff --git a/cpp/ql/test/library-tests/dataflow/asDefinition/test.ql b/cpp/ql/test/library-tests/dataflow/asDefinition/test.ql new file mode 100644 index 00000000000..b996f47fb49 --- /dev/null +++ b/cpp/ql/test/library-tests/dataflow/asDefinition/test.ql @@ -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