Skip to content

Commit 212ac6c

Browse files
committed
compile_commands.json: ensure that order of include paths are kept
1 parent 4365704 commit 212ac6c

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

cmake/clang_tidy.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ find_program(RUN_CLANG_TIDY NAMES run-clang-tidy run-clang-tidy-13 run-clang-tid
1111
message(STATUS "RUN_CLANG_TIDY=${RUN_CLANG_TIDY}")
1212
if (RUN_CLANG_TIDY)
1313
# disable all compiler warnings since we are just interested in the tidy ones
14-
add_custom_target(run-clang-tidy ${RUN_CLANG_TIDY} -p=${CMAKE_BINARY_DIR} -j ${NPROC} -extra-arg=-w -quiet)
14+
add_custom_target(run-clang-tidy ${RUN_CLANG_TIDY} -checks=-performance-unnecessary-copy-initialization -p=${CMAKE_BINARY_DIR} -j ${NPROC} -extra-arg=-w -quiet)
1515
if (BUILD_GUI)
1616
add_dependencies(run-clang-tidy gui-build-deps)
1717
if (BUILD_TESTS)
1818
add_dependencies(run-clang-tidy triage-build-ui-deps)
1919
endif()
2020
endif()
21-
endif()
21+
endif()

lib/importproject.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,21 @@ static bool simplifyPathWithVariables(std::string &s, std::map<std::string, std:
147147

148148
void ImportProject::FileSettings::setIncludePaths(const std::string &basepath, const std::list<std::string> &in, std::map<std::string, std::string, cppcheck::stricmp> &variables)
149149
{
150-
std::list<std::string> listInc;
151-
// only parse each includePath once - so remove duplicates
152-
std::list<std::string> uniqueIncludePaths = in;
153-
uniqueIncludePaths.sort();
154-
uniqueIncludePaths.unique();
155-
156-
for (const std::string &it : uniqueIncludePaths) {
157-
if (it.empty())
150+
std::set<std::string> found;
151+
const std::list<std::string> copyIn(in);
152+
includePaths.clear();
153+
for (const std::string &ipath : copyIn) {
154+
if (ipath.empty())
158155
continue;
159-
if (it.compare(0,2,"%(")==0)
156+
if (ipath.compare(0,2,"%(")==0)
157+
continue;
158+
std::string s(Path::fromNativeSeparators(ipath));
159+
if (!found.insert(s).second)
160160
continue;
161-
std::string s(Path::fromNativeSeparators(it));
162161
if (s[0] == '/' || (s.size() > 1U && s.compare(1,2,":/") == 0)) {
163162
if (!endsWith(s,'/'))
164163
s += '/';
165-
listInc.push_back(s);
164+
includePaths.push_back(s);
166165
continue;
167166
}
168167

@@ -177,9 +176,8 @@ void ImportProject::FileSettings::setIncludePaths(const std::string &basepath, c
177176
}
178177
if (s.empty())
179178
continue;
180-
listInc.push_back(s + '/');
179+
includePaths.push_back(s + '/');
181180
}
182-
includePaths.swap(listInc);
183181
}
184182

185183
ImportProject::Type ImportProject::import(const std::string &filename, Settings *settings)

test/testimportproject.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class TestImportProject : public TestFixture {
5959
TEST_CASE(importCompileCommands8); // Windows: "C:\Users\danielm\cppcheck"
6060
TEST_CASE(importCompileCommands9);
6161
TEST_CASE(importCompileCommands10); // #10887: include path with space
62+
TEST_CASE(importCompileCommands11); // include path order
6263
TEST_CASE(importCompileCommandsArgumentsSection); // Handle arguments section
6364
TEST_CASE(importCompileCommandsNoCommandSection); // gracefully handles malformed json
6465
TEST_CASE(importCppcheckGuiProject);
@@ -210,7 +211,8 @@ class TestImportProject : public TestFixture {
210211
TestImporter importer;
211212
ASSERT_EQUALS(true, importer.importCompileCommands(istr));
212213
ASSERT_EQUALS(2, importer.fileSettings.size());
213-
ASSERT_EQUALS("C:/Users/dan/git/test-cppcheck/mylib/second src/", importer.fileSettings.begin()->includePaths.front());
214+
ASSERT_EQUALS("C:/Users/dan/git/test-cppcheck/mylib/src/", importer.fileSettings.begin()->includePaths.front());
215+
ASSERT_EQUALS("C:/Users/dan/git/test-cppcheck/mylib/second src/", importer.fileSettings.begin()->includePaths.back());
214216
}
215217

216218

@@ -282,6 +284,28 @@ class TestImportProject : public TestFixture {
282284
ASSERT_EQUALS("/home/danielm/cppcheck/test folder/", fs.includePaths.front());
283285
}
284286

287+
void importCompileCommands11() const { // include path order
288+
const char json[] =
289+
R"([{
290+
"file": "1.c" ,
291+
"directory": "/x",
292+
"arguments": [
293+
"cc",
294+
"-I",
295+
"def",
296+
"-I",
297+
"abc"
298+
]
299+
}])";
300+
std::istringstream istr(json);
301+
TestImporter importer;
302+
ASSERT_EQUALS(true, importer.importCompileCommands(istr));
303+
ASSERT_EQUALS(1, importer.fileSettings.size());
304+
const ImportProject::FileSettings &fs = importer.fileSettings.front();
305+
ASSERT_EQUALS("/x/def/", fs.includePaths.front());
306+
ASSERT_EQUALS("/x/abc/", fs.includePaths.back());
307+
}
308+
285309
void importCompileCommandsArgumentsSection() const {
286310
const char json[] = "[ { \"directory\": \"/tmp/\","
287311
"\"arguments\": [\"gcc\", \"-c\", \"src.c\"],"

0 commit comments

Comments
 (0)