Skip to content

Commit 5d3c27b

Browse files
committed
A2-10-1: add functions and types to identifier consideration
1 parent e300f67 commit 5d3c27b

File tree

5 files changed

+68
-49
lines changed

5 files changed

+68
-49
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A2-10-1`, `RULE-5-3` - `IdentifierHiding.ql`, `IdentifierHidingC.ql`:
2+
- Address FN reported in #118. Rule was missing detection of functions and types.

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

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ private Element getParentScope(Element e) {
5757

5858
/** A variable which is defined by the user, rather than being from a third party or compiler generated. */
5959
class UserVariable extends Variable {
60-
UserVariable() {
60+
UserVariable() { this instanceof UserDeclaration }
61+
}
62+
63+
/** A construct which is defined by the user, rather than being from a third party or compiler generated. */
64+
class UserDeclaration extends Declaration {
65+
UserDeclaration() {
6166
exists(getFile().getRelativePath()) and
62-
not isCompilerGenerated() and
67+
not this.(Variable).isCompilerGenerated() and
68+
not this.(Function).isCompilerGenerated() and
6369
not this.(Parameter).getFunction().isCompilerGenerated() and
6470
// compiler inferred parameters have name of p#0
6571
not this.(Parameter).getName() = "p#0"
@@ -78,7 +84,7 @@ class Scope extends Element {
7884

7985
Scope getStrictParent() { result = getParentScope(this) }
8086

81-
Declaration getADeclaration() { getParentScope(result) = this }
87+
UserDeclaration getADeclaration() { getParentScope(result) = this }
8288

8389
Expr getAnExpr() { this = getParentScope(result) }
8490

@@ -122,30 +128,30 @@ class GeneratedBlockStmt extends BlockStmt {
122128
GeneratedBlockStmt() { this.getLocation() instanceof UnknownLocation }
123129
}
124130

125-
/** Gets a variable that is in the potential scope of variable `v`. */
126-
private UserVariable getPotentialScopeOfVariable_candidate(UserVariable v) {
131+
/** Gets a Declaration that is in the potential scope of Declaration `v`. */
132+
private UserDeclaration getPotentialScopeOfDeclaration_candidate(UserDeclaration v) {
127133
exists(Scope s |
128-
result = s.getAVariable() and
134+
result = s.getADeclaration() and
129135
(
130-
// Variable in an ancestor scope, but only if there are less than 100 variables in this scope
131-
v = s.getAnAncestor().getAVariable() and
136+
// Declaration in an ancestor scope, but only if there are less than 100 variables in this scope
137+
v = s.getAnAncestor().getADeclaration() and
132138
s.getNumberOfVariables() < 100
133139
or
134-
// In the same scope, but not the same variable, and choose just one to report
135-
v = s.getAVariable() and
140+
// In the same scope, but not the same Declaration, and choose just one to report
141+
v = s.getADeclaration() and
136142
not result = v and
137143
v.getName() <= result.getName()
138144
)
139145
)
140146
}
141147

142-
/** Gets a variable that is in the potential scope of variable `v`. */
143-
private UserVariable getOuterScopesOfVariable_candidate(UserVariable v) {
148+
/** Gets a Declarationthat is in the potential scope of Declaration `v`. */
149+
private UserDeclaration getOuterScopesOfDeclaration_candidate(UserDeclaration v) {
144150
exists(Scope s |
145-
result = s.getAVariable() and
151+
result = s.getADeclaration() and
146152
(
147-
// Variable in an ancestor scope, but only if there are less than 100 variables in this scope
148-
v = s.getAnAncestor().getAVariable() and
153+
// Declaration in an ancestor scope, but only if there are less than 100 variables in this scope
154+
v = s.getAnAncestor().getADeclaration() and
149155
s.getNumberOfVariables() < 100
150156
)
151157
)
@@ -161,20 +167,20 @@ predicate inSameTranslationUnit(File f1, File f2) {
161167
}
162168

163169
/**
164-
* Gets a user variable which occurs in the "potential scope" of variable `v`.
170+
* Gets a user Declaration which occurs in the "outer scope" of Declaration `v`.
165171
*/
166172
cached
167-
UserVariable getPotentialScopeOfVariable(UserVariable v) {
168-
result = getPotentialScopeOfVariable_candidate(v) and
173+
UserDeclaration getPotentialScopeOfDeclarationStrict(UserDeclaration v) {
174+
result = getOuterScopesOfDeclaration_candidate(v) and
169175
inSameTranslationUnit(v.getFile(), result.getFile())
170176
}
171177

172178
/**
173-
* Gets a user variable which occurs in the "outer scope" of variable `v`.
179+
* Gets a user variable which occurs in the "potential scope" of variable `v`.
174180
*/
175181
cached
176-
UserVariable getPotentialScopeOfVariableStrict(UserVariable v) {
177-
result = getOuterScopesOfVariable_candidate(v) and
182+
UserDeclaration getPotentialScopeOfDeclaration(UserDeclaration v) {
183+
result = getPotentialScopeOfDeclaration_candidate(v) and
178184
inSameTranslationUnit(v.getFile(), result.getFile())
179185
}
180186

@@ -204,18 +210,9 @@ class TranslationUnit extends SourceFile {
204210
}
205211

206212
/** Holds if `v2` may hide `v1`. */
207-
private predicate hides_candidate(UserVariable v1, UserVariable v2) {
208-
not v1 = v2 and
209-
v2 = getPotentialScopeOfVariable(v1) and
210-
v1.getName() = v2.getName() and
211-
// Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
212-
not (v1.isMember() or v2.isMember())
213-
}
214-
215-
/** Holds if `v2` may hide `v1`. */
216-
private predicate hides_candidateStrict(UserVariable v1, UserVariable v2) {
213+
private predicate hides_candidateStrict(UserDeclaration v1, UserDeclaration v2) {
217214
not v1 = v2 and
218-
v2 = getPotentialScopeOfVariableStrict(v1) and
215+
v2 = getPotentialScopeOfDeclarationStrict(v1) and
219216
v1.getName() = v2.getName() and
220217
// Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
221218
not (v1.isMember() or v2.isMember()) and
@@ -239,6 +236,15 @@ private predicate hides_candidateStrict(UserVariable v1, UserVariable v2) {
239236
)
240237
}
241238

239+
/** Holds if `v2` may hide `v1`. */
240+
private predicate hides_candidate(UserDeclaration v1, UserDeclaration v2) {
241+
not v1 = v2 and
242+
v2 = getPotentialScopeOfDeclaration(v1) and
243+
v1.getName() = v2.getName() and
244+
// Member variables cannot hide other variables nor be hidden because the can be referenced through their qualified name.
245+
not (v1.isMember() or v2.isMember())
246+
}
247+
242248
/**
243249
* Gets the enclosing statement of the given variable, if any.
244250
*/
@@ -257,20 +263,20 @@ private Stmt getEnclosingStmt(LocalScopeVariable v) {
257263
}
258264

259265
/** Holds if `v2` hides `v1`. */
260-
predicate hides(UserVariable v1, UserVariable v2) {
266+
predicate hides(UserDeclaration v1, UserDeclaration v2) {
261267
hides_candidate(v1, v2) and
262268
// Confirm that there's no closer candidate variable which `v2` hides
263-
not exists(UserVariable mid |
269+
not exists(UserDeclaration mid |
264270
hides_candidate(v1, mid) and
265271
hides_candidate(mid, v2)
266272
)
267273
}
268274

269275
/** Holds if `v2` strictly (`v2` is in an inner scope compared to `v1`) hides `v1`. */
270-
predicate hidesStrict(UserVariable v1, UserVariable v2) {
276+
predicate hidesStrict(UserDeclaration v1, UserDeclaration v2) {
271277
hides_candidateStrict(v1, v2) and
272278
// Confirm that there's no closer candidate variable which `v2` hides
273-
not exists(UserVariable mid |
279+
not exists(UserDeclaration mid |
274280
hides_candidateStrict(v1, mid) and
275281
hides_candidateStrict(mid, v2)
276282
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ abstract class IdentifierHiddenSharedQuery extends Query { }
1111

1212
Query getQuery() { result instanceof IdentifierHiddenSharedQuery }
1313

14-
query predicate problems(UserVariable v2, string message, UserVariable v1, string varName) {
14+
query predicate problems(UserDeclaration v2, string message, UserDeclaration v1, string varName) {
1515
not isExcluded(v1, getQuery()) and
1616
not isExcluded(v2, getQuery()) and
1717
//ignore template variables for this rule
1818
not v1 instanceof TemplateVariable and
1919
not v2 instanceof TemplateVariable and
2020
hidesStrict(v1, v2) and
2121
varName = v1.getName() and
22-
message = "Variable is hiding variable $@."
22+
message = "Declaration is hiding declaration $@."
2323
}
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
| test.cpp:4:5:4:7 | id1 | Variable is hiding variable $@. | test.cpp:1:5:1:7 | id1 | id1 |
2-
| test.cpp:8:5:8:7 | id1 | Variable is hiding variable $@. | test.cpp:1:5:1:7 | id1 | id1 |
3-
| test.cpp:11:5:11:7 | id1 | Variable is hiding variable $@. | test.cpp:8:5:8:7 | id1 | id1 |
4-
| test.cpp:20:7:20:9 | id1 | Variable is hiding variable $@. | test.cpp:1:5:1:7 | id1 | id1 |
5-
| test.cpp:23:13:23:15 | id1 | Variable is hiding variable $@. | test.cpp:1:5:1:7 | id1 | id1 |
6-
| test.cpp:26:12:26:14 | id1 | Variable is hiding variable $@. | test.cpp:1:5:1:7 | id1 | id1 |
7-
| test.cpp:27:14:27:16 | id1 | Variable is hiding variable $@. | test.cpp:26:12:26:14 | id1 | id1 |
8-
| test.cpp:65:11:65:11 | i | Variable is hiding variable $@. | test.cpp:61:7:61:7 | i | i |
9-
| test.cpp:67:9:67:9 | i | Variable is hiding variable $@. | test.cpp:61:7:61:7 | i | i |
10-
| test.cpp:70:12:70:12 | i | Variable is hiding variable $@. | test.cpp:61:7:61:7 | i | i |
11-
| test.cpp:75:16:75:16 | i | Variable is hiding variable $@. | test.cpp:61:7:61:7 | i | i |
1+
| test.cpp:4:5:4:7 | id1 | Declaration is hiding declaration $@. | test.cpp:1:5:1:7 | id1 | id1 |
2+
| test.cpp:8:5:8:7 | id1 | Declaration is hiding declaration $@. | test.cpp:1:5:1:7 | id1 | id1 |
3+
| test.cpp:11:5:11:7 | id1 | Declaration is hiding declaration $@. | test.cpp:8:5:8:7 | id1 | id1 |
4+
| test.cpp:20:7:20:9 | id1 | Declaration is hiding declaration $@. | test.cpp:1:5:1:7 | id1 | id1 |
5+
| test.cpp:23:13:23:15 | id1 | Declaration is hiding declaration $@. | test.cpp:1:5:1:7 | id1 | id1 |
6+
| test.cpp:26:12:26:14 | id1 | Declaration is hiding declaration $@. | test.cpp:1:5:1:7 | id1 | id1 |
7+
| test.cpp:27:14:27:16 | id1 | Declaration is hiding declaration $@. | test.cpp:26:12:26:14 | id1 | id1 |
8+
| test.cpp:65:11:65:11 | i | Declaration is hiding declaration $@. | test.cpp:61:7:61:7 | i | i |
9+
| test.cpp:67:9:67:9 | i | Declaration is hiding declaration $@. | test.cpp:61:7:61:7 | i | i |
10+
| test.cpp:70:12:70:12 | i | Declaration is hiding declaration $@. | test.cpp:61:7:61:7 | i | i |
11+
| test.cpp:75:16:75:16 | i | Declaration is hiding declaration $@. | test.cpp:61:7:61:7 | i | i |
12+
| test.cpp:81:5:81:5 | a | Declaration is hiding declaration $@. | test.cpp:79:5:79:5 | a | a |
13+
| test.cpp:85:13:85:13 | a | Declaration is hiding declaration $@. | test.cpp:79:5:79:5 | a | a |

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@ void test_scope_order() {
7474

7575
} catch (int i) { // NON_COMPLIANT
7676
}
77+
}
78+
79+
int a;
80+
namespace b {
81+
int a() {} // NON_COMPLIANT
82+
} // namespace b
83+
84+
namespace b1 {
85+
typedef int a; // NON_COMPLIANT
7786
}

0 commit comments

Comments
 (0)