Ticket 7792: Suppression both exit_code and syntaxError when call cppcheck suppressions (#1345)

This commit is contained in:
tam do thanh 2018-09-08 16:09:49 +07:00 committed by Daniel Marjamäki
parent d7de46f50e
commit 7ad09b44c3
3 changed files with 51 additions and 3 deletions

View File

@ -54,7 +54,7 @@ static TimerResults S_timerResults;
static const CWE CWE398(398U); // Indicator of Poor Code Quality
CppCheck::CppCheck(ErrorLogger &errorLogger, bool useGlobalSuppressions)
: mErrorLogger(errorLogger), mExitCode(0), mUseGlobalSuppressions(useGlobalSuppressions), mTooManyConfigs(false), mSimplify(true)
: mErrorLogger(errorLogger), mExitCode(0), mSuppressInternalErrorFound(false), mUseGlobalSuppressions(useGlobalSuppressions), mTooManyConfigs(false), mSimplify(true)
{
}
@ -108,6 +108,7 @@ unsigned int CppCheck::check(const ImportProject::FileSettings &fs)
unsigned int CppCheck::checkFile(const std::string& filename, const std::string &cfgname, std::istream& fileStream)
{
mExitCode = 0;
mSuppressInternalErrorFound = false;
// only show debug warnings for accepted C/C++ source files
if (!Path::acceptFile(filename))
@ -450,6 +451,10 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
false);
reportErr(errmsg);
if(mSuppressInternalErrorFound){
internalErrorFound = false;
continue;
}
}
}
@ -499,6 +504,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
if (internalErrorFound && (mExitCode==0)) {
mExitCode = 1;
}
return mExitCode;
}
@ -749,6 +755,8 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st
void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg)
{
mSuppressInternalErrorFound = false;
if (!mSettings.library.reportErrors(msg.file0))
return;
@ -763,11 +771,15 @@ void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg)
const Suppressions::ErrorMessage errorMessage = msg.toSuppressionsErrorMessage();
if (mUseGlobalSuppressions) {
if (mSettings.nomsg.isSuppressed(errorMessage))
if (mSettings.nomsg.isSuppressed(errorMessage)) {
mSuppressInternalErrorFound = true;
return;
}
} else {
if (mSettings.nomsg.isSuppressedLocal(errorMessage))
if (mSettings.nomsg.isSuppressedLocal(errorMessage)) {
mSuppressInternalErrorFound = true;
return;
}
}
if (!mSettings.nofail.isSuppressed(errorMessage) && (mUseGlobalSuppressions || !mSettings.nomsg.isSuppressed(errorMessage)))

View File

@ -215,6 +215,8 @@ private:
unsigned int mExitCode;
bool mSuppressInternalErrorFound;
bool mUseGlobalSuppressions;
/** Are there too many configs? */

View File

@ -64,6 +64,8 @@ private:
TEST_CASE(unusedFunction);
TEST_CASE(matchglob);
TEST_CASE(suppressingSyntaxErrorAndExitCode);
}
void suppressionsBadId1() const {
@ -583,6 +585,38 @@ private:
ASSERT_EQUALS(true, Suppressions::matchglob("?y?", "xyz"));
ASSERT_EQUALS(true, Suppressions::matchglob("?/?/?", "x/y/z"));
}
void suppressingSyntaxErrorAndExitCode() {
std::map<std::string, std::string> files;
files["test.cpp"] = "fi if;";
ASSERT_EQUALS(0, checkSuppression(files, "*:test.cpp"));
ASSERT_EQUALS("", errout.str());
// multi files, but only suppression one
std::map<std::string, std::string> mfiles;
mfiles["test.cpp"] = "fi if;";
mfiles["test2.cpp"] = "fi if";
ASSERT_EQUALS(1, checkSuppression(mfiles, "*:test.cpp"));
ASSERT_EQUALS("[test2.cpp:1]: (error) syntax error\n", errout.str());
// multi error in file, but only suppression one error
std::map<std::string, std::string> file2;
file2["test.cpp"] = "fi fi\n"
"if if;";
ASSERT_EQUALS(1, checkSuppression(file2, "*:test.cpp:1")); // suppress all error at line 1 of test.cpp
ASSERT_EQUALS("[test.cpp:2]: (error) syntax error\n", errout.str());
// multi error in file, but only suppression one error (2)
std::map<std::string, std::string> file3;
file3["test.cpp"] = "void f(int x, int y){\n"
" int a = x/0;\n"
" int b = y/0;\n"
"}\n"
"f(0, 1);\n";
ASSERT_EQUALS(1, checkSuppression(file3, "zerodiv:test.cpp:3")); // suppress 'errordiv' at line 3 of test.cpp
}
};
REGISTER_TEST(TestSuppressions)