Better handling of CppcheckID suppressions

This commit is contained in:
Daniel Marjamäki 2020-07-16 15:27:07 +02:00
parent 47ff29f1c8
commit 118e9eb3e2
2 changed files with 24 additions and 8 deletions

View File

@ -1033,7 +1033,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti
const std::string &path = mPath; const std::string &path = mPath;
std::list<std::string> paths; std::list<std::string> paths;
std::list<std::string> suppressions; std::list<Suppressions::Suppression> suppressions;
Settings temp; Settings temp;
guiProject.analyzeAllVsConfigs.clear(); guiProject.analyzeAllVsConfigs.clear();
@ -1072,8 +1072,20 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti
guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::IgnorePathName, CppcheckXml::IgnorePathNameAttrib); guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::IgnorePathName, CppcheckXml::IgnorePathNameAttrib);
else if (strcmp(node->Name(), CppcheckXml::LibrariesElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::LibrariesElementName) == 0)
guiProject.libraries = readXmlStringList(node, "", CppcheckXml::LibraryElementName, nullptr); guiProject.libraries = readXmlStringList(node, "", CppcheckXml::LibraryElementName, nullptr);
else if (strcmp(node->Name(), CppcheckXml::SuppressionsElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::SuppressionsElementName) == 0) {
suppressions = readXmlStringList(node, "", CppcheckXml::SuppressionElementName, nullptr); for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) {
if (strcmp(child->Name(), CppcheckXml::SuppressionsElementName) != 0)
continue;
auto read = [](const char *s, const char *def) { return s ? s : def; };
Suppressions::Suppression s;
s.errorId = read(child->GetText(), "");
s.fileName = read(child->Attribute("fileName"), "");
s.lineNumber = child->IntAttribute("lineNumber", Suppressions::Suppression::NO_LINE);
s.symbolName = read(child->Attribute("symbolName"), "");
std::istringstream(read(child->Attribute("cppcheck-id"), "0")) >> s.cppcheckId;
suppressions.push_back(s);
}
}
else if (strcmp(node->Name(), CppcheckXml::VSConfigurationElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::VSConfigurationElementName) == 0)
guiProject.checkVsConfigs = readXmlStringList(node, "", CppcheckXml::VSConfigurationName, nullptr); guiProject.checkVsConfigs = readXmlStringList(node, "", CppcheckXml::VSConfigurationName, nullptr);
else if (strcmp(node->Name(), CppcheckXml::PlatformElementName) == 0) else if (strcmp(node->Name(), CppcheckXml::PlatformElementName) == 0)
@ -1130,8 +1142,8 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti
for (const std::string &p : paths) for (const std::string &p : paths)
guiProject.pathNames.push_back(p); guiProject.pathNames.push_back(p);
for (const std::string &supp : suppressions) for (const Suppressions::Suppression &supp : suppressions)
settings->nomsg.addSuppressionLine(supp); settings->nomsg.addSuppression(supp);
settings->checkHeaders = temp.checkHeaders; settings->checkHeaders = temp.checkHeaders;
settings->checkUnusedTemplates = temp.checkUnusedTemplates; settings->checkUnusedTemplates = temp.checkUnusedTemplates;
settings->maxCtuDepth = temp.maxCtuDepth; settings->maxCtuDepth = temp.maxCtuDepth;

View File

@ -98,8 +98,10 @@ std::string Suppressions::parseXmlFile(const char *filename)
s.lineNumber = std::atoi(text); s.lineNumber = std::atoi(text);
else if (std::strcmp(e2->Name(), "symbolName") == 0) else if (std::strcmp(e2->Name(), "symbolName") == 0)
s.symbolName = text; s.symbolName = text;
else if (*text && std::strcmp(e2->Name(), "cppcheckId") == 0)
std::istringstream(text) >> s.cppcheckId;
else else
return "Unknown suppression element <" + std::string(e2->Name()) + ">, expected <id>/<fileName>/<lineNumber>/<symbolName>"; return "Unknown suppression element <" + std::string(e2->Name()) + ">, expected <id>/<fileName>/<lineNumber>/<symbolName>/<cppcheckId>";
} }
const std::string err = addSuppression(s); const std::string err = addSuppression(s);
@ -317,6 +319,8 @@ std::string Suppressions::Suppression::getText() const
ret += " lineNumber=" + MathLib::toString(lineNumber); ret += " lineNumber=" + MathLib::toString(lineNumber);
if (!symbolName.empty()) if (!symbolName.empty())
ret += " symbolName=" + symbolName; ret += " symbolName=" + symbolName;
if (cppcheckId > 0)
ret += " cppcheckId=" + MathLib::toString(cppcheckId);
if (ret.compare(0,1," ")==0) if (ret.compare(0,1," ")==0)
return ret.substr(1); return ret.substr(1);
return ret; return ret;
@ -360,13 +364,13 @@ void Suppressions::dump(std::ostream & out) const
out << " lineNumber=\"" << suppression.lineNumber << '"'; out << " lineNumber=\"" << suppression.lineNumber << '"';
if (!suppression.symbolName.empty()) if (!suppression.symbolName.empty())
out << " symbolName=\"" << ErrorLogger::toxml(suppression.symbolName) << '\"'; out << " symbolName=\"" << ErrorLogger::toxml(suppression.symbolName) << '\"';
if (suppression.cppcheckId > 0)
out << " cppcheckId=\"" << suppression.cppcheckId << '\"';
out << " />" << std::endl; out << " />" << std::endl;
} }
out << " </suppressions>" << std::endl; out << " </suppressions>" << std::endl;
} }
#include <iostream>
std::list<Suppressions::Suppression> Suppressions::getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const std::list<Suppressions::Suppression> Suppressions::getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const
{ {
std::list<Suppression> result; std::list<Suppression> result;