Skip to content

Commit 61521e0

Browse files
committed
Scope: Simplify mechanism for identifying declaration order
1 parent dbd3fe6 commit 61521e0

File tree

1 file changed

+16
-34
lines changed

1 file changed

+16
-34
lines changed

cpp/common/src/codingstandards/cpp/Scope.qll

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -247,48 +247,30 @@ class TranslationUnit extends SourceFile {
247247

248248
/** Holds if `v2` strictly (`v2` is in an inner scope compared to `v1`) hides `v1`. */
249249
predicate hides_candidateStrict(UserVariable v1, UserVariable v2) {
250-
exists(Scope s, string name |
251-
v1 = s.getAHiddenVariable(name) and
252-
v2 = s.getAChildScope().getAHidingVariable(name) and
250+
exists(Scope parentScope, Scope childScope, string name |
251+
v1 = parentScope.getAHiddenVariable(name) and
252+
childScope = parentScope.getAChildScope() and
253+
v2 = childScope.getAHidingVariable(name) and
253254
not v1 = v2
254-
) and
255-
inSameTranslationUnitLate(v1.getFile(), v2.getFile()) and
256-
not (v1.isMember() or v2.isMember()) and
257-
(
258-
// If v1 is a local variable, ensure that v1 is declared before v2
255+
|
256+
// If v1 is a local variable defined in a `DeclStmt` ensure that it is declared before `v2`,
257+
// otherwise it would not be hidden
259258
(
260-
v1 instanceof LocalVariable and
261-
// Ignore variables declared in conditional expressions, as they apply to
262-
// the nested scope
263-
not v1 = any(ConditionDeclExpr cde).getVariable() and
264-
// Ignore variables declared in loops
265-
not exists(Loop l | l.getADeclaration() = v1)
259+
parentScope instanceof BlockStmt and
260+
exists(DeclStmt ds | ds.getADeclaration() = v1) and
261+
exists(parentScope.(BlockStmt).getIndexOfStmt(childScope))
266262
)
267263
implies
268264
exists(BlockStmt bs, DeclStmt v1Stmt, Stmt v2Stmt |
269-
v1 = v1Stmt.getADeclaration() and
270-
getEnclosingStmt(v2).getParentStmt*() = v2Stmt
265+
bs = parentScope and
266+
v2Stmt = childScope and
267+
v1Stmt.getADeclaration() = v1
271268
|
272269
bs.getIndexOfStmt(v1Stmt) <= bs.getIndexOfStmt(v2Stmt)
273270
)
274-
)
275-
}
276-
277-
/**
278-
* Gets the enclosing statement of the given variable, if any.
279-
*/
280-
private Stmt getEnclosingStmt(LocalScopeVariable v) {
281-
result.(DeclStmt).getADeclaration() = v
282-
or
283-
exists(ConditionDeclExpr cde |
284-
cde.getVariable() = v and
285-
result = cde.getEnclosingStmt()
286-
)
287-
or
288-
exists(CatchBlock cb |
289-
cb.getParameter() = v and
290-
result = cb.getEnclosingStmt()
291-
)
271+
) and
272+
inSameTranslationUnitLate(v1.getFile(), v2.getFile()) and
273+
not (v1.isMember() or v2.isMember())
292274
}
293275

294276
/** Holds if `v2` strictly (`v2` is in an inner scope compared to `v1`) hides `v1`. */

0 commit comments

Comments
 (0)