Skip to content

Commit

Permalink
Fix #13040 (Import project: compile_commands.json, fail to include he…
Browse files Browse the repository at this point in the history
…ader in project root folder)
  • Loading branch information
danmar committed Aug 27, 2024
1 parent 6faed30 commit 11dd3d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void ImportProject::fsSetIncludePaths(FileSettings& fs, const std::string &basep
}

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

if (s.find("$(") == std::string::npos) {
s = Path::simplifyPath(basepath + s);
Expand All @@ -174,7 +174,7 @@ void ImportProject::fsSetIncludePaths(FileSettings& fs, const std::string &basep
}
if (s.empty())
continue;
fs.includePaths.push_back(s + '/');
fs.includePaths.push_back(s.back() == '/' ? s : (s + '/'));
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/testimportproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class TestImportProject : public TestFixture {
TEST_CASE(importCompileCommands9);
TEST_CASE(importCompileCommands10); // #10887: include path with space
TEST_CASE(importCompileCommands11); // include path order
TEST_CASE(importCompileCommands12); // #13040: "directory" is parent directory, relative include paths
TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section
TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json
TEST_CASE(importCppcheckGuiProject);
Expand Down Expand Up @@ -319,6 +320,23 @@ class TestImportProject : public TestFixture {
ASSERT_EQUALS("/x/abc/", fs.includePaths.back());
}

void importCompileCommands12() const { // #13040
REDIRECT;
constexpr char json[] =
R"([{
"file": "/x/src/1.c" ,
"directory": "/x",
"command": "cc -c -I. src/1.c"
}])";
std::istringstream istr(json);
TestImporter importer;
ASSERT_EQUALS(true, importer.importCompileCommands(istr));
ASSERT_EQUALS(1, importer.fileSettings.size());
const FileSettings &fs = importer.fileSettings.front();
ASSERT_EQUALS(1, fs.includePaths.size());
ASSERT_EQUALS("/x/", fs.includePaths.front());
}

void importCompileCommandsArgumentsSection() const {
REDIRECT;
constexpr char json[] = "[ { \"directory\": \"/tmp/\","
Expand Down

0 comments on commit 11dd3d7

Please sign in to comment.