Skip to content

Commit

Permalink
some cleanups around ErrorMessage::FileLocation::mInfo (#6200)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Mar 28, 2024
1 parent e2720e1 commit 0c70758
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,8 +1493,8 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
std::string fileName = loc["file"].get<std::string>();
const int64_t lineNumber = loc["linenr"].get<int64_t>();
const int64_t column = loc["column"].get<int64_t>();
const std::string info = loc["info"].get<std::string>();
errmsg.callStack.emplace_back(std::move(fileName), info, lineNumber, column);
std::string info = loc["info"].get<std::string>();
errmsg.callStack.emplace_back(std::move(fileName), std::move(info), lineNumber, column);
}
}

Expand Down
18 changes: 9 additions & 9 deletions lib/ctu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -579,13 +579,13 @@ std::list<ErrorMessage::FileLocation> 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;
Expand Down
23 changes: 14 additions & 9 deletions lib/errorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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<int>(substrings[0]), strToInt<unsigned int>(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<int>(substrings[0]), strToInt<unsigned int>(substrings[1]));
loc.setfile(std::move(substrings[2]));

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

Expand Down
5 changes: 1 addition & 4 deletions lib/errorlogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions test/testerrorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -235,8 +236,7 @@ class TestErrorLogger : public TestFixture {
}

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

void SerializeFileLocation() const {
ErrorMessage::FileLocation loc1(":/,;", 654, 33);
ErrorMessage::FileLocation loc1(":/,;", "abcd:/,", 654, 33);
loc1.setfile("[]:;,()");
loc1.setinfo("abcd:/,");

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

Expand Down

0 comments on commit 0c70758

Please sign in to comment.