Skip to content

Commit 411ecde

Browse files
committed
IdentifierHidden: remove lambda special casing
This removes the special handling of lambda expressions, which was causing performance issues. Instead, we rely on the new behviour of the Scope library, which calculates identifier hiding for lambda expressions as part of the main calculation. This has one semantic change - the new code applies `isInSameTranslationUnit`, which reduces false positives where the identifier "hiding" in a lambda occurred with an outer variable in a different translation unit.
1 parent 117d0fb commit 411ecde

File tree

1 file changed

+1
-65
lines changed

1 file changed

+1
-65
lines changed

cpp/common/src/codingstandards/cpp/rules/identifierhidden/IdentifierHidden.qll

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,11 @@ import cpp
99
import codingstandards.cpp.Customizations
1010
import codingstandards.cpp.Exclusions
1111
import codingstandards.cpp.Scope
12-
import codingstandards.cpp.ConstHelpers
1312

1413
abstract class IdentifierHiddenSharedQuery extends Query { }
1514

1615
Query getQuery() { result instanceof IdentifierHiddenSharedQuery }
1716

18-
/**
19-
* a `Variable` that is nonvolatile and const
20-
* and of type `IntegralOrEnumType`
21-
*/
22-
class NonVolatileConstIntegralOrEnumVariable extends Variable {
23-
NonVolatileConstIntegralOrEnumVariable() {
24-
not this.isVolatile() and
25-
this.isConst() and
26-
this.getUnspecifiedType() instanceof IntegralOrEnumType
27-
}
28-
}
29-
30-
/**
31-
* Holds if declaration `innerDecl`, declared in a lambda, hides a declaration `outerDecl` by the lambda.
32-
*/
33-
predicate hiddenInLambda(UserVariable outerDecl, UserVariable innerDecl) {
34-
exists(
35-
Scope innerScope, LambdaExpression lambdaExpr, Scope lambdaExprScope, Scope outerScope,
36-
Closure lambdaClosure
37-
|
38-
// The variable `innerDecl` is declared inside of the lambda.
39-
innerScope.getADeclaration() = innerDecl and
40-
// Because a lambda is compiled down to a closure, we need to use the closure to determine if the declaration
41-
// is part of the lambda.
42-
innerScope.getAnAncestor() = lambdaClosure and
43-
// Next we determine the scope of the lambda expression to determine if `outerDecl` is visible in the scope of the lambda.
44-
lambdaClosure.getLambdaExpression() = lambdaExpr and
45-
lambdaExprScope.getAnExpr() = lambdaExpr and
46-
outerScope.getADeclaration() = outerDecl and
47-
lambdaExprScope.getStrictParent*() = outerScope and
48-
(
49-
// A definition can be hidden if it is in scope and it is captured by the lambda,
50-
exists(LambdaCapture cap |
51-
lambdaExpr.getACapture() = cap and
52-
// The outer declaration is captured by the lambda
53-
outerDecl.getAnAccess() = cap.getInitializer()
54-
)
55-
or
56-
// it is is non-local,
57-
outerDecl instanceof GlobalVariable
58-
or
59-
// it has static or thread local storage duration,
60-
(outerDecl.isThreadLocal() or outerDecl.isStatic())
61-
or
62-
//it is a reference that has been initialized with a constant expression.
63-
outerDecl.getType().stripTopLevelSpecifiers() instanceof ReferenceType and
64-
outerDecl.getInitializer().getExpr() instanceof Literal
65-
or
66-
// //it const non-volatile integral or enumeration type and has been initialized with a constant expression
67-
outerDecl instanceof NonVolatileConstIntegralOrEnumVariable and
68-
outerDecl.getInitializer().getExpr() instanceof Literal
69-
or
70-
//it is constexpr and has no mutable members
71-
outerDecl.isConstexpr() and
72-
not exists(Class c |
73-
c = outerDecl.getType() and not c.getAMember() instanceof MutableVariable
74-
)
75-
) and
76-
// Finally, the variables must have the same names.
77-
innerDecl.getName() = outerDecl.getName()
78-
)
79-
}
80-
8117
query predicate problems(
8218
UserVariable innerDecl, string message, UserVariable outerDecl, string varName
8319
) {
@@ -86,7 +22,7 @@ query predicate problems(
8622
//ignore template variables for this rule
8723
not outerDecl instanceof TemplateVariable and
8824
not innerDecl instanceof TemplateVariable and
89-
(hidesStrict(outerDecl, innerDecl) or hiddenInLambda(outerDecl, innerDecl)) and
25+
hidesStrict(outerDecl, innerDecl) and
9026
not excludedViaNestedNamespaces(outerDecl, innerDecl) and
9127
varName = outerDecl.getName() and
9228
message = "Variable is hiding variable $@."

0 commit comments

Comments
 (0)