Fixed #8925 (compile_commands.json: False positives in .mm (Objective-C++) files, that file type should maybe just be ignored)

This commit is contained in:
Daniel Marjamäki 2019-01-21 06:36:23 +01:00
parent a26287f491
commit 91435310cb
2 changed files with 21 additions and 6 deletions

View File

@ -253,24 +253,28 @@ void ImportProject::FileSettings::parseCommand(const std::string &command)
void ImportProject::importCompileCommands(std::istream &istr)
{
picojson::value v;
istr >> v;
if (!v.is<picojson::array>())
picojson::value compileCommands;
istr >> compileCommands;
if (!compileCommands.is<picojson::array>())
return;
for (const picojson::value &fileInfo : v.get<picojson::array>()) {
for (const picojson::value &fileInfo : compileCommands.get<picojson::array>()) {
picojson::object obj = fileInfo.get<picojson::object>();
std::string dirpath = obj["directory"].get<std::string>();
std::string dirpath = Path::fromNativeSeparators(obj["directory"].get<std::string>());
/* CMAKE produces the directory without trailing / so add it if not
* there - it is needed by setIncludePaths() */
if (!endsWith(dirpath, '/'))
dirpath += '/';
const std::string directory = Path::fromNativeSeparators(dirpath);
const std::string directory = dirpath;
const std::string command = obj["command"].get<std::string>();
const std::string file = Path::fromNativeSeparators(obj["file"].get<std::string>());
// Accept file?
if (!Path::acceptFile(file))
continue;
struct FileSettings fs;
if (Path::isAbsolute(file) || Path::fileExists(file))
fs.filename = file;

View File

@ -46,6 +46,7 @@ private:
TEST_CASE(importCompileCommands1);
TEST_CASE(importCompileCommands2); // #8563
TEST_CASE(importCompileCommands3); // check with existing trailing / in directory
TEST_CASE(importCompileCommands4); // only accept certain file types
}
void setDefines() const {
@ -125,6 +126,16 @@ private:
ASSERT_EQUALS(1, importer.fileSettings.size());
ASSERT_EQUALS("/tmp/src.c", importer.fileSettings.begin()->filename);
}
void importCompileCommands4() const {
const char json[] = "[ { \"directory\": \"/tmp/\","
"\"command\": \"gcc -c src.mm\","
"\"file\": \"src.mm\" } ]";
std::istringstream istr(json);
TestImporter importer;
importer.importCompileCommands(istr);
ASSERT_EQUALS(0, importer.fileSettings.size());
}
};
REGISTER_TEST(TestImportProject)