@@ -57,9 +57,15 @@ private Element getParentScope(Element e) {
5757
5858/** A variable which is defined by the user, rather than being from a third party or compiler generated. */
5959class 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 ( ) {
6166 exists ( getFile ( ) .getRelativePath ( ) ) and
62- not isCompilerGenerated ( ) and
67+ not this .( Variable ) .isCompilerGenerated ( ) and
68+ not this .( Function ) .isCompilerGenerated ( ) and
6369 not this .( Parameter ) .getFunction ( ) .isCompilerGenerated ( ) and
6470 // compiler inferred parameters have name of p#0
6571 not this .( Parameter ) .getName ( ) = "p#0"
@@ -78,7 +84,7 @@ class Scope extends Element {
7884
7985 Scope getStrictParent ( ) { result = getParentScope ( this ) }
8086
81- Declaration getADeclaration ( ) { getParentScope ( result ) = this }
87+ UserDeclaration getADeclaration ( ) { getParentScope ( result ) = this }
8288
8389 Expr getAnExpr ( ) { this = getParentScope ( result ) }
8490
@@ -122,30 +128,30 @@ class GeneratedBlockStmt extends BlockStmt {
122128 GeneratedBlockStmt ( ) { this .getLocation ( ) instanceof UnknownLocation }
123129}
124130
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 ) {
127133 exists ( Scope s |
128- result = s .getAVariable ( ) and
134+ result = s .getADeclaration ( ) and
129135 (
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
132138 s .getNumberOfVariables ( ) < 100
133139 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
136142 not result = v and
137143 v .getName ( ) <= result .getName ( )
138144 )
139145 )
140146}
141147
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 ) {
144150 exists ( Scope s |
145- result = s .getAVariable ( ) and
151+ result = s .getADeclaration ( ) and
146152 (
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
149155 s .getNumberOfVariables ( ) < 100
150156 )
151157 )
@@ -161,20 +167,20 @@ predicate inSameTranslationUnit(File f1, File f2) {
161167}
162168
163169/**
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`.
165171 */
166172cached
167- UserVariable getPotentialScopeOfVariable ( UserVariable v ) {
168- result = getPotentialScopeOfVariable_candidate ( v ) and
173+ UserDeclaration getPotentialScopeOfDeclarationStrict ( UserDeclaration v ) {
174+ result = getOuterScopesOfDeclaration_candidate ( v ) and
169175 inSameTranslationUnit ( v .getFile ( ) , result .getFile ( ) )
170176}
171177
172178/**
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`.
174180 */
175181cached
176- UserVariable getPotentialScopeOfVariableStrict ( UserVariable v ) {
177- result = getOuterScopesOfVariable_candidate ( v ) and
182+ UserDeclaration getPotentialScopeOfDeclaration ( UserDeclaration v ) {
183+ result = getPotentialScopeOfDeclaration_candidate ( v ) and
178184 inSameTranslationUnit ( v .getFile ( ) , result .getFile ( ) )
179185}
180186
@@ -204,18 +210,9 @@ class TranslationUnit extends SourceFile {
204210}
205211
206212/** 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 ) {
217214 not v1 = v2 and
218- v2 = getPotentialScopeOfVariableStrict ( v1 ) and
215+ v2 = getPotentialScopeOfDeclarationStrict ( v1 ) and
219216 v1 .getName ( ) = v2 .getName ( ) and
220217 // Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
221218 not ( v1 .isMember ( ) or v2 .isMember ( ) ) and
@@ -239,6 +236,15 @@ private predicate hides_candidateStrict(UserVariable v1, UserVariable v2) {
239236 )
240237}
241238
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+
242248/**
243249 * Gets the enclosing statement of the given variable, if any.
244250 */
@@ -257,20 +263,20 @@ private Stmt getEnclosingStmt(LocalScopeVariable v) {
257263}
258264
259265/** Holds if `v2` hides `v1`. */
260- predicate hides ( UserVariable v1 , UserVariable v2 ) {
266+ predicate hides ( UserDeclaration v1 , UserDeclaration v2 ) {
261267 hides_candidate ( v1 , v2 ) and
262268 // Confirm that there's no closer candidate variable which `v2` hides
263- not exists ( UserVariable mid |
269+ not exists ( UserDeclaration mid |
264270 hides_candidate ( v1 , mid ) and
265271 hides_candidate ( mid , v2 )
266272 )
267273}
268274
269275/** 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 ) {
271277 hides_candidateStrict ( v1 , v2 ) and
272278 // Confirm that there's no closer candidate variable which `v2` hides
273- not exists ( UserVariable mid |
279+ not exists ( UserDeclaration mid |
274280 hides_candidateStrict ( v1 , mid ) and
275281 hides_candidateStrict ( mid , v2 )
276282 )
0 commit comments