From 5bef9f711895e14d15e93cedd8598af510008668 Mon Sep 17 00:00:00 2001 From: Nick Rolfe Date: Thu, 9 Aug 2018 16:58:48 +0100 Subject: [PATCH 1/2] C++: test for resolving specialisations dependent on template aliases --- .../dependent_template_alias/test.cpp | 37 +++++++++++++++++++ .../dependent_template_alias/test.expected | 2 + .../dependent_template_alias/test.ql | 6 +++ 3 files changed, 45 insertions(+) create mode 100644 cpp/ql/test/library-tests/templates/dependent_template_alias/test.cpp create mode 100644 cpp/ql/test/library-tests/templates/dependent_template_alias/test.expected create mode 100644 cpp/ql/test/library-tests/templates/dependent_template_alias/test.ql diff --git a/cpp/ql/test/library-tests/templates/dependent_template_alias/test.cpp b/cpp/ql/test/library-tests/templates/dependent_template_alias/test.cpp new file mode 100644 index 00000000000..06ff13df9f6 --- /dev/null +++ b/cpp/ql/test/library-tests/templates/dependent_template_alias/test.cpp @@ -0,0 +1,37 @@ + +template +using Z = int; + +template +struct Thing { + int x; +}; + +template +struct Thing> { + int y; +}; + +// Note that float::Undefined is an error, so this should match the primary +// template, not the partial specialization. +Thing thing_float; + +void f() { + // If we incorrectly matched the partial specialization, this write to x would + // be an error. + thing_float.x = 1; +} + +// Now, a type that actually does define Undefined +struct S { + using Undefined = int; +}; + +// S::Undefined is okay, so this should match the partial specialization. +Thing thing_s; + +void g() { + // If we incorrectly matched the primary template, this write to y would be an + // error. + thing_s.y = 1; +} diff --git a/cpp/ql/test/library-tests/templates/dependent_template_alias/test.expected b/cpp/ql/test/library-tests/templates/dependent_template_alias/test.expected new file mode 100644 index 00000000000..a1127fad4e3 --- /dev/null +++ b/cpp/ql/test/library-tests/templates/dependent_template_alias/test.expected @@ -0,0 +1,2 @@ +| test.cpp:17:14:17:24 | thing_float | test.cpp:6:8:6:12 | Thing | test.cpp:7:7:7:7 | x | +| test.cpp:31:10:31:16 | thing_s | test.cpp:11:8:11:41 | Thing | test.cpp:12:7:12:7 | y | diff --git a/cpp/ql/test/library-tests/templates/dependent_template_alias/test.ql b/cpp/ql/test/library-tests/templates/dependent_template_alias/test.ql new file mode 100644 index 00000000000..d61a33c822c --- /dev/null +++ b/cpp/ql/test/library-tests/templates/dependent_template_alias/test.ql @@ -0,0 +1,6 @@ +import cpp + +from Variable v, Class c +where c = v.getType() + and v.getFile().getBaseName() = "test.cpp" +select v, c, c.getAMemberVariable() From df1f51463f384089de2b7edbbba6ce4c471779b1 Mon Sep 17 00:00:00 2001 From: Nick Rolfe Date: Thu, 9 Aug 2018 17:00:13 +0100 Subject: [PATCH 2/2] C++: extend test to cover template aliases --- .../templates/typedefs/template_typedefs.cpp | 20 ++++++++++++++----- .../typedefs/template_typedefs.expected | 4 +++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cpp/ql/test/library-tests/templates/typedefs/template_typedefs.cpp b/cpp/ql/test/library-tests/templates/typedefs/template_typedefs.cpp index 6f40c242acc..a755fe1f824 100644 --- a/cpp/ql/test/library-tests/templates/typedefs/template_typedefs.cpp +++ b/cpp/ql/test/library-tests/templates/typedefs/template_typedefs.cpp @@ -12,10 +12,10 @@ void functions() { b(static_cast(0)); c(0); d(0); - + e(0); e(0); - + f(0); f(0); } @@ -27,6 +27,7 @@ template struct D {}; template struct E {}; template struct F {}; template struct G {}; +template struct H {}; struct S { int x; }; typedef S S_t; @@ -34,18 +35,27 @@ typedef S S_t; typedef C C1; typedef D D1; +template +using Z = TZ; + void types() { A* a; B* b; C1 c; D1 d; - + E e1; E e2; - + E> e3; + F f1; F f2; - + F> f3; + + H> h1; + H h2; + H h3; + G g1; G g2; G g3; diff --git a/cpp/ql/test/library-tests/templates/typedefs/template_typedefs.expected b/cpp/ql/test/library-tests/templates/typedefs/template_typedefs.expected index 23174bb3e9a..33e0f26fb74 100644 --- a/cpp/ql/test/library-tests/templates/typedefs/template_typedefs.expected +++ b/cpp/ql/test/library-tests/templates/typedefs/template_typedefs.expected @@ -12,12 +12,14 @@ | F | int | | G<..(*)(..)> | pointer to {function returning {pointer to {int}} with arguments (int)} | | G | TG | -| G<__attribute((vector_size(32))) int> | {GNU 8 element vector of {int}} | +| G<__attribute((vector_size(32))) int> | GNU 8 element vector of {int} | | G | const {pointer to {const {int}}} | | G | reference to {pointer to {pointer to {int}}} | | G | pointer to {int} | | G | pointer to member of S with type {int} | | G | reference to {array of 3 {int}} | +| H | TH | +| H | int | | a | Ta | | a | int | | b | Tb |