Suppressions: Allow symbol-based inline suppressions
This commit is contained in:
parent
17b4721bd2
commit
f677322c69
|
@ -70,8 +70,7 @@ Preprocessor::~Preprocessor()
|
|||
|
||||
static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_settings)
|
||||
{
|
||||
std::list<std::string> suppressionIDs;
|
||||
|
||||
std::list<Suppressions::Suppression> inlineSuppressions;
|
||||
for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) {
|
||||
if (tok->comment) {
|
||||
std::istringstream iss(tok->str.substr(2));
|
||||
|
@ -79,13 +78,23 @@ static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_se
|
|||
iss >> word;
|
||||
if (word != "cppcheck-suppress")
|
||||
continue;
|
||||
iss >> word;
|
||||
if (iss)
|
||||
suppressionIDs.push_back(word);
|
||||
|
||||
Suppressions::Suppression s;
|
||||
iss >> s.errorId;
|
||||
if (!iss)
|
||||
continue;
|
||||
while (iss) {
|
||||
iss >> word;
|
||||
if (!iss)
|
||||
break;
|
||||
if (word.compare(0,11,"symbolName=")==0)
|
||||
s.symbolName = word.substr(11);
|
||||
}
|
||||
inlineSuppressions.push_back(s);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (suppressionIDs.empty())
|
||||
if (inlineSuppressions.empty())
|
||||
continue;
|
||||
|
||||
// Relative filename
|
||||
|
@ -98,12 +107,15 @@ static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_se
|
|||
}
|
||||
}
|
||||
}
|
||||
relativeFilename = Path::simplifyPath(relativeFilename);
|
||||
|
||||
// Add the suppressions.
|
||||
for (const std::string &errorId : suppressionIDs) {
|
||||
_settings.nomsg.addSuppression(Suppressions::Suppression(errorId, relativeFilename, tok->location.line));
|
||||
for (Suppressions::Suppression &suppr : inlineSuppressions) {
|
||||
suppr.fileName = relativeFilename;
|
||||
suppr.lineNumber = tok->location.line;
|
||||
_settings.nomsg.addSuppression(suppr);
|
||||
}
|
||||
suppressionIDs.clear();
|
||||
inlineSuppressions.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,6 +47,8 @@ private:
|
|||
TEST_CASE(suppressionsMultiFile);
|
||||
TEST_CASE(suppressionsPathSeparator);
|
||||
|
||||
TEST_CASE(inlinesuppress_symbolname);
|
||||
|
||||
TEST_CASE(inlinesuppress_unusedFunction); // #4210 - unusedFunction
|
||||
TEST_CASE(globalsuppress_unusedFunction); // #4946
|
||||
TEST_CASE(suppressionWithRelativePaths); // #4733
|
||||
|
@ -395,6 +397,26 @@ private:
|
|||
ASSERT_EQUALS(true, s2.isSuppressed(errorMessage("abc", "include/1.h", 142)));
|
||||
}
|
||||
|
||||
void inlinesuppress_symbolname() {
|
||||
Suppressions suppressions;
|
||||
|
||||
checkSuppression("void f() {\n"
|
||||
" int a;\n"
|
||||
" /* cppcheck-suppress uninitvar symbolName=a */\n"
|
||||
" a++;\n"
|
||||
"}\n",
|
||||
"");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
checkSuppression("void f() {\n"
|
||||
" int a,b;\n"
|
||||
" /* cppcheck-suppress uninitvar symbolName=b */\n"
|
||||
" a++; b++;\n"
|
||||
"}\n",
|
||||
"");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
|
||||
}
|
||||
|
||||
void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction"
|
||||
Suppressions suppressions;
|
||||
suppressions.addSuppression(Suppressions::Suppression("unusedFunction", "test.c", 3));
|
||||
|
|
Loading…
Reference in New Issue