diff --git a/cpp/ql/lib/semmle/code/cpp/Declaration.qll b/cpp/ql/lib/semmle/code/cpp/Declaration.qll index 6f791234b63..dfb148a84f8 100644 --- a/cpp/ql/lib/semmle/code/cpp/Declaration.qll +++ b/cpp/ql/lib/semmle/code/cpp/Declaration.qll @@ -278,6 +278,8 @@ class Declaration extends Locatable, @declaration { or variable_template_argument(underlyingElement(this), index, unresolveElement(result)) or + alias_template_argument(underlyingElement(this), index, unresolveElement(result)) + or template_template_argument(underlyingElement(this), index, unresolveElement(result)) or concept_template_argument(underlyingElement(this), index, unresolveElement(result)) @@ -290,6 +292,8 @@ class Declaration extends Locatable, @declaration { or variable_template_argument_value(underlyingElement(this), index, unresolveElement(result)) or + alias_template_argument_value(underlyingElement(this), index, unresolveElement(result)) + or template_template_argument_value(underlyingElement(this), index, unresolveElement(result)) or concept_template_argument_value(underlyingElement(this), index, unresolveElement(result)) diff --git a/cpp/ql/lib/semmle/code/cpp/Element.qll b/cpp/ql/lib/semmle/code/cpp/Element.qll index 17af69eddac..7d5106edfec 100644 --- a/cpp/ql/lib/semmle/code/cpp/Element.qll +++ b/cpp/ql/lib/semmle/code/cpp/Element.qll @@ -278,6 +278,15 @@ private predicate isFromTemplateInstantiationRec(Element e, Element instantiatio instantiation.(Variable).isConstructedFrom(_) and e = instantiation or + instantiation.(UsingAliasTypedefType).isConstructedFrom(_) and + e = instantiation + or + instantiation.(TemplateTemplateParameterInstantiation).isConstructedFrom(_) and + e = instantiation + or + exists(instantiation.(ConceptIdExpr).getConcept()) and + e = instantiation + or isFromTemplateInstantiationRec(e.getEnclosingElement(), instantiation) } @@ -291,6 +300,15 @@ private predicate isFromUninstantiatedTemplateRec(Element e, Element template) { is_variable_template(unresolveElement(template)) and e = template or + is_alias_template(unresolveElement(template)) and + e = template + or + usertypes(unresolveElement(template), _, 8) and // template template parameter + e = template + or + template instanceof @concept_template and + e = template + or isFromUninstantiatedTemplateRec(e.getEnclosingElement(), template) } diff --git a/cpp/ql/lib/semmle/code/cpp/TypedefType.qll b/cpp/ql/lib/semmle/code/cpp/TypedefType.qll index 1e330842d09..d43c81e5635 100644 --- a/cpp/ql/lib/semmle/code/cpp/TypedefType.qll +++ b/cpp/ql/lib/semmle/code/cpp/TypedefType.qll @@ -64,7 +64,8 @@ class CTypedefType extends TypedefType { } /** - * A using alias C++ typedef type. For example the type declared in the following code: + * A C++ type alias or alias template. For example the type declared in the following + * code: * ``` * using my_int2 = int; * ``` @@ -77,6 +78,66 @@ class UsingAliasTypedefType extends TypedefType { override string explain() { result = "using {" + this.getBaseType().explain() + "} as \"" + this.getName() + "\"" } + + /** + * Holds if this alias is constructed from another alias as a result of + * template instantiation. + */ + predicate isConstructedFrom(UsingAliasTypedefType t) { + alias_instantiation(underlyingElement(this), unresolveElement(t)) + } +} + +/** + * A C++ alias template. For example the type declared in the following code: + * ``` + * template + * using my_type = T; + * ``` + */ +class AliasTemplateTypedefType extends TypedefType { + AliasTemplateTypedefType() { is_alias_template(underlyingElement(this)) } + + override string getAPrimaryQlClass() { result = "AliasTemplateTypedefType" } + + /** + * Gets a alias instantiated from this template. + * + * For example for `MyAliasTemplate` in the following code, the results are + * `MyAliasTemplate` and `MyAliasTemplate`: + * ``` + * template + * using MyAliasTemplate = ; + * + * MyAliasTemplate instance1; + * + * MyAliasTemplate instance2; + * ``` + */ + UsingAliasTypedefType getAnInstantiation() { result.isConstructedFrom(this) } +} + +/** + * A C++ alias template instantiation. For example the `my_int_type` type declared in + * the following code: + * ``` + * template + * using my_type = T; + * + * using my_int_type = my_type; + * ``` + */ +class AliasTemplateInstantiationTypedefType extends UsingAliasTypedefType { + AliasTemplateTypedefType ta; + + AliasTemplateInstantiationTypedefType() { ta.getAnInstantiation() = this } + + override string getAPrimaryQlClass() { result = "AliasTemplateInstantiationTypedefType" } + + /** + * Gets the alias template from which this instantiation was instantiated. + */ + AliasTemplateTypedefType getTemplate() { result = ta } } /** diff --git a/cpp/ql/lib/semmlecode.cpp.dbscheme b/cpp/ql/lib/semmlecode.cpp.dbscheme index 770002bb023..837c4e02326 100644 --- a/cpp/ql/lib/semmlecode.cpp.dbscheme +++ b/cpp/ql/lib/semmlecode.cpp.dbscheme @@ -960,6 +960,22 @@ variable_template_argument_value( int arg_value: @expr ref ); +is_alias_template(unique int id: @usertype ref); +alias_instantiation( + unique int to: @usertype ref, + int from: @usertype ref +); +alias_template_argument( + int variable_id: @usertype ref, + int index: int ref, + int arg_type: @type ref +); +alias_template_argument_value( + int variable_id: @usertype ref, + int index: int ref, + int arg_value: @expr ref +); + template_template_instantiation( int to: @usertype ref, int from: @usertype ref