From 188b493ac964266ecd20d8168792f38c6cccdbe1 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 29 Jan 2024 16:55:32 +0200 Subject: [PATCH 01/13] Fix issue with missing files listed in project --- lib/cppcheck.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 4c3bcb7d0b6..467e3080b34 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -59,6 +59,7 @@ #include #include #include +#include #include "json.h" @@ -622,7 +623,14 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string if (mSettings.checks.isEnabled(Checks::unusedFunction) && !mUnusedFunctionsCheck) mUnusedFunctionsCheck.reset(new CheckUnusedFunctions()); - mExitCode = 0; + if (!Path::isFile(filename)) { + std::string fixedpath = Path::simplifyPath(filename); + fixedpath = Path::toNativeSeparators(fixedpath); + mErrorLogger.reportOut(std::string("File ") + fixedpath + ' ' + std::string("does not exists. Skipping..."), Color::FgMagenta); + return mExitCode; + } + + FilesDeleter filesDeleter; if (Settings::terminated()) return mExitCode; From bfde17130e6f87d278a3594551b1499f8fd2f918 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 29 Jan 2024 16:57:42 +0200 Subject: [PATCH 02/13] Fix include --- lib/cppcheck.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 467e3080b34..43c49fce52a 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -59,7 +59,6 @@ #include #include #include -#include #include "json.h" From b71d9ef978d2848c1d99178d19cff4c50731c834 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 29 Jan 2024 20:35:18 +0200 Subject: [PATCH 03/13] Fix --- test/testsuppressions.cpp | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index d206f1f1b2a..db2c0e817a1 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -1190,9 +1190,14 @@ class TestSuppressions : public TestFixture { ASSERT_EQUALS("", settings.supprs.nomsg.addSuppressionLine("uninitvar")); settings.exitCode = 1; - const char code[] = "int f() { int a; return a; }"; - ASSERT_EQUALS(0, cppCheck.check(FileWithDetails("test.c"), code)); // <- no unsuppressed error is seen - ASSERT_EQUALS("[test.c:1]: (error) Uninitialized variable: a\n", errout_str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched. + ScopedFile file("test.cpp", + "int f()\n" + "{\n" + " int a;\n" + " return a;\n" + "}"); + ASSERT_EQUALS(0, cppCheck.check(file.path())); // <- no unsuppressed error is seen + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched. } void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction" @@ -1223,16 +1228,16 @@ class TestSuppressions : public TestFixture { settings.inlineSuppressions = true; settings.relativePaths = true; settings.basePaths.emplace_back("/somewhere"); - const char code[] = - "struct Point\n" - "{\n" - " // cppcheck-suppress unusedStructMember\n" - " int x;\n" - " // cppcheck-suppress unusedStructMember\n" - " int y;\n" - "};"; - ASSERT_EQUALS(0, cppCheck.check(FileWithDetails("/somewhere/test.cpp"), code)); - ASSERT_EQUALS("",errout_str()); + ScopedFile file("test.cpp", + "struct Point\n" + "{\n" + " // cppcheck-suppress unusedStructMember\n" + " int x;\n" + " // cppcheck-suppress unusedStructMember\n" + " int y;\n" + "};"); + ASSERT_EQUALS(0, cppCheck.check(file.path())); + ASSERT_EQUALS("",errout.str()); } void suppressingSyntaxErrorsInternal(unsigned int (TestSuppressions::*check)(const char[], const std::string &)) { // syntaxErrors should be suppressible (#7076) From d9f65ffb789b3a9d7f3e37883b527059dfddda5c Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 30 Jan 2024 12:08:18 +0200 Subject: [PATCH 04/13] Add test --- lib/cppcheck.cpp | 21 +++++++++++++++------ lib/cppcheck.h | 3 +++ lib/errorlogger.cpp | 1 + test/testcppcheck.cpp | 11 +++++++++++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 43c49fce52a..155e78a931f 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -625,7 +625,8 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string if (!Path::isFile(filename)) { std::string fixedpath = Path::simplifyPath(filename); fixedpath = Path::toNativeSeparators(fixedpath); - mErrorLogger.reportOut(std::string("File ") + fixedpath + ' ' + std::string("does not exists. Skipping..."), Color::FgMagenta); + std::string errorMsg(std::string("File ") + fixedpath + ' ' + std::string("does not exists. Skipping...")); + fileNotFoundError(filename, errorMsg); return mExitCode; } @@ -1031,21 +1032,29 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string return mExitCode; } -// TODO: replace with ErrorMessage::fromInternalError() -void CppCheck::internalError(const std::string &filename, const std::string &msg) +static ErrorMessage makeError(const std::string &filename, const std::string &msg, const std::string &id) { const std::string fullmsg("Bailing out from analysis: " + msg); ErrorMessage::FileLocation loc1(filename, 0, 0); - ErrorMessage errmsg({std::move(loc1)}, + return ErrorMessage(callstack, emptyString, Severity::error, fullmsg, - "internalError", + id, Certainty::normal); +} - mErrorLogger.reportErr(errmsg); +// TODO: replace with ErrorMessage::fromInternalError() +void CppCheck::internalError(const std::string &filename, const std::string &msg) +{ + mErrorLogger.reportErr(makeError(filename, msg, "internalError")); +} + +void CppCheck::fileNotFoundError(const std::string &filename, const std::string &msg) +{ + mErrorLogger.reportErr(makeError(filename, msg, "fileNotFound")); } //--------------------------------------------------------------------------- diff --git a/lib/cppcheck.h b/lib/cppcheck.h index 9143760dcff..e5790f2a599 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -172,6 +172,9 @@ class CPPCHECKLIB CppCheck : ErrorLogger { /** @brief There has been an internal error => Report information message */ void internalError(const std::string &filename, const std::string &msg); + /** @brief File is missing => Report information message */ + void fileNotFoundError(const std::string &filename, const std::string &msg); + /** * @brief Check a file using stream * @param file the file diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 17c5a82c977..29e97777869 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -44,6 +44,7 @@ const std::set ErrorLogger::mCriticalErrorIds{ "cppcheckError", "cppcheckLimit", + "fileNotFound", "internalAstError", "instantiationError", "internalError", diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index 34f07414dd9..b7e0048850f 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -54,6 +54,7 @@ class TestCppcheck : public TestFixture { void run() override { TEST_CASE(getErrorMessages); TEST_CASE(checkWithFile); + TEST_CASE(checkWithoutFile); TEST_CASE(checkWithFS); TEST_CASE(suppress_error_library); TEST_CASE(unique_errors); @@ -112,6 +113,16 @@ class TestCppcheck : public TestFixture { ASSERT_EQUALS("nullPointer", *errorLogger.ids.cbegin()); } + void checkWithoutFile() const + { + ErrorLogger2 errorLogger; + CppCheck cppcheck(errorLogger, false, {}); + ASSERT_EQUALS(0, cppcheck.check("NotAFile")); + + ASSERT_EQUALS(1, errorLogger.ids.size()); + ASSERT_EQUALS("fileNotFound", *errorLogger.ids.cbegin()); + } + void checkWithFS() const { ScopedFile file("test.cpp", From 0faa83956ded4c9f00f8a638847349cba91dbd2e Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 30 Jan 2024 13:11:56 +0200 Subject: [PATCH 05/13] Fix --- lib/cppcheck.cpp | 28 +++++++++++++++++++--------- test/testsuppressions.cpp | 29 ++++++++++++----------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 155e78a931f..f5c1da2746a 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -553,6 +553,13 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file) unsigned int CppCheck::check(const FileWithDetails &file) { + if (!Path::isFile(path)) { + std::string fixedpath = Path::simplifyPath(path); + fixedpath = Path::toNativeSeparators(fixedpath); + std::string errorMsg(std::string("File ") + fixedpath + ' ' + std::string("does not exists. Skipping...")); + fileNotFoundError(Path::simplifyPath(path), errorMsg); + return 0; + } if (mSettings.clang) return checkClang(file); @@ -567,10 +574,20 @@ unsigned int CppCheck::check(const FileWithDetails &file, const std::string &con unsigned int CppCheck::check(const FileSettings &fs) { +<<<<<<< HEAD // TODO: move to constructor when CppCheck no longer owns the settings if (mSettings.checks.isEnabled(Checks::unusedFunction) && !mUnusedFunctionsCheck) mUnusedFunctionsCheck.reset(new CheckUnusedFunctions()); +======= + if (!Path::isFile(fs.filename)) { + std::string fixedpath = Path::simplifyPath(fs.filename); + fixedpath = Path::toNativeSeparators(fixedpath); + std::string errorMsg(std::string("File ") + fixedpath + ' ' + std::string("does not exists. Skipping...")); + fileNotFoundError(fs.filename, errorMsg); + return 0; + } +>>>>>>> 8f585fe73 (Fix) CppCheck temp(mErrorLogger, mUseGlobalSuppressions, mExecuteCommand); temp.mSettings = mSettings; if (!temp.mSettings.userDefines.empty()) @@ -622,14 +639,6 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string if (mSettings.checks.isEnabled(Checks::unusedFunction) && !mUnusedFunctionsCheck) mUnusedFunctionsCheck.reset(new CheckUnusedFunctions()); - if (!Path::isFile(filename)) { - std::string fixedpath = Path::simplifyPath(filename); - fixedpath = Path::toNativeSeparators(fixedpath); - std::string errorMsg(std::string("File ") + fixedpath + ' ' + std::string("does not exists. Skipping...")); - fileNotFoundError(filename, errorMsg); - return mExitCode; - } - FilesDeleter filesDeleter; if (Settings::terminated()) @@ -1036,7 +1045,8 @@ static ErrorMessage makeError(const std::string &filename, const std::string &ms { const std::string fullmsg("Bailing out from analysis: " + msg); - ErrorMessage::FileLocation loc1(filename, 0, 0); + const ErrorMessage::FileLocation loc1(filename, 0, 0); + std::list callstack(1, loc1); return ErrorMessage(callstack, emptyString, diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index db2c0e817a1..a22845de980 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -1190,14 +1190,9 @@ class TestSuppressions : public TestFixture { ASSERT_EQUALS("", settings.supprs.nomsg.addSuppressionLine("uninitvar")); settings.exitCode = 1; - ScopedFile file("test.cpp", - "int f()\n" - "{\n" - " int a;\n" - " return a;\n" - "}"); - ASSERT_EQUALS(0, cppCheck.check(file.path())); // <- no unsuppressed error is seen - ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched. + const char code[] = "int f() { int a; return a; }"; + ASSERT_EQUALS(0, cppCheck.check("test.c", code)); // <- no unsuppressed error is seen + ASSERT_EQUALS("[test.c:1]: (error) Uninitialized variable: a\n", errout.str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched. } void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction" @@ -1228,15 +1223,15 @@ class TestSuppressions : public TestFixture { settings.inlineSuppressions = true; settings.relativePaths = true; settings.basePaths.emplace_back("/somewhere"); - ScopedFile file("test.cpp", - "struct Point\n" - "{\n" - " // cppcheck-suppress unusedStructMember\n" - " int x;\n" - " // cppcheck-suppress unusedStructMember\n" - " int y;\n" - "};"); - ASSERT_EQUALS(0, cppCheck.check(file.path())); + const char code[] = + "struct Point\n" + "{\n" + " // cppcheck-suppress unusedStructMember\n" + " int x;\n" + " // cppcheck-suppress unusedStructMember\n" + " int y;\n" + "};"; + ASSERT_EQUALS(0, cppCheck.check("/somewhere/test.cpp", code)); ASSERT_EQUALS("",errout.str()); } From e8268e76bd285119fdaece48f930097f22264c1d Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 30 Jan 2024 15:12:58 +0200 Subject: [PATCH 06/13] Nits --- lib/cppcheck.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index f5c1da2746a..e42c8119023 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -556,7 +556,7 @@ unsigned int CppCheck::check(const FileWithDetails &file) if (!Path::isFile(path)) { std::string fixedpath = Path::simplifyPath(path); fixedpath = Path::toNativeSeparators(fixedpath); - std::string errorMsg(std::string("File ") + fixedpath + ' ' + std::string("does not exists. Skipping...")); + const std::string errorMsg("File " + fixedpath + " does not exists. Skipping file."); fileNotFoundError(Path::simplifyPath(path), errorMsg); return 0; } @@ -583,7 +583,7 @@ unsigned int CppCheck::check(const FileSettings &fs) if (!Path::isFile(fs.filename)) { std::string fixedpath = Path::simplifyPath(fs.filename); fixedpath = Path::toNativeSeparators(fixedpath); - std::string errorMsg(std::string("File ") + fixedpath + ' ' + std::string("does not exists. Skipping...")); + const std::string errorMsg("File " + fixedpath + " does not exists. Skipping file."); fileNotFoundError(fs.filename, errorMsg); return 0; } @@ -1059,7 +1059,7 @@ static ErrorMessage makeError(const std::string &filename, const std::string &ms // TODO: replace with ErrorMessage::fromInternalError() void CppCheck::internalError(const std::string &filename, const std::string &msg) { - mErrorLogger.reportErr(makeError(filename, msg, "internalError")); + mErrorLogger.reportErr(makeError(filename, "Bailing out from analysis:" + msg, "internalError")); } void CppCheck::fileNotFoundError(const std::string &filename, const std::string &msg) From 84d233ba8b0e1f01af345d2ab028295a22063506 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Wed, 31 Jan 2024 15:26:51 +0200 Subject: [PATCH 07/13] add test --- .../invalid-project/invalid-project.vcxproj | 123 ++++++++++++++++++ test/cli/more-projects_test.py | 6 + 2 files changed, 129 insertions(+) create mode 100644 test/cli/invalid-project/invalid-project.vcxproj diff --git a/test/cli/invalid-project/invalid-project.vcxproj b/test/cli/invalid-project/invalid-project.vcxproj new file mode 100644 index 00000000000..12114b955f2 --- /dev/null +++ b/test/cli/invalid-project/invalid-project.vcxproj @@ -0,0 +1,123 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {7319858B-261C-4F0D-B022-92BB896242DD} + invalidProjet + 10.0.16299.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + + + + + Level3 + Disabled + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + + + + + \ No newline at end of file diff --git a/test/cli/more-projects_test.py b/test/cli/more-projects_test.py index 7824bb88f5c..007d17fbf02 100644 --- a/test/cli/more-projects_test.py +++ b/test/cli/more-projects_test.py @@ -864,3 +864,9 @@ def test_shared_items_project(tmpdir = ""): # Assume no errors, and that shared items code files have been checked as well assert any('2/2 files checked 100% done' in x for x in lines) assert stderr == '' + + +def test_project_missing_files(): + ret, _, stderr = cppcheck(['--template=cppcheck1', '--project=' + os.path.join('invalid-project', 'invalid-project.vcxproj')]) + assert ret == 0 + assert stderr == '[invalid-project/main.c:0]: (error) File invalid-project/main.c does not exists. Skipping file.\n' From e5b140308d9b2373e21690a3fc8fa7395a006fba Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Wed, 31 Jan 2024 15:46:18 +0200 Subject: [PATCH 08/13] CI fix --- test/cli/more-projects_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/cli/more-projects_test.py b/test/cli/more-projects_test.py index 007d17fbf02..4b10a224e70 100644 --- a/test/cli/more-projects_test.py +++ b/test/cli/more-projects_test.py @@ -867,6 +867,7 @@ def test_shared_items_project(tmpdir = ""): def test_project_missing_files(): + filename = os.path.join('invalid-project', 'main.c') ret, _, stderr = cppcheck(['--template=cppcheck1', '--project=' + os.path.join('invalid-project', 'invalid-project.vcxproj')]) assert ret == 0 - assert stderr == '[invalid-project/main.c:0]: (error) File invalid-project/main.c does not exists. Skipping file.\n' + assert stderr == '[{}:0]: (error) File {} does not exists. Skipping file.\n'.format(filename, filename) From 018ebc3eca678c06e0f3bdb3ad15f5e6cd1cd352 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Thu, 1 Feb 2024 17:33:04 +0200 Subject: [PATCH 09/13] Remodel --- lib/cppcheck.cpp | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index e42c8119023..1d64c1751e4 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -551,15 +551,21 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file) return mExitCode; } +static ErrorMessage makeError(const std::string &filename, int line, unsigned int column, const std::string &msg, const std::string &id) +{ + const ErrorMessage::FileLocation loc1(filename, line, column); + std::list callstack(1, loc1); + + return ErrorMessage(callstack, + emptyString, + Severity::error, + msg, + id, + Certainty::normal); +} + unsigned int CppCheck::check(const FileWithDetails &file) { - if (!Path::isFile(path)) { - std::string fixedpath = Path::simplifyPath(path); - fixedpath = Path::toNativeSeparators(fixedpath); - const std::string errorMsg("File " + fixedpath + " does not exists. Skipping file."); - fileNotFoundError(Path::simplifyPath(path), errorMsg); - return 0; - } if (mSettings.clang) return checkClang(file); @@ -574,6 +580,7 @@ unsigned int CppCheck::check(const FileWithDetails &file, const std::string &con unsigned int CppCheck::check(const FileSettings &fs) { +<<<<<<< HEAD <<<<<<< HEAD // TODO: move to constructor when CppCheck no longer owns the settings if (mSettings.checks.isEnabled(Checks::unusedFunction) && !mUnusedFunctionsCheck) @@ -588,6 +595,8 @@ unsigned int CppCheck::check(const FileSettings &fs) return 0; } >>>>>>> 8f585fe73 (Fix) +======= +>>>>>>> 74a6847dd (Remodel) CppCheck temp(mErrorLogger, mUseGlobalSuppressions, mExecuteCommand); temp.mSettings = mSettings; if (!temp.mSettings.userDefines.empty()) @@ -714,7 +723,14 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string output.msg, "syntaxError", Certainty::normal); - reportErr(errmsg); + if(output.type == simplecpp::Output::Type::FILE_NOT_FOUND){ + const std::string fixedpath = Path::toNativeSeparators(file); + const std::string errorMsg("File " + fixedpath + " does not exists. Skipping file."); + fileNotFoundError(file, errorMsg); + } + else{ + reportErr(makeError(file, output.location.line, output.location.col, output.msg, "syntaxError")); + } return mExitCode; } @@ -1041,6 +1057,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string return mExitCode; } +<<<<<<< HEAD static ErrorMessage makeError(const std::string &filename, const std::string &msg, const std::string &id) { const std::string fullmsg("Bailing out from analysis: " + msg); @@ -1056,15 +1073,17 @@ static ErrorMessage makeError(const std::string &filename, const std::string &ms Certainty::normal); } +======= +>>>>>>> 74a6847dd (Remodel) // TODO: replace with ErrorMessage::fromInternalError() void CppCheck::internalError(const std::string &filename, const std::string &msg) { - mErrorLogger.reportErr(makeError(filename, "Bailing out from analysis:" + msg, "internalError")); + mErrorLogger.reportErr(makeError(filename, 0, 0U, "Bailing out from analysis:" + msg, "internalError")); } void CppCheck::fileNotFoundError(const std::string &filename, const std::string &msg) { - mErrorLogger.reportErr(makeError(filename, msg, "fileNotFound")); + mErrorLogger.reportErr(makeError(filename, 0, 0U, msg, "fileNotFound")); } //--------------------------------------------------------------------------- From 0ef564169818013ed0807792ee39e022d2268305 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Fri, 2 Feb 2024 17:00:15 +0200 Subject: [PATCH 10/13] Add fix --- lib/cppcheck.cpp | 32 +++++++------------------------- test/cli/more-projects_test.py | 2 +- test/testcppcheck.cpp | 3 ++- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 1d64c1751e4..3fec328d3f9 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -580,13 +580,9 @@ unsigned int CppCheck::check(const FileWithDetails &file, const std::string &con unsigned int CppCheck::check(const FileSettings &fs) { -<<<<<<< HEAD -<<<<<<< HEAD // TODO: move to constructor when CppCheck no longer owns the settings if (mSettings.checks.isEnabled(Checks::unusedFunction) && !mUnusedFunctionsCheck) mUnusedFunctionsCheck.reset(new CheckUnusedFunctions()); - -======= if (!Path::isFile(fs.filename)) { std::string fixedpath = Path::simplifyPath(fs.filename); fixedpath = Path::toNativeSeparators(fixedpath); @@ -594,9 +590,6 @@ unsigned int CppCheck::check(const FileSettings &fs) fileNotFoundError(fs.filename, errorMsg); return 0; } ->>>>>>> 8f585fe73 (Fix) -======= ->>>>>>> 74a6847dd (Remodel) CppCheck temp(mErrorLogger, mUseGlobalSuppressions, mExecuteCommand); temp.mSettings = mSettings; if (!temp.mSettings.userDefines.empty()) @@ -714,19 +707,16 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string std::string locfile = Path::fromNativeSeparators(output.location.file()); if (mSettings.relativePaths) locfile = Path::getRelativePath(locfile, mSettings.basePaths); - - ErrorMessage::FileLocation loc1(locfile, output.location.line, output.location.col); - - ErrorMessage errmsg({std::move(loc1)}, - "", // TODO: is this correct? - Severity::error, - output.msg, - "syntaxError", - Certainty::normal); if(output.type == simplecpp::Output::Type::FILE_NOT_FOUND){ const std::string fixedpath = Path::toNativeSeparators(file); const std::string errorMsg("File " + fixedpath + " does not exists. Skipping file."); - fileNotFoundError(file, errorMsg); + + reportErr(ErrorMessage(std::list (), + emptyString, + Severity::error, + errorMsg, + "fileNotFound", + Certainty::normal)); } else{ reportErr(makeError(file, output.location.line, output.location.col, output.msg, "syntaxError")); @@ -1057,7 +1047,6 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string return mExitCode; } -<<<<<<< HEAD static ErrorMessage makeError(const std::string &filename, const std::string &msg, const std::string &id) { const std::string fullmsg("Bailing out from analysis: " + msg); @@ -1073,19 +1062,12 @@ static ErrorMessage makeError(const std::string &filename, const std::string &ms Certainty::normal); } -======= ->>>>>>> 74a6847dd (Remodel) // TODO: replace with ErrorMessage::fromInternalError() void CppCheck::internalError(const std::string &filename, const std::string &msg) { mErrorLogger.reportErr(makeError(filename, 0, 0U, "Bailing out from analysis:" + msg, "internalError")); } -void CppCheck::fileNotFoundError(const std::string &filename, const std::string &msg) -{ - mErrorLogger.reportErr(makeError(filename, 0, 0U, msg, "fileNotFound")); -} - //--------------------------------------------------------------------------- // CppCheck - A function that checks a normal token list //--------------------------------------------------------------------------- diff --git a/test/cli/more-projects_test.py b/test/cli/more-projects_test.py index 4b10a224e70..6599f052328 100644 --- a/test/cli/more-projects_test.py +++ b/test/cli/more-projects_test.py @@ -870,4 +870,4 @@ def test_project_missing_files(): filename = os.path.join('invalid-project', 'main.c') ret, _, stderr = cppcheck(['--template=cppcheck1', '--project=' + os.path.join('invalid-project', 'invalid-project.vcxproj')]) assert ret == 0 - assert stderr == '[{}:0]: (error) File {} does not exists. Skipping file.\n'.format(filename, filename) + assert stderr == ': (error) File {} does not exists. Skipping file.\n'.format(filename) diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index b7e0048850f..19497fd9fc2 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -117,10 +117,11 @@ class TestCppcheck : public TestFixture { { ErrorLogger2 errorLogger; CppCheck cppcheck(errorLogger, false, {}); - ASSERT_EQUALS(0, cppcheck.check("NotAFile")); + ASSERT_EQUALS(1, cppcheck.check("NotAFile")); ASSERT_EQUALS(1, errorLogger.ids.size()); ASSERT_EQUALS("fileNotFound", *errorLogger.ids.cbegin()); + ASSERT_EQUALS("File NotAFile does not exists. Skipping file.", errorLogger.errmsgs.cbegin()->shortMessage()); } void checkWithFS() const From c13123953807224508f42c0db5418293b1f9abd5 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Fri, 30 Aug 2024 10:53:43 +0000 Subject: [PATCH 11/13] Nits --- lib/cppcheck.cpp | 11 +- lib/cppcheck.h | 3 - .../invalid-project/invalid-project.vcxproj | 123 ---------------- test/cli/more-projects_test.py | 133 +++++++++++++++++- test/testcppcheck.cpp | 2 +- test/testsuppressions.cpp | 16 ++- 6 files changed, 142 insertions(+), 146 deletions(-) delete mode 100644 test/cli/invalid-project/invalid-project.vcxproj diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 3fec328d3f9..54b6e38ea2e 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -583,13 +583,6 @@ unsigned int CppCheck::check(const FileSettings &fs) // TODO: move to constructor when CppCheck no longer owns the settings if (mSettings.checks.isEnabled(Checks::unusedFunction) && !mUnusedFunctionsCheck) mUnusedFunctionsCheck.reset(new CheckUnusedFunctions()); - if (!Path::isFile(fs.filename)) { - std::string fixedpath = Path::simplifyPath(fs.filename); - fixedpath = Path::toNativeSeparators(fixedpath); - const std::string errorMsg("File " + fixedpath + " does not exists. Skipping file."); - fileNotFoundError(fs.filename, errorMsg); - return 0; - } CppCheck temp(mErrorLogger, mUseGlobalSuppressions, mExecuteCommand); temp.mSettings = mSettings; if (!temp.mSettings.userDefines.empty()) @@ -708,7 +701,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string if (mSettings.relativePaths) locfile = Path::getRelativePath(locfile, mSettings.basePaths); if(output.type == simplecpp::Output::Type::FILE_NOT_FOUND){ - const std::string fixedpath = Path::toNativeSeparators(file); + const std::string fixedpath = Path::toNativeSeparators(file.path()); const std::string errorMsg("File " + fixedpath + " does not exists. Skipping file."); reportErr(ErrorMessage(std::list (), @@ -719,7 +712,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string Certainty::normal)); } else{ - reportErr(makeError(file, output.location.line, output.location.col, output.msg, "syntaxError")); + reportErr(makeError(file.path(), output.location.line, output.location.col, output.msg, "syntaxError")); } return mExitCode; } diff --git a/lib/cppcheck.h b/lib/cppcheck.h index e5790f2a599..9143760dcff 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -172,9 +172,6 @@ class CPPCHECKLIB CppCheck : ErrorLogger { /** @brief There has been an internal error => Report information message */ void internalError(const std::string &filename, const std::string &msg); - /** @brief File is missing => Report information message */ - void fileNotFoundError(const std::string &filename, const std::string &msg); - /** * @brief Check a file using stream * @param file the file diff --git a/test/cli/invalid-project/invalid-project.vcxproj b/test/cli/invalid-project/invalid-project.vcxproj deleted file mode 100644 index 12114b955f2..00000000000 --- a/test/cli/invalid-project/invalid-project.vcxproj +++ /dev/null @@ -1,123 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 15.0 - {7319858B-261C-4F0D-B022-92BB896242DD} - invalidProjet - 10.0.16299.0 - - - - Application - true - v141 - MultiByte - - - Application - false - v141 - true - MultiByte - - - Application - true - v141 - MultiByte - - - Application - false - v141 - true - MultiByte - - - - - - - - - - - - - - - - - - - - - - - Level3 - Disabled - true - true - - - - - Level3 - Disabled - true - true - - - - - Level3 - MaxSpeed - true - true - true - true - - - true - true - - - - - Level3 - MaxSpeed - true - true - true - true - - - true - true - - - - - - - - - \ No newline at end of file diff --git a/test/cli/more-projects_test.py b/test/cli/more-projects_test.py index 6599f052328..aaf17c12795 100644 --- a/test/cli/more-projects_test.py +++ b/test/cli/more-projects_test.py @@ -865,9 +865,136 @@ def test_shared_items_project(tmpdir = ""): assert any('2/2 files checked 100% done' in x for x in lines) assert stderr == '' +import shutil -def test_project_missing_files(): - filename = os.path.join('invalid-project', 'main.c') - ret, _, stderr = cppcheck(['--template=cppcheck1', '--project=' + os.path.join('invalid-project', 'invalid-project.vcxproj')]) +def test_project_missing_files(tmpdir): + filename = os.path.join(tmpdir, 'main.c') + project_file = os.path.join(tmpdir, 'helloworld.vcxproj') + with open(project_file, 'wt') as f: + f.write( +""" + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {7319858B-261C-4F0D-B022-92BB896242DD} + invalidProjet + 10.0.16299.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + + + + + Level3 + Disabled + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + + + + +""") + ret, _, stderr = cppcheck(['--template=cppcheck1', '--project=' + os.path.join(tmpdir, 'helloworld.vcxproj')]) assert ret == 0 assert stderr == ': (error) File {} does not exists. Skipping file.\n'.format(filename) diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index 19497fd9fc2..9198708dd33 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -117,7 +117,7 @@ class TestCppcheck : public TestFixture { { ErrorLogger2 errorLogger; CppCheck cppcheck(errorLogger, false, {}); - ASSERT_EQUALS(1, cppcheck.check("NotAFile")); + ASSERT_EQUALS(1, cppcheck.check(FileWithDetails("NotAFile"))); ASSERT_EQUALS(1, errorLogger.ids.size()); ASSERT_EQUALS("fileNotFound", *errorLogger.ids.cbegin()); diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index a22845de980..153d3410cb2 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -1190,9 +1190,10 @@ class TestSuppressions : public TestFixture { ASSERT_EQUALS("", settings.supprs.nomsg.addSuppressionLine("uninitvar")); settings.exitCode = 1; - const char code[] = "int f() { int a; return a; }"; - ASSERT_EQUALS(0, cppCheck.check("test.c", code)); // <- no unsuppressed error is seen - ASSERT_EQUALS("[test.c:1]: (error) Uninitialized variable: a\n", errout.str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched. + ScopedFile file("test.c", "int f() { int a; return a; }"); + FileSettings fs{file.path()}; + ASSERT_EQUALS(0, cppCheck.check(fs)); // <- no unsuppressed error is seen + ASSERT_EQUALS("[test.c:1]: (error) Uninitialized variable: a\n", errout_str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched. } void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction" @@ -1223,16 +1224,17 @@ class TestSuppressions : public TestFixture { settings.inlineSuppressions = true; settings.relativePaths = true; settings.basePaths.emplace_back("/somewhere"); - const char code[] = + ScopedFile file("test.cpp", "struct Point\n" "{\n" " // cppcheck-suppress unusedStructMember\n" " int x;\n" " // cppcheck-suppress unusedStructMember\n" " int y;\n" - "};"; - ASSERT_EQUALS(0, cppCheck.check("/somewhere/test.cpp", code)); - ASSERT_EQUALS("",errout.str()); + "};"); + FileSettings fs{file.path()}; + ASSERT_EQUALS(0, cppCheck.check(fs)); + ASSERT_EQUALS("", errout_str()); } void suppressingSyntaxErrorsInternal(unsigned int (TestSuppressions::*check)(const char[], const std::string &)) { // syntaxErrors should be suppressible (#7076) From 34d7292865a8e13eece91507acecead795d7a065 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Fri, 30 Aug 2024 17:59:06 +0300 Subject: [PATCH 12/13] Fixes --- lib/cppcheck.cpp | 43 ++++++++-------------------------- test/cli/more-projects_test.py | 14 +++++------ test/testsuppressions.cpp | 14 +++++------ 3 files changed, 24 insertions(+), 47 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 54b6e38ea2e..67be1dd51e9 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -554,14 +554,9 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file) static ErrorMessage makeError(const std::string &filename, int line, unsigned int column, const std::string &msg, const std::string &id) { const ErrorMessage::FileLocation loc1(filename, line, column); - std::list callstack(1, loc1); + const std::list callstack(1, loc1); - return ErrorMessage(callstack, - emptyString, - Severity::error, - msg, - id, - Certainty::normal); + return ErrorMessage(callstack, emptyString, Severity::error, msg, id, Certainty::normal); } unsigned int CppCheck::check(const FileWithDetails &file) @@ -634,8 +629,6 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string if (mSettings.checks.isEnabled(Checks::unusedFunction) && !mUnusedFunctionsCheck) mUnusedFunctionsCheck.reset(new CheckUnusedFunctions()); - FilesDeleter filesDeleter; - if (Settings::terminated()) return mExitCode; @@ -700,18 +693,18 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string std::string locfile = Path::fromNativeSeparators(output.location.file()); if (mSettings.relativePaths) locfile = Path::getRelativePath(locfile, mSettings.basePaths); - if(output.type == simplecpp::Output::Type::FILE_NOT_FOUND){ + if (output.type == simplecpp::Output::Type::FILE_NOT_FOUND) { const std::string fixedpath = Path::toNativeSeparators(file.path()); - const std::string errorMsg("File " + fixedpath + " does not exists. Skipping file."); + const std::string errorMsg("File " + fixedpath + " does not exist. Skipping file."); reportErr(ErrorMessage(std::list (), - emptyString, - Severity::error, - errorMsg, - "fileNotFound", - Certainty::normal)); + emptyString, + Severity::error, + errorMsg, + "fileNotFound", + Certainty::normal)); } - else{ + else { reportErr(makeError(file.path(), output.location.line, output.location.col, output.msg, "syntaxError")); } return mExitCode; @@ -773,7 +766,6 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string return mExitCode; // known results => no need to reanalyze file } } - FilesDeleter filesDeleter; // write dump file xml prolog @@ -1040,21 +1032,6 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string return mExitCode; } -static ErrorMessage makeError(const std::string &filename, const std::string &msg, const std::string &id) -{ - const std::string fullmsg("Bailing out from analysis: " + msg); - - const ErrorMessage::FileLocation loc1(filename, 0, 0); - std::list callstack(1, loc1); - - return ErrorMessage(callstack, - emptyString, - Severity::error, - fullmsg, - id, - Certainty::normal); -} - // TODO: replace with ErrorMessage::fromInternalError() void CppCheck::internalError(const std::string &filename, const std::string &msg) { diff --git a/test/cli/more-projects_test.py b/test/cli/more-projects_test.py index aaf17c12795..a88cea55e77 100644 --- a/test/cli/more-projects_test.py +++ b/test/cli/more-projects_test.py @@ -898,7 +898,7 @@ def test_project_missing_files(tmpdir): invalidProjet 10.0.16299.0 - + Application true @@ -925,22 +925,22 @@ def test_project_missing_files(tmpdir): true MultiByte - + - + - + - + - + @@ -991,7 +991,7 @@ def test_project_missing_files(tmpdir): - + """) diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 153d3410cb2..2dc8b31247d 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -1225,13 +1225,13 @@ class TestSuppressions : public TestFixture { settings.relativePaths = true; settings.basePaths.emplace_back("/somewhere"); ScopedFile file("test.cpp", - "struct Point\n" - "{\n" - " // cppcheck-suppress unusedStructMember\n" - " int x;\n" - " // cppcheck-suppress unusedStructMember\n" - " int y;\n" - "};"); + "struct Point\n" + "{\n" + " // cppcheck-suppress unusedStructMember\n" + " int x;\n" + " // cppcheck-suppress unusedStructMember\n" + " int y;\n" + "};"); FileSettings fs{file.path()}; ASSERT_EQUALS(0, cppCheck.check(fs)); ASSERT_EQUALS("", errout_str()); From d34c6fe0bc62d1fe304dc8ab9bfe183345f39f8f Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Fri, 30 Aug 2024 18:00:33 +0300 Subject: [PATCH 13/13] Python fix --- test/cli/more-projects_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/cli/more-projects_test.py b/test/cli/more-projects_test.py index a88cea55e77..981b0a4e07a 100644 --- a/test/cli/more-projects_test.py +++ b/test/cli/more-projects_test.py @@ -865,7 +865,6 @@ def test_shared_items_project(tmpdir = ""): assert any('2/2 files checked 100% done' in x for x in lines) assert stderr == '' -import shutil def test_project_missing_files(tmpdir): filename = os.path.join(tmpdir, 'main.c')