Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .selfcheck_suppressions
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ nullPointerRedundantCheck:externals/tinyxml2/tinyxml2.cpp
knownConditionTrueFalse:externals/tinyxml2/tinyxml2.cpp
useStlAlgorithm:externals/simplecpp/simplecpp.cpp
missingMemberCopy:externals/simplecpp/simplecpp.h
shadowFunction:externals/simplecpp/simplecpp.cpp
shadowFunction:externals/simplecpp/simplecpp.h
Comment on lines +44 to +45
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great if you could publish an upstream PR which fixes these so we can drop the suppressions on the next bump.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked and we have -Wshadow-field from Clang enabled in simplecpp. And -Wshadow from GCC only reports shadowing in constructors (as expected), so I am curious about these.

20 changes: 10 additions & 10 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ void MainWindow::saveSettings() const
mUI->mResults->saveSettings(mSettings);
}

void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLibrary, const bool checkConfiguration)
void MainWindow::doAnalyzeProject(ImportProject p, const bool doCheckLibrary, const bool doCheckConfiguration)
{
Settings checkSettings;
auto supprs = std::make_shared<Suppressions>();
Expand Down Expand Up @@ -610,8 +610,8 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLibrary, cons

mUI->mResults->setCheckDirectory(checkPath);
checkSettings.force = false;
checkSettings.checkLibrary = checkLibrary;
checkSettings.checkConfiguration = checkConfiguration;
checkSettings.checkLibrary = doCheckLibrary;
checkSettings.checkConfiguration = doCheckConfiguration;

if (mProjectFile)
qDebug() << "Checking project file" << mProjectFile->getFilename();
Expand All @@ -638,7 +638,7 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLibrary, cons
mUI->mResults->setCheckSettings(checkSettings);
}

void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrary, const bool checkConfiguration)
void MainWindow::doAnalyzeFiles(const QStringList &files, const bool doCheckLibrary, const bool doCheckConfiguration)
{
if (files.isEmpty())
return;
Expand Down Expand Up @@ -679,7 +679,7 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
// TODO: lock UI here?
mUI->mResults->checkingStarted(fdetails.size());
mThread->setFiles(std::move(fdetails));
if (mProjectFile && !checkConfiguration)
if (mProjectFile && !doCheckConfiguration)
mThread->setAddonsAndTools(mProjectFile->getAddonsAndTools());
mThread->setSuppressions(mProjectFile ? mProjectFile->getCheckingSuppressions() : QList<SuppressionList::Suppression>());
QDir inf(mCurrentDirectory);
Expand All @@ -689,8 +689,8 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
checkLockDownUI(); // lock UI while checking

mUI->mResults->setCheckDirectory(checkPath);
checkSettings.checkLibrary = checkLibrary;
checkSettings.checkConfiguration = checkConfiguration;
checkSettings.checkLibrary = doCheckLibrary;
checkSettings.checkConfiguration = doCheckConfiguration;

if (mProjectFile)
qDebug() << "Checking project file" << mProjectFile->getFilename();
Expand Down Expand Up @@ -1883,7 +1883,7 @@ bool MainWindow::loadLastResults()
return true;
}

void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringList& recheckFiles, const bool checkLibrary, const bool checkConfiguration)
void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringList& recheckFiles, const bool doCheckLibrary, const bool doCheckConfiguration)
{
Settings::terminate(false);

Expand Down Expand Up @@ -2011,7 +2011,7 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringLis
msg.exec();
return;
}
doAnalyzeProject(p, checkLibrary, checkConfiguration); // TODO: avoid copy
doAnalyzeProject(p, doCheckLibrary, doCheckConfiguration); // TODO: avoid copy
return;
}

Expand All @@ -2024,7 +2024,7 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringLis
if (paths.isEmpty()) {
paths << mCurrentDirectory;
}
doAnalyzeFiles(paths, checkLibrary, checkConfiguration);
doAnalyzeFiles(paths, doCheckLibrary, doCheckConfiguration);
}

