Skip to content

Commit 66b2b5d

Browse files
authored
Merge pull request github#18308 from jketema/template-parameters
C++: Introduce a new base class for template parameters
2 parents dfb3483 + ccd3681 commit 66b2b5d

File tree

18 files changed

+78
-48
lines changed

18 files changed

+78
-48
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+
* A new class `TemplateParameterBase` was introduced, which represents C++ non-type template parameters, type template parameters, and template template parameters.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: deprecated
3+
---
4+
* The `TemplateParameter` class, representing C++ type template parameters has been deprecated. Use `TypeTemplateParameter` instead.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ class ClassTemplateSpecialization extends Class {
952952
result.getNamespace() = this.getNamespace() and
953953
// It is distinguished by the fact that each of its template arguments
954954
// is a distinct template parameter.
955-
count(TemplateParameter tp | tp = result.getATemplateArgument()) =
955+
count(TemplateParameterBase tp | tp = result.getATemplateArgument()) =
956956
count(int i | exists(result.getTemplateArgument(i)))
957957
}
958958

@@ -1006,7 +1006,7 @@ private predicate isPartialClassTemplateSpecialization(Class c) {
10061006
*/
10071007

10081008
exists(Type ta | ta = c.getATemplateArgument() and ta.involvesTemplateParameter()) and
1009-
count(TemplateParameter tp | tp = c.getATemplateArgument()) !=
1009+
count(TemplateParameterBase tp | tp = c.getATemplateArgument()) !=
10101010
count(int i | exists(c.getTemplateArgument(i)))
10111011
}
10121012

