Skip to content

Commit 141d2ac

Browse files
PKEuSdanmar
authored andcommitted
Refactorization: Improved internal implementation of severity and certainty levels
Backported from LCppC.
1 parent c25b4f1 commit 141d2ac

85 files changed

Lines changed: 875 additions & 893 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cli/cmdlineparser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
309309
return false;
310310
}
311311
// when "style" is enabled, also enable "warning", "performance" and "portability"
312-
if (mSettings->isEnabled(Settings::STYLE)) {
312+
if (mSettings->severity.isEnabled(Severity::style)) {
313313
mSettings->addEnabled("warning");
314314
mSettings->addEnabled("performance");
315315
mSettings->addEnabled("portability");
@@ -430,7 +430,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
430430

431431
// Inconclusive checking
432432
else if (std::strcmp(argv[i], "--inconclusive") == 0)
433-
mSettings->inconclusive = true;
433+
mSettings->certainty.enable(Certainty::inconclusive);
434434

435435
// Enables inline suppressions.
436436
else if (std::strcmp(argv[i], "--inline-suppr") == 0)
@@ -938,7 +938,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
938938
else if ((def || mSettings->preprocessOnly) && !maxconfigs)
939939
mSettings->maxConfigs = 1U;
940940

941-
if (mSettings->isEnabled(Settings::UNUSED_FUNCTION) && mSettings->jobs > 1) {
941+
if (mSettings->checks.isEnabled(Checks::unusedFunction) && mSettings->jobs > 1) {
942942
printMessage("cppcheck: unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.");
943943
}
944944

cli/cppcheckexecutor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
130130
++iter;
131131
else {
132132
// If the include path is not found, warn user and remove the non-existing path from the list.
133-
if (settings.isEnabled(Settings::INFORMATION))
133+
if (settings.severity.isEnabled(Severity::information))
134134
std::cout << "(information) Couldn't find path given by -I '" << path << '\'' << std::endl;
135135
iter = settings.includePaths.erase(iter);
136136
}
@@ -853,7 +853,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
853853
if (!tryLoadLibrary(settings.library, argv[0], lib.c_str())) {
854854
const std::string msg("Failed to load the library " + lib);
855855
const std::list<ErrorMessage::FileLocation> callstack;
856-
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", false);
856+
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg, "failedToLoadCfg", Certainty::normal);
857857
reportErr(errmsg);
858858
return EXIT_FAILURE;
859859
}
@@ -879,7 +879,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
879879
"std.cfg should be available in " + cfgfolder + " or the FILESDIR "
880880
"should be configured.");
881881
#endif
882-
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg+" "+details, "failedToLoadCfg", false);
882+
ErrorMessage errmsg(callstack, emptyString, Severity::information, msg+" "+details, "failedToLoadCfg", Certainty::normal);
883883
reportErr(errmsg);
884884
return EXIT_FAILURE;
885885
}
@@ -963,7 +963,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
963963

964964
cppcheck.analyseWholeProgram(mSettings->buildDir, mFiles);
965965

