Skip to content

Commit df7d212

Browse files
author
Matthew Gretton-Dann
committed
C++: Basic lib support for template param values
1 parent 48c7d1d commit df7d212

4 files changed

Lines changed: 35 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,15 @@ class Class extends UserType {
614614
class_template_argument(underlyingElement(this), i, unresolveElement(result))
615615
}
616616

617+
/**
618+
* Gets the `i`th template argument value used to instantiate this class from a
619+
* class template. When called on a class template, this will return the
620+
* `i`th template parameter value.
621+
*/
622+
override Expr getTemplateArgumentValue(int i) {
623+
class_template_argument_value(underlyingElement(this), i, unresolveElement(result))
624+
}
625+
617626
/**
618627
* Holds if this class/struct is polymorphic (has a virtual function, or
619628
* inherits one).

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ abstract class Declaration extends Locatable, @declaration {
203203
*/
204204
Type getTemplateArgument(int index) { none() }
205205

206+
/**
207+
* Gets the `i`th template argument value used to instantiate this declaration
208+
* from a template. When called on a template, this will return the `i`th template
209+
* parameter value if it exists.
210+
*/
211+
Expr getTemplateArgumentValue(int index) { none() }
212+
206213
/** Gets the number of template arguments for this declaration. */
207214
final int getNumberOfTemplateArguments() {
208215
result = count(int i | exists(getTemplateArgument(i)))

cpp/ql/src/semmle/code/cpp/Function.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,16 @@ class Function extends Declaration, ControlFlowNode, AccessHolder, @function {
352352
function_template_argument(underlyingElement(this), index, unresolveElement(result))
353353
}
354354

355+
/**
356+
* Gets the value of the `i`th template argument used to instantiate this
357+
* function from a function template if that argument was a 'non-type'
358+
* argument. When called on a function template, this with return the value
359+
* of the `i`th template parameter.
360+
*/
361+
override Expr getTemplateArgumentValue(int index) {
362+
function_template_argument_value(underlyingElement(this), index, unresolveElement(result))
363+
}
364+
355365
/**
356366
* Holds if this function is defined in several files. This is illegal in
357367
* C (though possible in some C++ compilers), and likely indicates that

cpp/ql/src/semmle/code/cpp/Variable.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ class Variable extends Declaration, @variable {
164164
variable_template_argument(underlyingElement(this), index, unresolveElement(result))
165165
}
166166

167+
/**
168+
* Gets the `i`th template argument value used to instantiate this variable from a
169+
* variable template. When called on a variable template, this will return the
170+
* `i`th template parameter value.
171+
*/
172+
override Expr getTemplateArgumentValue(int index) {
173+
variable_template_argument_value(underlyingElement(this), index, unresolveElement(result))
174+
}
175+
167176
/**
168177
* Holds if this is a compiler-generated variable. For example, a
169178
* [range-based for loop](http://en.cppreference.com/w/cpp/language/range-for)

0 commit comments

Comments
 (0)