@@ -1091,7 +1091,7 @@ class ProxyClass extends UserType {
10911091
override Location getLocation() { result = this.getTemplateParameter().getDefinitionLocation() }
10921092

10931093
/** Gets the template parameter for which this is the proxy class. */
1094-
TemplateParameter getTemplateParameter() {
1094+
TypeTemplateParameter getTemplateParameter() {
10951095
is_proxy_class_for(underlyingElement(this), unresolveElement(result))
10961096
}
10971097
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class Declaration extends Locatable, @declaration {
187187
this instanceof Parameter or
188188
this instanceof ProxyClass or
189189
this instanceof LocalVariable or
190-
this instanceof TemplateParameter or
190+
this instanceof TypeTemplateParameter or
191191
this.(UserType).isLocal()
192192
)
193193
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private string getScopePrefix(Declaration decl) {
3333
result = "(" + type.getEnclosingFunction().(DumpFunction).getIdentityString() + ")::"
3434
)
3535
or
36-
decl instanceof TemplateParameter and result = ""
36+
decl instanceof TypeTemplateParameter and result = ""
3737
}
3838

3939
/**

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,28 @@ class RoutineType extends Type, @routinetype {
16661666
}
16671667
}
16681668

1669+
abstract private class TemplateParameterImpl extends Locatable {
1670+
override string getAPrimaryQlClass() { result = "TemplateParameterImpl" }
1671+
}
1672+
1673+
/**
1674+
* A C++ template parameter.
1675+
*
1676+
* In the example below, `T`, `TT`, and `I` are template parameters:
1677+
* ```
1678+
* template <class T, template<typename> TT, int I>
1679+
* class C { };
1680+
* ```
1681+
*/
1682+
final class TemplateParameterBase = TemplateParameterImpl;
1683+
1684+
/**
1685+
* A C++ `typename` (or `class`) template parameter.
1686+
*
1687+
* DEPRECATED: Use `TypeTemplateParameter` instead.
1688+
*/
1689+
deprecated class TemplateParameter = TypeTemplateParameter;
1690+
16691691
/**
16701692
* A C++ `typename` (or `class`) template parameter.
16711693
*
@@ -1675,12 +1697,12 @@ class RoutineType extends Type, @routinetype {
16751697
* class C { };
16761698
* ```
16771699
*/
1678-
class TemplateParameter extends UserType {
1679-
TemplateParameter() {
1700+
class TypeTemplateParameter extends UserType, TemplateParameterImpl {
1701+
TypeTemplateParameter() {
16801702
usertypes(underlyingElement(this), _, 7) or usertypes(underlyingElement(this), _, 8)
16811703
}
16821704

1683-
override string getAPrimaryQlClass() { result = "TemplateParameter" }
1705+
override string getAPrimaryQlClass() { result = "TypeTemplateParameter" }
16841706

16851707
override predicate involvesTemplateParameter() { any() }
16861708
}
@@ -1695,7 +1717,7 @@ class TemplateParameter extends UserType {
16951717
* void foo(const Container<Elem> &value) { }
16961718
* ```
16971719
*/
1698-
class TemplateTemplateParameter extends TemplateParameter {
1720+
class TemplateTemplateParameter extends TypeTemplateParameter {
16991721
TemplateTemplateParameter() { usertypes(underlyingElement(this), _, 8) }
17001722

17011723
override string getAPrimaryQlClass() { result = "TemplateTemplateParameter" }
@@ -1707,7 +1729,7 @@ class TemplateTemplateParameter extends TemplateParameter {
17071729
* auto val = some_typed_expr();
17081730
* ```
17091731
*/
1710-
class AutoType extends TemplateParameter {
1732+
class AutoType extends TypeTemplateParameter {
17111733
AutoType() { usertypes(underlyingElement(this), "auto", 7) }
17121734

17131735
override string getAPrimaryQlClass() { result = "AutoType" }

cpp/ql/lib/semmle/code/cpp/commons/Dependency.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Symbol extends DependsSource {
6666
not this.(TypeDeclarationEntry).getType() instanceof LocalEnum and
6767
not this.(TypeDeclarationEntry).getType() instanceof LocalClass and
6868
not this.(TypeDeclarationEntry).getType() instanceof LocalTypedefType and
69-
not this.(TypeDeclarationEntry).getType() instanceof TemplateParameter
69+
not this.(TypeDeclarationEntry).getType() instanceof TypeTemplateParameter
7070
or
7171
this instanceof NamespaceDeclarationEntry
7272
)

cpp/ql/lib/semmle/code/cpp/dataflow/ExternalFlow.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ private string getTypeNameWithoutFunctionTemplates(Function f, int n, int remain
504504
result = getParameterTypeWithoutTemplateArguments(templateFunction, n)
505505
)
506506
or
507-
exists(string mid, TemplateParameter tp, Function templateFunction |
507+
exists(string mid, TypeTemplateParameter tp, Function templateFunction |
508508
mid = getTypeNameWithoutFunctionTemplates(f, n, remaining + 1) and
509509
templateFunction = getFullyTemplatedFunction(f) and
510510
tp = templateFunction.getTemplateArgument(remaining) and
@@ -529,7 +529,7 @@ private string getTypeNameWithoutClassTemplates(Function f, int n, int remaining
529529
remaining = 0 and
530530
result = getTypeNameWithoutFunctionTemplates(f, n, 0)
531531
or
532-
exists(string mid, TemplateParameter tp, Class template |
532+
exists(string mid, TypeTemplateParameter tp, Class template |
533533
mid = getTypeNameWithoutClassTemplates(f, n, remaining + 1) and
534534
isClassConstructedFrom(f.getDeclaringType(), template) and
535535
tp = template.getTemplateArgument(remaining) and

cpp/ql/lib/semmle/code/cpp/internal/QualifiedName.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Declaration extends @declaration {
130130
this instanceof Parameter or
131131
this instanceof ProxyClass or
132132
this instanceof LocalVariable or
133-
this instanceof TemplateParameter or
133+
this instanceof TypeTemplateParameter or
134134
this.(UserType).isLocal()
135135
)
136136
}
@@ -226,8 +226,8 @@ class ProxyClass extends UserType {
226226
ProxyClass() { usertypes(this, _, 9) }
227227
}
228228

229-
class TemplateParameter extends UserType {
230-
TemplateParameter() { usertypes(this, _, 7) or usertypes(this, _, 8) }
229+
class TypeTemplateParameter extends UserType {
230+
TypeTemplateParameter() { usertypes(this, _, 7) or usertypes(this, _, 8) }
231231
}
232232

233233
class TemplateClass extends UserType {

cpp/ql/src/Best Practices/Unused Entities/UnusedLocals.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import cpp
2626
*/
2727
class TemplateDependentType extends Type {
2828
TemplateDependentType() {
29-
this instanceof TemplateParameter
29+
this instanceof TypeTemplateParameter
3030
or
3131
exists(TemplateDependentType t |
3232
this.refersToDirectly(t) and

0 commit comments

Comments
 (0)