From 01ceb9bae7b91cf2eef544de2214b6ae5f9952e5 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 25 Jul 2018 16:26:25 +0200 Subject: [PATCH] fixed issue 8661: Misleading error message when compilation database is not recognized --- cli/cmdlineparser.cpp | 13 +++++++++++-- lib/importproject.cpp | 17 +++++++++++------ lib/importproject.h | 11 ++++++++++- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index ec2c54a82..39468e93f 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -503,14 +503,23 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) // --project else if (std::strncmp(argv[i], "--project=", 10) == 0) { - _settings->project.import(argv[i]+10); - if (std::strstr(argv[i], ".sln") || std::strstr(argv[i], ".vcxproj")) { + const std::string projectFile = argv[i]+10; + const ImportProject::Type projType = _settings->project.import(projectFile); + if (projType == ImportProject::VS_SLN || projType == ImportProject::VS_VCXPROJ) { if (!CppCheckExecutor::tryLoadLibrary(_settings->library, argv[0], "windows.cfg")) { // This shouldn't happen normally. printMessage("cppcheck: Failed to load 'windows.cfg'. Your Cppcheck installation is broken. Please re-install."); return false; } } + if (projType == ImportProject::MISSING) { + printMessage("cppcheck: Failed to open project '" + projectFile + "'."); + return false; + } + if (projType == ImportProject::UNKNOWN) { + printMessage("cppcheck: Failed to load project '" + projectFile + "'. The format is unknown."); + return false; + } } // Report progress diff --git a/lib/importproject.cpp b/lib/importproject.cpp index abc36e55e..f07e546ea 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -167,24 +167,29 @@ void ImportProject::FileSettings::setIncludePaths(const std::string &basepath, c includePaths.swap(I); } -void ImportProject::import(const std::string &filename) +ImportProject::Type ImportProject::import(const std::string &filename) { std::ifstream fin(filename); if (!fin.is_open()) - return; - if (filename.find("compile_commands.json") != std::string::npos) { + return MISSING; + if (endsWith(filename, "compile_commands.json", 21)) { importCompileCommands(fin); - } else if (filename.find(".sln") != std::string::npos) { + return COMPILE_DB; + } else if (endsWith(filename, ".sln", 4)) { std::string path(Path::getPathFromFilename(Path::fromNativeSeparators(filename))); if (!path.empty() && !endsWith(path,'/')) path += '/'; importSln(fin,path); - } else if (filename.find(".vcxproj") != std::string::npos) { + return VS_SLN; + } else if (endsWith(filename, ".vcxproj", 8)) { std::map variables; importVcxproj(filename, variables, emptyString); - } else if (filename.find(".bpr") != std::string::npos) { + return VS_VCXPROJ; + } else if (endsWith(filename, ".bpr", 4)) { importBcb6Prj(filename); + return BORLAND; } + return UNKNOWN; } void ImportProject::importCompileCommands(std::istream &istr) diff --git a/lib/importproject.h b/lib/importproject.h index 04be54fb7..f7a2149d6 100644 --- a/lib/importproject.h +++ b/lib/importproject.h @@ -47,6 +47,15 @@ namespace cppcheck { */ class CPPCHECKLIB ImportProject { public: + enum Type { + UNKNOWN, + MISSING, + COMPILE_DB, + VS_SLN, + VS_VCXPROJ, + BORLAND + }; + /** File settings. Multiple configurations for a file is allowed. */ struct CPPCHECKLIB FileSettings { FileSettings() : platformType(cppcheck::Platform::Unspecified), msc(false), useMfc(false) {} @@ -73,7 +82,7 @@ public: void ignoreOtherConfigs(const std::string &cfg); void ignoreOtherPlatforms(cppcheck::Platform::PlatformType platformType); - void import(const std::string &filename); + Type import(const std::string &filename); protected: void importCompileCommands(std::istream &istr); private: