Skip to content

Commit c8cbc8e

Browse files
authored
Merge pull request #751 from jbj/hides-parameter-crossfile
C++: Improvements to "Declaration hides parameter"
2 parents a656c69 + b65e2f8 commit c8cbc8e

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

cpp/ql/src/Best Practices/Hiding/DeclarationHidesParameter.ql

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,38 @@
88
* @tags maintainability
99
* readability
1010
*/
11-
import cpp
1211

12+
import cpp
1313

14-
/* Names of parameters in the implementation of a function.
15-
Notice that we need to exclude parameter names used in prototype
16-
declarations and only include the ones from the actual definition.
17-
We also exclude names from functions that have multiple definitions.
18-
This should not happen in a single application but since we
19-
have a system wide view it is likely to happen for instance for
20-
the main function. */
14+
/**
15+
* Gets the parameter of `f` with name `name`, which has to come from the
16+
* _definition_ of `f` and not a prototype declaration.
17+
* We also exclude names from functions that have multiple definitions.
18+
* This should not happen in a single application but since we
19+
* have a system wide view it is likely to happen for instance for
20+
* the main function.
21+
*/
2122
ParameterDeclarationEntry functionParameterNames(Function f, string name) {
2223
exists(FunctionDeclarationEntry fe |
23-
result.getFunctionDeclarationEntry() = fe
24-
and fe.getFunction() = f
25-
and fe.getLocation() = f.getDefinitionLocation()
26-
and strictcount(f.getDefinitionLocation()) = 1
27-
and result.getName() = name
24+
result.getFunctionDeclarationEntry() = fe and
25+
fe.getFunction() = f and
26+
fe.getLocation() = f.getDefinitionLocation() and
27+
result.getFile() = fe.getFile() and // Work around CPP-331
28+
strictcount(f.getDefinitionLocation()) = 1 and
29+
result.getName() = name
2830
)
2931
}
3032

31-
from Function f, LocalVariable lv, ParameterDeclarationEntry pde
32-
where f = lv.getFunction() and
33-
pde = functionParameterNames(f, lv.getName()) and
34-
not lv.isInMacroExpansion()
35-
select lv, "Local variable '"+ lv.getName() +"' hides a $@.",
36-
pde, "parameter of the same name"
33+
/** Gets a local variable in `f` with name `name`. */
34+
pragma[nomagic]
35+
LocalVariable localVariableNames(Function f, string name) {
36+
name = result.getName() and
37+
f = result.getFunction()
38+
}
39+
40+
from Function f, LocalVariable lv, ParameterDeclarationEntry pde, string name
41+
where
42+
lv = localVariableNames(f, name) and
43+
pde = functionParameterNames(f, name) and
44+
not lv.isInMacroExpansion()
45+
select lv, "Local variable '" + lv.getName() + "' hides a $@.", pde, "parameter of the same name"

0 commit comments

Comments
 (0)