void MainWindow::newProjectFile()
Expand Down
18 changes: 9 additions & 9 deletions gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ private slots:
* @brief Analyze the project.
* @param projectFile Pointer to the project to analyze.
* @param recheckFiles files to recheck, empty => check all files
* @param checkLibrary Flag to indicate if the library should be checked.
* @param checkConfiguration Flag to indicate if the configuration should be checked.
* @param doCheckLibrary Flag to indicate if the library should be checked.
* @param doCheckConfiguration Flag to indicate if the configuration should be checked.
*/
void analyzeProject(const ProjectFile *projectFile, const QStringList& recheckFiles, bool checkLibrary = false, bool checkConfiguration = false);
void analyzeProject(const ProjectFile *projectFile, const QStringList& recheckFiles, bool doCheckLibrary = false, bool doCheckConfiguration = false);

/**
* @brief Set current language
Expand Down Expand Up @@ -309,19 +309,19 @@ private slots:
/**
* @brief Analyze project
* @param p imported project
* @param checkLibrary Flag to indicate if library should be checked
* @param checkConfiguration Flag to indicate if the configuration should be checked.
* @param doCheckLibrary Flag to indicate if library should be checked
* @param doCheckConfiguration Flag to indicate if the configuration should be checked.
*/
void doAnalyzeProject(ImportProject p, bool checkLibrary = false, bool checkConfiguration = false);
void doAnalyzeProject(ImportProject p, bool doCheckLibrary = false, bool doCheckConfiguration = false);

/**
* @brief Analyze all files specified in parameter files
*
* @param files List of files and/or directories to analyze
* @param checkLibrary Flag to indicate if library should be checked
* @param checkConfiguration Flag to indicate if the configuration should be checked.
* @param doCheckLibrary Flag to indicate if library should be checked
* @param doCheckConfiguration Flag to indicate if the configuration should be checked.
*/
void doAnalyzeFiles(const QStringList &files, bool checkLibrary = false, bool checkConfiguration = false);
void doAnalyzeFiles(const QStringList &files, bool doCheckLibrary = false, bool doCheckConfiguration = false);

