Skip to content

Commit 01a7a53

Browse files
authored
Merge pull request #18360 from github/jketema/template-parameters-3
C++: Support arguments and instantiations of template template parameters
2 parents 2e1e46c + 0942945 commit 01a7a53

File tree

12 files changed

+10259
-481
lines changed

12 files changed

+10259
-481
lines changed

cpp/downgrades/4813509d85b45ae17421c036905199f7324cf228/old.dbscheme

Lines changed: 2358 additions & 0 deletions
Large diffs are not rendered by default.

cpp/downgrades/4813509d85b45ae17421c036905199f7324cf228/semmlecode.cpp.dbscheme

Lines changed: 2343 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
description: Support template template parameter arguments and instantiations
2+
compatibility: full
3+
template_template_instantiation.rel: delete
4+
template_template_argument.rel: delete
5+
template_template_argument_value.rel: delete
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: feature
3+
---
4+
* A new class `TemplateTemplateParameterInstantiation` was introduced, which represents instantiations of template template parameters.
5+
* A new predicate `getAnInstantiation` was added to the `TemplateTemplateParameter` class, which yields instantiations of template template parameters.
6+
* The `getTemplateArgumentType` and `getTemplateArgumentValue` predicates of the `Declaration` class now also yield template arguments of template template parameters.

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,13 @@ class Class extends UserType {
570570
/**
571571
* Holds if this class, struct or union is constructed from another class as
572572
* a result of template instantiation. It originates either from a class
573-
* template or from a class nested in a class template.
573+
* template, a class nested in a class template, or a template template
574+
* parameter.
574575
*/
575-
predicate isConstructedFrom(Class c) {
576-
class_instantiation(underlyingElement(this), unresolveElement(c))
576+
predicate isConstructedFrom(UserType t) {
577+
class_instantiation(underlyingElement(this), unresolveElement(t))
578+
or
579+
template_template_instantiation(underlyingElement(this), unresolveElement(t))
577580
}
578581

579582
/**

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ class Declaration extends Locatable, @declaration {
277277
function_template_argument(underlyingElement(this), index, unresolveElement(result))
278278
or
279279
variable_template_argument(underlyingElement(this), index, unresolveElement(result))
280+
or
281+
template_template_argument(underlyingElement(this), index, unresolveElement(result))
280282
}
281283

282284
private Expr getTemplateArgumentValue(int index) {
@@ -285,6 +287,8 @@ class Declaration extends Locatable, @declaration {
285287
function_template_argument_value(underlyingElement(this), index, unresolveElement(result))
286288
or
287289
variable_template_argument_value(underlyingElement(this), index, unresolveElement(result))
290+
or
291+
template_template_argument_value(underlyingElement(this), index, unresolveElement(result))
288292
}
289293
}
290294

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ class TemplateTemplateParameter extends TypeTemplateParameter {
7575
TemplateTemplateParameter() { usertypes(underlyingElement(this), _, 8) }
7676

7777
override string getAPrimaryQlClass() { result = "TemplateTemplateParameter" }
78+
79+
/**
80+
* Gets a class instantiated from this template template parameter.
81+
*
82+
* For example for `Container<T>` in the following code, the result is
83+
* `Container<Elem>`:
84+
* ```
85+
* template <template <typename T> class Container, class Elem>
86+
* void foo(const Container<Elem> &value) { }
87+
* ```
88+
*/
89+
Class getAnInstantiation() { result.isConstructedFrom(this) }
7890
}
7991

8092
/**
@@ -90,3 +102,32 @@ class AutoType extends TypeTemplateParameter {
90102

91103
override Location getLocation() { result instanceof UnknownDefaultLocation }
92104
}
105+
106+
/**
107+
* A class that is an instantiation of a template template parameter. For example,
108+
* in the following code there is a `Container<Elem>` instantiation:
109+
* ```
110+
* template <template <typename T> class Container, class Elem>
111+
* void foo(const Container<Elem> &value) { }
112+
* ```
113+
* For the `Container` template itself, see `TemplateTemplateParameter`.
114+
*/
115+
class TemplateTemplateParameterInstantiation extends Class {
116+
TemplateTemplateParameter ttp;
117+
118+
TemplateTemplateParameterInstantiation() { ttp.getAnInstantiation() = this }
119+
120+
override string getAPrimaryQlClass() { result = "TemplateTemplateParameterInstantiation" }
121+
122+
/**
123+
* Gets the template template parameter from which this instantiation was instantiated.
124+
*
125+
* For example for `Container<Elem>` in the following code, the result is
126+
* `Container<T>`:
127+
* ```
128+
* template <template <typename T> class Container, class Elem>
129+
* void foo(const Container<Elem> &value) { }
130+
* ```
131+
*/
132+
TemplateTemplateParameter getTemplate() { result = ttp }
133+
}

cpp/ql/lib/semmlecode.cpp.dbscheme

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,21 @@ variable_template_argument_value(
861861
int arg_value: @expr ref
862862
);
863863

864+
template_template_instantiation(
865+
int to: @usertype ref,
866+
int from: @usertype ref
867+
);
868+
template_template_argument(
869+
int type_id: @usertype ref,
870+
int index: int ref,
871+
int arg_type: @type ref
872+
);
873+
template_template_argument_value(
874+
int type_id: @usertype ref,
875+
int index: int ref,
876+
int arg_value: @expr ref
877+
);
878+
864879
routinetypes(
865880
unique int id: @routinetype,
866881
int return_type: @type ref

0 commit comments

Comments
 (0)