Skip to content

Commit 74ec570

Browse files
authored
some XML parsing cleanups (danmar#6515)
1 parent f88d2c8 commit 74ec570

File tree

9 files changed

+171
-144
lines changed

9 files changed

+171
-144
lines changed

cli/cmdlineparser.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,33 +1157,35 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
11571157
Settings::Rule rule;
11581158

11591159
for (const tinyxml2::XMLElement *subnode = node->FirstChildElement(); subnode; subnode = subnode->NextSiblingElement()) {
1160+
const char * const subname = subnode->Name();
11601161
const char * const subtext = subnode->GetText();
1161-
if (std::strcmp(subnode->Name(), "tokenlist") == 0) {
1162+
if (std::strcmp(subname, "tokenlist") == 0) {
11621163
rule.tokenlist = empty_if_null(subtext);
11631164
}
1164-
else if (std::strcmp(subnode->Name(), "pattern") == 0) {
1165+
else if (std::strcmp(subname, "pattern") == 0) {
11651166
rule.pattern = empty_if_null(subtext);
11661167
}
1167-
else if (std::strcmp(subnode->Name(), "message") == 0) {
1168+
else if (std::strcmp(subname, "message") == 0) {
11681169
for (const tinyxml2::XMLElement *msgnode = subnode->FirstChildElement(); msgnode; msgnode = msgnode->NextSiblingElement()) {
1170+
const char * const msgname = msgnode->Name();
11691171
const char * const msgtext = msgnode->GetText();
1170-
if (std::strcmp(msgnode->Name(), "severity") == 0) {
1172+
if (std::strcmp(msgname, "severity") == 0) {
11711173
rule.severity = severityFromString(empty_if_null(msgtext));
11721174
}
1173-
else if (std::strcmp(msgnode->Name(), "id") == 0) {
1175+
else if (std::strcmp(msgname, "id") == 0) {
11741176
rule.id = empty_if_null(msgtext);
11751177
}
1176-
else if (std::strcmp(msgnode->Name(), "summary") == 0) {
1178+
else if (std::strcmp(msgname, "summary") == 0) {
11771179
rule.summary = empty_if_null(msgtext);
11781180
}
11791181
else {
1180-
mLogger.printError("unable to load rule-file '" + ruleFile + "' - unknown element '" + msgnode->Name() + "' encountered in 'message'.");
1182+
mLogger.printError("unable to load rule-file '" + ruleFile + "' - unknown element '" + msgname + "' encountered in 'message'.");
11811183
return Result::Fail;
11821184
}
11831185
}
11841186
}
11851187
else {
1186-
mLogger.printError("unable to load rule-file '" + ruleFile + "' - unknown element '" + subnode->Name() + "' encountered in 'rule'.");
1188+
mLogger.printError("unable to load rule-file '" + ruleFile + "' - unknown element '" + subname + "' encountered in 'rule'.");
11871189
return Result::Fail;
11881190
}
11891191
}

lib/checkbufferoverrun.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,9 +966,10 @@ Check::FileInfo * CheckBufferOverrun::loadFileInfoFromXml(const tinyxml2::XMLEle
966966

967967
auto *fileInfo = new MyFileInfo;
968968
for (const tinyxml2::XMLElement *e = xmlElement->FirstChildElement(); e; e = e->NextSiblingElement()) {
969-
if (e->Name() == arrayIndex)
969+
const char* name = e->Name();
970+
if (name == arrayIndex)
970971
fileInfo->unsafeArrayIndex = CTU::loadUnsafeUsageListFromXml(e);
971-
else if (e->Name() == pointerArith)
972+
else if (name == pointerArith)
972973
fileInfo->unsafePointerArith = CTU::loadUnsafeUsageListFromXml(e);
973974
}
974975

lib/checkunusedfunctions.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,12 @@ void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLo
450450
const char* functionName = e2->Attribute("functionName");
451451
if (functionName == nullptr)
452452
continue;
453-
if (std::strcmp(e2->Name(),"functioncall") == 0) {
453+
const char* name = e2->Name();
454+
if (std::strcmp(name,"functioncall") == 0) {
454455
calls.insert(functionName);
455456
continue;
456457
}
457-
if (std::strcmp(e2->Name(),"functiondecl") == 0) {
458+
if (std::strcmp(name,"functiondecl") == 0) {
458459
const char* lineNumber = e2->Attribute("lineNumber");
459460
if (lineNumber) {
460461
// cppcheck-suppress templateInstantiation - TODO: fix this - see #11631

lib/ctu.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static std::string readAttrString(const tinyxml2::XMLElement *e, const char *att
172172
const char *value = e->Attribute(attr);
173173
if (!value && error)
174174
*error = true;
175-
return value ? value : "";
175+
return empty_if_null(value);
176176
}
177177

178178
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
232232
void CTU::FileInfo::loadFromXml(const tinyxml2::XMLElement *xmlElement)
233233
{
234234
for (const tinyxml2::XMLElement *e = xmlElement->FirstChildElement(); e; e = e->NextSiblingElement()) {
235-
if (std::strcmp(e->Name(), "function-call") == 0) {
235+
const char* name = e->Name();
236+
if (std::strcmp(name, "function-call") == 0) {
236237
FunctionCall functionCall;
237238
if (functionCall.loadFromXml(e))
238239
functionCalls.push_back(std::move(functionCall));
239-
} else if (std::strcmp(e->Name(), "nested-call") == 0) {
240+
} else if (std::strcmp(name, "nested-call") == 0) {
240241
NestedCall nestedCall;
241242
if (nestedCall.loadFromXml(e))
242243
nestedCalls.push_back(std::move(nestedCall));

lib/errorlogger.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg)
190190
hash = attr ? strToInt<std::size_t>(attr) : 0;
191191

192192
for (const tinyxml2::XMLElement *e = errmsg->FirstChildElement(); e; e = e->NextSiblingElement()) {
193-
if (std::strcmp(e->Name(),"location")==0) {
193+
const char* name = e->Name();
194+
if (std::strcmp(name,"location")==0) {
194195
const char *strfile = e->Attribute("file");
195196
const char *strinfo = e->Attribute("info");
196197
const char *strline = e->Attribute("line");
@@ -201,7 +202,7 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg)
201202
const int line = strline ? strToInt<int>(strline) : 0;
202203
const int column = strcolumn ? strToInt<int>(strcolumn) : 0;
203204
callStack.emplace_front(file, info, line, column);
204-
} else if (std::strcmp(e->Name(),"symbol")==0) {
205+
} else if (std::strcmp(name,"symbol")==0) {
205206
mSymbolNames += e->GetText();
206207
}
207208
}

0 commit comments

Comments
 (0)