Skip to content

Commit d11d6f1

Browse files
committed
Detect shadowed arguments
1 parent 0be78bb commit d11d6f1

4 files changed

Lines changed: 34 additions & 13 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2798,26 +2798,43 @@ void CheckOther::checkShadowVariables()
27982798
for (const Scope & scope : symbolDatabase->scopeList) {
27992799
if (!scope.isExecutable() || scope.type == Scope::eLambda)
28002800
continue;
2801+
const Scope *functionScope = &scope;
2802+
while (functionScope && functionScope->type != Scope::ScopeType::eFunction && functionScope->type != Scope::ScopeType::eLambda)
2803+
functionScope = functionScope->nestedIn;
28012804
for (const Variable &var : scope.varlist) {
2805+
if (functionScope && functionScope->type == Scope::ScopeType::eFunction && functionScope->function) {
2806+
bool shadowArg = false;
2807+
for (const Variable &arg : functionScope->function->argumentList) {
2808+
if (arg.nameToken() && var.name() == arg.name()) {
2809+
shadowError(var.nameToken(), arg.nameToken(), "argument");
2810+
shadowArg = true;
2811+
break;
2812+
}
2813+
}
2814+
if (shadowArg)
2815+
continue;
2816+
}
2817+
28022818
const Token *shadowed = findShadowed(scope.nestedIn, var.name(), var.nameToken()->linenr());
28032819
if (!shadowed)
28042820
continue;
28052821
if (scope.type == Scope::eFunction && scope.className == var.name())
28062822
continue;
2807-
shadowError(var.nameToken(), shadowed, shadowed->varId() != 0);
2823+
shadowError(var.nameToken(), shadowed, (shadowed->varId() != 0) ? "variable" : "function");
28082824
}
28092825
}
28102826
}
28112827

2812-
void CheckOther::shadowError(const Token *var, const Token *shadowed, bool shadowVar)
2828+
void CheckOther::shadowError(const Token *var, const Token *shadowed, std::string type)
28132829
{
28142830
ErrorPath errorPath;
28152831
errorPath.push_back(ErrorPathItem(shadowed, "Shadowed declaration"));
28162832
errorPath.push_back(ErrorPathItem(var, "Shadow variable"));
2817-
const std::string &varname = var ? var->str() : (shadowVar ? "var" : "f");
2818-
const char *id = shadowVar ? "shadowVar" : "shadowFunction";
2819-
std::string message = "$symbol:" + varname + "\nLocal variable \'$symbol\' shadows outer " + (shadowVar ? "variable" : "function");
2820-
reportError(errorPath, Severity::style, id, message, CWE398, false);
2833+
const std::string &varname = var ? var->str() : type;
2834+
const std::string Type = char(std::toupper(type[0])) + type.substr(1);
2835+
const std::string id = "shadow" + Type;
2836+
const std::string message = "$symbol:" + varname + "\nLocal variable \'$symbol\' shadows outer " + type;
2837+
reportError(errorPath, Severity::style, id.c_str(), message, CWE398, false);
28212838
}
28222839

28232840
static bool isVariableExpression(const Token* tok)

‎lib/checkother.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class CPPCHECKLIB CheckOther : public Check {
259259
void accessMovedError(const Token *tok, const std::string &varname, const ValueFlow::Value *value, bool inconclusive);
260260
void funcArgNamesDifferent(const std::string & functionName, nonneg int index, const Token* declaration, const Token* definition);
261261
void funcArgOrderDifferent(const std::string & functionName, const Token * declaration, const Token * definition, const std::vector<const Token*> & declarations, const std::vector<const Token*> & definitions);
262-
void shadowError(const Token *var, const Token *shadowed, bool shadowVar);
262+
void shadowError(const Token *var, const Token *shadowed, std::string type);
263263
void constArgumentError(const Token *tok, const Token *ftok, const ValueFlow::Value *value);
264264
void comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2);
265265

@@ -323,8 +323,9 @@ class CPPCHECKLIB CheckOther : public Check {
323323
c.accessMovedError(nullptr, "v", nullptr, false);
324324
c.funcArgNamesDifferent("function", 1, nullptr, nullptr);
325325
c.redundantBitwiseOperationInSwitchError(nullptr, "varname");
326-
c.shadowError(nullptr, nullptr, false);
327-
c.shadowError(nullptr, nullptr, true);
326+
c.shadowError(nullptr, nullptr, "variable");
327+
c.shadowError(nullptr, nullptr, "function");
328+
c.shadowError(nullptr, nullptr, "argument");
328329
c.constArgumentError(nullptr, nullptr, nullptr);
329330
c.comparePointersError(nullptr, nullptr, nullptr);
330331

‎lib/cppcheck.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,13 +634,13 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
634634

635635
picojson::object obj = res.get<picojson::object>();
636636

637-
const std::string filename = obj["file"].get<std::string>();
637+
const std::string fileName = obj["file"].get<std::string>();
638638
const int64_t lineNumber = obj["linenr"].get<int64_t>();
639639
const int64_t column = obj["col"].get<int64_t>();
640640

641641
ErrorLogger::ErrorMessage errmsg;
642642

643-
errmsg._callStack.emplace_back(ErrorLogger::ErrorMessage::FileLocation(filename, lineNumber));
643+
errmsg._callStack.emplace_back(ErrorLogger::ErrorMessage::FileLocation(fileName, lineNumber));
644644
errmsg._callStack.back().col = column;
645645

646646
errmsg._id = obj["errorId"].get<std::string>();
@@ -650,7 +650,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
650650
errmsg._severity = Severity::fromString(severity);
651651
if (errmsg._severity == Severity::SeverityType::none)
652652
continue;
653-
errmsg.file0 = filename;
653+
errmsg.file0 = fileName;
654654

655655
reportErr(errmsg);
656656
}

‎test/testother.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2918,7 +2918,7 @@ class TestOther : public TestFixture {
29182918
"}");
29192919
ASSERT_EQUALS("", errout.str());
29202920

2921-
check("void foo(int x) {\n"
2921+
check("void foo(int arg) {\n"
29222922
" printf(\"%i\", ({int x = do_something(); x == 0;}));\n"
29232923
"}");
29242924
ASSERT_EQUALS("", errout.str());
@@ -7573,6 +7573,9 @@ class TestOther : public TestFixture {
75737573
" auto f = [](){ int x; }"
75747574
"}");
75757575
ASSERT_EQUALS("", errout.str());
7576+
7577+
check("void f(int x) { int x; }");
7578+
ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:1]: (style) Local variable 'x' shadows outer argument\n", errout.str());
75767579
}
75777580

75787581
void constArgument() {

0 commit comments

Comments
 (0)