diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 146456684..21ff348ae 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -424,15 +424,17 @@ void ImportProject::importCompileCommands(std::istream &istr) continue; struct FileSettings fs; - if (Path::isAbsolute(file) || Path::fileExists(file)) - fs.filename = file; - else { - std::string path = directory; - if (!path.empty() && !endsWith(path,'/')) - path += '/'; - path += file; - fs.filename = Path::simplifyPath(path); - } + if (Path::isAbsolute(file)) + fs.filename = Path::simplifyPath(file); +#ifdef _WIN32 + else if (file[0] == '/' && directory.size() > 2 && std::isalpha(directory[0]) && directory[1] == ':') + // directory: C:\foo\bar + // file: /xy/z.c + // => c:/xy/z.c + fs.filename = Path::simplifyPath(directory.substr(0,2) + file); +#endif + else + fs.filename = Path::simplifyPath(directory + file); fs.parseCommand(command); // read settings; -D, -I, -U, -std, -m*, -f* std::map variables; fs.setIncludePaths(directory, fs.includePaths, variables); diff --git a/test/cli/test-inline-suppress.py b/test/cli/test-inline-suppress.py index b4a35094e..5c4fef176 100644 --- a/test/cli/test-inline-suppress.py +++ b/test/cli/test-inline-suppress.py @@ -7,8 +7,8 @@ import re from testutils import cppcheck def create_unused_function_compile_commands(): - compile_commands = os.path.join('proj-inline-suppress-unusedFunction', 'compile_commands.json') - prjpath = os.path.join(os.getcwd(), 'proj-inline-suppress-unusedFunction') + prjpath = os.path.realpath('proj-inline-suppress-unusedFunction') + compile_commands = os.path.join(prjpath, 'compile_commands.json') j = [{'directory': prjpath, 'command': '/usr/bin/c++ -I"' + prjpath + '" -o "' + os.path.join(prjpath, 'B.cpp.o') + '" -c "' + os.path.join(prjpath, 'B.cpp') + '"', 'file': os.path.join(prjpath, 'B.cpp')}, diff --git a/test/cli/test-proj2.py b/test/cli/test-proj2.py index 3918bf404..63495690d 100644 --- a/test/cli/test-proj2.py +++ b/test/cli/test-proj2.py @@ -19,7 +19,7 @@ def realpath(s): def create_compile_commands(): j = [{'directory': realpath('proj2/a'), 'command': 'gcc -c a.c', 'file': 'a.c'}, - {'directory': realpath('proj2/b'), 'command': 'gcc -c b.c', 'file': 'b.c'}] + {'directory': realpath('proj2'), 'command': 'gcc -c b/b.c', 'file': 'b/b.c'}] f = open('proj2/' + COMPILE_COMMANDS_JSON, 'wt') f.write(json.dumps(j)) diff --git a/test/testimportproject.cpp b/test/testimportproject.cpp index aea5ba890..0d7a04469 100644 --- a/test/testimportproject.cpp +++ b/test/testimportproject.cpp @@ -44,7 +44,7 @@ private: TEST_CASE(setIncludePaths2); TEST_CASE(setIncludePaths3); // macro names are case insensitive TEST_CASE(importCompileCommands1); - TEST_CASE(importCompileCommands2); // #8563 + TEST_CASE(importCompileCommands2); // #8563, #9567 TEST_CASE(importCompileCommands3); // check with existing trailing / in directory TEST_CASE(importCompileCommands4); // only accept certain file types TEST_CASE(importCompileCommands5); // Windows/CMake/Ninja generated comile_commands.json @@ -116,16 +116,30 @@ private: } void importCompileCommands2() const { + // Absolute file path +#ifdef _WIN32 const char json[] = R"([{ - "directory": "/tmp", - "command": "gcc -c src.c", - "file": "src.c" + "directory": "C:/foo", + "command": "gcc -c /bar.c", + "file": "/bar.c" }])"; std::istringstream istr(json); TestImporter importer; importer.importCompileCommands(istr); ASSERT_EQUALS(1, importer.fileSettings.size()); - ASSERT_EQUALS("/tmp/src.c", importer.fileSettings.begin()->filename); + ASSERT_EQUALS("C:/bar.c", importer.fileSettings.begin()->filename); +#else + const char json[] = R"([{ + "directory": "/foo", + "command": "gcc -c bar.c", + "file": "/bar.c" + }])"; + std::istringstream istr(json); + TestImporter importer; + importer.importCompileCommands(istr); + ASSERT_EQUALS(1, importer.fileSettings.size()); + ASSERT_EQUALS("/bar.c", importer.fileSettings.begin()->filename); +#endif } void importCompileCommands3() const {