@@ -57,18 +57,10 @@ 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 ( ) { 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 ( ) {
6661 exists ( getFile ( ) .getRelativePath ( ) ) and
67- not this .( Variable ) .isCompilerGenerated ( ) and
68- not this .( Function ) .isCompilerGenerated ( ) and
62+ not isCompilerGenerated ( ) and
6963 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
7264 // compiler inferred parameters have name of p#0
7365 not this .( Parameter ) .getName ( ) = "p#0"
7466 }
@@ -82,13 +74,11 @@ class Scope extends Element {
8274
8375 int getNumberOfVariables ( ) { result = count ( getAVariable ( ) ) }
8476
85- int getNumberOfDeclarations ( ) { result = count ( getADeclaration ( ) ) }
86-
8777 Scope getAnAncestor ( ) { result = this .getStrictParent + ( ) }
8878
8979 Scope getStrictParent ( ) { result = getParentScope ( this ) }
9080
91- UserDeclaration getADeclaration ( ) { getParentScope ( result ) = this }
81+ Declaration getADeclaration ( ) { getParentScope ( result ) = this }
9282
9383 Expr getAnExpr ( ) { this = getParentScope ( result ) }
9484
@@ -132,31 +122,31 @@ class GeneratedBlockStmt extends BlockStmt {
132122 GeneratedBlockStmt ( ) { this .getLocation ( ) instanceof UnknownLocation }
133123}
134124
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 ) {
137127 exists ( Scope s |
138- result = s .getADeclaration ( ) and
128+ result = s .getAVariable ( ) and
139129 (
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
143133 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
146136 not result = v and
147137 v .getName ( ) <= result .getName ( )
148138 )
149139 )
150140}
151141
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 ) {
154144 exists ( Scope s |
155- result = s .getADeclaration ( ) and
145+ result = s .getAVariable ( ) and
156146 (
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
160150 )
161151 )
162152}
@@ -171,20 +161,20 @@ predicate inSameTranslationUnit(File f1, File f2) {
171161}
172162
173163/**
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`.
175165 */
176166cached
177- UserDeclaration getPotentialScopeOfDeclarationStrict ( UserDeclaration v ) {
178- result = getPotentialScopeOfDeclarationStrict_candidate ( v ) and
167+ UserVariable getPotentialScopeOfVariable ( UserVariable v ) {
168+ result = getPotentialScopeOfVariable_candidate ( v ) and
179169 inSameTranslationUnit ( v .getFile ( ) , result .getFile ( ) )
180170}
181171
182172/**
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`.
184174 */
185175cached
186- UserDeclaration getPotentialScopeOfDeclaration ( UserDeclaration v ) {
187- result = getPotentialScopeOfDeclaration_candidate ( v ) and
176+ UserVariable getPotentialScopeOfVariableStrict ( UserVariable v ) {
177+ result = getOuterScopesOfVariable_candidate ( v ) and
188178 inSameTranslationUnit ( v .getFile ( ) , result .getFile ( ) )
189179}
190180
@@ -214,9 +204,18 @@ class TranslationUnit extends SourceFile {
214204}
215205
216206/** 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 ) {
218217 not v1 = v2 and
219- v2 = getPotentialScopeOfDeclarationStrict ( v1 ) and
218+ v2 = getPotentialScopeOfVariableStrict ( v1 ) and
220219 v1 .getName ( ) = v2 .getName ( ) and
221220 // Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
222221 not ( v1 .isMember ( ) or v2 .isMember ( ) ) and
@@ -240,15 +239,6 @@ private predicate hides_candidateStrict(UserDeclaration v1, UserDeclaration v2)
240239 )
241240}
242241
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-
252242/**
253243 * Gets the enclosing statement of the given variable, if any.
254244 */
@@ -267,22 +257,20 @@ private Stmt getEnclosingStmt(LocalScopeVariable v) {
267257}
268258
269259/** Holds if `v2` hides `v1`. */
270- predicate hides ( UserDeclaration v1 , UserDeclaration v2 ) {
260+ predicate hides ( UserVariable v1 , UserVariable v2 ) {
271261 hides_candidate ( v1 , v2 ) and
272262 // Confirm that there's no closer candidate variable which `v2` hides
273- not exists ( UserDeclaration mid |
263+ not exists ( UserVariable mid |
274264 hides_candidate ( v1 , mid ) and
275265 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+ )
279267}
280268
281269/** 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 ) {
283271 hides_candidateStrict ( v1 , v2 ) and
284272 // Confirm that there's no closer candidate variable which `v2` hides
285- not exists ( UserDeclaration mid |
273+ not exists ( UserVariable mid |
286274 hides_candidateStrict ( v1 , mid ) and
287275 hides_candidateStrict ( mid , v2 )
288276 )
@@ -303,7 +291,7 @@ predicate hasBlockScope(Declaration decl) { exists(BlockStmt b | b.getADeclarati
303291/**
304292 * identifiers in nested (named/nonglobal) namespaces are exceptions to hiding due to being able access via fully qualified ids
305293 */
306- predicate excludedViaNestedNamespaces ( UserDeclaration outerDecl , UserDeclaration innerDecl ) {
294+ predicate excludedViaNestedNamespaces ( UserVariable outerDecl , UserVariable innerDecl ) {
307295 exists ( Namespace inner , Namespace outer |
308296 outer .getAChildNamespace + ( ) = inner and
309297 //outer is not global
0 commit comments