|
8 | 8 | * @tags maintainability |
9 | 9 | * readability |
10 | 10 | */ |
11 | | -import cpp |
12 | 11 |
|
| 12 | +import cpp |
13 | 13 |
|
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 | + */ |
21 | 22 | ParameterDeclarationEntry functionParameterNames(Function f, string name) { |
22 | 23 | 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 |
28 | 30 | ) |
29 | 31 | } |
30 | 32 |
|
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