@@ -9,75 +9,11 @@ import cpp
99import codingstandards.cpp.Customizations
1010import codingstandards.cpp.Exclusions
1111import codingstandards.cpp.Scope
12- import codingstandards.cpp.ConstHelpers
1312
1413abstract class IdentifierHiddenSharedQuery extends Query { }
1514
1615Query 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-
8117query 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