diff --git a/lib/suppressions.cpp b/lib/suppressions.cpp index fe483e347fe..b45ba151914 100644 --- a/lib/suppressions.cpp +++ b/lib/suppressions.cpp @@ -136,7 +136,10 @@ std::string SuppressionList::parseXmlFile(const char *filename) s.lineNumber = strToInt(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(text); else return std::string("unknown element '") + name + "' in suppressions XML '" + filename + "', expected id/fileName/lineNumber/symbolName/hash."; diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 90c64eb809e..18fce1f1eb2 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -119,6 +119,7 @@ class TestSuppressions : public TestFixture { TEST_CASE(addSuppressionLineMultiple); TEST_CASE(suppressionsParseXmlFile); + TEST_CASE(xmlMacroSuppressions); TEST_CASE(toString); @@ -1705,6 +1706,27 @@ class TestSuppressions : public TestFixture { ASSERT_EQUALS("sym", suppr.symbolName); } + { + ScopedFile file("suppressparsexml.xml", + "\n" + "\n" + "uninitvar\n" + "file.c\n" + "MACRO_NAME\n" + "\n" + ""); + + 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; @@ -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( + "\n" + "\n" + "uninitvar\n" + "file.c\n" + "VAR\n" + "\n" + "", + + "#define VAR x\n" + "int f(void) {\n" + " int VAR;\n" + " return VAR;\n" + "}\n", + + "" + ); + testXmlSuppressions( + "\n" + "\n" + "uninitvar\n" + "file.c\n" + "WRONG\n" + "\n" + "", + + "#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;