diff --git a/cpp/ql/src/Best Practices/Hiding/DeclarationHidesParameter.ql b/cpp/ql/src/Best Practices/Hiding/DeclarationHidesParameter.ql index 54b59d3f5c2f..91317c53af85 100644 --- a/cpp/ql/src/Best Practices/Hiding/DeclarationHidesParameter.ql +++ b/cpp/ql/src/Best Practices/Hiding/DeclarationHidesParameter.ql @@ -8,29 +8,38 @@ * @tags maintainability * readability */ -import cpp +import cpp -/* Names of parameters in the implementation of a function. - Notice that we need to exclude parameter names used in prototype - declarations and only include the ones from the actual definition. - We also exclude names from functions that have multiple definitions. - This should not happen in a single application but since we - have a system wide view it is likely to happen for instance for - the main function. */ +/** + * Gets the parameter of `f` with name `name`, which has to come from the + * _definition_ of `f` and not a prototype declaration. + * We also exclude names from functions that have multiple definitions. + * This should not happen in a single application but since we + * have a system wide view it is likely to happen for instance for + * the main function. + */ ParameterDeclarationEntry functionParameterNames(Function f, string name) { exists(FunctionDeclarationEntry fe | - result.getFunctionDeclarationEntry() = fe - and fe.getFunction() = f - and fe.getLocation() = f.getDefinitionLocation() - and strictcount(f.getDefinitionLocation()) = 1 - and result.getName() = name + result.getFunctionDeclarationEntry() = fe and + fe.getFunction() = f and + fe.getLocation() = f.getDefinitionLocation() and + result.getFile() = fe.getFile() and // Work around CPP-331 + strictcount(f.getDefinitionLocation()) = 1 and + result.getName() = name ) } -from Function f, LocalVariable lv, ParameterDeclarationEntry pde -where f = lv.getFunction() and - pde = functionParameterNames(f, lv.getName()) and - not lv.isInMacroExpansion() -select lv, "Local variable '"+ lv.getName() +"' hides a $@.", - pde, "parameter of the same name" +/** Gets a local variable in `f` with name `name`. */ +pragma[nomagic] +LocalVariable localVariableNames(Function f, string name) { + name = result.getName() and + f = result.getFunction() +} + +from Function f, LocalVariable lv, ParameterDeclarationEntry pde, string name +where + lv = localVariableNames(f, name) and + pde = functionParameterNames(f, name) and + not lv.isInMacroExpansion() +select lv, "Local variable '" + lv.getName() + "' hides a $@.", pde, "parameter of the same name"