Skip to content

Commit 566eafc

Browse files
authored
Merge pull request #823 from dave-bartolomeo/dave/IdentityString
C++: Declaration.getIdentityString and Type.getTypeIdentityString
2 parents 7e298cf + 1e7dced commit 566eafc

File tree

19 files changed

+1643
-823
lines changed

19 files changed

+1643
-823
lines changed

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

Lines changed: 13 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

@@ -886,8 +872,19 @@ class TemplateClass extends Class {
886872
* A class that is an instantiation of a template.
887873
*/
888874
class ClassTemplateInstantiation extends Class {
875+
TemplateClass tc;
876+
889877
ClassTemplateInstantiation() {
890-
exists(TemplateClass tc | tc.getAnInstantiation() = this)
878+
tc.getAnInstantiation() = this
879+
}
880+
881+
/**
882+
* Gets the class template from which this instantiation was instantiated.
883+
*
884+
* Example: For `std::vector<float>`, returns `std::vector<T>`.
885+
*/
886+
TemplateClass getTemplate() {
887+
result = tc
891888
}
892889
}
893890

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: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Function extends Declaration, ControlFlowNode, AccessHolder, @function {
2020
override string getName() { functions(underlyingElement(this),result,_) }
2121

2222
/**
23+
* DEPRECATED: Use `getIdentityString(Declaration)` from `semmle.code.cpp.Print` instead.
2324
* Gets the full signature of this function, including return type, parameter
2425
* types, and template arguments.
2526
*
@@ -332,18 +333,11 @@ class Function extends Declaration, ControlFlowNode, AccessHolder, @function {
332333
}
333334

334335
/**
335-
* Gets an argument used to instantiate this class from a template
336-
* 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.
337339
*/
338-
Type getATemplateArgument() {
339-
exists(int i | this.getTemplateArgument(i) = result )
340-
}
341-
342-
/**
343-
* Gets a particular argument used to instantiate this class from a
344-
* template class.
345-
*/
346-
Type getTemplateArgument(int index) {
340+
override Type getTemplateArgument(int index) {
347341
function_template_argument(underlyingElement(this),index,unresolveElement(result))
348342
}
349343

@@ -1088,8 +1082,19 @@ class TemplateFunction extends Function {
10881082
* A function that is an instantiation of a template.
10891083
*/
10901084
class FunctionTemplateInstantiation extends Function {
1085+
TemplateFunction tf;
1086+
10911087
FunctionTemplateInstantiation() {
1092-
exists(TemplateFunction tf | tf.getAnInstantiation() = this)
1088+
tf.getAnInstantiation() = this
1089+
}
1090+
1091+
/**
1092+
* Gets the function template from which this instantiation was instantiated.
1093+
*
1094+
* Example: For `int const& std::min<int>(int const&, int const&)`, returns `T const& min<T>(T const&, T const&)`.
1095+
*/
1096+
TemplateFunction getTemplate() {
1097+
result = tf
10931098
}
10941099
}
10951100

0 commit comments

Comments
 (0)