From 5bfac48a923c997403e022a319094b2bc02ec00f Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 24 Mar 2024 23:47:39 +0100 Subject: [PATCH] cleaned up creation of `ErrorMessage::FileLocation` objects and error callstacks --- lib/cppcheck.cpp | 19 +++++++------------ lib/errorlogger.cpp | 9 +++------ lib/forwardanalyzer.cpp | 5 ++--- lib/preprocessor.cpp | 3 +-- lib/valueflow.cpp | 4 ++-- test/testerrorlogger.cpp | 6 ++---- 6 files changed, 17 insertions(+), 29 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index a077c726f04..69757c8289d 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -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 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, @@ -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 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, @@ -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 callstack(1, loc1); + ErrorMessage::FileLocation loc1(filename, 0, 0); - ErrorMessage errmsg(std::move(callstack), + ErrorMessage errmsg({std::move(loc1)}, emptyString, Severity::error, fullmsg, @@ -1418,15 +1415,13 @@ void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &token ErrorMessage::FileLocation loc(file, line, 0); - const std::list 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); diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index fdffd82e283..e3ce0d1ece7 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -244,14 +244,11 @@ ErrorMessage ErrorMessage::fromInternalError(const InternalError &internalError, std::list 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), diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 4385e393cb8..c3ae5c5d4df 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -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 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); } } diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index c78f1d9754e..938b52229cd 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -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, diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 021a8f29327..024f87cb59b 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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 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); } diff --git a/test/testerrorlogger.cpp b/test/testerrorlogger.cpp index b8bb6c74da4..446fefb70eb 100644 --- a/test/testerrorlogger.cpp +++ b/test/testerrorlogger.cpp @@ -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); @@ -435,9 +435,7 @@ class TestErrorLogger : public TestFixture { loc1.setfile("[]:;,()"); loc1.setinfo("abcd:/,"); - std::list 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"