Skip to content

Commit 0c70758

Browse files
authored
some cleanups around ErrorMessage::FileLocation::mInfo (#6200)
1 parent e2720e1 commit 0c70758

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

lib/cppcheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,8 +1493,8 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
14931493
std::string fileName = loc["file"].get<std::string>();
14941494
const int64_t lineNumber = loc["linenr"].get<int64_t>();
14951495
const int64_t column = loc["column"].get<int64_t>();
1496-
const std::string info = loc["info"].get<std::string>();
1497-
errmsg.callStack.emplace_back(std::move(fileName), info, lineNumber, column);
1496+
std::string info = loc["info"].get<std::string>();
1497+
errmsg.callStack.emplace_back(std::move(fileName), std::move(info), lineNumber, column);
14981498
}
14991499
}
15001500

lib/ctu.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ bool CTU::FileInfo::FunctionCall::loadFromXml(const tinyxml2::XMLElement *xmlEle
210210
if (std::strcmp(e2->Name(), "path") != 0)
211211
continue;
212212
std::string file = readAttrString(e2, ATTR_LOC_FILENAME, &error);
213+
std::string info = readAttrString(e2, ATTR_INFO, &error);
213214
const int line = readAttrInt(e2, ATTR_LOC_LINENR, &error);
214215
const int column = readAttrInt(e2, ATTR_LOC_COLUMN, &error);
215-
ErrorMessage::FileLocation loc(file, line, column);
216-
loc.setinfo(readAttrString(e2, ATTR_INFO, &error));
217-
// TODO: loc is unused
216+
ErrorMessage::FileLocation loc(file, std::move(info), line, column);
217+
(void)loc; // TODO: loc is unused
218218
}
219219
return !error;
220220
}
@@ -347,10 +347,10 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer *tokenizer)
347347
functionCall.warning = !value.errorSeverity();
348348
for (const ErrorPathItem &i : value.errorPath) {
349349
const std::string& file = tokenizer->list.file(i.first);
350+
const std::string& info = i.second;
350351
const int line = i.first->linenr();
351352
const int column = i.first->column();
352-
ErrorMessage::FileLocation loc(file, line, column);
353-
loc.setinfo(i.second);
353+
ErrorMessage::FileLocation loc(file, info, line, column);
354354
functionCall.callValuePath.push_back(std::move(loc));
355355
}
356356
fileInfo->functionCalls.push_back(std::move(functionCall));
@@ -579,13 +579,13 @@ std::list<ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueTy
579579
std::copy(functionCall->callValuePath.cbegin(), functionCall->callValuePath.cend(), std::back_inserter(locationList));
580580
}
581581

582-
ErrorMessage::FileLocation fileLoc(path[index]->location.fileName, path[index]->location.lineNumber, path[index]->location.column);
583-
fileLoc.setinfo("Calling function " + path[index]->callFunctionName + ", " + std::to_string(path[index]->callArgNr) + getOrdinalText(path[index]->callArgNr) + " argument is " + value1);
582+
std::string info_s = "Calling function " + path[index]->callFunctionName + ", " + std::to_string(path[index]->callArgNr) + getOrdinalText(path[index]->callArgNr) + " argument is " + value1;
583+
ErrorMessage::FileLocation fileLoc(path[index]->location.fileName, std::move(info_s), path[index]->location.lineNumber, path[index]->location.column);
584584
locationList.push_back(std::move(fileLoc));
585585
}
586586

587-
ErrorMessage::FileLocation fileLoc2(unsafeUsage.location.fileName, unsafeUsage.location.lineNumber, unsafeUsage.location.column);
588-
fileLoc2.setinfo(replaceStr(info, "ARG", unsafeUsage.myArgumentName));
587+
std::string info_s = replaceStr(info, "ARG", unsafeUsage.myArgumentName);
588+
ErrorMessage::FileLocation fileLoc2(unsafeUsage.location.fileName, std::move(info_s), unsafeUsage.location.lineNumber, unsafeUsage.location.column);
589589
locationList.push_back(std::move(fileLoc2));
590590

591591
return locationList;

lib/errorlogger.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,19 @@ ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenLis
138138
if (!tok)
139139
continue;
140140

141-
std::string info = e.second;
141+
const std::string& path_info = e.second;
142142

143-
if (startsWith(info,"$symbol:") && info.find('\n') < info.size()) {
144-
const std::string::size_type pos = info.find('\n');
145-
const std::string &symbolName = info.substr(8, pos - 8);
146-
info = replaceStr(info.substr(pos+1), "$symbol", symbolName);
143+
std::string info;
144+
if (startsWith(path_info,"$symbol:") && path_info.find('\n') < path_info.size()) {
145+
const std::string::size_type pos = path_info.find('\n');
146+
const std::string symbolName = path_info.substr(8, pos - 8);
147+
info = replaceStr(path_info.substr(pos+1), "$symbol", symbolName);
148+
}
149+
else {
150+
info = path_info;
147151
}
148152

149-
callStack.emplace_back(tok, info, tokenList);
153+
callStack.emplace_back(tok, std::move(info), tokenList);
150154
}
151155

152156
if (tokenList && !tokenList->getFiles().empty())
@@ -410,10 +414,11 @@ void ErrorMessage::deserialize(const std::string &data)
410414

411415
// (*loc).line << '\t' << (*loc).column << '\t' << (*loc).getfile(false) << '\t' << loc->getOrigFile(false) << '\t' << loc->getinfo();
412416

413-
ErrorMessage::FileLocation loc(substrings[3], strToInt<int>(substrings[0]), strToInt<unsigned int>(substrings[1]));
414-
loc.setfile(std::move(substrings[2]));
417+
std::string info;
415418
if (substrings.size() == 5)
416-
loc.setinfo(substrings[4]);
419+
info = std::move(substrings[4]);
420+
ErrorMessage::FileLocation loc(substrings[3], std::move(info), strToInt<int>(substrings[0]), strToInt<unsigned int>(substrings[1]));
421+
loc.setfile(std::move(substrings[2]));
417422

418423
callStack.push_back(std::move(loc));
419424

lib/errorlogger.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,9 @@ class CPPCHECKLIB ErrorMessage {
9292
int line; // negative value means "no line"
9393
unsigned int column;
9494

95-
std::string getinfo() const {
95+
const std::string& getinfo() const {
9696
return mInfo;
9797
}
98-
void setinfo(const std::string &i) {
99-
mInfo = i;
100-
}
10198

10299
private:
103100
std::string mOrigFileName;

test/testerrorlogger.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class TestErrorLogger : public TestFixture {
3535
private:
3636
const ErrorMessage::FileLocation fooCpp5{"foo.cpp", 5, 1};
3737
const ErrorMessage::FileLocation barCpp8{"bar.cpp", 8, 1};
38+
const ErrorMessage::FileLocation barCpp8_i{"bar.cpp", "ä", 8, 1};
3839

3940
void run() override {
4041
TEST_CASE(PatternSearchReplace);
@@ -235,8 +236,7 @@ class TestErrorLogger : public TestFixture {
235236
}
236237

237238
void ToXmlV2Locations() const {
238-
std::list<ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8 };
239-
locs.back().setinfo("ä");
239+
std::list<ErrorMessage::FileLocation> locs = { fooCpp5, barCpp8_i };
240240
ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
241241
std::string header("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results version=\"2\">\n");
242242
header += " <cppcheck version=\"";
@@ -431,9 +431,8 @@ class TestErrorLogger : public TestFixture {
431431
}
432432

433433
void SerializeFileLocation() const {
434-
ErrorMessage::FileLocation loc1(":/,;", 654, 33);
434+
ErrorMessage::FileLocation loc1(":/,;", "abcd:/,", 654, 33);
435435
loc1.setfile("[]:;,()");
436-
loc1.setinfo("abcd:/,");
437436

438437
ErrorMessage msg({std::move(loc1)}, emptyString, Severity::error, "Programming error", "errorId", Certainty::inconclusive);
439438

0 commit comments

Comments
 (0)