Fixed #10149 (compile database; incomplete handling of directory and file)

This commit is contained in:
Daniel Marjamäki 2021-01-31 10:14:22 +01:00
parent 5810171336
commit 6a24b4f7c8
4 changed files with 33 additions and 17 deletions

View File

@ -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<std::string, std::string, cppcheck::stricmp> variables;
fs.setIncludePaths(directory, fs.includePaths, variables);

View File

@ -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')},

View File

@ -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))

View File

@ -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 {