Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some XML parsing cleanups #6515

Merged
merged 4 commits into from
Sep 18, 2024
Merged
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
18 changes: 10 additions & 8 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/ctu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down
5 changes: 3 additions & 2 deletions lib/errorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg)
hash = attr ? strToInt<std::size_t>(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");
Expand All @@ -201,7 +202,7 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg)
const int line = strline ? strToInt<int>(strline) : 0;
const int column = strcolumn ? strToInt<int>(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();
}
}
Expand Down
Loading
Loading