Browse Source

Improvements to CompileCommands::get_arguments when file_path is a header file

merge-requests/395/head
eidheim 7 years ago
parent
commit
c05d825858
  1. 33
      src/compile_commands.cc

33
src/compile_commands.cc

@ -82,41 +82,35 @@ CompileCommands::CompileCommands(const boost::filesystem::path &build_path) {
} }
} }
std::vector<std::string> CompileCommands::get_arguments(const boost::filesystem::path &build_path, const boost::filesystem::path &file_path_) { std::vector<std::string> CompileCommands::get_arguments(const boost::filesystem::path &build_path, const boost::filesystem::path &file_path) {
auto file_path = file_path_;
std::string default_std_argument = "-std=c++1y"; std::string default_std_argument = "-std=c++1y";
auto extension = file_path.extension().string(); auto extension = file_path.extension().string();
bool is_header = CompileCommands::is_header(file_path) || extension.empty(); // Include std C++ headers that are without extensions bool is_header = CompileCommands::is_header(file_path) || extension.empty(); // Include std C++ headers that are without extensions
// If header file, use a source file in the same folder if one exists // If header file, use source file flags if they are in the same folder
std::vector<boost::filesystem::path> file_paths;
if(is_header && !extension.empty()) { if(is_header && !extension.empty()) {
auto parent_path = file_path.parent_path(); auto parent_path = file_path.parent_path();
auto stem = file_path.stem();
CompileCommands compile_commands(build_path); CompileCommands compile_commands(build_path);
bool found = false;
for(auto &command : compile_commands.commands) { for(auto &command : compile_commands.commands) {
// Priority on source file with the same filename (extension excluded) as the header file if(command.file.parent_path() == parent_path)
if(command.file.stem() == stem) { file_paths.emplace_back(command.file);
file_path = command.file;
break;
}
if(!found && command.file.parent_path() == parent_path) {
file_path = command.file;
found = true;
}
} }
} }
if(file_paths.empty())
file_paths.emplace_back(file_path);
std::vector<std::string> arguments; std::vector<std::string> arguments;
if(!build_path.empty()) { if(!build_path.empty()) {
clangmm::CompilationDatabase db(build_path.string()); clangmm::CompilationDatabase db(build_path.string());
if(db) { if(db) {
clangmm::CompileCommands commands(file_path.string(), db); for(auto &file_path : file_paths) {
auto cmds = commands.get_commands(); clangmm::CompileCommands compile_commands(file_path.string(), db);
for(auto &cmd : cmds) { auto commands = compile_commands.get_commands();
auto cmd_arguments = cmd.get_arguments(); for(auto &command : commands) {
auto cmd_arguments = command.get_arguments();
bool ignore_next = false; bool ignore_next = false;
for(size_t c = 1; c < cmd_arguments.size(); c++) { for(size_t c = 1; c < cmd_arguments.size(); c++) {
if(ignore_next) { if(ignore_next) {
@ -134,6 +128,7 @@ std::vector<std::string> CompileCommands::get_arguments(const boost::filesystem:
} }
} }
} }
}
else else
arguments.emplace_back(default_std_argument); arguments.emplace_back(default_std_argument);
} }

Loading…
Cancel
Save