Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3414c10

Browse files
C++: Hoist getTemplateArgument() and friends into Declaration
1 parent 1c6b14e commit 3414c10

5 files changed

Lines changed: 45 additions & 78 deletions

File tree

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -583,26 +583,12 @@ class Class extends UserType {
583583
class_instantiation(underlyingElement(this), unresolveElement(c))
584584
}
585585

586-
/**
587-
* Gets a template argument used to instantiate this class from a class
588-
* template. When called on a class template, this will return a template
589-
* parameter.
590-
*/
591-
Type getATemplateArgument() {
592-
exists(int i | this.getTemplateArgument(i) = result )
593-
}
594-
595-
/** Gets the number of template arguments for this class. */
596-
int getNumberOfTemplateArguments() {
597-
result = count(int i | exists(getTemplateArgument(i)))
598-
}
599-
600586
/**
601587
* Gets the `i`th template argument used to instantiate this class from a
602588
* class template. When called on a class template, this will return the
603589
* `i`th template parameter.
604590
*/
605-
Type getTemplateArgument(int i) {
591+
override Type getTemplateArgument(int i) {
606592
class_template_argument(underlyingElement(this),i,unresolveElement(result))
607593
}
608594

@@ -895,7 +881,7 @@ class ClassTemplateInstantiation extends Class {
895881
/**
896882
* Gets the class template from which this instantiation was instantiated.
897883
*
898-
* Example: For `std::vector<float>()`, returns `std::vector<T>`.
884+
* Example: For `std::vector<float>`, returns `std::vector<T>`.
899885
*/
900886
TemplateClass getTemplate() {
901887
result = tc

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,27 @@ abstract class Declaration extends Locatable, @declaration {
187187
Class getDeclaringType() {
188188
this = result.getAMember()
189189
}
190+
191+
/**
192+
* Gets a template argument used to instantiate this declaration from a template.
193+
* When called on a template, this will return a template parameter.
194+
*/
195+
final Type getATemplateArgument() {
196+
result = getTemplateArgument(_)
197+
}
198+
199+
/**
200+
* Gets the `i`th template argument used to instantiate this declaration from a
201+
* template. When called on a template, this will return the `i`th template parameter.
202+
*/
203+
Type getTemplateArgument(int index) {
204+
none()
205+
}
206+
207+
/** Gets the number of template arguments for this declaration. */
208+
final int getNumberOfTemplateArguments() {
209+
result = count(int i | exists(getTemplateArgument(i)))
210+
}
190211
}
191212

192213
/**

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,11 @@ class Function extends Declaration, ControlFlowNode, AccessHolder, @function {
333333
}
334334

335335
/**
336-
* Gets an argument used to instantiate this class from a template
337-
* class.
336+
* Gets the `i`th template argument used to instantiate this function from a
337+
* function template. When called on a function template, this will return the
338+
* `i`th template parameter.
338339
*/
339-
Type getATemplateArgument() {
340-
exists(int i | this.getTemplateArgument(i) = result )
341-
}
342-
343-
/**
344-
* Gets a particular argument used to instantiate this class from a
345-
* template class.
346-
*/
347-
Type getTemplateArgument(int index) {
340+
override Type getTemplateArgument(int index) {
348341
function_template_argument(underlyingElement(this),index,unresolveElement(result))
349342
}
350343

@@ -1098,7 +1091,7 @@ class FunctionTemplateInstantiation extends Function {
10981091
/**
10991092
* Gets the function template from which this instantiation was instantiated.
11001093
*
1101-
* Example: For `min<int>()`, returns `min<T>`.
1094+
* Example: For `int const& std::min<int>(int const&, int const&)`, returns `T const& min<T>(T const&, T const&)`.
11021095
*/
11031096
TemplateFunction getTemplate() {
11041097
result = tf

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

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ private abstract class DumpDeclaration extends Declaration {
4646
string getIdentityString() {
4747
none()
4848
}
49+
50+
language[monotonicAggregates]
51+
final string getTemplateArgumentsString() {
52+
if exists(this.getATemplateArgument()) then (
53+
result = "<" +
54+
strictconcat(int i |
55+
exists(this.getTemplateArgument(i)) |
56+
this.getTemplateArgument(i).(DumpType).getTypeIdentityString(), ", " order by i
57+
) + ">"
58+
)
59+
else
60+
result = ""
61+
}
4962
}
5063

5164
/**
@@ -342,19 +355,6 @@ private class UserDumpType extends DumpType, DumpDeclaration, UserType {
342355
)
343356
}
344357

345-
language[monotonicAggregates]
346-
private string getTemplateArgumentsString() {
347-
if exists(this.(Class).getATemplateArgument()) then (
348-
result = "<" +
349-
strictconcat(int i |
350-
exists(this.(Class).getTemplateArgument(i)) |
351-
this.(Class).getTemplateArgument(i).(DumpType).getTypeIdentityString(), ", " order by i
352-
) + ">"
353-
)
354-
else
355-
result = ""
356-
}
357-
358358
override string getTypeSpecifier() {
359359
result = getIdentityString()
360360
}
@@ -368,39 +368,13 @@ private class DumpVariable extends DumpDeclaration, Variable {
368368
result = type.getTypeSpecifier() + type.getDeclaratorPrefix() + " " + getScopePrefix(this) + this.getName() + this.getTemplateArgumentsString() + type.getDeclaratorSuffixBeforeQualifiers() + type.getDeclaratorSuffix()
369369
)
370370
}
371-
372-
language[monotonicAggregates]
373-
private string getTemplateArgumentsString() {
374-
if exists(getATemplateArgument()) then (
375-
result = "<" +
376-
strictconcat(int i |
377-
exists(getTemplateArgument(i)) |
378-
getTemplateArgument(i).(DumpType).getTypeIdentityString(), ", " order by i
379-
) + ">"
380-
)
381-
else
382-
result = ""
383-
}
384371
}
385372

386373
private class DumpFunction extends DumpDeclaration, Function {
387374
override string getIdentityString() {
388375
result = getType().(DumpType).getTypeSpecifier() + getType().(DumpType).getDeclaratorPrefix() + " " + getScopePrefix(this) + getName() + getTemplateArgumentsString() + getDeclaratorSuffixBeforeQualifiers() + getDeclaratorSuffix()
389376
}
390377

391-
language[monotonicAggregates]
392-
private string getTemplateArgumentsString() {
393-
if exists(getATemplateArgument()) then (
394-
result = "<" +
395-
strictconcat(int i |
396-
exists(getTemplateArgument(i)) |
397-
getTemplateArgument(i).(DumpType).getTypeIdentityString(), ", " order by i
398-
) + ">"
399-
)
400-
else
401-
result = ""
402-
}
403-
404378
language[monotonicAggregates]
405379
private string getDeclaratorSuffixBeforeQualifiers() {
406380
result = "(" +

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,11 @@ class Variable extends Declaration, @variable {
131131
}
132132

133133
/**
134-
* Gets an argument used to instantiate this variable from a template
135-
* variable.
134+
* Gets the `i`th template argument used to instantiate this variable from a
135+
* variable template. When called on a variable template, this will return the
136+
* `i`th template parameter.
136137
*/
137-
Type getATemplateArgument() {
138-
exists(int i | this.getTemplateArgument(i) = result)
139-
}
140-
141-
/**
142-
* Gets a particular argument used to instantiate this variable from a
143-
* template variable.
144-
*/
145-
Type getTemplateArgument(int index) {
138+
override Type getTemplateArgument(int index) {
146139
variable_template_argument(underlyingElement(this), index, unresolveElement(result))
147140
}
148141

0 commit comments

Comments
 (0)