From 2e2d4a00551da7164602a91f3f7f25ab1cc85393 Mon Sep 17 00:00:00 2001 From: Falital Date: Fri, 13 Nov 2020 15:52:24 +0100 Subject: [PATCH] Added Hash to plist file names (#2898) --- cli/cmdlineparser.cpp | 10 ++++++++++ cli/filelister.cpp | 2 +- lib/cppcheck.cpp | 3 ++- lib/path.h | 5 +++++ test/testcmdlineparser.cpp | 18 ++++++++++++++++++ test/testfilelister.cpp | 6 ++++++ 6 files changed, 42 insertions(+), 2 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 044a87df3..d9f55b370 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -576,6 +576,16 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) mSettings->plistOutput = "./"; else if (!endsWith(mSettings->plistOutput,'/')) mSettings->plistOutput += '/'; + + const std::string plistOutput = Path::toNativeSeparators(mSettings->plistOutput); + if (!FileLister::isDirectory(plistOutput)) + { + std::string message("cppcheck: error: plist folder does not exist: \""); + message += plistOutput; + message += "\"."; + printMessage(message); + return false; + } } // --project diff --git a/cli/filelister.cpp b/cli/filelister.cpp index e55d47f1e..1490e5e7f 100644 --- a/cli/filelister.cpp +++ b/cli/filelister.cpp @@ -64,7 +64,7 @@ static BOOL myFileExists(const std::string& path) if (fa != INVALID_FILE_ATTRIBUTES && !(fa & FILE_ATTRIBUTE_DIRECTORY)) result = TRUE; #else - const BOOL result = PathFileExistsA(path.c_str()); + const BOOL result = PathFileExistsA(path.c_str()) && !PathIsDirectoryA(path.c_str()); #endif return result; } diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index e0af1a682..4604029c1 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -516,7 +516,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string filename2 = filename.substr(filename.rfind('/') + 1); else filename2 = filename; - filename2 = mSettings.plistOutput + filename2.substr(0, filename2.find('.')) + ".plist"; + std::size_t fileNameHash = std::hash{}(filename); + filename2 = mSettings.plistOutput + filename2.substr(0, filename2.find('.')) + "_" + std::to_string(fileNameHash) + ".plist"; plistFile.open(filename2); plistFile << ErrorLogger::plistHeader(version(), files); } diff --git a/lib/path.h b/lib/path.h index 588a30bef..af5319c49 100644 --- a/lib/path.h +++ b/lib/path.h @@ -173,6 +173,11 @@ public: */ static std::string stripDirectoryPart(const std::string &file); + /** + * @brief Checks if a File exists + * @param path Path to be checked if it is a File + * @return true if given path is a File + */ static bool fileExists(const std::string &file); }; diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 2aa276d33..c2c39f347 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -106,6 +106,8 @@ private: TEST_CASE(stdc99); TEST_CASE(stdcpp11); TEST_CASE(platform); + TEST_CASE(plistEmpty); + TEST_CASE(plistDoesNotExist); TEST_CASE(suppressionsOld); // TODO: Create and test real suppression file TEST_CASE(suppressions); TEST_CASE(suppressionsNoFile); @@ -717,6 +719,22 @@ private: ASSERT(settings.platformType == Settings::Win64); } + void plistEmpty() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--plist-output=", "file.cpp"}; + settings.plistOutput = ""; + ASSERT(defParser.parseFromArgs(3, argv)); + ASSERT(settings.plistOutput == "./"); + } + + void plistDoesNotExist() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--plist-output=./cppcheck_reports", "file.cpp"}; + settings.plistOutput = ""; + // Fails since folder pointed by --plist-output= does not exist + ASSERT_EQUALS(false, defParser.parseFromArgs(3, argv)); + } + void suppressionsOld() { // TODO: Fails because there is no suppr.txt file! REDIRECT; diff --git a/test/testfilelister.cpp b/test/testfilelister.cpp index afe69bdd7..94304c173 100644 --- a/test/testfilelister.cpp +++ b/test/testfilelister.cpp @@ -47,6 +47,7 @@ private: TEST_CASE(isDirectory); TEST_CASE(recursiveAddFiles); + TEST_CASE(fileExists); } void isDirectory() const { @@ -79,6 +80,11 @@ private: // Make sure headers are not added.. ASSERT(files.find("lib/tokenize.h") == files.end()); } + + void fileExists() const { + ASSERT_EQUALS(false, FileLister::fileExists("lib")); + ASSERT_EQUALS(true, FileLister::fileExists("readme.txt")); + } }; REGISTER_TEST(TestFileLister)