Skip to content

Commit 93546a2

Browse files
authored
Fix #14565 xml suppression: add macro attribute for specifying macro name (#8749)
1 parent 354edf0 commit 93546a2

2 files changed

Lines changed: 85 additions & 1 deletion

File tree

lib/suppressions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ std::string SuppressionList::parseXmlFile(const char *filename)
136136
s.lineNumber = strToInt<int>(text);
137137
else if (std::strcmp(name, "symbolName") == 0)
138138
s.symbolName = text;
139-
else if (*text && std::strcmp(name, "hash") == 0)
139+
else if (std::strcmp(name, "macroName") == 0) {
140+
s.macroName = text;
141+
s.type = SuppressionList::Type::macro;
142+
} else if (*text && std::strcmp(name, "hash") == 0)
140143
s.hash = strToInt<std::size_t>(text);
141144
else
142145
return std::string("unknown element '") + name + "' in suppressions XML '" + filename + "', expected id/fileName/lineNumber/symbolName/hash.";

test/testsuppressions.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class TestSuppressions : public TestFixture {
119119
TEST_CASE(addSuppressionLineMultiple);
120120

121121
TEST_CASE(suppressionsParseXmlFile);
122+
TEST_CASE(xmlMacroSuppressions);
122123

123124
TEST_CASE(toString);
124125

@@ -1705,6 +1706,25 @@ class TestSuppressions : public TestFixture {
17051706
ASSERT_EQUALS("sym", suppr.symbolName);
17061707
}
17071708

1709+
{
1710+
ScopedFile file("suppressparsexml.xml",
1711+
"<suppressions>\n"
1712+
"<suppress>\n"
1713+
"<id>uninitvar</id>\n"
1714+
"<macroName>MACRO_NAME</macroName>\n"
1715+
"</suppress>\n"
1716+
"</suppressions>");
1717+
1718+
SuppressionList supprList;
1719+
ASSERT_EQUALS("", supprList.parseXmlFile(file.path().c_str()));
1720+
const auto& supprs = supprList.getSuppressions();
1721+
ASSERT_EQUALS(1, supprs.size());
1722+
const auto& suppr = *supprs.cbegin();
1723+
ASSERT_EQUALS("uninitvar", suppr.errorId);
1724+
ASSERT_EQUALS("MACRO_NAME", suppr.macroName);
1725+
ASSERT_EQUALS_ENUM(SuppressionList::Type::macro, suppr.type);
1726+
}
1727+
17081728
// no file specified
17091729
{
17101730
SuppressionList supprList;
@@ -1758,6 +1778,67 @@ class TestSuppressions : public TestFixture {
17581778
}
17591779
}
17601780

1781+
#define testXmlSuppressions(...) testXmlSuppressions_(__FILE__,__LINE__,__VA_ARGS__)
1782+
void testXmlSuppressions_(const char *thisfile,
1783+
int thisline,
1784+
const std::string &xml,
1785+
const std::string &code,
1786+
const std::string &expected)
1787+
{
1788+
const char *xmlpath = "testsupressions.xml";
1789+
const char *sourcepath = "test.c";
1790+
1791+
Suppressions supprs;
1792+
const ScopedFile xmlfile(xmlpath, xml);
1793+
ASSERT_EQUALS_LOC("", supprs.nomsg.parseXmlFile(xmlpath), thisfile, thisline);
1794+
1795+
Settings settings;
1796+
settings.templateFormat = templateFormat;
1797+
settings.quiet = true;
1798+
1799+
const FileWithDetails sourcefile(sourcepath, Standards::Language::C, 0);
1800+
CppCheck instance(settings, supprs, *this, nullptr, true, nullptr);
1801+
instance.checkBuffer(sourcefile, code.c_str(), code.size());
1802+
1803+
ASSERT_EQUALS_LOC(expected, errout_str(), thisfile, thisline);
1804+
}
1805+
1806+
void xmlMacroSuppressions()
1807+
{
1808+
testXmlSuppressions(
1809+
"<suppressions>\n"
1810+
"<suppress>\n"
1811+
"<id>uninitvar</id>\n"
1812+
"<macroName>VAR</macroName>\n"
1813+
"</suppress>\n"
1814+
"</suppressions>",
1815+
1816+
"#define VAR x\n"
1817+
"int f(void) {\n"
1818+
" int VAR;\n"
1819+
" return VAR;\n"
1820+
"}\n",
1821+
1822+
""
1823+
);
1824+
testXmlSuppressions(
1825+
"<suppressions>\n"
1826+
"<suppress>\n"
1827+
"<id>uninitvar</id>\n"
1828+
"<macroName>WRONG</macroName>\n"
1829+
"</suppress>\n"
1830+
"</suppressions>",
1831+
1832+
"#define VAR x\n"
1833+
"int f(void) {\n"
1834+
" int VAR;\n"
1835+
" return VAR;\n"
1836+
"}\n",
1837+
1838+
"[test.c:4:12]: (error) Uninitialized variable: x [uninitvar]\n"
1839+
);
1840+
}
1841+
17611842
void addSuppressionDuplicate() const {
17621843
SuppressionList supprs;
17631844

0 commit comments

Comments
 (0)