Skip to content

Commit ef110cd

Browse files
committed
IdentifierHidden: add missing exception testcases and address nested named namespaces exception
1 parent 9be5a7b commit ef110cd

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,18 @@ predicate hasClassScope(Declaration decl) { exists(decl.getDeclaringType()) }
295295

296296
/** Holds if `decl` has block scope. */
297297
predicate hasBlockScope(Declaration decl) { exists(BlockStmt b | b.getADeclaration() = decl) }
298+
299+
/**
300+
* identifiers in nested (named/nonglobal) namespaces are exceptions to hiding due to being able access via fully qualified ids
301+
*/
302+
predicate excludedViaNestedNamespaces(UserDeclaration v2, UserDeclaration v1) {
303+
exists(Namespace inner, Namespace outer |
304+
outer.getAChildNamespace+() = inner and
305+
//outer is not global
306+
not outer instanceof GlobalNamespace and
307+
not outer.isAnonymous() and
308+
not inner.isAnonymous() and
309+
v2.getNamespace() = inner and
310+
v1.getNamespace() = outer
311+
)
312+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ query predicate problems(UserDeclaration v2, string message, UserDeclaration v1,
1818
not v1 instanceof TemplateVariable and
1919
not v2 instanceof TemplateVariable and
2020
hidesStrict(v1, v2) and
21+
not excludedViaNestedNamespaces(v2, v1) and
2122
varName = v1.getName() and
2223
message = "Declaration is hiding declaration $@."
2324
}

cpp/common/test/rules/identifierhidden/test.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,23 @@ int a() {} // NON_COMPLIANT
8383

8484
namespace b1 {
8585
typedef int a; // NON_COMPLIANT
86-
}
86+
}
87+
88+
namespace ns_exception1_outer {
89+
int a1; // COMPLIANT - exception
90+
namespace ns_exception1_inner {
91+
void a1(); // COMPLIANT - exception
92+
}
93+
} // namespace ns_exception1_outer
94+
95+
void f4() {
96+
int a1, b;
97+
auto lambda1 = [a1]() {
98+
int b = 10; // COMPLIANT - exception - non captured variable b
99+
};
100+
101+
auto lambda2 = [b]() {
102+
int b = 10; // NON_COMPLIANT[FALSE_NEGATIVE] - not an exception - captured
103+
// variable b
104+
};
105+
}

0 commit comments

Comments
 (0)