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
5 changes: 4 additions & 1 deletion lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ std::string SuppressionList::parseXmlFile(const char *filename)
s.lineNumber = strToInt<int>(text);
else if (std::strcmp(name, "symbolName") == 0)
s.symbolName = text;
else if (*text && std::strcmp(name, "hash") == 0)
else if (std::strcmp(name, "macroName") == 0) {
s.macroName = text;
s.type = SuppressionList::Type::macro;
} else if (*text && std::strcmp(name, "hash") == 0)
s.hash = strToInt<std::size_t>(text);
else
return std::string("unknown element '") + name + "' in suppressions XML '" + filename + "', expected id/fileName/lineNumber/symbolName/hash.";
Expand Down
85 changes: 85 additions & 0 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class TestSuppressions : public TestFixture {
TEST_CASE(addSuppressionLineMultiple);

TEST_CASE(suppressionsParseXmlFile);
TEST_CASE(xmlMacroSuppressions);

TEST_CASE(toString);

Expand Down Expand Up @@ -1705,6 +1706,27 @@ class TestSuppressions : public TestFixture {
ASSERT_EQUALS("sym", suppr.symbolName);
}

{
ScopedFile file("suppressparsexml.xml",
"<suppressions>\n"
"<suppress>\n"
"<id>uninitvar</id>\n"
"<fileName>file.c</fileName>\n"
"<macroName>MACRO_NAME</macroName>\n"
"</suppress>\n"
"</suppressions>");

SuppressionList supprList;
ASSERT_EQUALS("", supprList.parseXmlFile(file.path().c_str()));
const auto& supprs = supprList.getSuppressions();
ASSERT_EQUALS(1, supprs.size());
const auto& suppr = *supprs.cbegin();
ASSERT_EQUALS("uninitvar", suppr.errorId);
ASSERT_EQUALS("file.c", suppr.fileName);
ASSERT_EQUALS("MACRO_NAME", suppr.macroName);
ASSERT_EQUALS_ENUM(SuppressionList::Type::macro, suppr.type);
}

// no file specified
{
SuppressionList supprList;
Expand Down Expand Up @@ -1758,6 +1780,69 @@ class TestSuppressions : public TestFixture {
}
}

#define testXmlSuppressions(...) testXmlSuppressions_(__FILE__,__LINE__,__VA_ARGS__)
void testXmlSuppressions_(const char *thisfile,
int thisline,
const std::string &xml,
const std::string &code,
const std::string &expected)
{
const char *xmlpath = "testsupressions.xml";
const char *sourcepath = "test.c";

Suppressions supprs;
const ScopedFile xmlfile(xmlpath, xml);
ASSERT_EQUALS_LOC("", supprs.nomsg.parseXmlFile(xmlpath), thisfile, thisline);

Settings settings;
settings.templateFormat = templateFormat;
settings.quiet = true;

const FileWithDetails sourcefile(sourcepath, Standards::Language::C, 0);
CppCheck instance(settings, supprs, *this, nullptr, true, nullptr);
instance.checkBuffer(sourcefile, code.c_str(), code.size());

ASSERT_EQUALS_LOC(expected, errout_str(), thisfile, thisline);
}

void xmlMacroSuppressions()
{
testXmlSuppressions(
"<suppressions>\n"
"<suppress>\n"
"<id>uninitvar</id>\n"
"<fileName>file.c</fileName>\n"
"<macroName>VAR</macroName>\n"
"</suppress>\n"
"</suppressions>",

"#define VAR x\n"
"int f(void) {\n"
" int VAR;\n"
" return VAR;\n"
"}\n",

""
);
testXmlSuppressions(
"<suppressions>\n"
"<suppress>\n"
"<id>uninitvar</id>\n"
"<fileName>file.c</fileName>\n"
"<macroName>WRONG</macroName>\n"
"</suppress>\n"
"</suppressions>",

"#define VAR x\n"
"int f(void) {\n"
" int VAR;\n"
" return VAR;\n"
"}\n",

"[test.c:4:12]: (error) Uninitialized variable: x [uninitvar]\n"
);
}

void addSuppressionDuplicate() const {
SuppressionList supprs;

Expand Down
Loading