Skip to content

Commit 9a97ccf

Browse files
authored
fixed #14746 import project: include path in compile_commands.json not handled well (#8591)
1 parent fd03958 commit 9a97ccf

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

lib/importproject.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,8 +1699,10 @@ void ImportProject::setRelativePaths(const std::string &filename)
16991699
const std::vector<std::string> basePaths{Path::fromNativeSeparators(Path::getCurrentPath())};
17001700
for (auto &fs: fileSettings) {
17011701
fs.file.setPath(Path::getRelativePath(fs.filename(), basePaths));
1702-
for (auto &includePath: fs.includePaths)
1703-
includePath = Path::getRelativePath(includePath, basePaths);
1702+
for (auto &includePath: fs.includePaths) {
1703+
const std::string rel = Path::getRelativePath(includePath, basePaths);
1704+
includePath = rel.empty() ? "." : rel;
1705+
}
17041706
}
17051707
}
17061708

lib/importproject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class CPPCHECKLIB WARN_UNUSED ImportProject {
110110
bool importCompileCommands(std::istream &istr);
111111
bool importCppcheckGuiProject(std::istream &istr, Settings &settings, Suppressions &supprs);
112112
static std::string collectArgs(const std::string &cmd, std::vector<std::string> &args);
113+
void setRelativePaths(const std::string &filename);
113114

114115
struct SharedItemsProject {
115116
bool successful = false;
@@ -123,7 +124,6 @@ class CPPCHECKLIB WARN_UNUSED ImportProject {
123124

124125
private:
125126
static void parseArgs(FileSettings &fs, const std::vector<std::string> &args);
126-
void setRelativePaths(const std::string &filename);
127127

128128
bool importSln(std::istream &istr, const std::string &path, const std::vector<std::string> &fileFilters);
129129
bool importSlnx(const std::string& filename, const std::vector<std::string>& fileFilters);

test/testimportproject.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TestImporter final : public ImportProject {
4242
using ImportProject::collectArgs;
4343
using ImportProject::fsSetDefines;
4444
using ImportProject::fsSetIncludePaths;
45+
using ImportProject::setRelativePaths;
4546
};
4647

4748

@@ -56,6 +57,7 @@ class TestImportProject : public TestFixture {
5657
TEST_CASE(setIncludePaths1);
5758
TEST_CASE(setIncludePaths2);
5859
TEST_CASE(setIncludePaths3); // macro names are case insensitive
60+
TEST_CASE(setRelativePathsInclude); // #14746
5961
TEST_CASE(importCompileCommands1);
6062
TEST_CASE(importCompileCommands2); // #8563, #9567
6163
TEST_CASE(importCompileCommands3); // check with existing trailing / in directory
@@ -134,6 +136,18 @@ class TestImportProject : public TestFixture {
134136
ASSERT_EQUALS("c:/abc/other/", fs.includePaths.front());
135137
}
136138

139+
void setRelativePathsInclude() const {
140+
const std::string cwd = Path::fromNativeSeparators(Path::getCurrentPath());
141+
TestImporter importer;
142+
FileSettings fs{cwd + "/sub/a.c", Standards::Language::C, 0};
143+
fs.includePaths.push_back(cwd + "/");
144+
importer.fileSettings.push_back(fs);
145+
importer.setRelativePaths("compile_commands.json");
146+
fs = importer.fileSettings.front();
147+
ASSERT_EQUALS(".", fs.includePaths.front());
148+
ASSERT_EQUALS("sub/a.c", fs.filename());
149+
}
150+
137151
void importCompileCommands1() const {
138152
REDIRECT;
139153
constexpr char json[] = R"([{

0 commit comments

Comments
 (0)