@@ -57,18 +57,10 @@ private Element getParentScope(Element e) {
57
57
58
58
/** A variable which is defined by the user, rather than being from a third party or compiler generated. */
59
59
class UserVariable extends Variable {
60
- UserVariable ( ) { this instanceof UserDeclaration }
61
- }
62
-
63
- /** A construct which is defined by the user, rather than being from a third party or compiler generated. */
64
- class UserDeclaration extends Declaration {
65
- UserDeclaration ( ) {
60
+ UserVariable ( ) {
66
61
exists ( getFile ( ) .getRelativePath ( ) ) and
67
- not this .( Variable ) .isCompilerGenerated ( ) and
68
- not this .( Function ) .isCompilerGenerated ( ) and
62
+ not isCompilerGenerated ( ) and
69
63
not this .( Parameter ) .getFunction ( ) .isCompilerGenerated ( ) and
70
- // Class template instantiations are compiler generated instances that share the same parent scope. This will result in a cross-product on class template instantiations because they have the same name and same parent scope. We therefore exclude these from consideration like we do with other compiler generated identifiers of interest.
71
- not this instanceof ClassTemplateInstantiation and
72
64
// compiler inferred parameters have name of p#0
73
65
not this .( Parameter ) .getName ( ) = "p#0"
74
66
}
@@ -82,13 +74,11 @@ class Scope extends Element {
82
74
83
75
int getNumberOfVariables ( ) { result = count ( getAVariable ( ) ) }
84
76
85
- int getNumberOfDeclarations ( ) { result = count ( getADeclaration ( ) ) }
86
-
87
77
Scope getAnAncestor ( ) { result = this .getStrictParent + ( ) }
88
78
89
79
Scope getStrictParent ( ) { result = getParentScope ( this ) }
90
80
91
- UserDeclaration getADeclaration ( ) { getParentScope ( result ) = this }
81
+ Declaration getADeclaration ( ) { getParentScope ( result ) = this }
92
82
93
83
Expr getAnExpr ( ) { this = getParentScope ( result ) }
94
84
@@ -132,31 +122,31 @@ class GeneratedBlockStmt extends BlockStmt {
132
122
GeneratedBlockStmt ( ) { this .getLocation ( ) instanceof UnknownLocation }
133
123
}
134
124
135
- /** Gets a Declaration that is in the potential scope of Declaration `v`. */
136
- private UserDeclaration getPotentialScopeOfDeclaration_candidate ( UserDeclaration v ) {
125
+ /** Gets a variable that is in the potential scope of variable `v`. */
126
+ private UserVariable getPotentialScopeOfVariable_candidate ( UserVariable v ) {
137
127
exists ( Scope s |
138
- result = s .getADeclaration ( ) and
128
+ result = s .getAVariable ( ) and
139
129
(
140
- // Declaration in an ancestor scope, but only if there are less than 100 declarations in this scope
141
- v = s .getAnAncestor ( ) .getADeclaration ( ) and
142
- s .getNumberOfDeclarations ( ) < 100
130
+ // Variable in an ancestor scope, but only if there are less than 100 variables in this scope
131
+ v = s .getAnAncestor ( ) .getAVariable ( ) and
132
+ s .getNumberOfVariables ( ) < 100
143
133
or
144
- // In the same scope, but not the same Declaration , and choose just one to report
145
- v = s .getADeclaration ( ) and
134
+ // In the same scope, but not the same variable , and choose just one to report
135
+ v = s .getAVariable ( ) and
146
136
not result = v and
147
137
v .getName ( ) <= result .getName ( )
148
138
)
149
139
)
150
140
}
151
141
152
- /** Gets a Declaration that is in the potential scope of Declaration `v`. */
153
- private UserDeclaration getPotentialScopeOfDeclarationStrict_candidate ( UserDeclaration v ) {
142
+ /** Gets a variable that is in the potential scope of variable `v`. */
143
+ private UserVariable getOuterScopesOfVariable_candidate ( UserVariable v ) {
154
144
exists ( Scope s |
155
- result = s .getADeclaration ( ) and
145
+ result = s .getAVariable ( ) and
156
146
(
157
- // Declaration in an ancestor scope, but only if there are less than 100 variables in this scope
158
- v = s .getAnAncestor ( ) .getADeclaration ( ) and
159
- s .getNumberOfDeclarations ( ) < 100
147
+ // Variable in an ancestor scope, but only if there are less than 100 variables in this scope
148
+ v = s .getAnAncestor ( ) .getAVariable ( ) and
149
+ s .getNumberOfVariables ( ) < 100
160
150
)
161
151
)
162
152
}
@@ -171,20 +161,20 @@ predicate inSameTranslationUnit(File f1, File f2) {
171
161
}
172
162
173
163
/**
174
- * Gets a user Declaration which occurs in the "outer scope" of Declaration `v`.
164
+ * Gets a user variable which occurs in the "potential scope" of variable `v`.
175
165
*/
176
166
cached
177
- UserDeclaration getPotentialScopeOfDeclarationStrict ( UserDeclaration v ) {
178
- result = getPotentialScopeOfDeclarationStrict_candidate ( v ) and
167
+ UserVariable getPotentialScopeOfVariable ( UserVariable v ) {
168
+ result = getPotentialScopeOfVariable_candidate ( v ) and
179
169
inSameTranslationUnit ( v .getFile ( ) , result .getFile ( ) )
180
170
}
181
171
182
172
/**
183
- * Gets a user variable which occurs in the "potential scope" of variable `v`.
173
+ * Gets a user variable which occurs in the "outer scope" of variable `v`.
184
174
*/
185
175
cached
186
- UserDeclaration getPotentialScopeOfDeclaration ( UserDeclaration v ) {
187
- result = getPotentialScopeOfDeclaration_candidate ( v ) and
176
+ UserVariable getPotentialScopeOfVariableStrict ( UserVariable v ) {
177
+ result = getOuterScopesOfVariable_candidate ( v ) and
188
178
inSameTranslationUnit ( v .getFile ( ) , result .getFile ( ) )
189
179
}
190
180
@@ -214,9 +204,18 @@ class TranslationUnit extends SourceFile {
214
204
}
215
205
216
206
/** Holds if `v2` may hide `v1`. */
217
- private predicate hides_candidateStrict ( UserDeclaration v1 , UserDeclaration v2 ) {
207
+ private predicate hides_candidate ( UserVariable v1 , UserVariable v2 ) {
208
+ not v1 = v2 and
209
+ v2 = getPotentialScopeOfVariable ( v1 ) and
210
+ v1 .getName ( ) = v2 .getName ( ) and
211
+ // Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
212
+ not ( v1 .isMember ( ) or v2 .isMember ( ) )
213
+ }
214
+
215
+ /** Holds if `v2` may hide `v1`. */
216
+ private predicate hides_candidateStrict ( UserVariable v1 , UserVariable v2 ) {
218
217
not v1 = v2 and
219
- v2 = getPotentialScopeOfDeclarationStrict ( v1 ) and
218
+ v2 = getPotentialScopeOfVariableStrict ( v1 ) and
220
219
v1 .getName ( ) = v2 .getName ( ) and
221
220
// Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
222
221
not ( v1 .isMember ( ) or v2 .isMember ( ) ) and
@@ -240,15 +239,6 @@ private predicate hides_candidateStrict(UserDeclaration v1, UserDeclaration v2)
240
239
)
241
240
}
242
241
243
- /** Holds if `v2` may hide `v1`. */
244
- private predicate hides_candidate ( UserDeclaration v1 , UserDeclaration v2 ) {
245
- not v1 = v2 and
246
- v2 = getPotentialScopeOfDeclaration ( v1 ) and
247
- v1 .getName ( ) = v2 .getName ( ) and
248
- // Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
249
- not ( v1 .isMember ( ) or v2 .isMember ( ) )
250
- }
251
-
252
242
/**
253
243
* Gets the enclosing statement of the given variable, if any.
254
244
*/
@@ -267,22 +257,20 @@ private Stmt getEnclosingStmt(LocalScopeVariable v) {
267
257
}
268
258
269
259
/** Holds if `v2` hides `v1`. */
270
- predicate hides ( UserDeclaration v1 , UserDeclaration v2 ) {
260
+ predicate hides ( UserVariable v1 , UserVariable v2 ) {
271
261
hides_candidate ( v1 , v2 ) and
272
262
// Confirm that there's no closer candidate variable which `v2` hides
273
- not exists ( UserDeclaration mid |
263
+ not exists ( UserVariable mid |
274
264
hides_candidate ( v1 , mid ) and
275
265
hides_candidate ( mid , v2 )
276
- ) and
277
- // Unlike `hidesStrict`, that requires a different scope, `hides` considers declarations in the same scope. This will include function overloads based on their name. To remove overloads from consideration, we exclude them.
278
- not v1 .( Function ) .getAnOverload ( ) = v2
266
+ )
279
267
}
280
268
281
269
/** Holds if `v2` strictly (`v2` is in an inner scope compared to `v1`) hides `v1`. */
282
- predicate hidesStrict ( UserDeclaration v1 , UserDeclaration v2 ) {
270
+ predicate hidesStrict ( UserVariable v1 , UserVariable v2 ) {
283
271
hides_candidateStrict ( v1 , v2 ) and
284
272
// Confirm that there's no closer candidate variable which `v2` hides
285
- not exists ( UserDeclaration mid |
273
+ not exists ( UserVariable mid |
286
274
hides_candidateStrict ( v1 , mid ) and
287
275
hides_candidateStrict ( mid , v2 )
288
276
)
@@ -303,7 +291,7 @@ predicate hasBlockScope(Declaration decl) { exists(BlockStmt b | b.getADeclarati
303
291
/**
304
292
* identifiers in nested (named/nonglobal) namespaces are exceptions to hiding due to being able access via fully qualified ids
305
293
*/
306
- predicate excludedViaNestedNamespaces ( UserDeclaration outerDecl , UserDeclaration innerDecl ) {
294
+ predicate excludedViaNestedNamespaces ( UserVariable outerDecl , UserVariable innerDecl ) {
307
295
exists ( Namespace inner , Namespace outer |
308
296
outer .getAChildNamespace + ( ) = inner and
309
297
//outer is not global
0 commit comments