From 74ec5705de4e61b941c2fddfd1cb8e98ba4740f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Wed, 18 Sep 2024 20:33:47 +0200 Subject: [PATCH] some XML parsing cleanups (#6515) --- cli/cmdlineparser.cpp | 18 +-- lib/checkbufferoverrun.cpp | 5 +- lib/checkunusedfunctions.cpp | 5 +- lib/ctu.cpp | 7 +- lib/errorlogger.cpp | 5 +- lib/importproject.cpp | 222 ++++++++++++++++++----------------- lib/platform.cpp | 30 ++--- lib/suppressions.cpp | 15 +-- lib/utils.h | 8 +- 9 files changed, 171 insertions(+), 144 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 0820b1fdabb..d3e31cdf307 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -1157,33 +1157,35 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a Settings::Rule rule; for (const tinyxml2::XMLElement *subnode = node->FirstChildElement(); subnode; subnode = subnode->NextSiblingElement()) { + const char * const subname = subnode->Name(); const char * const subtext = subnode->GetText(); - if (std::strcmp(subnode->Name(), "tokenlist") == 0) { + if (std::strcmp(subname, "tokenlist") == 0) { rule.tokenlist = empty_if_null(subtext); } - else if (std::strcmp(subnode->Name(), "pattern") == 0) { + else if (std::strcmp(subname, "pattern") == 0) { rule.pattern = empty_if_null(subtext); } - else if (std::strcmp(subnode->Name(), "message") == 0) { + else if (std::strcmp(subname, "message") == 0) { for (const tinyxml2::XMLElement *msgnode = subnode->FirstChildElement(); msgnode; msgnode = msgnode->NextSiblingElement()) { + const char * const msgname = msgnode->Name(); const char * const msgtext = msgnode->GetText(); - if (std::strcmp(msgnode->Name(), "severity") == 0) { + if (std::strcmp(msgname, "severity") == 0) { rule.severity = severityFromString(empty_if_null(msgtext)); } - else if (std::strcmp(msgnode->Name(), "id") == 0) { + else if (std::strcmp(msgname, "id") == 0) { rule.id = empty_if_null(msgtext); } - else if (std::strcmp(msgnode->Name(), "summary") == 0) { + else if (std::strcmp(msgname, "summary") == 0) { rule.summary = empty_if_null(msgtext); } else { - mLogger.printError("unable to load rule-file '" + ruleFile + "' - unknown element '" + msgnode->Name() + "' encountered in 'message'."); + mLogger.printError("unable to load rule-file '" + ruleFile + "' - unknown element '" + msgname + "' encountered in 'message'."); return Result::Fail; } } } else { - mLogger.printError("unable to load rule-file '" + ruleFile + "' - unknown element '" + subnode->Name() + "' encountered in 'rule'."); + mLogger.printError("unable to load rule-file '" + ruleFile + "' - unknown element '" + subname + "' encountered in 'rule'."); return Result::Fail; } } diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 204b2b974c2..cc769deebe0 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -966,9 +966,10 @@ Check::FileInfo * CheckBufferOverrun::loadFileInfoFromXml(const tinyxml2::XMLEle auto *fileInfo = new MyFileInfo; for (const tinyxml2::XMLElement *e = xmlElement->FirstChildElement(); e; e = e->NextSiblingElement()) { - if (e->Name() == arrayIndex) + const char* name = e->Name(); + if (name == arrayIndex) fileInfo->unsafeArrayIndex = CTU::loadUnsafeUsageListFromXml(e); - else if (e->Name() == pointerArith) + else if (name == pointerArith) fileInfo->unsafePointerArith = CTU::loadUnsafeUsageListFromXml(e); } diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index b25d93fa832..a5978a9072b 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -450,11 +450,12 @@ void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLo const char* functionName = e2->Attribute("functionName"); if (functionName == nullptr) continue; - if (std::strcmp(e2->Name(),"functioncall") == 0) { + const char* name = e2->Name(); + if (std::strcmp(name,"functioncall") == 0) { calls.insert(functionName); continue; } - if (std::strcmp(e2->Name(),"functiondecl") == 0) { + if (std::strcmp(name,"functiondecl") == 0) { const char* lineNumber = e2->Attribute("lineNumber"); if (lineNumber) { // cppcheck-suppress templateInstantiation - TODO: fix this - see #11631 diff --git a/lib/ctu.cpp b/lib/ctu.cpp index 4f542bacf48..0cf0273cfe0 100644 --- a/lib/ctu.cpp +++ b/lib/ctu.cpp @@ -172,7 +172,7 @@ static std::string readAttrString(const tinyxml2::XMLElement *e, const char *att const char *value = e->Attribute(attr); if (!value && error) *error = true; - return value ? value : ""; + return empty_if_null(value); } static long long readAttrInt(const tinyxml2::XMLElement *e, const char *attr, bool *error) @@ -232,11 +232,12 @@ bool CTU::FileInfo::NestedCall::loadFromXml(const tinyxml2::XMLElement *xmlEleme void CTU::FileInfo::loadFromXml(const tinyxml2::XMLElement *xmlElement) { for (const tinyxml2::XMLElement *e = xmlElement->FirstChildElement(); e; e = e->NextSiblingElement()) { - if (std::strcmp(e->Name(), "function-call") == 0) { + const char* name = e->Name(); + if (std::strcmp(name, "function-call") == 0) { FunctionCall functionCall; if (functionCall.loadFromXml(e)) functionCalls.push_back(std::move(functionCall)); - } else if (std::strcmp(e->Name(), "nested-call") == 0) { + } else if (std::strcmp(name, "nested-call") == 0) { NestedCall nestedCall; if (nestedCall.loadFromXml(e)) nestedCalls.push_back(std::move(nestedCall)); diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 17c5a82c977..06901a488de 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -190,7 +190,8 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg) hash = attr ? strToInt(attr) : 0; for (const tinyxml2::XMLElement *e = errmsg->FirstChildElement(); e; e = e->NextSiblingElement()) { - if (std::strcmp(e->Name(),"location")==0) { + const char* name = e->Name(); + if (std::strcmp(name,"location")==0) { const char *strfile = e->Attribute("file"); const char *strinfo = e->Attribute("info"); const char *strline = e->Attribute("line"); @@ -201,7 +202,7 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg) const int line = strline ? strToInt(strline) : 0; const int column = strcolumn ? strToInt(strcolumn) : 0; callStack.emplace_front(file, info, line, column); - } else if (std::strcmp(e->Name(),"symbol")==0) { + } else if (std::strcmp(name,"symbol")==0) { mSymbolNames += e->GetText(); } } diff --git a/lib/importproject.cpp b/lib/importproject.cpp index d3a28530435..e44f7ad70f4 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -484,12 +484,14 @@ namespace { if (a) name = a; for (const tinyxml2::XMLElement *e = cfg->FirstChildElement(); e; e = e->NextSiblingElement()) { - if (!e->GetText()) + const char * const text = e->GetText(); + if (!text) continue; - if (std::strcmp(e->Name(),"Configuration")==0) - configuration = e->GetText(); - else if (std::strcmp(e->Name(),"Platform")==0) { - platformStr = e->GetText(); + const char * ename = e->Name(); + if (std::strcmp(ename,"Configuration")==0) + configuration = text; + else if (std::strcmp(ename,"Platform")==0) { + platformStr = text; if (platformStr == "Win32") platform = Win32; else if (platformStr == "x64") @@ -511,37 +513,41 @@ namespace { if (condAttr) condition = condAttr; for (const tinyxml2::XMLElement *e1 = idg->FirstChildElement(); e1; e1 = e1->NextSiblingElement()) { - if (std::strcmp(e1->Name(), "ClCompile") == 0) { + const char* name = e1->Name(); + if (std::strcmp(name, "ClCompile") == 0) { enhancedInstructionSet = "StreamingSIMDExtensions2"; for (const tinyxml2::XMLElement *e = e1->FirstChildElement(); e; e = e->NextSiblingElement()) { - if (e->GetText()) { - if (std::strcmp(e->Name(), "PreprocessorDefinitions") == 0) - preprocessorDefinitions = e->GetText(); - else if (std::strcmp(e->Name(), "AdditionalIncludeDirectories") == 0) { - if (!additionalIncludePaths.empty()) - additionalIncludePaths += ';'; - additionalIncludePaths += e->GetText(); - } else if (std::strcmp(e->Name(), "LanguageStandard") == 0) { - if (std::strcmp(e->GetText(), "stdcpp14") == 0) - cppstd = Standards::CPP14; - else if (std::strcmp(e->GetText(), "stdcpp17") == 0) - cppstd = Standards::CPP17; - else if (std::strcmp(e->GetText(), "stdcpp20") == 0) - cppstd = Standards::CPP20; - else if (std::strcmp(e->GetText(), "stdcpplatest") == 0) - cppstd = Standards::CPPLatest; - } else if (std::strcmp(e->Name(), "EnableEnhancedInstructionSet") == 0) { - enhancedInstructionSet = e->GetText(); - } + const char * const text = e->GetText(); + if (!text) + continue; + const char * const ename = e->Name(); + if (std::strcmp(ename, "PreprocessorDefinitions") == 0) + preprocessorDefinitions = text; + else if (std::strcmp(ename, "AdditionalIncludeDirectories") == 0) { + if (!additionalIncludePaths.empty()) + additionalIncludePaths += ';'; + additionalIncludePaths += text; + } else if (std::strcmp(ename, "LanguageStandard") == 0) { + if (std::strcmp(text, "stdcpp14") == 0) + cppstd = Standards::CPP14; + else if (std::strcmp(text, "stdcpp17") == 0) + cppstd = Standards::CPP17; + else if (std::strcmp(text, "stdcpp20") == 0) + cppstd = Standards::CPP20; + else if (std::strcmp(text, "stdcpplatest") == 0) + cppstd = Standards::CPPLatest; + } else if (std::strcmp(ename, "EnableEnhancedInstructionSet") == 0) { + enhancedInstructionSet = text; } } } - else if (std::strcmp(e1->Name(), "Link") == 0) { + else if (std::strcmp(name, "Link") == 0) { for (const tinyxml2::XMLElement *e = e1->FirstChildElement(); e; e = e->NextSiblingElement()) { - if (!e->GetText()) + const char * const text = e->GetText(); + if (!text) continue; if (std::strcmp(e->Name(), "EntryPointSymbol") == 0) { - entryPointSymbol = e->GetText(); + entryPointSymbol = text; } } } @@ -636,9 +642,9 @@ static void importPropertyGroup(const tinyxml2::XMLElement *node, std::mapAttribute("Label"); if (labelAttribute && std::strcmp(labelAttribute, "UserMacros") == 0) { for (const tinyxml2::XMLElement *propertyGroup = node->FirstChildElement(); propertyGroup; propertyGroup = propertyGroup->NextSiblingElement()) { - const std::string name(propertyGroup->Name()); - const char *text = propertyGroup->GetText(); - variables[name] = std::string(text ? text : ""); + const char* name = propertyGroup->Name(); + const char *text = empty_if_null(propertyGroup->GetText()); + variables[name] = text; } } else if (!labelAttribute) { @@ -675,7 +681,8 @@ static void loadVisualStudioProperties(const std::string &props, std::mapFirstChildElement(); node; node = node->NextSiblingElement()) { - if (std::strcmp(node->Name(), "ImportGroup") == 0) { + const char* name = node->Name(); + if (std::strcmp(name, "ImportGroup") == 0) { const char *labelAttribute = node->Attribute("Label"); if (labelAttribute == nullptr || std::strcmp(labelAttribute, "PropertySheets") != 0) continue; @@ -691,9 +698,9 @@ static void loadVisualStudioProperties(const std::string &props, std::mapName(),"PropertyGroup")==0) { + } else if (std::strcmp(name,"PropertyGroup")==0) { importPropertyGroup(node, variables, includePath, nullptr); - } else if (std::strcmp(node->Name(),"ItemDefinitionGroup")==0) { + } else if (std::strcmp(name,"ItemDefinitionGroup")==0) { itemDefinitionGroupList.emplace_back(node, additionalIncludeDirectories); } } @@ -723,7 +730,8 @@ bool ImportProject::importVcxproj(const std::string &filename, std::mapFirstChildElement(); node; node = node->NextSiblingElement()) { - if (std::strcmp(node->Name(), "ItemGroup") == 0) { + const char* name = node->Name(); + if (std::strcmp(name, "ItemGroup") == 0) { const char *labelAttribute = node->Attribute("Label"); if (labelAttribute && std::strcmp(labelAttribute, "ProjectConfigurations") == 0) { for (const tinyxml2::XMLElement *cfg = node->FirstChildElement(); cfg; cfg = cfg->NextSiblingElement()) { @@ -746,11 +754,11 @@ bool ImportProject::importVcxproj(const std::string &filename, std::mapName(), "ItemDefinitionGroup") == 0) { + } else if (std::strcmp(name, "ItemDefinitionGroup") == 0) { itemDefinitionGroupList.emplace_back(node, additionalIncludeDirectories); - } else if (std::strcmp(node->Name(), "PropertyGroup") == 0) { + } else if (std::strcmp(name, "PropertyGroup") == 0) { importPropertyGroup(node, variables, includePath, &useOfMfc); - } else if (std::strcmp(node->Name(), "ImportGroup") == 0) { + } else if (std::strcmp(name, "ImportGroup") == 0) { const char *labelAttribute = node->Attribute("Label"); if (labelAttribute && std::strcmp(labelAttribute, "PropertySheets") == 0) { for (const tinyxml2::XMLElement *e = node->FirstChildElement(); e; e = e->NextSiblingElement()) { @@ -948,7 +956,8 @@ bool ImportProject::importBcb6Prj(const std::string &projectFilename) std::string cflag1; for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) { - if (std::strcmp(node->Name(), "FILELIST") == 0) { + const char* name = node->Name(); + if (std::strcmp(name, "FILELIST") == 0) { for (const tinyxml2::XMLElement *f = node->FirstChildElement(); f; f = f->NextSiblingElement()) { if (std::strcmp(f->Name(), "FILE") == 0) { const char *filename = f->Attribute("FILENAME"); @@ -956,23 +965,24 @@ bool ImportProject::importBcb6Prj(const std::string &projectFilename) compileList.emplace_back(filename); } } - } else if (std::strcmp(node->Name(), "MACROS") == 0) { + } else if (std::strcmp(name, "MACROS") == 0) { for (const tinyxml2::XMLElement *m = node->FirstChildElement(); m; m = m->NextSiblingElement()) { - if (std::strcmp(m->Name(), "INCLUDEPATH") == 0) { + const char* mname = m->Name(); + if (std::strcmp(mname, "INCLUDEPATH") == 0) { const char *v = m->Attribute("value"); if (v) includePath = v; - } else if (std::strcmp(m->Name(), "USERDEFINES") == 0) { + } else if (std::strcmp(mname, "USERDEFINES") == 0) { const char *v = m->Attribute("value"); if (v) userdefines = v; - } else if (std::strcmp(m->Name(), "SYSDEFINES") == 0) { + } else if (std::strcmp(mname, "SYSDEFINES") == 0) { const char *v = m->Attribute("value"); if (v) sysdefines = v; } } - } else if (std::strcmp(node->Name(), "OPTIONS") == 0) { + } else if (std::strcmp(name, "OPTIONS") == 0) { for (const tinyxml2::XMLElement *m = node->FirstChildElement(); m; m = m->NextSiblingElement()) { if (std::strcmp(m->Name(), "CFLAG1") == 0) { const char *v = m->Attribute("value"); @@ -1225,10 +1235,6 @@ static std::string istream_to_string(std::istream &istr) return std::string(std::istreambuf_iterator(istr), eos); } -static const char * readSafe(const char *s, const char *def) { - return s ? s : def; -} - bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *settings) { tinyxml2::XMLDocument doc; @@ -1259,117 +1265,123 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti // TODO: this should support all available command-line options for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) { - if (strcmp(node->Name(), CppcheckXml::RootPathName) == 0) { - if (node->Attribute(CppcheckXml::RootPathNameAttrib)) { - temp.basePaths.push_back(joinRelativePath(path, node->Attribute(CppcheckXml::RootPathNameAttrib))); + const char* name = node->Name(); + if (strcmp(name, CppcheckXml::RootPathName) == 0) { + const char* attr = node->Attribute(CppcheckXml::RootPathNameAttrib); + if (attr) { + temp.basePaths.push_back(joinRelativePath(path, attr)); temp.relativePaths = true; } - } else if (strcmp(node->Name(), CppcheckXml::BuildDirElementName) == 0) - temp.buildDir = joinRelativePath(path, readSafe(node->GetText(), "")); - else if (strcmp(node->Name(), CppcheckXml::IncludeDirElementName) == 0) + } else if (strcmp(name, CppcheckXml::BuildDirElementName) == 0) + temp.buildDir = joinRelativePath(path, empty_if_null(node->GetText())); + else if (strcmp(name, CppcheckXml::IncludeDirElementName) == 0) temp.includePaths = readXmlStringList(node, path, CppcheckXml::DirElementName, CppcheckXml::DirNameAttrib); - else if (strcmp(node->Name(), CppcheckXml::DefinesElementName) == 0) + else if (strcmp(name, CppcheckXml::DefinesElementName) == 0) temp.userDefines = join(readXmlStringList(node, "", CppcheckXml::DefineName, CppcheckXml::DefineNameAttrib), ";"); - else if (strcmp(node->Name(), CppcheckXml::UndefinesElementName) == 0) { + else if (strcmp(name, CppcheckXml::UndefinesElementName) == 0) { for (const std::string &u : readXmlStringList(node, "", CppcheckXml::UndefineName, nullptr)) temp.userUndefs.insert(u); - } else if (strcmp(node->Name(), CppcheckXml::ImportProjectElementName) == 0) { - const std::string t_str = readSafe(node->GetText(), ""); + } else if (strcmp(name, CppcheckXml::ImportProjectElementName) == 0) { + const std::string t_str = empty_if_null(node->GetText()); if (!t_str.empty()) guiProject.projectFile = path + t_str; } - else if (strcmp(node->Name(), CppcheckXml::PathsElementName) == 0) + else if (strcmp(name, CppcheckXml::PathsElementName) == 0) paths = readXmlStringList(node, path, CppcheckXml::PathName, CppcheckXml::PathNameAttrib); - else if (strcmp(node->Name(), CppcheckXml::ExcludeElementName) == 0) + else if (strcmp(name, CppcheckXml::ExcludeElementName) == 0) guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::ExcludePathName, CppcheckXml::ExcludePathNameAttrib); - else if (strcmp(node->Name(), CppcheckXml::FunctionContracts) == 0) + else if (strcmp(name, CppcheckXml::FunctionContracts) == 0) ; - else if (strcmp(node->Name(), CppcheckXml::VariableContractsElementName) == 0) + else if (strcmp(name, CppcheckXml::VariableContractsElementName) == 0) ; - else if (strcmp(node->Name(), CppcheckXml::IgnoreElementName) == 0) + else if (strcmp(name, CppcheckXml::IgnoreElementName) == 0) guiProject.excludedPaths = readXmlStringList(node, "", CppcheckXml::IgnorePathName, CppcheckXml::IgnorePathNameAttrib); - else if (strcmp(node->Name(), CppcheckXml::LibrariesElementName) == 0) + else if (strcmp(name, CppcheckXml::LibrariesElementName) == 0) guiProject.libraries = readXmlStringList(node, "", CppcheckXml::LibraryElementName, nullptr); - else if (strcmp(node->Name(), CppcheckXml::SuppressionsElementName) == 0) { + else if (strcmp(name, CppcheckXml::SuppressionsElementName) == 0) { for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) { if (strcmp(child->Name(), CppcheckXml::SuppressionElementName) != 0) continue; SuppressionList::Suppression s; - s.errorId = readSafe(child->GetText(), ""); - s.fileName = readSafe(child->Attribute("fileName"), ""); + s.errorId = empty_if_null(child->GetText()); + s.fileName = empty_if_null(child->Attribute("fileName")); if (!s.fileName.empty()) s.fileName = joinRelativePath(path, s.fileName); s.lineNumber = child->IntAttribute("lineNumber", SuppressionList::Suppression::NO_LINE); - s.symbolName = readSafe(child->Attribute("symbolName"), ""); - s.hash = strToInt(readSafe(child->Attribute("hash"), "0")); + s.symbolName = empty_if_null(child->Attribute("symbolName")); + s.hash = strToInt(default_if_null(child->Attribute("hash"), "0")); suppressions.push_back(std::move(s)); } - } else if (strcmp(node->Name(), CppcheckXml::VSConfigurationElementName) == 0) + } else if (strcmp(name, CppcheckXml::VSConfigurationElementName) == 0) guiProject.checkVsConfigs = readXmlStringList(node, emptyString, CppcheckXml::VSConfigurationName, nullptr); - else if (strcmp(node->Name(), CppcheckXml::PlatformElementName) == 0) - guiProject.platform = readSafe(node->GetText(), ""); - else if (strcmp(node->Name(), CppcheckXml::AnalyzeAllVsConfigsElementName) == 0) - guiProject.analyzeAllVsConfigs = readSafe(node->GetText(), ""); - else if (strcmp(node->Name(), CppcheckXml::Parser) == 0) + else if (strcmp(name, CppcheckXml::PlatformElementName) == 0) + guiProject.platform = empty_if_null(node->GetText()); + else if (strcmp(name, CppcheckXml::AnalyzeAllVsConfigsElementName) == 0) + guiProject.analyzeAllVsConfigs = empty_if_null(node->GetText()); + else if (strcmp(name, CppcheckXml::Parser) == 0) temp.clang = true; - else if (strcmp(node->Name(), CppcheckXml::AddonsElementName) == 0) { + else if (strcmp(name, CppcheckXml::AddonsElementName) == 0) { const auto& addons = readXmlStringList(node, emptyString, CppcheckXml::AddonElementName, nullptr); temp.addons.insert(addons.cbegin(), addons.cend()); } - else if (strcmp(node->Name(), CppcheckXml::TagsElementName) == 0) + else if (strcmp(name, CppcheckXml::TagsElementName) == 0) node->Attribute(CppcheckXml::TagElementName); // FIXME: Write some warning - else if (strcmp(node->Name(), CppcheckXml::ToolsElementName) == 0) { + else if (strcmp(name, CppcheckXml::ToolsElementName) == 0) { const std::list toolList = readXmlStringList(node, emptyString, CppcheckXml::ToolElementName, nullptr); for (const std::string &toolName : toolList) { if (toolName == CppcheckXml::ClangTidy) temp.clangTidy = true; } - } else if (strcmp(node->Name(), CppcheckXml::CheckHeadersElementName) == 0) - temp.checkHeaders = (strcmp(readSafe(node->GetText(), ""), "true") == 0); - else if (strcmp(node->Name(), CppcheckXml::CheckLevelExhaustiveElementName) == 0) + } else if (strcmp(name, CppcheckXml::CheckHeadersElementName) == 0) + temp.checkHeaders = (strcmp(default_if_null(node->GetText(), ""), "true") == 0); + else if (strcmp(name, CppcheckXml::CheckLevelExhaustiveElementName) == 0) checkLevelExhaustive = true; - else if (strcmp(node->Name(), CppcheckXml::CheckUnusedTemplatesElementName) == 0) - temp.checkUnusedTemplates = (strcmp(readSafe(node->GetText(), ""), "true") == 0); - else if (strcmp(node->Name(), CppcheckXml::InlineSuppression) == 0) - temp.inlineSuppressions = (strcmp(readSafe(node->GetText(), ""), "true") == 0); - else if (strcmp(node->Name(), CppcheckXml::MaxCtuDepthElementName) == 0) - temp.maxCtuDepth = strToInt(readSafe(node->GetText(), "2")); // TODO: bail out when missing? - else if (strcmp(node->Name(), CppcheckXml::MaxTemplateRecursionElementName) == 0) - temp.maxTemplateRecursion = strToInt(readSafe(node->GetText(), "100")); // TODO: bail out when missing? - else if (strcmp(node->Name(), CppcheckXml::CheckUnknownFunctionReturn) == 0) + else if (strcmp(name, CppcheckXml::CheckUnusedTemplatesElementName) == 0) + temp.checkUnusedTemplates = (strcmp(default_if_null(node->GetText(), ""), "true") == 0); + else if (strcmp(name, CppcheckXml::InlineSuppression) == 0) + temp.inlineSuppressions = (strcmp(default_if_null(node->GetText(), ""), "true") == 0); + else if (strcmp(name, CppcheckXml::MaxCtuDepthElementName) == 0) + temp.maxCtuDepth = strToInt(default_if_null(node->GetText(), "2")); // TODO: bail out when missing? + else if (strcmp(name, CppcheckXml::MaxTemplateRecursionElementName) == 0) + temp.maxTemplateRecursion = strToInt(default_if_null(node->GetText(), "100")); // TODO: bail out when missing? + else if (strcmp(name, CppcheckXml::CheckUnknownFunctionReturn) == 0) ; // TODO - else if (strcmp(node->Name(), Settings::SafeChecks::XmlRootName) == 0) { + else if (strcmp(name, Settings::SafeChecks::XmlRootName) == 0) { for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) { - if (strcmp(child->Name(), Settings::SafeChecks::XmlClasses) == 0) + const char* childname = child->Name(); + if (strcmp(childname, Settings::SafeChecks::XmlClasses) == 0) temp.safeChecks.classes = true; - else if (strcmp(child->Name(), Settings::SafeChecks::XmlExternalFunctions) == 0) + else if (strcmp(childname, Settings::SafeChecks::XmlExternalFunctions) == 0) temp.safeChecks.externalFunctions = true; - else if (strcmp(child->Name(), Settings::SafeChecks::XmlInternalFunctions) == 0) + else if (strcmp(childname, Settings::SafeChecks::XmlInternalFunctions) == 0) temp.safeChecks.internalFunctions = true; - else if (strcmp(child->Name(), Settings::SafeChecks::XmlExternalVariables) == 0) + else if (strcmp(childname, Settings::SafeChecks::XmlExternalVariables) == 0) temp.safeChecks.externalVariables = true; else { - printError("Unknown '" + std::string(Settings::SafeChecks::XmlRootName) + "' element '" + std::string(child->Name()) + "' in Cppcheck project file"); + printError("Unknown '" + std::string(Settings::SafeChecks::XmlRootName) + "' element '" + childname + "' in Cppcheck project file"); return false; } } - } else if (strcmp(node->Name(), CppcheckXml::TagWarningsElementName) == 0) + } else if (strcmp(name, CppcheckXml::TagWarningsElementName) == 0) ; // TODO // Cppcheck Premium features - else if (strcmp(node->Name(), CppcheckXml::BughuntingElementName) == 0) + else if (strcmp(name, CppcheckXml::BughuntingElementName) == 0) temp.premiumArgs += " --bughunting"; - else if (strcmp(node->Name(), CppcheckXml::CertIntPrecisionElementName) == 0) - temp.premiumArgs += std::string(" --cert-c-int-precision=") + readSafe(node->GetText(), "0"); - else if (strcmp(node->Name(), CppcheckXml::CodingStandardsElementName) == 0) { + else if (strcmp(name, CppcheckXml::CertIntPrecisionElementName) == 0) + temp.premiumArgs += std::string(" --cert-c-int-precision=") + default_if_null(node->GetText(), "0"); + else if (strcmp(name, CppcheckXml::CodingStandardsElementName) == 0) { for (const tinyxml2::XMLElement *child = node->FirstChildElement(); child; child = child->NextSiblingElement()) { - if (strcmp(child->Name(), CppcheckXml::CodingStandardElementName) == 0 && child->GetText()) - temp.premiumArgs += std::string(" --") + child->GetText(); + if (strcmp(child->Name(), CppcheckXml::CodingStandardElementName) == 0) { + const char* text = child->GetText(); + if (text) + temp.premiumArgs += std::string(" --") + text; + } } } - else if (strcmp(node->Name(), CppcheckXml::ProjectNameElementName) == 0) + else if (strcmp(name, CppcheckXml::ProjectNameElementName) == 0) ; // no-op else { - printError("Unknown element '" + std::string(node->Name()) + "' in Cppcheck project file"); + printError("Unknown element '" + std::string(name) + "' in Cppcheck project file"); return false; } } diff --git a/lib/platform.cpp b/lib/platform.cpp index ef0072676fd..06e2fd49ba7 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -250,37 +250,39 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) bool error = false; for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) { - if (std::strcmp(node->Name(), "default-sign") == 0) { + const char* name = node->Name(); + if (std::strcmp(name, "default-sign") == 0) { const char* str = node->GetText(); if (str) defaultSign = *str; else error = true; - } else if (std::strcmp(node->Name(), "char_bit") == 0) + } else if (std::strcmp(name, "char_bit") == 0) char_bit = xmlTextAsUInt(node, error); - else if (std::strcmp(node->Name(), "sizeof") == 0) { + else if (std::strcmp(name, "sizeof") == 0) { for (const tinyxml2::XMLElement *sz = node->FirstChildElement(); sz; sz = sz->NextSiblingElement()) { - if (std::strcmp(sz->Name(), "short") == 0) + const char* szname = sz->Name(); + if (std::strcmp(szname, "short") == 0) sizeof_short = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "bool") == 0) + else if (std::strcmp(szname, "bool") == 0) sizeof_bool = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "int") == 0) + else if (std::strcmp(szname, "int") == 0) sizeof_int = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "long") == 0) + else if (std::strcmp(szname, "long") == 0) sizeof_long = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "long-long") == 0) + else if (std::strcmp(szname, "long-long") == 0) sizeof_long_long = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "float") == 0) + else if (std::strcmp(szname, "float") == 0) sizeof_float = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "double") == 0) + else if (std::strcmp(szname, "double") == 0) sizeof_double = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "long-double") == 0) + else if (std::strcmp(szname, "long-double") == 0) sizeof_long_double = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "pointer") == 0) + else if (std::strcmp(szname, "pointer") == 0) sizeof_pointer = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "size_t") == 0) + else if (std::strcmp(szname, "size_t") == 0) sizeof_size_t = xmlTextAsUInt(sz, error); - else if (std::strcmp(sz->Name(), "wchar_t") == 0) + else if (std::strcmp(szname, "wchar_t") == 0) sizeof_wchar_t = xmlTextAsUInt(sz, error); } } diff --git a/lib/suppressions.cpp b/lib/suppressions.cpp index f2e2836f3c4..37dd297fa97 100644 --- a/lib/suppressions.cpp +++ b/lib/suppressions.cpp @@ -123,19 +123,20 @@ std::string SuppressionList::parseXmlFile(const char *filename) Suppression s; for (const tinyxml2::XMLElement * e2 = e->FirstChildElement(); e2; e2 = e2->NextSiblingElement()) { - const char *text = e2->GetText() ? e2->GetText() : ""; - if (std::strcmp(e2->Name(), "id") == 0) + const char *name = e2->Name(); + const char *text = empty_if_null(e2->GetText()); + if (std::strcmp(name, "id") == 0) s.errorId = text; - else if (std::strcmp(e2->Name(), "fileName") == 0) + else if (std::strcmp(name, "fileName") == 0) s.fileName = text; - else if (std::strcmp(e2->Name(), "lineNumber") == 0) + else if (std::strcmp(name, "lineNumber") == 0) s.lineNumber = strToInt(text); - else if (std::strcmp(e2->Name(), "symbolName") == 0) + else if (std::strcmp(name, "symbolName") == 0) s.symbolName = text; - else if (*text && std::strcmp(e2->Name(), "hash") == 0) + else if (*text && std::strcmp(name, "hash") == 0) s.hash = strToInt(text); else - return std::string("unknown element '") + e2->Name() + "' in suppressions XML '" + filename + "', expected id/fileName/lineNumber/symbolName/hash."; + return std::string("unknown element '") + name + "' in suppressions XML '" + filename + "', expected id/fileName/lineNumber/symbolName/hash."; } const std::string err = addSuppression(std::move(s)); diff --git a/lib/utils.h b/lib/utils.h index fb4e2b25ed0..3c9c78bca28 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -385,10 +385,16 @@ namespace cppcheck } } +template +static inline T* default_if_null(T* p, T* def) +{ + return p ? p : def; +} + template static inline T* empty_if_null(T* p) { - return p ? p : ""; + return default_if_null(p, ""); } /**