fixed issue 8661: Misleading error message when compilation database is not recognized

This commit is contained in:
firewave 2018-07-25 16:26:25 +02:00 committed by Daniel Marjamäki
parent 58c91c4645
commit 01ceb9bae7
3 changed files with 32 additions and 9 deletions

View File

@ -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

View File

@ -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<std::string, std::string, cppcheck::stricmp> 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)

View File

@ -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: