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

Commit c81df7e

Browse files
committed
Change relative paths to absolute ones when using compile_commands.json file
Signed-off-by: caozhong <zhong.z.cao@intel.com>
1 parent 8cf0799 commit c81df7e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

include/conf/load.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,40 @@ namespace color_coded
4141

4242
return line;
4343
}
44+
45+
template <typename It>
46+
It make_absolute(It const begin, It const end, fs::path const &base)
47+
{
48+
static const std::regex reg_dual
49+
{
50+
"\\s*(-I|-isystem|-iquote)\\s*$",
51+
std::regex::optimize
52+
};
53+
54+
std::smatch match;
55+
if (std::regex_search(*begin, match, reg_dual))
56+
{
57+
for (auto next = begin + 1; next < end; ++next)
58+
{
59+
if (next->size() == 0)
60+
{ continue; }
61+
else
62+
{
63+
auto path = next->c_str();
64+
if (path[0] != '/')
65+
*next = fs::absolute(path, base).string();
66+
return next +1;
67+
}
68+
}
69+
70+
return end + 1;
71+
}
72+
else
73+
{
74+
*begin = make_absolute(*begin, base);
75+
return begin +1;
76+
}
77+
}
4478
}
4579

4680
inline args_t load_compilation_database(std::string const &file, fs::path filename)
@@ -106,6 +140,7 @@ namespace color_coded
106140
if(compile_commands.empty())
107141
{ return {}; }
108142

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

@@ -114,6 +149,9 @@ namespace color_coded
114149
commands.erase(std::remove(commands.begin(), commands.end(), filename), commands.end());
115150
commands.erase(std::remove(commands.begin(), commands.end(), compile_commands[0].Filename), commands.end());
116151

152+
for (auto it = commands.begin(); it < commands.end();)
153+
{ it = detail::make_absolute(it, commands.end(), base); }
154+
117155
return commands;
118156
}
119157

0 commit comments

Comments
 (0)