diff --git a/cpp/ql/lib/change-notes/2024-12-18-non-type-template-parameter.md b/cpp/ql/lib/change-notes/2024-12-18-non-type-template-parameter.md new file mode 100644 index 000000000000..41fe400ed66a --- /dev/null +++ b/cpp/ql/lib/change-notes/2024-12-18-non-type-template-parameter.md @@ -0,0 +1,4 @@ +--- +category: feature +--- +* A new class `NonTypeTemplateParameter` was introduced, which represents C++ non-type template parameters. diff --git a/cpp/ql/lib/cpp.qll b/cpp/ql/lib/cpp.qll index 5162248c4b85..ccd32c368e4b 100644 --- a/cpp/ql/lib/cpp.qll +++ b/cpp/ql/lib/cpp.qll @@ -35,6 +35,7 @@ import semmle.code.cpp.Field import semmle.code.cpp.Function import semmle.code.cpp.MemberFunction import semmle.code.cpp.Parameter +import semmle.code.cpp.TemplateParameter import semmle.code.cpp.Variable import semmle.code.cpp.Initializer import semmle.code.cpp.FriendDecl diff --git a/cpp/ql/lib/semmle/code/cpp/TemplateParameter.qll b/cpp/ql/lib/semmle/code/cpp/TemplateParameter.qll new file mode 100644 index 000000000000..6c10f648825d --- /dev/null +++ b/cpp/ql/lib/semmle/code/cpp/TemplateParameter.qll @@ -0,0 +1,92 @@ +/** + * Provides a hierarchy of classes for modeling C/C++ template parameters. + */ + +import semmle.code.cpp.Type +private import semmle.code.cpp.internal.ResolveClass + +abstract private class TemplateParameterImpl extends Locatable { + override string getAPrimaryQlClass() { result = "TemplateParameterImpl" } +} + +/** + * A C++ template parameter. + * + * In the example below, `T`, `TT`, and `I` are template parameters: + * ``` + * template TT, int I> + * class C { }; + * ``` + */ +final class TemplateParameterBase = TemplateParameterImpl; + +/** + * A C++ non-type template parameter. + * + * In the example below, `I` is a non-type template parameter: + * ``` + * template + * class C { }; + * ``` + */ +class NonTypeTemplateParameter extends Literal, TemplateParameterImpl { + NonTypeTemplateParameter() { nontype_template_parameters(underlyingElement(this)) } + + override string getAPrimaryQlClass() { result = "NonTypeTemplateParameter" } +} + +/** + * A C++ `typename` (or `class`) template parameter. + * + * DEPRECATED: Use `TypeTemplateParameter` instead. + */ +deprecated class TemplateParameter = TypeTemplateParameter; + +/** + * A C++ `typename` (or `class`) template parameter. + * + * In the example below, `T` is a template parameter: + * ``` + * template + * class C { }; + * ``` + */ +class TypeTemplateParameter extends UserType, TemplateParameterImpl { + TypeTemplateParameter() { + usertypes(underlyingElement(this), _, 7) or usertypes(underlyingElement(this), _, 8) + } + + override string getAPrimaryQlClass() { result = "TypeTemplateParameter" } + + override predicate involvesTemplateParameter() { any() } +} + +/** + * A C++ template template parameter. + * + * In the example below, `T` is a template template parameter (although its name + * may be omitted): + * ``` + * template