Skip to content

Commit 8fe2699

Browse files
authored
Merge pull request #19603 from github/idrissrio/comments-using
C++: Add support for getting literals in using declarations
2 parents 35691db + 10fb806 commit 8fe2699

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
4+
* Added a predicate `getReferencedMember` to `UsingDeclarationEntry`, which yields a member depending on a type template parameter.

cpp/ql/lib/semmle/code/cpp/Namespace.qll

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,27 @@ class UsingDeclarationEntry extends UsingEntry {
174174
*/
175175
Declaration getDeclaration() { usings(underlyingElement(this), unresolveElement(result), _, _) }
176176

177-
override string toString() { result = "using " + this.getDeclaration().getDescription() }
177+
/**
178+
* Gets the member that is referenced by this using declaration, where the member depends on a
179+
* type template parameter.
180+
*
181+
* For example:
182+
* ```
183+
* template <typename T>
184+
* class A {
185+
* using T::m;
186+
* };
187+
* ```
188+
* Here, `getReferencedMember()` yields the member `m` of `T`. Observe that,
189+
* as `T` is not instantiated, `m` is represented by a `Literal` and not
190+
* a `Declaration`.
191+
*/
192+
Literal getReferencedMember() { usings(underlyingElement(this), unresolveElement(result), _, _) }
193+
194+
override string toString() {
195+
result = "using " + this.getDeclaration().getDescription() or
196+
result = "using " + this.getReferencedMember()
197+
}
178198
}
179199

180200
/**

cpp/ql/test/library-tests/comments/binding/commentBinding.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99
| multi.c:5:27:5:36 | // Multi 3 | declaration of multi3 |
1010
| templates.cpp:3:3:3:8 | // Foo | declaration of foo |
1111
| templates.cpp:7:3:7:8 | // Bar | definition of bar |
12+
| templates.cpp:16:3:16:20 | // using T::member | using member |
13+
| templates.cpp:19:3:19:28 | // using T::nested::member | using member |
14+
| templates.cpp:25:3:25:20 | // using T::member | using member |

cpp/ql/test/library-tests/comments/binding/templates.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,18 @@ class Cl {
1010
}
1111
};
1212

13+
14+
template <typename T>
15+
class Derived : public T {
16+
// using T::member
17+
using T::member;
18+
19+
// using T::nested::member
20+
using T::nested::member;
21+
};
22+
23+
template <typename T>
24+
class Base {
25+
// using T::member
26+
using T::member;
27+
};

0 commit comments

Comments
 (0)