/**
* @brief Get our default cppcheck settings and read project file.
Expand Down
4 changes: 2 additions & 2 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2071,10 +2071,10 @@ void CheckCondition::checkCompareValueOutOfTypeRange()
}
}

void CheckCondition::compareValueOutOfTypeRangeError(const Token *comparison, const std::string &type, MathLib::bigint value, bool result)
void CheckCondition::compareValueOutOfTypeRangeError(const Token *comp, const std::string &type, MathLib::bigint value, bool result)
{
reportError(
comparison,
comp,
Severity::style,
"compareValueOutOfTypeRangeError",
"Comparing expression of type '" + type + "' against value " + MathLib::toString(value) + ". Condition is always " + bool_to_string(result) + ".",
Expand Down
2 changes: 1 addition & 1 deletion lib/checkcondition.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class CPPCHECKLIB CheckCondition : public Check {
void assignmentInCondition(const Token *eq);

void checkCompareValueOutOfTypeRange();
void compareValueOutOfTypeRangeError(const Token *comparison, const std::string &type, MathLib::bigint value, bool result);
void compareValueOutOfTypeRangeError(const Token *comp, const std::string &type, MathLib::bigint value, bool result);

void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;

Expand Down
62 changes: 41 additions & 21 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4144,46 +4144,65 @@ void CheckOther::checkShadowVariables()
const Scope *functionScope = &scope;
while (functionScope && functionScope->type != ScopeType::eFunction && functionScope->type != ScopeType::eLambda)
functionScope = functionScope->nestedIn;
for (const Variable &var : scope.varlist) {
if (var.nameToken() && var.nameToken()->isExpandedMacro()) // #8903
continue;
const auto checkVar = [&](const Variable &var) {
if (!var.nameToken())
return;

if (var.nameToken()->isExpandedMacro()) // #8903
return;

if (functionScope && functionScope->type == ScopeType::eFunction && functionScope->function) {
if (!var.isArgument() && functionScope && functionScope->type == ScopeType::eFunction && functionScope->function) {
const auto & argList = functionScope->function->argumentList;
auto it = std::find_if(argList.cbegin(), argList.cend(), [&](const Variable& arg) {
return arg.nameToken() && var.name() == arg.name();
});
if (it != argList.end()) {
shadowError(var.nameToken(), it->nameToken(), "argument");
continue;
shadowError(var.nameToken(), "local variable", it->nameToken(), "argument");
return;
}
}

const Token *shadowed = findShadowed(scope.nestedIn, var, var.nameToken()->linenr());
if (!shadowed)
shadowed = findShadowed(scope.functionOf, var, var.nameToken()->linenr());
if (!shadowed)
continue;
return;
if (scope.type == ScopeType::eFunction && scope.className == var.name())
continue;
return;
if (functionScope->functionOf && functionScope->functionOf->isClassOrStructOrUnion() && functionScope->function &&
(functionScope->function->isStatic() || functionScope->function->isFriend()) &&
shadowed->variable() && !shadowed->variable()->isLocal())
continue;
shadowError(var.nameToken(), shadowed, (shadowed->varId() != 0) ? "variable" : "function");
}
return;
if (var.scope() && var.scope()->function && var.scope()->function->isConstructor()) {
if (shadowed->variable() && shadowed->variable()->isMember())
return;
if (shadowed->function() && shadowed->function()->nestedIn &&
shadowed->function()->nestedIn->isClassOrStruct())
return;
}
shadowError(var.nameToken(), var.isArgument() ? "argument" : "local variable",
shadowed, (shadowed->varId() != 0) ?
(shadowed->variable()->isMember() ? "member" : "variable") : "function");
};
for (const Variable &var : scope.varlist)
checkVar(var);
if (functionScope && functionScope->type == ScopeType::eFunction && functionScope->function)
for (const Variable &arg: functionScope->function->argumentList)
checkVar(arg);
}
}

void CheckOther::shadowError(const Token *var, const Token *shadowed, const std::string& type)
void CheckOther::shadowError(const Token *shadows, const std::string &shadowsType,
const Token *shadowed, const std::string &shadowedType)
{
ErrorPath errorPath;
errorPath.emplace_back(shadowed, "Shadowed declaration");
errorPath.emplace_back(var, "Shadow variable");
const std::string &varname = var ? var->str() : type;
const std::string Type = char(std::toupper(type[0])) + type.substr(1);
const std::string id = "shadow" + Type;
const std::string message = "$symbol:" + varname + "\nLocal variable \'$symbol\' shadows outer " + type;
errorPath.emplace_back(shadowed, "Shadowed " + shadowedType);
errorPath.emplace_back(shadows, "Shadow " + shadowsType);
const std::string &varname = shadows ? shadows->str() : shadowsType;
const std::string ShadowsType = char(std::toupper(shadowsType[0])) + shadowsType.substr(1);
const std::string ShadowedType = char(std::toupper(shadowedType[0])) + shadowedType.substr(1);
const std::string id = "shadow" + ShadowedType;
const std::string message = "$symbol:" + varname + "\n" + ShadowsType + " \'$symbol\' shadows outer " + shadowedType;
reportError(std::move(errorPath), Severity::style, id.c_str(), message, CWE398, Certainty::normal);
}

Expand Down Expand Up @@ -4865,9 +4884,10 @@ void CheckOther::getErrorMessages(ErrorLogger *errorLogger, const Settings *sett
c.accessMovedError(nullptr, "v", nullptr, false);
c.funcArgNamesDifferent("function", 1, nullptr, nullptr);
c.redundantBitwiseOperationInSwitchError(nullptr, "varname");
c.shadowError(nullptr, nullptr, "variable");
c.shadowError(nullptr, nullptr, "function");
c.shadowError(nullptr, nullptr, "argument");
c.shadowError(nullptr, "local variable", nullptr, "variable");
c.shadowError(nullptr, "local variable", nullptr, "argument");
c.shadowError(nullptr, "local variable", nullptr, "function");
c.shadowError(nullptr, "local variable", nullptr, "member");
c.knownArgumentError(nullptr, nullptr, nullptr, "x", false);
c.knownPointerToBoolError(nullptr, nullptr);
c.comparePointersError(nullptr, nullptr, nullptr);
Expand Down
2 changes: 1 addition & 1 deletion lib/checkother.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class CPPCHECKLIB CheckOther : public Check {
void accessMovedError(const Token *tok, const std::string &varname, const ValueFlow::Value *value, bool inconclusive);
void funcArgNamesDifferent(const std::string & functionName, nonneg int index, const Token* declaration, const Token* definition);
void funcArgOrderDifferent(const std::string & functionName, const Token * declaration, const Token * definition, const std::vector<const Token*> & declarations, const std::vector<const Token*> & definitions);
void shadowError(const Token *var, const Token *shadowed, const std::string& type);
void shadowError(const Token *shadows, const std::string &shadowsType, const Token *shadowed, const std::string &shadowedType);
void knownArgumentError(const Token *tok, const Token *ftok, const ValueFlow::Value *value, const std::string &varexpr, bool isVariableExpressionHidden);
void knownPointerToBoolError(const Token* tok, const ValueFlow::Value* value);
void comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2);
Expand Down
8 changes: 4 additions & 4 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLo
}
}

void CheckUnusedFunctions::updateFunctionData(const CheckUnusedFunctions& check)
void CheckUnusedFunctions::updateFunctionData(const CheckUnusedFunctions& checkUnusedFunctions)
{
for (const auto& entry : check.mFunctions)
for (const auto& entry : checkUnusedFunctions.mFunctions)
{
FunctionUsage &usage = mFunctions[entry.first];
if (!usage.lineNumber) {
Expand All @@ -518,6 +518,6 @@ void CheckUnusedFunctions::updateFunctionData(const CheckUnusedFunctions& check)
usage.usedOtherFile |= entry.second.usedOtherFile;
usage.usedSameFile |= entry.second.usedSameFile;
}
mFunctionDecl.insert(mFunctionDecl.cend(), check.mFunctionDecl.cbegin(), check.mFunctionDecl.cend());
mFunctionCalls.insert(check.mFunctionCalls.cbegin(), check.mFunctionCalls.cend());
mFunctionDecl.insert(mFunctionDecl.cend(), checkUnusedFunctions.mFunctionDecl.cbegin(), checkUnusedFunctions.mFunctionDecl.cend());
mFunctionCalls.insert(checkUnusedFunctions.mFunctionCalls.cbegin(), checkUnusedFunctions.mFunctionCalls.cend());
}
2 changes: 1 addition & 1 deletion lib/checkunusedfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CPPCHECKLIB CheckUnusedFunctions {
// Return true if an error is reported.
bool check(const Settings& settings, ErrorLogger& errorLogger) const;

void updateFunctionData(const CheckUnusedFunctions& check);
void updateFunctionData(const CheckUnusedFunctions& checkUnusedFunctions);

private:
static void unusedFunctionError(ErrorLogger& errorLogger,
Expand Down
4 changes: 2 additions & 2 deletions lib/filesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ class FileWithDetails
return mFsFileId;
}

void setFsFileId(std::size_t fsFileId)
void setFsFileId(std::size_t id)
{
mFsFileId = fsFileId;
mFsFileId = id;
}
private:
std::string mPath;
Expand Down
Loading
Loading