Skip to content

Commit 94e9b6e

Browse files
committed
C++: Introduce a new base class for template parameters
This will enable us to support non-type template parameters, which we currently do not support, and error template parameters, which might become relevant in the `build-mode: none` context.
1 parent 7ab06fc commit 94e9b6e

File tree

17 files changed

+76
-48
lines changed

17 files changed

+76
-48
lines changed
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: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,30 @@ class RoutineType extends Type, @routinetype {
16661666
}
16671667
}
16681668

1669+
/**
1670+
* A C++ template parameter.
1671+
*
1672+
* In the example below, `T` and `I` are template parameters:
1673+
* ```
1674+
* template <class T, int I>
1675+
* class C { };
1676+
* ```
1677+
*/
1678+
abstract class TemplateParameterBase extends Locatable {
1679+
TemplateParameterBase() {
1680+
usertypes(underlyingElement(this), _, 7) or usertypes(underlyingElement(this), _, 8)
1681+
}
1682+
1683+
override string getAPrimaryQlClass() { result = "TemplateParameterBase" }
1684+
}
1685+
1686+
/**
1687+
* A C++ `typename` (or `class`) template parameter.
1688+
*
1689+
* DEPRECATED: Use `TypeTemplateParameter` instead.
1690+
*/
1691+
deprecated class TemplateParameter = TypeTemplateParameter;
1692+
16691693
/**
16701694
* A C++ `typename` (or `class`) template parameter.
16711695
*
@@ -1675,12 +1699,12 @@ class RoutineType extends Type, @routinetype {
16751699
* class C { };
16761700
* ```
16771701
*/
1678-
class TemplateParameter extends UserType {
1679-
TemplateParameter() {
1702+
class TypeTemplateParameter extends UserType, TemplateParameterBase {
1703+
TypeTemplateParameter() {
16801704
usertypes(underlyingElement(this), _, 7) or usertypes(underlyingElement(this), _, 8)
16811705
}
16821706

1683-
override string getAPrimaryQlClass() { result = "TemplateParameter" }
1707+
override string getAPrimaryQlClass() { result = "TypeTemplateParameter" }
16841708

16851709
override predicate involvesTemplateParameter() { any() }
16861710
}
@@ -1695,7 +1719,7 @@ class TemplateParameter extends UserType {
16951719
* void foo(const Container<Elem> &value) { }
16961720
* ```
16971721
*/
1698-
class TemplateTemplateParameter extends TemplateParameter {
1722+
class TemplateTemplateParameter extends TypeTemplateParameter {
16991723
TemplateTemplateParameter() { usertypes(underlyingElement(this), _, 8) }
17001724

17011725
override string getAPrimaryQlClass() { result = "TemplateTemplateParameter" }
@@ -1707,7 +1731,7 @@ class TemplateTemplateParameter extends TemplateParameter {
17071731
* auto val = some_typed_expr();
17081732
* ```
17091733
*/
1710-
class AutoType extends TemplateParameter {
1734+
class AutoType extends TypeTemplateParameter {
17111735
AutoType() { usertypes(underlyingElement(this), "auto", 7) }
17121736

17131737
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

cpp/ql/src/Likely Bugs/Arithmetic/PointlessSelfComparison.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ predicate nanTest(EqualityOperation cmp) {
4949
pointlessSelfComparison(cmp) and
5050
exists(Type t | t = cmp.getLeftOperand().getUnspecifiedType() |
5151
t instanceof FloatingPointType or
52-
t instanceof TemplateParameter
52+
t instanceof TypeTemplateParameter
5353
)
5454
}
5555

0 commit comments

Comments
 (0)