966-
if (settings.isEnabled(Settings::INFORMATION) || settings.checkConfiguration) {
966+
if (settings.severity.isEnabled(Severity::information) || settings.checkConfiguration) {
967967
const bool enableUnusedFunctionCheck = cppcheck.isUnusedFunctionCheckEnabled();
968968

969969
if (settings.jointSuppressionReport) {
@@ -982,7 +982,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
982982
if (!settings.checkConfiguration) {
983983
cppcheck.tooManyConfigsError("",0U);
984984

985-
if (settings.isEnabled(Settings::MISSING_INCLUDE) && (Preprocessor::missingIncludeFlag || Preprocessor::missingSystemIncludeFlag)) {
985+
if (settings.checks.isEnabled(Checks::missingInclude) && (Preprocessor::missingIncludeFlag || Preprocessor::missingSystemIncludeFlag)) {
986986
const std::list<ErrorMessage::FileLocation> callStack;
987987
ErrorMessage msg(callStack,
988988
emptyString,
@@ -994,7 +994,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
994994
"as include directories for Cppcheck. To see what files Cppcheck cannot find use "
995995
"--check-config.",
996996
Preprocessor::missingIncludeFlag ? "missingInclude" : "missingIncludeSystem",
997-
false);
997+
Certainty::normal);
998998
reportInfo(msg);
999999
}
10001000
}

gui/checkthread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void CheckThread::parseClangErrors(const QString &tool, const QString &file0, QS
396396
const std::string f0 = file0.toStdString();
397397
const std::string msg = e.message.toStdString();
398398
const std::string id = e.errorId.toStdString();
399-
ErrorMessage errmsg(callstack, f0, e.severity, msg, id, false);
399+
ErrorMessage errmsg(callstack, f0, e.severity, msg, id, Certainty::normal);
400400
mResult.reportErr(errmsg);
401401
}
402402
}

gui/erroritem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ ErrorItem::ErrorItem(const ErrorMessage &errmsg)
4747
, errorId(QString::fromStdString(errmsg.id))
4848
, severity(errmsg.severity)
4949
, incomplete(errmsg.incomplete)
50-
, inconclusive(errmsg.inconclusive)
50+
, inconclusive(errmsg.certainty == Certainty::inconclusive)
5151
, summary(QString::fromStdString(errmsg.shortMessage()))
5252
, message(QString::fromStdString(errmsg.verboseMessage()))
5353
, cwe(errmsg.cwe.id)

gui/mainwindow.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -974,22 +974,22 @@ Settings MainWindow::getCppcheckSettings()
974974
addIncludeDirs(includes, result);
975975
}
976976

977-
result.addEnabled("warning");
978-
result.addEnabled("style");
979-
result.addEnabled("performance");
980-
result.addEnabled("portability");
981-
result.addEnabled("information");
982-
result.addEnabled("missingInclude");
977+
result.severity.enable(Severity::warning);
978+
result.severity.enable(Severity::style);
979+
result.severity.enable(Severity::performance);
980+
result.severity.enable(Severity::portability);
981+
result.severity.enable(Severity::information);
982+
result.checks.enable(Checks::missingInclude);
983983
if (!result.buildDir.empty())
984-
result.addEnabled("unusedFunction");
984+
result.checks.enable(Checks::unusedFunction);
985985
result.debugwarnings = mSettings->value(SETTINGS_SHOW_DEBUG_WARNINGS, false).toBool();
986986
result.quiet = false;
987987
result.verbose = true;
988988
result.force = mSettings->value(SETTINGS_CHECK_FORCE, 1).toBool();
989989
result.xml = false;
990990
result.jobs = mSettings->value(SETTINGS_CHECK_THREADS, 1).toInt();
991991
result.inlineSuppressions = mSettings->value(SETTINGS_INLINE_SUPPRESSIONS, false).toBool();
992-
result.inconclusive = mSettings->value(SETTINGS_INCONCLUSIVE_ERRORS, false).toBool();
992+
result.certainty.setEnabled(Certainty::inconclusive, mSettings->value(SETTINGS_INCONCLUSIVE_ERRORS, false).toBool());
993993
if (!mProjectFile || result.platformType == cppcheck::Platform::Unspecified)
994994
result.platform((cppcheck::Platform::PlatformType) mSettings->value(SETTINGS_CHECKED_PLATFORM, 0).toInt());
995995
result.standards.setCPP(mSettings->value(SETTINGS_STD_CPP, QString()).toString().toStdString());

lib/bughuntingchecks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ static void uninit(const Token *tok, const ExprEngine::Value &value, ExprEngine:
474474
return;
475475
}
476476

477-
if (inconclusive && !dataBase->settings->inconclusive)
477+
if (inconclusive && !dataBase->settings->certainty.isEnabled(Certainty::inconclusive))
478478
return;
479479

480480
// Avoid FP for array declaration

lib/check.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ void Check::reportError(const ErrorMessage &errmsg)
4646
}
4747

4848

49-
void Check::reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, bool inconclusive)
49+
void Check::reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty)
5050
{
51-
const ErrorMessage errmsg(callstack, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, inconclusive, mSettings ? mSettings->bugHunting : false);
51+
const ErrorMessage errmsg(callstack, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty, mSettings ? mSettings->bugHunting : false);
5252
if (mErrorLogger)
5353
mErrorLogger->reportErr(errmsg);
5454
else
5555
reportError(errmsg);
5656
}
5757

58-
void Check::reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, bool inconclusive)
58+
void Check::reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty)
5959
{
60-
const ErrorMessage errmsg(errorPath, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, inconclusive, mSettings ? mSettings->bugHunting : false);
60+
const ErrorMessage errmsg(errorPath, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty, mSettings ? mSettings->bugHunting : false);
6161
if (mErrorLogger)
6262
mErrorLogger->reportErr(errmsg);
6363
else

lib/check.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,24 @@ class CPPCHECKLIB Check {
133133

134134
/** report an error */
135135
void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg) {
136-
reportError(tok, severity, id, msg, CWE(0U), false);
136+
reportError(tok, severity, id, msg, CWE(0U), Certainty::normal);
137137
}
138138

139139
/** report an error */
140-
void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, bool inconclusive) {
140+
void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty) {
141141
const std::list<const Token *> callstack(1, tok);
142-
reportError(callstack, severity, id, msg, cwe, inconclusive);
142+
reportError(callstack, severity, id, msg, cwe, certainty);
143143
}
144144

