Skip to content

Commit 5606a6c

Browse files
committed
IdentifierHidden: improve variable names and docs/overall readability
1 parent 97ccd29 commit 5606a6c

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,14 @@ predicate hasBlockScope(Declaration decl) { exists(BlockStmt b | b.getADeclarati
303303
/**
304304
* identifiers in nested (named/nonglobal) namespaces are exceptions to hiding due to being able access via fully qualified ids
305305
*/
306-
predicate excludedViaNestedNamespaces(UserDeclaration v2, UserDeclaration v1) {
306+
predicate excludedViaNestedNamespaces(UserDeclaration outerDecl, UserDeclaration innerDecl) {
307307
exists(Namespace inner, Namespace outer |
308308
outer.getAChildNamespace+() = inner and
309309
//outer is not global
310310
not outer instanceof GlobalNamespace and
311311
not outer.isAnonymous() and
312312
not inner.isAnonymous() and
313-
v2.getNamespace() = inner and
314-
v1.getNamespace() = outer
313+
innerDecl.getNamespace() = inner and
314+
outerDecl.getNamespace() = outer
315315
)
316316
}

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@ abstract class IdentifierHiddenSharedQuery extends Query { }
1212
Query getQuery() { result instanceof IdentifierHiddenSharedQuery }
1313

1414
/**
15-
* There is a lambda that contains a declaration
16-
* that hides something that is captured
17-
* and the lambda exists in the function where this lamda is enclosed
15+
* Holds if declaration `innerDecl`, declared in a lambda, hides a declaration `outerDecl` captured by the lambda.
1816
*/
19-
predicate hiddenInLambda(UserDeclaration v2, UserDeclaration v1) {
17+
predicate hiddenInLambda(UserVariable outerDecl, UserVariable innerDecl) {
2018
exists(Scope s, Closure le |
21-
s.getADeclaration() = v2 and
19+
//innerDecl declared inside of the lambda
20+
s.getADeclaration() = innerDecl and
2221
s.getAnAncestor() = le and
23-
le.getEnclosingFunction().getBasicBlock().(Scope) = v1.getParentScope() and
24-
exists(LambdaCapture cap, Variable v |
25-
v.getAnAccess() = cap.getInitializer().(VariableAccess) and
26-
v = v1 and
22+
le.getEnclosingFunction().getBasicBlock().(Scope) = outerDecl.getParentScope() and
23+
exists(LambdaCapture cap |
24+
outerDecl.getAnAccess() = cap.getInitializer().(VariableAccess) and
2725
le.getLambdaExpression().getACapture() = cap
2826
) and
29-
v2.getName() = v1.getName()
27+
innerDecl.getName() = outerDecl.getName()
3028
)
3129
}
3230

33-
query predicate problems(UserDeclaration v2, string message, UserDeclaration v1, string varName) {
34-
not isExcluded(v1, getQuery()) and
35-
not isExcluded(v2, getQuery()) and
31+
query predicate problems(
32+
UserDeclaration innerDecl, string message, UserDeclaration outerDecl, string varName
33+
) {
34+
not isExcluded(outerDecl, getQuery()) and
35+
not isExcluded(innerDecl, getQuery()) and
3636
//ignore template variables for this rule
37-
not v1 instanceof TemplateVariable and
38-
not v2 instanceof TemplateVariable and
39-
//ignore types for this rule
40-
not v2 instanceof Type and
41-
not v1 instanceof Type and
42-
(hidesStrict(v1, v2) or hiddenInLambda(v2, v1)) and
43-
not excludedViaNestedNamespaces(v2, v1) and
44-
varName = v1.getName() and
37+
not outerDecl instanceof TemplateVariable and
38+
not innerDecl instanceof TemplateVariable and
39+
//ignore types for this rule as the Misra C/C++ 23 version of this rule (rule 6.4.1 and 6.4.2) focuses solely on variables and functions
40+
not innerDecl instanceof Type and
41+
not outerDecl instanceof Type and
42+
(hidesStrict(outerDecl, innerDecl) or hiddenInLambda(outerDecl, innerDecl)) and
43+
not excludedViaNestedNamespaces(outerDecl, innerDecl) and
44+
varName = outerDecl.getName() and
4545
message = "Declaration is hiding declaration $@."
4646
}

0 commit comments

Comments
 (0)