Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Change relative paths to absolute ones when using compile_commands.js…
Browse files Browse the repository at this point in the history
…on file

Signed-off-by: caozhong <[email protected]>
  • Loading branch information
CaoZhongZ committed May 27, 2021
1 parent 8cf0799 commit cee775a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions include/conf/load.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ namespace color_coded

return line;
}

template <typename It>
It make_absolute(It const begin, It const end, fs::path const &base) {
static const std::regex reg_dual
{
"\\s*(-I|-isystem|-iquote)\\s*$",
std::regex::optimize
};

std::smatch match;
if (std::regex_search(*begin, match, reg_dual))
{
for (auto next = begin + 1; next < end; ++next)
{
if (next->size() > 0 && next->c_str()[0] != '/')
{
*next = fs::absolute(next->c_str(), base).string();
return next +1;
}
}

return end + 1;
}
else
{
*begin = make_absolute(*begin, base);
return begin +1;
}
}
}

inline args_t load_compilation_database(std::string const &file, fs::path filename)
Expand Down Expand Up @@ -106,6 +135,7 @@ namespace color_coded
if(compile_commands.empty())
{ return {}; }

const auto base(fs::path{compile_commands[0].Directory});
// Skip argv[0] which is the name of the clang executable.
args_t commands((compile_commands[0].CommandLine.begin() + 1), compile_commands[0].CommandLine.end());

Expand All @@ -114,6 +144,9 @@ namespace color_coded
commands.erase(std::remove(commands.begin(), commands.end(), filename), commands.end());
commands.erase(std::remove(commands.begin(), commands.end(), compile_commands[0].Filename), commands.end());

for (auto it = commands.begin(); it < commands.end();)
{ it = detail::make_absolute(it, commands.end(), base); }

return commands;
}

Expand Down

0 comments on commit cee775a

Please sign in to comment.