diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 69757c8289d..51643b8ab95 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1493,8 +1493,8 @@ void CppCheck::executeAddons(const std::vector& files, const std::s std::string fileName = loc["file"].get(); const int64_t lineNumber = loc["linenr"].get(); const int64_t column = loc["column"].get(); - const std::string info = loc["info"].get(); - errmsg.callStack.emplace_back(std::move(fileName), info, lineNumber, column); + std::string info = loc["info"].get(); + errmsg.callStack.emplace_back(std::move(fileName), std::move(info), lineNumber, column); } } diff --git a/lib/ctu.cpp b/lib/ctu.cpp index f186ade9543..19a0e834136 100644 --- a/lib/ctu.cpp +++ b/lib/ctu.cpp @@ -210,11 +210,11 @@ bool CTU::FileInfo::FunctionCall::loadFromXml(const tinyxml2::XMLElement *xmlEle if (std::strcmp(e2->Name(), "path") != 0) continue; std::string file = readAttrString(e2, ATTR_LOC_FILENAME, &error); + std::string info = readAttrString(e2, ATTR_INFO, &error); const int line = readAttrInt(e2, ATTR_LOC_LINENR, &error); const int column = readAttrInt(e2, ATTR_LOC_COLUMN, &error); - ErrorMessage::FileLocation loc(file, line, column); - loc.setinfo(readAttrString(e2, ATTR_INFO, &error)); - // TODO: loc is unused + ErrorMessage::FileLocation loc(file, std::move(info), line, column); + (void)loc; // TODO: loc is unused } return !error; } @@ -347,10 +347,10 @@ CTU::FileInfo *CTU::getFileInfo(const Tokenizer *tokenizer) functionCall.warning = !value.errorSeverity(); for (const ErrorPathItem &i : value.errorPath) { const std::string& file = tokenizer->list.file(i.first); + const std::string& info = i.second; const int line = i.first->linenr(); const int column = i.first->column(); - ErrorMessage::FileLocation loc(file, line, column); - loc.setinfo(i.second); + ErrorMessage::FileLocation loc(file, info, line, column); functionCall.callValuePath.push_back(std::move(loc)); } fileInfo->functionCalls.push_back(std::move(functionCall)); @@ -579,13 +579,13 @@ std::list CTU::FileInfo::getErrorPath(InvalidValueTy std::copy(functionCall->callValuePath.cbegin(), functionCall->callValuePath.cend(), std::back_inserter(locationList)); } - ErrorMessage::FileLocation fileLoc(path[index]->location.fileName, path[index]->location.lineNumber, path[index]->location.column); - fileLoc.setinfo("Calling function " + path[index]->callFunctionName + ", " + std::to_string(path[index]->callArgNr) + getOrdinalText(path[index]->callArgNr) + " argument is " + value1); + std::string info_s = "Calling function " + path[index]->callFunctionName + ", " + std::to_string(path[index]->callArgNr) + getOrdinalText(path[index]->callArgNr) + " argument is " + value1; + ErrorMessage::FileLocation fileLoc(path[index]->location.fileName, std::move(info_s), path[index]->location.lineNumber, path[index]->location.column); locationList.push_back(std::move(fileLoc)); } - ErrorMessage::FileLocation fileLoc2(unsafeUsage.location.fileName, unsafeUsage.location.lineNumber, unsafeUsage.location.column); - fileLoc2.setinfo(replaceStr(info, "ARG", unsafeUsage.myArgumentName)); + std::string info_s = replaceStr(info, "ARG", unsafeUsage.myArgumentName); + ErrorMessage::FileLocation fileLoc2(unsafeUsage.location.fileName, std::move(info_s), unsafeUsage.location.lineNumber, unsafeUsage.location.column); locationList.push_back(std::move(fileLoc2)); return locationList; diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index e3ce0d1ece7..b9792f51dff 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -138,15 +138,19 @@ ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenLis if (!tok) continue; - std::string info = e.second; + const std::string& path_info = e.second; - if (startsWith(info,"$symbol:") && info.find('\n') < info.size()) { - const std::string::size_type pos = info.find('\n'); - const std::string &symbolName = info.substr(8, pos - 8); - info = replaceStr(info.substr(pos+1), "$symbol", symbolName); + std::string info; + if (startsWith(path_info,"$symbol:") && path_info.find('\n') < path_info.size()) { + const std::string::size_type pos = path_info.find('\n'); + const std::string symbolName = path_info.substr(8, pos - 8); + info = replaceStr(path_info.substr(pos+1), "$symbol", symbolName); + } + else { + info = path_info; } - callStack.emplace_back(tok, info, tokenList); + callStack.emplace_back(tok, std::move(info), tokenList); } if (tokenList && !tokenList->getFiles().empty()) @@ -410,10 +414,11 @@ void ErrorMessage::deserialize(const std::string &data) // (*loc).line << '\t' << (*loc).column << '\t' << (*loc).getfile(false) << '\t' << loc->getOrigFile(false) << '\t' << loc->getinfo(); - ErrorMessage::FileLocation loc(substrings[3], strToInt(substrings[0]), strToInt(substrings[1])); - loc.setfile(std::move(substrings[2])); + std::string info; if (substrings.size() == 5) - loc.setinfo(substrings[4]); + info = std::move(substrings[4]); + ErrorMessage::FileLocation loc(substrings[3], std::move(info), strToInt(substrings[0]), strToInt(substrings[1])); + loc.setfile(std::move(substrings[2])); callStack.push_back(std::move(loc)); diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 39b61df3e5b..f37bec4bd7d 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -92,12 +92,9 @@ class CPPCHECKLIB ErrorMessage { int line; // negative value means "no line" unsigned int column; - std::string getinfo() const { + const std::string& getinfo() const { return mInfo; } - void setinfo(const std::string &i) { - mInfo = i; - } private: std::string mOrigFileName; diff --git a/test/testerrorlogger.cpp b/test/testerrorlogger.cpp index 446fefb70eb..ea3e2234275 100644 --- a/test/testerrorlogger.cpp +++ b/test/testerrorlogger.cpp @@ -35,6 +35,7 @@ class TestErrorLogger : public TestFixture { private: const ErrorMessage::FileLocation fooCpp5{"foo.cpp", 5, 1}; const ErrorMessage::FileLocation barCpp8{"bar.cpp", 8, 1}; + const ErrorMessage::FileLocation barCpp8_i{"bar.cpp", "ä", 8, 1}; void run() override { TEST_CASE(PatternSearchReplace); @@ -235,8 +236,7 @@ class TestErrorLogger : public TestFixture { } void ToXmlV2Locations() const { - std::list locs = { fooCpp5, barCpp8 }; - locs.back().setinfo("ä"); + std::list locs = { fooCpp5, barCpp8_i }; ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal); std::string header("\n\n"); header += "