@@ -57,9 +57,15 @@ 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 ( ) {
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 ( ) {
61
66
exists ( getFile ( ) .getRelativePath ( ) ) and
62
- not isCompilerGenerated ( ) and
67
+ not this .( Variable ) .isCompilerGenerated ( ) and
68
+ not this .( Function ) .isCompilerGenerated ( ) and
63
69
not this .( Parameter ) .getFunction ( ) .isCompilerGenerated ( ) and
64
70
// compiler inferred parameters have name of p#0
65
71
not this .( Parameter ) .getName ( ) = "p#0"
@@ -78,7 +84,7 @@ class Scope extends Element {
78
84
79
85
Scope getStrictParent ( ) { result = getParentScope ( this ) }
80
86
81
- Declaration getADeclaration ( ) { getParentScope ( result ) = this }
87
+ UserDeclaration getADeclaration ( ) { getParentScope ( result ) = this }
82
88
83
89
Expr getAnExpr ( ) { this = getParentScope ( result ) }
84
90
@@ -122,30 +128,30 @@ class GeneratedBlockStmt extends BlockStmt {
122
128
GeneratedBlockStmt ( ) { this .getLocation ( ) instanceof UnknownLocation }
123
129
}
124
130
125
- /** Gets a variable that is in the potential scope of variable `v`. */
126
- private UserVariable getPotentialScopeOfVariable_candidate ( UserVariable v ) {
131
+ /** Gets a Declaration that is in the potential scope of Declaration `v`. */
132
+ private UserDeclaration getPotentialScopeOfDeclaration_candidate ( UserDeclaration v ) {
127
133
exists ( Scope s |
128
- result = s .getAVariable ( ) and
134
+ result = s .getADeclaration ( ) and
129
135
(
130
- // Variable in an ancestor scope, but only if there are less than 100 variables in this scope
131
- v = s .getAnAncestor ( ) .getAVariable ( ) and
136
+ // Declaration in an ancestor scope, but only if there are less than 100 variables in this scope
137
+ v = s .getAnAncestor ( ) .getADeclaration ( ) and
132
138
s .getNumberOfVariables ( ) < 100
133
139
or
134
- // In the same scope, but not the same variable , and choose just one to report
135
- v = s .getAVariable ( ) and
140
+ // In the same scope, but not the same Declaration , and choose just one to report
141
+ v = s .getADeclaration ( ) and
136
142
not result = v and
137
143
v .getName ( ) <= result .getName ( )
138
144
)
139
145
)
140
146
}
141
147
142
- /** Gets a variable that is in the potential scope of variable `v`. */
143
- private UserVariable getOuterScopesOfVariable_candidate ( UserVariable v ) {
148
+ /** Gets a Declarationthat is in the potential scope of Declaration `v`. */
149
+ private UserDeclaration getOuterScopesOfDeclaration_candidate ( UserDeclaration v ) {
144
150
exists ( Scope s |
145
- result = s .getAVariable ( ) and
151
+ result = s .getADeclaration ( ) and
146
152
(
147
- // Variable in an ancestor scope, but only if there are less than 100 variables in this scope
148
- v = s .getAnAncestor ( ) .getAVariable ( ) and
153
+ // Declaration in an ancestor scope, but only if there are less than 100 variables in this scope
154
+ v = s .getAnAncestor ( ) .getADeclaration ( ) and
149
155
s .getNumberOfVariables ( ) < 100
150
156
)
151
157
)
@@ -161,20 +167,20 @@ predicate inSameTranslationUnit(File f1, File f2) {
161
167
}
162
168
163
169
/**
164
- * Gets a user variable which occurs in the "potential scope" of variable `v`.
170
+ * Gets a user Declaration which occurs in the "outer scope" of Declaration `v`.
165
171
*/
166
172
cached
167
- UserVariable getPotentialScopeOfVariable ( UserVariable v ) {
168
- result = getPotentialScopeOfVariable_candidate ( v ) and
173
+ UserDeclaration getPotentialScopeOfDeclarationStrict ( UserDeclaration v ) {
174
+ result = getOuterScopesOfDeclaration_candidate ( v ) and
169
175
inSameTranslationUnit ( v .getFile ( ) , result .getFile ( ) )
170
176
}
171
177
172
178
/**
173
- * Gets a user variable which occurs in the "outer scope" of variable `v`.
179
+ * Gets a user variable which occurs in the "potential scope" of variable `v`.
174
180
*/
175
181
cached
176
- UserVariable getPotentialScopeOfVariableStrict ( UserVariable v ) {
177
- result = getOuterScopesOfVariable_candidate ( v ) and
182
+ UserDeclaration getPotentialScopeOfDeclaration ( UserDeclaration v ) {
183
+ result = getPotentialScopeOfDeclaration_candidate ( v ) and
178
184
inSameTranslationUnit ( v .getFile ( ) , result .getFile ( ) )
179
185
}
180
186
@@ -204,18 +210,9 @@ class TranslationUnit extends SourceFile {
204
210
}
205
211
206
212
/** Holds if `v2` may hide `v1`. */
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 ) {
213
+ private predicate hides_candidateStrict ( UserDeclaration v1 , UserDeclaration v2 ) {
217
214
not v1 = v2 and
218
- v2 = getPotentialScopeOfVariableStrict ( v1 ) and
215
+ v2 = getPotentialScopeOfDeclarationStrict ( v1 ) and
219
216
v1 .getName ( ) = v2 .getName ( ) and
220
217
// Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
221
218
not ( v1 .isMember ( ) or v2 .isMember ( ) ) and
@@ -239,6 +236,15 @@ private predicate hides_candidateStrict(UserVariable v1, UserVariable v2) {
239
236
)
240
237
}
241
238
239
+ /** Holds if `v2` may hide `v1`. */
240
+ private predicate hides_candidate ( UserDeclaration v1 , UserDeclaration v2 ) {
241
+ not v1 = v2 and
242
+ v2 = getPotentialScopeOfDeclaration ( v1 ) and
243
+ v1 .getName ( ) = v2 .getName ( ) and
244
+ // Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
245
+ not ( v1 .isMember ( ) or v2 .isMember ( ) )
246
+ }
247
+
242
248
/**
243
249
* Gets the enclosing statement of the given variable, if any.
244
250
*/
@@ -257,20 +263,20 @@ private Stmt getEnclosingStmt(LocalScopeVariable v) {
257
263
}
258
264
259
265
/** Holds if `v2` hides `v1`. */
260
- predicate hides ( UserVariable v1 , UserVariable v2 ) {
266
+ predicate hides ( UserDeclaration v1 , UserDeclaration v2 ) {
261
267
hides_candidate ( v1 , v2 ) and
262
268
// Confirm that there's no closer candidate variable which `v2` hides
263
- not exists ( UserVariable mid |
269
+ not exists ( UserDeclaration mid |
264
270
hides_candidate ( v1 , mid ) and
265
271
hides_candidate ( mid , v2 )
266
272
)
267
273
}
268
274
269
275
/** Holds if `v2` strictly (`v2` is in an inner scope compared to `v1`) hides `v1`. */
270
- predicate hidesStrict ( UserVariable v1 , UserVariable v2 ) {
276
+ predicate hidesStrict ( UserDeclaration v1 , UserDeclaration v2 ) {
271
277
hides_candidateStrict ( v1 , v2 ) and
272
278
// Confirm that there's no closer candidate variable which `v2` hides
273
- not exists ( UserVariable mid |
279
+ not exists ( UserDeclaration mid |
274
280
hides_candidateStrict ( v1 , mid ) and
275
281
hides_candidateStrict ( mid , v2 )
276
282
)
0 commit comments