Skip to content

Commit c241b08

Browse files
committed
C++: Don't unresolve 'this'
For example, if you have 3 types called T, where t1 and t2 are defined but t3 isn't, then you will have unspecifiedtype(t1, t1) unspecifiedtype(t2, t2) unspecifiedtype(t3, t3) t1 = resolve(t1) t1 = resolve(t3) t2 = resolve(t2) t2 = resolve(t3) so given Type getUnspecifiedType() { unspecifiedtype(unresolve(this), unresolve(result)) } you get t1.getUnspecifiedType() = t2. I think that in general the best thing to do is to not unresolve 'this', but to just take the underlying value.
1 parent a1e4404 commit c241b08

37 files changed

+422
-418
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AutogeneratedFile extends File {
3535
cached AutogeneratedFile() {
3636
exists(int limit, int head |
3737
head <= 5 and
38-
limit = max(int line | locations_default(_, unresolveElement(this), head, _, line, _)) + 5
38+
limit = max(int line | locations_default(_, underlyingElement(this), head, _, line, _)) + 5
3939
|
4040
exists (Comment c | c.getFile() = this and c.getLocation().getStartLine() <= limit and isAutogeneratedComment(c))
4141
)

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private import semmle.code.cpp.internal.Type
1212
*/
1313
class Class extends UserType {
1414
Class() {
15-
isClass(unresolveElement(this))
15+
isClass(underlyingElement(this))
1616
}
1717

1818
/** Gets a child declaration of this class. */
@@ -75,7 +75,7 @@ class Class extends UserType {
7575
* If you also want template instantiations of results, see
7676
* `getAMember(int)`.
7777
*/
78-
Declaration getCanonicalMember(int index) { member(unresolveElement(this),index,unresolveElement(result)) }
78+
Declaration getCanonicalMember(int index) { member(underlyingElement(this),index,unresolveElement(result)) }
7979

8080
/**
8181
* Gets the (zero-based) `index`th canonical member declared in this
@@ -94,7 +94,7 @@ class Class extends UserType {
9494
* DEPRECATED: Use `getCanonicalMember(int)` or `getAMember(int)` instead.
9595
* Gets the `index`th member of this class.
9696
*/
97-
deprecated Declaration getMember(int index) { member(unresolveElement(this),index,unresolveElement(result)) }
97+
deprecated Declaration getMember(int index) { member(underlyingElement(this),index,unresolveElement(result)) }
9898

9999
/**
100100
* DEPRECATED: As this includes a somewhat arbitrary number of
@@ -416,7 +416,7 @@ class Class extends UserType {
416416
* compiled for. For this reason, the `is_pod_class` predicate is
417417
* generated by the extractor.
418418
*/
419-
predicate isPOD() { is_pod_class(unresolveElement(this)) }
419+
predicate isPOD() { is_pod_class(underlyingElement(this)) }
420420

421421
/**
422422
* Holds if this class is abstract, in other words whether it declares one
@@ -513,7 +513,7 @@ class Class extends UserType {
513513
* classes.
514514
*/
515515
int getVirtualBaseClassByteOffset(Class base) {
516-
virtual_base_offsets(unresolveElement(this), unresolveElement(base), result)
516+
virtual_base_offsets(underlyingElement(this), unresolveElement(base), result)
517517
}
518518

519519
/**
@@ -566,15 +566,15 @@ class Class extends UserType {
566566
* The alignment of this type in bytes (on the machine where facts were
567567
* extracted).
568568
*/
569-
int getAlignment() { usertypesize(unresolveElement(this),_,result) }
569+
int getAlignment() { usertypesize(underlyingElement(this),_,result) }
570570

571571
/**
572572
* Holds if this class is constructed from another class as a result of
573573
* template instantiation. It originates either from a class template or
574574
* from a class nested in a class template.
575575
*/
576576
predicate isConstructedFrom(Class c) {
577-
class_instantiation(unresolveElement(this), unresolveElement(c))
577+
class_instantiation(underlyingElement(this), unresolveElement(c))
578578
}
579579

580580
/**
@@ -597,7 +597,7 @@ class Class extends UserType {
597597
* `i`th template parameter.
598598
*/
599599
Type getTemplateArgument(int i) {
600-
class_template_argument(unresolveElement(this),i,unresolveElement(result))
600+
class_template_argument(underlyingElement(this),i,unresolveElement(result))
601601
}
602602

603603
/**
@@ -614,7 +614,7 @@ class Class extends UserType {
614614

615615
/** Holds if this class was declared 'final'. */
616616
predicate isFinal() {
617-
usertype_final(unresolveElement(this))
617+
usertype_final(underlyingElement(this))
618618
}
619619

620620
/** Gets a link target which references this class. */
@@ -631,7 +631,7 @@ class Class extends UserType {
631631
* using lowercase letters (e.g. "01234567-89ab-cdef-0123-456789abcdef").
632632
*/
633633
string getUuid() {
634-
usertype_uuid(unresolveElement(this), result)
634+
usertype_uuid(underlyingElement(this), result)
635635
}
636636

637637
private Type getAFieldSubobjectType() {
@@ -764,7 +764,7 @@ class ClassDerivation extends Locatable, @derivation {
764764
* struct D : T {};
765765
*/
766766
Type getBaseType() {
767-
derivations(unresolveElement(this),_,_,unresolveElement(result),_)
767+
derivations(underlyingElement(this),_,_,unresolveElement(result),_)
768768
}
769769

770770
/**
@@ -775,7 +775,7 @@ class ClassDerivation extends Locatable, @derivation {
775775
* struct D : B {};
776776
*/
777777
Class getDerivedClass() {
778-
derivations(unresolveElement(this),unresolveElement(result),_,_,_)
778+
derivations(underlyingElement(this),unresolveElement(result),_,_,_)
779779
}
780780

781781
/**
@@ -784,12 +784,12 @@ class ClassDerivation extends Locatable, @derivation {
784784
* derivation of B2 in "struct D : B1, B2 { ... };" would be 1.
785785
*/
786786
int getIndex() {
787-
derivations(unresolveElement(this),_,result,_,_)
787+
derivations(underlyingElement(this),_,result,_,_)
788788
}
789789

790790
/** Gets a specifier (for example "public") applied to the derivation. */
791791
Specifier getASpecifier() {
792-
derspecifiers(unresolveElement(this),unresolveElement(result))
792+
derspecifiers(underlyingElement(this),unresolveElement(result))
793793
}
794794

795795
/** Holds if the derivation has specifier `s`. */
@@ -804,7 +804,7 @@ class ClassDerivation extends Locatable, @derivation {
804804

805805
/** Gets the location of the derivation. */
806806
override Location getLocation() {
807-
derivations(unresolveElement(this),_,_,_,result)
807+
derivations(underlyingElement(this),_,_,_,result)
808808
}
809809

810810
/**
@@ -816,7 +816,7 @@ class ClassDerivation extends Locatable, @derivation {
816816
* classes.
817817
*/
818818
int getByteOffset() {
819-
direct_base_offsets(unresolveElement(this), result)
819+
direct_base_offsets(underlyingElement(this), result)
820820
}
821821

822822
override string toString() {
@@ -869,7 +869,7 @@ class AbstractClass extends Class {
869869
* of class templates).
870870
*/
871871
class TemplateClass extends Class {
872-
TemplateClass() { usertypes(unresolveElement(this),_,6) }
872+
TemplateClass() { usertypes(underlyingElement(this),_,6) }
873873
Class getAnInstantiation() {
874874
result.isConstructedFrom(this) and
875875
exists(result.getATemplateArgument())
@@ -998,7 +998,7 @@ class VirtualBaseClass extends Class {
998998
*/
999999
class ProxyClass extends UserType {
10001000
ProxyClass() {
1001-
usertypes(unresolveElement(this),_,9)
1001+
usertypes(underlyingElement(this),_,9)
10021002
}
10031003

10041004
/** Gets the location of the proxy class. */
@@ -1008,7 +1008,7 @@ class ProxyClass extends UserType {
10081008

10091009
/** Gets the template parameter for which this is the proxy class. */
10101010
TemplateParameter getTemplateParameter() {
1011-
is_proxy_class_for(unresolveElement(this),unresolveElement(result))
1011+
is_proxy_class_for(underlyingElement(this),unresolveElement(result))
10121012
}
10131013
}
10141014

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import semmle.code.cpp.Element
66
*/
77
class Comment extends Locatable, @comment {
88
override string toString() { result = this.getContents() }
9-
override Location getLocation() { comments(unresolveElement(this),_,result) }
10-
string getContents() { comments(unresolveElement(this),result,_) }
11-
Element getCommentedElement() { commentbinding(unresolveElement(this),unresolveElement(result)) }
9+
override Location getLocation() { comments(underlyingElement(this),_,result) }
10+
string getContents() { comments(underlyingElement(this),result,_) }
11+
Element getCommentedElement() { commentbinding(underlyingElement(this),unresolveElement(result)) }
1212
}
1313

1414
/**

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ class Diagnostic extends Locatable, @diagnostic {
77
* Gets the severity of the message, on a range from 1 to 5: 1=remark,
88
* 2=warning, 3=discretionary error, 4=error, 5=catastrophic error.
99
*/
10-
int getSeverity() { diagnostics(unresolveElement(this), result, _, _, _, _) }
10+
int getSeverity() { diagnostics(underlyingElement(this), result, _, _, _, _) }
1111

1212
/** Gets the error code for this compiler message. */
13-
string getTag() { diagnostics(unresolveElement(this), _, result, _, _, _) }
13+
string getTag() { diagnostics(underlyingElement(this), _, result, _, _, _) }
1414
predicate hasTag(string s) { this.getTag() = s }
1515

1616
/**
1717
* Gets the error message text associated with this compiler
1818
* diagnostic.
1919
*/
20-
string getMessage() { diagnostics(unresolveElement(this), _, _, result, _, _) }
20+
string getMessage() { diagnostics(underlyingElement(this), _, _, result, _, _) }
2121

2222
/**
2323
* Gets the full error message text associated with this compiler
2424
* diagnostic.
2525
*/
26-
string getFullMessage() { diagnostics(unresolveElement(this), _, _, _, result, _) }
26+
string getFullMessage() { diagnostics(underlyingElement(this), _, _, _, result, _) }
2727

2828
/** Gets the source location corresponding to the compiler message. */
29-
override Location getLocation() { diagnostics(unresolveElement(this), _, _, _, _, result) }
29+
override Location getLocation() { diagnostics(underlyingElement(this), _, _, _, _, result) }
3030

3131
override string toString() { result = this.getMessage() }
3232

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ cached @element unresolveElement(Element e) {
1616
resolveElement(result) = e
1717
}
1818

19+
cached @element underlyingElement(Element e) {
20+
result = e
21+
}
22+
1923
/**
2024
* A C/C++ element. This class is the base class for all C/C++
2125
* elements, such as functions, classes, expressions, and so on.
@@ -136,7 +140,7 @@ class Element extends @element {
136140
| this = s and result = s.getParent())
137141

138142
or
139-
using_container(unresolveElement(result), unresolveElement(this))
143+
using_container(unresolveElement(result), underlyingElement(this))
140144
}
141145

142146
/**
@@ -158,17 +162,17 @@ class Element extends @element {
158162
}
159163

160164
private Element getEnclosingElementPref() {
161-
enclosingfunction(unresolveElement(this), unresolveElement(result)) or
165+
enclosingfunction(underlyingElement(this), unresolveElement(result)) or
162166
result.(Function) = stmtEnclosingElement(this) or
163167
this.(LocalScopeVariable).getFunction() = result or
164-
enumconstants(unresolveElement(this), unresolveElement(result), _, _, _, _) or
165-
derivations(unresolveElement(this), unresolveElement(result), _, _, _) or
166-
stmtparents(unresolveElement(this), _, unresolveElement(result)) or
167-
exprparents(unresolveElement(this), _, unresolveElement(result)) or
168-
namequalifiers(unresolveElement(this), unresolveElement(result), _, _) or
169-
initialisers(unresolveElement(this), unresolveElement(result), _, _) or
170-
exprconv(unresolveElement(result), unresolveElement(this)) or
171-
param_decl_bind(unresolveElement(this),_,unresolveElement(result))
168+
enumconstants(underlyingElement(this), unresolveElement(result), _, _, _, _) or
169+
derivations(underlyingElement(this), unresolveElement(result), _, _, _) or
170+
stmtparents(underlyingElement(this), _, unresolveElement(result)) or
171+
exprparents(underlyingElement(this), _, unresolveElement(result)) or
172+
namequalifiers(underlyingElement(this), unresolveElement(result), _, _) or
173+
initialisers(underlyingElement(this), unresolveElement(result), _, _) or
174+
exprconv(unresolveElement(result), underlyingElement(this)) or
175+
param_decl_bind(underlyingElement(this),_,unresolveElement(result))
172176
}
173177

174178
/** Gets the closest `Element` enclosing this one. */
@@ -181,7 +185,7 @@ class Element extends @element {
181185
or
182186
result = exprEnclosingElement(this)
183187
or
184-
var_decls(unresolveElement(this), unresolveElement(result), _, _, _)
188+
var_decls(underlyingElement(this), unresolveElement(result), _, _, _)
185189
)
186190
)
187191
}
@@ -247,7 +251,7 @@ private predicate isFromUninstantiatedTemplateRec(Element e, Element template) {
247251
*/
248252
class StaticAssert extends Locatable, @static_assert {
249253
override string toString() { result = "static_assert(..., \"" + getMessage() + "\")" }
250-
Expr getCondition() { static_asserts(unresolveElement(this), unresolveElement(result), _, _) }
251-
string getMessage() { static_asserts(unresolveElement(this), _, result, _) }
252-
override Location getLocation() { static_asserts(unresolveElement(this), _, _, result) }
254+
Expr getCondition() { static_asserts(underlyingElement(this), unresolveElement(result), _, _) }
255+
string getMessage() { static_asserts(underlyingElement(this), _, result, _) }
256+
override Location getLocation() { static_asserts(underlyingElement(this), _, _, result) }
253257
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private import semmle.code.cpp.internal.Type
77
class Enum extends UserType, IntegralOrEnumType {
88
/** Gets an enumerator of this enumeration. */
99
EnumConstant getAnEnumConstant() { result.getDeclaringEnum() = this }
10-
EnumConstant getEnumConstant(int index) { enumconstants(unresolveElement(result),unresolveElement(this),index,_,_,_) }
10+
EnumConstant getEnumConstant(int index) { enumconstants(unresolveElement(result),underlyingElement(this),index,_,_,_) }
1111

1212
/**
1313
* Gets a descriptive string for the enum. This method is only intended to
@@ -24,15 +24,15 @@ class Enum extends UserType, IntegralOrEnumType {
2424
* For example: `enum E : int`.
2525
*/
2626
predicate hasExplicitUnderlyingType() {
27-
derivations(_, unresolveElement(this), _, _, _)
27+
derivations(_, underlyingElement(this), _, _, _)
2828
}
2929

3030
/**
3131
* The type of the enum-base [N4140 7.2], if it is specified.
3232
* For example: `int` in `enum E : int`.
3333
*/
3434
Type getExplicitUnderlyingType() {
35-
derivations(_, unresolveElement(this), _, unresolveElement(result), _)
35+
derivations(_, underlyingElement(this), _, unresolveElement(result), _)
3636
}
3737
}
3838

@@ -72,7 +72,7 @@ class NestedEnum extends Enum {
7272
*/
7373
class ScopedEnum extends Enum {
7474
ScopedEnum() {
75-
usertypes(unresolveElement(this),_,13)
75+
usertypes(underlyingElement(this),_,13)
7676
}
7777
}
7878

@@ -87,7 +87,7 @@ class EnumConstant extends Declaration, @enumconstant {
8787
/**
8888
* Gets the enumeration of which this enumerator is a member.
8989
*/
90-
Enum getDeclaringEnum() { enumconstants(unresolveElement(this),unresolveElement(result),_,_,_,_) }
90+
Enum getDeclaringEnum() { enumconstants(underlyingElement(this),unresolveElement(result),_,_,_,_) }
9191

9292
override Class getDeclaringType() {
9393
result = this.getDeclaringEnum().getDeclaringType()
@@ -96,7 +96,7 @@ class EnumConstant extends Declaration, @enumconstant {
9696
/**
9797
* Gets the name of this enumerator.
9898
*/
99-
override string getName() { enumconstants(unresolveElement(this),_,_,_,result,_) }
99+
override string getName() { enumconstants(underlyingElement(this),_,_,_,result,_) }
100100

101101
/**
102102
* Gets the value that this enumerator is initialized to, as a
@@ -106,13 +106,13 @@ class EnumConstant extends Declaration, @enumconstant {
106106
string getValue() { result = this.getInitializer().getExpr().getValue() }
107107

108108
/** Gets the type of this enumerator. */
109-
Type getType() { enumconstants(unresolveElement(this),_,_,unresolveElement(result),_,_) }
109+
Type getType() { enumconstants(underlyingElement(this),_,_,unresolveElement(result),_,_) }
110110

111111
/** Gets the location of a declaration of this enumerator. */
112112
override Location getADeclarationLocation() { result = this.getDefinitionLocation() }
113113

114114
/** Gets the location of the definition of this enumerator. */
115-
override Location getDefinitionLocation() { enumconstants(unresolveElement(this),_,_,_,_,result) }
115+
override Location getDefinitionLocation() { enumconstants(underlyingElement(this),_,_,_,_,result) }
116116

117117
/** Gets the location of the definition of this enumerator. */
118118
override Location getLocation() { result = this.getDefinitionLocation() }
@@ -124,7 +124,7 @@ class EnumConstant extends Declaration, @enumconstant {
124124
EnumConstantAccess getAnAccess() { result.getTarget() = this }
125125

126126
/** Gets a specifier of this enumerator. */
127-
override Specifier getASpecifier() { varspecifiers(unresolveElement(this),unresolveElement(result)) }
127+
override Specifier getASpecifier() { varspecifiers(underlyingElement(this),unresolveElement(result)) }
128128

129129
/**
130130
* An attribute of this enumerator.
@@ -133,6 +133,6 @@ class EnumConstant extends Declaration, @enumconstant {
133133
* which is only supported by Clang.
134134
*/
135135
Attribute getAnAttribute() {
136-
varattributes(unresolveElement(this), unresolveElement(result))
136+
varattributes(underlyingElement(this), unresolveElement(result))
137137
}
138138
}

0 commit comments

Comments
 (0)