diff --git a/src/project.cc b/src/project.cc index 7ea4e37a..6df0d84a 100644 --- a/src/project.cc +++ b/src/project.cc @@ -171,8 +171,12 @@ std::vector LoadFromCompileCommandsJson(const std::vector result; result.reserve(entries.size()); - for (const auto& entry : entries) + for (auto& entry : entries) { + if (entry.args.empty() && !entry.command.empty()) + entry.args = SplitString(entry.command, " "); + result.push_back(GetCompilationEntryFromCompileCommandEntry(extra_flags, entry)); + } return result; } @@ -211,7 +215,7 @@ std::vector LoadFromDirectoryListing(const std::vector LoadCompilationEntriesFromDirectory(const std::vector& extra_flags, const std::string& project_directory) { // TODO: Figure out if this function or the clang one is faster. - //return LoadFromCompileCommandsJson(project_directory); + //return LoadFromCompileCommandsJson(extra_flags, project_directory); std::cerr << "Trying to load compile_commands.json" << std::endl; CXCompilationDatabase_Error cx_db_load_error; diff --git a/src/utils.cc b/src/utils.cc index e5dd420c..79a4b7c2 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -66,6 +66,23 @@ std::string ReplaceAll(const std::string& source, const std::string& from, const return result; } +std::vector SplitString(const std::string& str, const std::string& delimiter) { + // http://stackoverflow.com/a/13172514 + std::vector strings; + + std::string::size_type pos = 0; + std::string::size_type prev = 0; + while ((pos = str.find(delimiter, prev)) != std::string::npos) { + strings.push_back(str.substr(prev, pos - prev)); + prev = pos + 1; + } + + // To get the last substring (or only, if delimiter is not found) + strings.push_back(str.substr(prev)); + + return strings; +} + static std::vector GetFilesInFolderHelper(std::string folder, bool recursive, std::string output_prefix) { std::vector result; diff --git a/src/utils.h b/src/utils.h index f9501457..d3389d2a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -18,6 +18,8 @@ bool StartsWith(const std::string& value, const std::string& start); bool EndsWith(const std::string& value, const std::string& ending); std::string ReplaceAll(const std::string& source, const std::string& from, const std::string& to); +std::vector SplitString(const std::string& str, const std::string& delimiter); + inline bool StartsWithAny(const std::vector& values, const std::string& start) { return std::any_of(std::begin(values), std::end(values), [&start](const std::string& value) { return StartsWith(value, start);