Skip to content

Commit e6b0081

Browse files
authored
Fix #13040 (Import project: compile_commands.json, fail to include header in project root folder) (danmar#6724)
1 parent af5b6ef commit e6b0081

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Diff for: lib/importproject.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void ImportProject::fsSetIncludePaths(FileSettings& fs, const std::string &basep
164164
}
165165

166166
if (endsWith(s,'/')) // this is a temporary hack, simplifyPath can crash if path ends with '/'
167-
s.erase(s.size() - 1U); // TODO: Use std::string::pop_back() as soon as travis supports it
167+
s.pop_back();
168168

169169
if (s.find("$(") == std::string::npos) {
170170
s = Path::simplifyPath(basepath + s);
@@ -174,7 +174,7 @@ void ImportProject::fsSetIncludePaths(FileSettings& fs, const std::string &basep
174174
}
175175
if (s.empty())
176176
continue;
177-
fs.includePaths.push_back(s + '/');
177+
fs.includePaths.push_back(s.back() == '/' ? s : (s + '/'));
178178
}
179179
}
180180

Diff for: test/testimportproject.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class TestImportProject : public TestFixture {
6262
TEST_CASE(importCompileCommands9);
6363
TEST_CASE(importCompileCommands10); // #10887: include path with space
6464
TEST_CASE(importCompileCommands11); // include path order
65+
TEST_CASE(importCompileCommands12); // #13040: "directory" is parent directory, relative include paths
6566
TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section
6667
TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json
6768
TEST_CASE(importCppcheckGuiProject);
@@ -319,6 +320,23 @@ class TestImportProject : public TestFixture {
319320
ASSERT_EQUALS("/x/abc/", fs.includePaths.back());
320321
}
321322

323+
void importCompileCommands12() const { // #13040
324+
REDIRECT;
325+
constexpr char json[] =
326+
R"([{
327+
"file": "/x/src/1.c" ,
328+
"directory": "/x",
329+
"command": "cc -c -I. src/1.c"
330+
}])";
331+
std::istringstream istr(json);
332+
TestImporter importer;
333+
ASSERT_EQUALS(true, importer.importCompileCommands(istr));
334+
ASSERT_EQUALS(1, importer.fileSettings.size());
335+
const FileSettings &fs = importer.fileSettings.front();
336+
ASSERT_EQUALS(1, fs.includePaths.size());
337+
ASSERT_EQUALS("/x/", fs.includePaths.front());
338+
}
339+
322340
void importCompileCommandsArgumentsSection() const {
323341
REDIRECT;
324342
constexpr char json[] = "[ { \"directory\": \"/tmp/\","

0 commit comments

Comments
 (0)