Skip to content

Commit

Permalink
cleaned up creation of ErrorMessage::FileLocation objects and error…
Browse files Browse the repository at this point in the history
… callstacks
  • Loading branch information
firewave committed Mar 25, 2024
1 parent fd7bd50 commit 5bfac48
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 29 deletions.
19 changes: 7 additions & 12 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,9 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
if (mSettings.relativePaths)
file = Path::getRelativePath(file, mSettings.basePaths);

const ErrorMessage::FileLocation loc1(file, output.location.line, output.location.col);
std::list<ErrorMessage::FileLocation> callstack(1, loc1);
ErrorMessage::FileLocation loc1(file, output.location.line, output.location.col);

ErrorMessage errmsg(std::move(callstack),
ErrorMessage errmsg({std::move(loc1)},
"",
Severity::error,
output.msg,
Expand Down Expand Up @@ -978,10 +977,9 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
if (mSettings.relativePaths)
file = Path::getRelativePath(file, mSettings.basePaths);

const ErrorMessage::FileLocation loc1(file, o.location.line, o.location.col);
std::list<ErrorMessage::FileLocation> callstack(1, loc1);
ErrorMessage::FileLocation loc1(file, o.location.line, o.location.col);

ErrorMessage errmsg(std::move(callstack),
ErrorMessage errmsg({std::move(loc1)},
filename,
Severity::error,
o.msg,
Expand Down Expand Up @@ -1062,10 +1060,9 @@ void CppCheck::internalError(const std::string &filename, const std::string &msg
{
const std::string fullmsg("Bailing out from analysis: " + msg);

const ErrorMessage::FileLocation loc1(filename, 0, 0);
std::list<ErrorMessage::FileLocation> callstack(1, loc1);
ErrorMessage::FileLocation loc1(filename, 0, 0);

ErrorMessage errmsg(std::move(callstack),
ErrorMessage errmsg({std::move(loc1)},
emptyString,
Severity::error,
fullmsg,
Expand Down Expand Up @@ -1418,15 +1415,13 @@ void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &token

ErrorMessage::FileLocation loc(file, line, 0);

const std::list<ErrorMessage::FileLocation> callStack(1, loc);

// Create error message
std::string summary;
if (rule.summary.empty())
summary = "found '" + str.substr(pos1, pos2 - pos1) + "'";
else
summary = rule.summary;
const ErrorMessage errmsg(callStack, tokenizer.list.getSourceFilePath(), rule.severity, summary, rule.id, Certainty::normal);
const ErrorMessage errmsg({std::move(loc)}, tokenizer.list.getSourceFilePath(), rule.severity, summary, rule.id, Certainty::normal);

// Report error
reportErr(errmsg);
Expand Down
9 changes: 3 additions & 6 deletions lib/errorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,11 @@ ErrorMessage ErrorMessage::fromInternalError(const InternalError &internalError,

std::list<ErrorMessage::FileLocation> locationList;
if (tokenList && internalError.token) {
ErrorMessage::FileLocation loc(internalError.token, tokenList);
locationList.push_back(std::move(loc));
locationList.emplace_back(internalError.token, tokenList);
} else {
ErrorMessage::FileLocation loc2(filename, 0, 0);
locationList.push_back(std::move(loc2));
locationList.emplace_back(filename, 0, 0);
if (tokenList && (filename != tokenList->getSourceFilePath())) {
ErrorMessage::FileLocation loc(tokenList->getSourceFilePath(), 0, 0);
locationList.push_back(std::move(loc));
locationList.emplace_back(tokenList->getSourceFilePath(), 0, 0);
}
}
ErrorMessage errmsg(std::move(locationList),
Expand Down
5 changes: 2 additions & 3 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,9 +839,8 @@ namespace {

void reportError(Severity severity, const std::string& id, const std::string& msg) {
if (errorLogger) {
const ErrorMessage::FileLocation loc(tokenList.getSourceFilePath(), 1, 1);
const std::list<ErrorMessage::FileLocation> callstack{loc};
const ErrorMessage errmsg(callstack, tokenList.getSourceFilePath(), severity, msg, id, Certainty::normal);
ErrorMessage::FileLocation loc(tokenList.getSourceFilePath(), 1, 1);
const ErrorMessage errmsg({std::move(loc)}, tokenList.getSourceFilePath(), severity, msg, id, Certainty::normal);
errorLogger->reportErr(errmsg);
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,7 @@ void Preprocessor::error(const std::string &filename, unsigned int linenr, const
if (mSettings.relativePaths)
file = Path::getRelativePath(file, mSettings.basePaths);

ErrorMessage::FileLocation loc(file, linenr, 0);
locationList.push_back(std::move(loc));
locationList.emplace_back(file, linenr, 0);
}
mErrorLogger->reportErr(ErrorMessage(std::move(locationList),
mFile0,
Expand Down
4 changes: 2 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ static void bailoutInternal(const std::string& type, const TokenList &tokenlist,
{
if (function.find("operator") != std::string::npos)
function = "(valueFlow)";
std::list<ErrorMessage::FileLocation> callstack(1, ErrorMessage::FileLocation(tok, &tokenlist));
ErrorMessage::FileLocation loc(tok, &tokenlist);
const std::string location = Path::stripDirectoryPart(file) + ":" + std::to_string(line) + ":";
ErrorMessage errmsg(std::move(callstack), tokenlist.getSourceFilePath(), Severity::debug,
ErrorMessage errmsg({std::move(loc)}, tokenlist.getSourceFilePath(), Severity::debug,
(file.empty() ? "" : location) + function + " bailout: " + what, type, Certainty::normal);
errorLogger->reportErr(errmsg);
}
Expand Down
6 changes: 2 additions & 4 deletions test/testerrorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class TestErrorLogger : public TestFixture {
}

void FileLocationConstruct() const {
ErrorMessage::FileLocation loc("foo.cpp", 1, 2);
const ErrorMessage::FileLocation loc("foo.cpp", 1, 2);
ASSERT_EQUALS("foo.cpp", loc.getOrigFile());
ASSERT_EQUALS("foo.cpp", loc.getfile());
ASSERT_EQUALS(1, loc.line);
Expand Down Expand Up @@ -435,9 +435,7 @@ class TestErrorLogger : public TestFixture {
loc1.setfile("[]:;,()");
loc1.setinfo("abcd:/,");

std::list<ErrorMessage::FileLocation> locs{std::move(loc1)};

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

const std::string msg_str = msg.serialize();
ASSERT_EQUALS("7 errorId"
Expand Down

0 comments on commit 5bfac48

Please sign in to comment.