145145
/** report an error */
146146
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) {
147-
reportError(callstack, severity, id, msg, CWE(0U), false);
147+
reportError(callstack, severity, id, msg, CWE(0U), Certainty::normal);
148148
}
149149

150150
/** report an error */
151-
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, bool inconclusive);
151+
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty);
152152

153-
void reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, bool inconclusive);
153+
void reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty);
154154

155155
ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, const std::string& bug) const;
156156

lib/check64bit.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace {
4242

4343
void Check64BitPortability::pointerassignment()
4444
{
45-
if (!mSettings->isEnabled(Settings::PORTABILITY))
45+
if (!mSettings->severity.isEnabled(Severity::portability))
4646
return;
4747

4848
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
@@ -124,7 +124,7 @@ void Check64BitPortability::assignmentAddressToIntegerError(const Token *tok)
124124
"Assigning a pointer to an integer (int/long/etc) is not portable across different platforms and "
125125
"compilers. For example in 32-bit Windows and linux they are same width, but in 64-bit Windows and linux "
126126
"they are of different width. In worst case you end up assigning 64-bit address to 32-bit integer. The safe "
127-
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, false);
127+
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, Certainty::normal);
128128
}
129129

130130
void Check64BitPortability::assignmentIntegerToAddressError(const Token *tok)
@@ -135,7 +135,7 @@ void Check64BitPortability::assignmentIntegerToAddressError(const Token *tok)
135135
"Assigning an integer (int/long/etc) to a pointer is not portable across different platforms and "
136136
"compilers. For example in 32-bit Windows and linux they are same width, but in 64-bit Windows and linux "
137137
"they are of different width. In worst case you end up assigning 64-bit integer to 32-bit pointer. The safe "
138-
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, false);
138+
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, Certainty::normal);
139139
}
140140

141141
void Check64BitPortability::returnPointerError(const Token *tok)
@@ -146,7 +146,7 @@ void Check64BitPortability::returnPointerError(const Token *tok)
146146
"Returning an address value in a function with integer (int/long/etc) return type is not portable across "
147147
"different platforms and compilers. For example in 32-bit Windows and Linux they are same width, but in "
148148
"64-bit Windows and Linux they are of different width. In worst case you end up casting 64-bit address down "
149-
"to 32-bit integer. The safe way is to always return an integer.", CWE758, false);
149+
"to 32-bit integer. The safe way is to always return an integer.", CWE758, Certainty::normal);
150150
}
151151

152152
void Check64BitPortability::returnIntegerError(const Token *tok)
@@ -157,5 +157,5 @@ void Check64BitPortability::returnIntegerError(const Token *tok)
157157
"Returning an integer (int/long/etc) in a function with pointer return type is not portable across different "
158158
"platforms and compilers. For example in 32-bit Windows and Linux they are same width, but in 64-bit Windows "
159159
"and Linux they are of different width. In worst case you end up casting 64-bit integer down to 32-bit pointer. "
160-
"The safe way is to always return a pointer.", CWE758, false);
160+
"The safe way is to always return a pointer.", CWE758, Certainty::normal);
161161
}

lib/checkassert.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace {
4040

4141
void CheckAssert::assertWithSideEffects()
4242
{
43-
if (!mSettings->isEnabled(Settings::WARNING))
43+
if (!mSettings->severity.isEnabled(Severity::warning))
4444
return;
4545

4646
for (const Token* tok = mTokenizer->list.front(); tok; tok = tok->next()) {
@@ -104,7 +104,7 @@ void CheckAssert::sideEffectInAssertError(const Token *tok, const std::string& f
104104
"Non-pure function: '$symbol' is called inside assert statement. "
105105
"Assert statements are removed from release builds so the code inside "
106106
"assert statement is not executed. If the code is needed also in release "
107-
"builds, this is a bug.", CWE398, false);
107+
"builds, this is a bug.", CWE398, Certainty::normal);
108108
}
109109

110110
void CheckAssert::assignmentInAssertError(const Token *tok, const std::string& varname)
@@ -116,7 +116,7 @@ void CheckAssert::assignmentInAssertError(const Token *tok, const std::string& v
116116
"Variable '$symbol' is modified inside assert statement. "
117117
"Assert statements are removed from release builds so the code inside "
118118
"assert statement is not executed. If the code is needed also in release "
119-
"builds, this is a bug.", CWE398, false);
119+
"builds, this is a bug.", CWE398, Certainty::normal);
120120
}
121121

122122
// checks if side effects happen on the variable prior to tmp

0 commit comments

Comments
 (0)