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

Skip to content

Commit d672a6e

Browse files
authored
Merge pull request #1376 from jbj/getName-direct
C++: Use Definition.qll's getName
2 parents 59a006e + f8644b1 commit d672a6e

9 files changed

Lines changed: 75 additions & 81 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ abstract class Declaration extends Locatable, @declaration {
116116
* To test whether this declaration has a particular name in the global
117117
* namespace, use `hasGlobalName`.
118118
*/
119-
string getName() { result = underlyingElement(this).(Q::Declaration).getName() }
119+
abstract string getName();
120120

121121
/** Holds if this declaration has the given name. */
122122
predicate hasName(string name) { name = this.getName() }

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ class EnumConstant extends Declaration, @enumconstant {
100100
result = this.getDeclaringEnum().getDeclaringType()
101101
}
102102

103+
/**
104+
* Gets the name of this enumerator.
105+
*/
106+
override string getName() { enumconstants(underlyingElement(this),_,_,_,result,_) }
107+
103108
/**
104109
* Gets the value that this enumerator is initialized to, as a
105110
* string. This can be a value explicitly given to the enumerator, or an

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class FriendDecl extends Declaration, @frienddecl {
3535
/** Gets the location of this friend declaration. */
3636
override Location getLocation() { frienddecls(underlyingElement(this),_,_,result) }
3737

38+
/** Gets a descriptive string for this friend declaration. */
39+
override string getName() {
40+
result = this.getDeclaringClass().getName() + "'s friend"
41+
}
42+
3843
/**
3944
* Friend declarations do not have specifiers. It makes no difference
4045
* whether they are declared in a public, protected or private section of

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ private import semmle.code.cpp.internal.ResolveClass
1717
* in more detail in `Declaration.qll`.
1818
*/
1919
class Function extends Declaration, ControlFlowNode, AccessHolder, @function {
20+
override string getName() { functions(underlyingElement(this),result,_) }
21+
2022
/**
2123
* DEPRECATED: Use `getIdentityString(Declaration)` from `semmle.code.cpp.Print` instead.
2224
* Gets the full signature of this function, including return type, parameter

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ deprecated class FinallyBlock extends Block {
148148
deprecated class Property extends Declaration {
149149
Property() { none() }
150150

151+
/** Gets the name of this property. */
152+
override string getName() { none() }
153+
151154
/**
152155
* Gets nothing (provided for compatibility with Declaration).
153156
*

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

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import semmle.code.cpp.Location
22
import semmle.code.cpp.Declaration
33
private import semmle.code.cpp.internal.ResolveClass
4-
private import semmle.code.cpp.internal.QualifiedName as Q
54

65
/**
76
* A C/C++ function parameter or catch block parameter.
@@ -14,6 +13,26 @@ private import semmle.code.cpp.internal.QualifiedName as Q
1413
* have multiple declarations.
1514
*/
1615
class Parameter extends LocalScopeVariable, @parameter {
16+
17+
/**
18+
* Gets the canonical name, or names, of this parameter.
19+
*
20+
* The canonical names are the first non-empty category from the
21+
* following list:
22+
* 1. The name given to the parameter at the function's definition or
23+
* (for catch block parameters) at the catch block.
24+
* 2. A name given to the parameter at a function declaration.
25+
* 3. The name "p#i" where i is the index of the parameter.
26+
*/
27+
override string getName() {
28+
exists (VariableDeclarationEntry vde
29+
| vde = getANamedDeclarationEntry() and result = vde.getName()
30+
| vde.isDefinition() or not getANamedDeclarationEntry().isDefinition())
31+
or
32+
(not exists(getANamedDeclarationEntry()) and
33+
result = "p#" + this.getIndex().toString())
34+
}
35+
1736
/**
1837
* Gets the name of this parameter, including it's type.
1938
*
@@ -34,6 +53,27 @@ class Parameter extends LocalScopeVariable, @parameter {
3453
else result = typeString + nameString))
3554
}
3655

56+
private VariableDeclarationEntry getANamedDeclarationEntry() {
57+
result = getAnEffectiveDeclarationEntry() and result.getName() != ""
58+
}
59+
60+
/**
61+
* Gets a declaration entry corresponding to this declaration.
62+
*
63+
* This predicate is the same as getADeclarationEntry(), except that for
64+
* parameters of instantiated function templates, gives the declaration
65+
* entry of the prototype instantiation of the parameter (as
66+
* non-prototype instantiations don't have declaration entries of their
67+
* own).
68+
*/
69+
private VariableDeclarationEntry getAnEffectiveDeclarationEntry() {
70+
if getFunction().isConstructedFrom(_)
71+
then exists (Function prototypeInstantiation
72+
| prototypeInstantiation.getParameter(getIndex()) = result.getVariable() and
73+
getFunction().isConstructedFrom(prototypeInstantiation))
74+
else result = getADeclarationEntry()
75+
}
76+
3777
/**
3878
* Gets the name of this parameter in the given block (which should be
3979
* the body of a function with which the parameter is associated).
@@ -55,9 +95,7 @@ class Parameter extends LocalScopeVariable, @parameter {
5595
* In other words, this predicate holds precisely when the result of
5696
* `getName()` is not "p#i" (where `i` is the index of the parameter).
5797
*/
58-
predicate isNamed() {
59-
exists(underlyingElement(this).(Q::Parameter).getANamedDeclarationEntry())
60-
}
98+
predicate isNamed() { exists(getANamedDeclarationEntry()) }
6199

62100
/**
63101
* Gets the function to which this parameter belongs, if it is a function
@@ -97,13 +135,8 @@ class Parameter extends LocalScopeVariable, @parameter {
97135
* of the declaration locations.
98136
*/
99137
override Location getLocation() {
100-
exists(VariableDeclarationEntry vde |
101-
vde = underlyingElement(this).(Q::Parameter).getAnEffectiveDeclarationEntry() and
102-
result = vde.getLocation()
103-
|
104-
vde.isDefinition()
105-
or
106-
not underlyingElement(this).(Q::Parameter).getAnEffectiveDeclarationEntry().isDefinition()
138+
exists(VariableDeclarationEntry vde | vde = getAnEffectiveDeclarationEntry() and result = vde.getLocation() |
139+
vde.isDefinition() or not getAnEffectiveDeclarationEntry().isDefinition()
107140
)
108141
}
109142
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ private import semmle.code.cpp.internal.ResolveClass
99
* `Enum`, and `TypedefType`.
1010
*/
1111
class UserType extends Type, Declaration, NameQualifyingElement, AccessHolder, @usertype {
12-
override string getName() {
13-
result = Declaration.super.getName()
14-
}
12+
/**
13+
* Gets the name of this type.
14+
*/
15+
override string getName() { usertypes(underlyingElement(this),result,_) }
1516

1617
/**
1718
* Gets the simple name of this type, without any template parameters. For example

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class Variable extends Declaration, @variable {
4141
/** Holds if this variable is `volatile`. */
4242
predicate isVolatile() { this.getType().isVolatile() }
4343

44+
/** Gets the name of this variable. */
45+
override string getName() { none() }
46+
4447
/** Gets the type of this variable. */
4548
Type getType() { none() }
4649

@@ -294,6 +297,8 @@ deprecated class StackVariable extends Variable {
294297
* A local variable can be declared by a `DeclStmt` or a `ConditionDeclExpr`.
295298
*/
296299
class LocalVariable extends LocalScopeVariable, @localvariable {
300+
override string getName() { localvariables(underlyingElement(this),_,result) }
301+
297302
override Type getType() { localvariables(underlyingElement(this),unresolveElement(result),_) }
298303

299304
override Function getFunction() {
@@ -306,6 +311,8 @@ class LocalVariable extends LocalScopeVariable, @localvariable {
306311
* A C/C++ variable which has global scope or namespace scope.
307312
*/
308313
class GlobalOrNamespaceVariable extends Variable, @globalvariable {
314+
override string getName() { globalvariables(underlyingElement(this),_,result) }
315+
309316
override Type getType() { globalvariables(underlyingElement(this),unresolveElement(result),_) }
310317

311318
override Element getEnclosingElement() { none() }
@@ -353,6 +360,8 @@ class MemberVariable extends Variable, @membervariable {
353360
/** Holds if this member is public. */
354361
predicate isPublic() { this.hasSpecifier("public") }
355362

363+
override string getName() { membervariables(underlyingElement(this),_,result) }
364+
356365
override Type getType() {
357366
if (strictcount(this.getAType()) = 1) then (
358367
result = this.getAType()

cpp/ql/src/semmle/code/cpp/internal/QualifiedName.qll

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
private import semmle.code.cpp.Declaration as D
12
/**
23
* INTERNAL: Do not use. Provides classes and predicates for getting names of
34
* declarations, especially qualified names. Import this library `private` and
@@ -37,8 +38,7 @@ abstract class Declaration extends @declaration {
3738
string toString() { result = "QualifiedName Declaration" }
3839

3940
/** Gets the name of this declaration. */
40-
cached
41-
abstract string getName();
41+
final string getName() { result = this.(D::Declaration).getName() }
4242

4343
string getTypeQualifierWithoutArgs() {
4444
exists(UserType declaringType |
@@ -133,8 +133,6 @@ abstract class Declaration extends @declaration {
133133
}
134134

135135
class Variable extends Declaration, @variable {
136-
override string getName() { none() }
137-
138136
VariableDeclarationEntry getADeclarationEntry() { result.getDeclaration() = this }
139137
}
140138

@@ -147,7 +145,6 @@ class TemplateVariable extends Variable {
147145
class LocalScopeVariable extends Variable, @localscopevariable { }
148146

149147
class LocalVariable extends LocalScopeVariable, @localvariable {
150-
override string getName() { localvariables(this, _, result) }
151148
}
152149

153150
/**
@@ -174,75 +171,20 @@ class Parameter extends LocalScopeVariable, @parameter {
174171
int index;
175172

176173
Parameter() { params(this, function, index, _) }
177-
178-
/**
179-
* Gets the canonical name, or names, of this parameter.
180-
*
181-
* The canonical names are the first non-empty category from the
182-
* following list:
183-
* 1. The name given to the parameter at the function's definition or
184-
* (for catch block parameters) at the catch block.
185-
* 2. A name given to the parameter at a function declaration.
186-
* 3. The name "p#i" where i is the index of the parameter.
187-
*/
188-
override string getName() {
189-
exists(VariableDeclarationEntry vde |
190-
vde = getANamedDeclarationEntry() and result = vde.getName()
191-
|
192-
vde.isDefinition() or not getANamedDeclarationEntry().isDefinition()
193-
)
194-
or
195-
not exists(getANamedDeclarationEntry()) and
196-
result = "p#" + index.toString()
197-
}
198-
199-
VariableDeclarationEntry getANamedDeclarationEntry() {
200-
result = getAnEffectiveDeclarationEntry() and exists(result.getName())
201-
}
202-
203-
/**
204-
* Gets a declaration entry corresponding to this declaration.
205-
*
206-
* This predicate is the same as getADeclarationEntry(), except that for
207-
* parameters of instantiated function templates, gives the declaration
208-
* entry of the prototype instantiation of the parameter (as
209-
* non-prototype instantiations don't have declaration entries of their
210-
* own).
211-
*/
212-
VariableDeclarationEntry getAnEffectiveDeclarationEntry() {
213-
if function.(Function).isConstructedFrom(_)
214-
then
215-
exists(Function prototypeInstantiation |
216-
prototypeInstantiation.getParameter(index) = result.getVariable() and
217-
function.(Function).isConstructedFrom(prototypeInstantiation)
218-
)
219-
else result = getADeclarationEntry()
220-
}
221174
}
222175

223176
class GlobalOrNamespaceVariable extends Variable, @globalvariable {
224-
override string getName() { globalvariables(this, _, result) }
225-
}
226-
227-
class MemberVariable extends Variable, @membervariable {
228-
MemberVariable() { this.isMember() }
229-
230-
override string getName() { membervariables(this, _, result) }
231177
}
232178

233179
// Unlike the usual `EnumConstant`, this one doesn't have a
234180
// `getDeclaringType()`. This simplifies the recursive computation of type
235181
// qualifier names since it can assume that any declaration with a
236182
// `getDeclaringType()` should use that type in its type qualifier name.
237183
class EnumConstant extends Declaration, @enumconstant {
238-
override string getName() { enumconstants(this, _, _, _, result, _) }
239-
240184
UserType getDeclaringEnum() { enumconstants(this, result, _, _, _, _) }
241185
}
242186

243187
class Function extends Declaration, @function {
244-
override string getName() { functions(this, result, _) }
245-
246188
predicate isConstructedFrom(Function f) { function_instantiation(this, f) }
247189

248190
Parameter getParameter(int n) { params(result, this, n, _) }
@@ -258,8 +200,6 @@ class TemplateFunction extends Function {
258200
}
259201

260202
class UserType extends Declaration, @usertype {
261-
override string getName() { result = getUserTypeNameWithArgs(this) }
262-
263203
predicate isLocal() { enclosingfunction(this, _) }
264204

265205
// Gets a member of this class, if it's a class.
@@ -291,10 +231,6 @@ class TemplateClass extends UserType {
291231
}
292232

293233
class FriendDecl extends Declaration, @frienddecl {
294-
override string getName() {
295-
result = getUserTypeNameWithArgs(this.getDeclaringClass()) + "'s friend"
296-
}
297-
298234
UserType getDeclaringClass() { frienddecls(this, result, _, _) }
299235
}
300236

0 commit comments

Comments
 (0)