Fixed #8563 (CPPCheck not able to locate file through compilation database)

This commit is contained in:
Daniel Marjamäki 2019-01-05 23:11:43 +01:00
parent 817c748e4d
commit ea0232653f
4 changed files with 32 additions and 4 deletions

View File

@ -265,7 +265,15 @@ void ImportProject::importCompileCommands(std::istream &istr)
const std::string file = Path::fromNativeSeparators(obj["file"].get<std::string>());
struct FileSettings fs;
fs.filename = file;
if (Path::isAbsolute(file) || Path::fileExists(file))
fs.filename = file;
else {
std::string path = directory;
if (!path.empty() && !endsWith(path,'/'))
path += '/';
path += file;
fs.filename = Path::simplifyPath(path);
}
fs.parseCommand(command); // read settings; -D, -I, -U, -std
std::map<std::string, std::string, cppcheck::stricmp> variables;
fs.setIncludePaths(directory, fs.includePaths, variables);

View File

@ -26,6 +26,7 @@
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#ifndef _WIN32
@ -242,3 +243,9 @@ std::string Path::stripDirectoryPart(const std::string &file)
}
return file;
}
bool Path::fileExists(const std::string &file)
{
std::ifstream f(file.c_str());
return f.is_open();
}

View File

@ -172,6 +172,8 @@ public:
* @return filename without directory path part.
*/
static std::string stripDirectoryPart(const std::string &file);
static bool fileExists(const std::string &file);
};
/// @}

View File

@ -43,7 +43,8 @@ private:
TEST_CASE(setIncludePaths1);
TEST_CASE(setIncludePaths2);
TEST_CASE(setIncludePaths3); // macro names are case insensitive
TEST_CASE(importCompileCommands);
TEST_CASE(importCompileCommands1);
TEST_CASE(importCompileCommands2); // #8563
}
void setDefines() const {
@ -91,8 +92,7 @@ private:
ASSERT_EQUALS("c:/abc/other/", fs.includePaths.front());
}
void importCompileCommands() const {
void importCompileCommands1() const {
const char json[] = "[ { \"directory\": \"/tmp\","
"\"command\": \"gcc -I/tmp -DCFGDIR=\\\\\\\"/usr/local/share/Cppcheck\\\\\\\" -DTEST1 -DTEST2=2 -o /tmp/src.o -c /tmp/src.c\","
"\"file\": \"/tmp/src.c\" } ]";
@ -102,6 +102,17 @@ private:
ASSERT_EQUALS(1, importer.fileSettings.size());
ASSERT_EQUALS("CFGDIR=\"/usr/local/share/Cppcheck\";TEST1=1;TEST2=2", importer.fileSettings.begin()->defines);
}
void importCompileCommands2() const {
const char json[] = "[ { \"directory\": \"/tmp\","
"\"command\": \"gcc -c src.c\","
"\"file\": \"src.c\" } ]";
std::istringstream istr(json);
TestImporter importer;
importer.importCompileCommands(istr);
ASSERT_EQUALS(1, importer.fileSettings.size());
ASSERT_EQUALS("/tmp/src.c", importer.fileSettings.begin()->filename);
}
};
REGISTER_TEST(TestImportProject)