Skip to content

Commit ea02326

Browse files
committed
Fixed #8563 (CPPCheck not able to locate file through compilation database)
1 parent 817c748 commit ea02326

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

lib/importproject.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,15 @@ void ImportProject::importCompileCommands(std::istream &istr)
265265
const std::string file = Path::fromNativeSeparators(obj["file"].get<std::string>());
266266

267267
struct FileSettings fs;
268-
fs.filename = file;
268+
if (Path::isAbsolute(file) || Path::fileExists(file))
269+
fs.filename = file;
270+
else {
271+
std::string path = directory;
272+
if (!path.empty() && !endsWith(path,'/'))
273+
path += '/';
274+
path += file;
275+
fs.filename = Path::simplifyPath(path);
276+
}
269277
fs.parseCommand(command); // read settings; -D, -I, -U, -std
270278
std::map<std::string, std::string, cppcheck::stricmp> variables;
271279
fs.setIncludePaths(directory, fs.includePaths, variables);

lib/path.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <cctype>
2727
#include <cstdlib>
2828
#include <cstring>
29+
#include <fstream>
2930
#include <sstream>
3031

3132
#ifndef _WIN32
@@ -242,3 +243,9 @@ std::string Path::stripDirectoryPart(const std::string &file)
242243
}
243244
return file;
244245
}
246+
247+
bool Path::fileExists(const std::string &file)
248+
{
249+
std::ifstream f(file.c_str());
250+
return f.is_open();
251+
}

lib/path.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ class CPPCHECKLIB Path {
172172
* @return filename without directory path part.
173173
*/
174174
static std::string stripDirectoryPart(const std::string &file);
175+
176+
static bool fileExists(const std::string &file);
175177
};
176178

177179
/// @}

test/testimportproject.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class TestImportProject : public TestFixture {
4343
TEST_CASE(setIncludePaths1);
4444
TEST_CASE(setIncludePaths2);
4545
TEST_CASE(setIncludePaths3); // macro names are case insensitive
46-
TEST_CASE(importCompileCommands);
46+
TEST_CASE(importCompileCommands1);
47+
TEST_CASE(importCompileCommands2); // #8563
4748
}
4849

4950
void setDefines() const {
@@ -91,8 +92,7 @@ class TestImportProject : public TestFixture {
9192
ASSERT_EQUALS("c:/abc/other/", fs.includePaths.front());
9293
}
9394

94-
void importCompileCommands() const {
95-
95+
void importCompileCommands1() const {
9696
const char json[] = "[ { \"directory\": \"/tmp\","
9797
"\"command\": \"gcc -I/tmp -DCFGDIR=\\\\\\\"/usr/local/share/Cppcheck\\\\\\\" -DTEST1 -DTEST2=2 -o /tmp/src.o -c /tmp/src.c\","
9898
"\"file\": \"/tmp/src.c\" } ]";
@@ -102,6 +102,17 @@ class TestImportProject : public TestFixture {
102102
ASSERT_EQUALS(1, importer.fileSettings.size());
103103
ASSERT_EQUALS("CFGDIR=\"/usr/local/share/Cppcheck\";TEST1=1;TEST2=2", importer.fileSettings.begin()->defines);
104104
}
105+
106+
void importCompileCommands2() const {
107+
const char json[] = "[ { \"directory\": \"/tmp\","
108+
"\"command\": \"gcc -c src.c\","
109+
"\"file\": \"src.c\" } ]";
110+
std::istringstream istr(json);
111+
TestImporter importer;
112+
importer.importCompileCommands(istr);
113+
ASSERT_EQUALS(1, importer.fileSettings.size());
114+
ASSERT_EQUALS("/tmp/src.c", importer.fileSettings.begin()->filename);
115+
}
105116
};
106117

107118
REGISTER_TEST(TestImportProject)

0 commit comments

Comments
 (0)