Skip to content

Commit

Permalink
Converting absolute paths to relative on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewtff committed Aug 17, 2017
1 parent ef147df commit 9570a24
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dist-clang.files
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,8 @@ src/client/clean_command.hh
src/client/command.cc
src/client/command.hh
src/client/command_test.cc
src/client/configuration.cc
src/client/configuration.hh
src/client/configuration.proto
src/client/driver_command.cc
src/client/driver_command.hh
Expand Down
38 changes: 37 additions & 1 deletion src/client/clang_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <base/c_utils.h>
#include <base/logging.h>
#include <base/process_impl.h>
#include <base/string_utils.h>

#include <clang/Driver/Options.h>

Expand All @@ -13,6 +14,37 @@
namespace dist_clang {
namespace client {

namespace {

String GetRelativePath(const String& current_dir, const String& path) {
List<String> current_dir_parts, path_parts;
base::SplitString<'/'>(current_dir, current_dir_parts);
base::SplitString<'/'>(path, path_parts);

auto current_dir_part = current_dir_parts.begin();
auto path_part = path_parts.begin();
while (current_dir_part != current_dir_parts.end() &&
path_part != path_parts.end() &&
*current_dir_part == *path_part) {
++current_dir_part;
++path_part;
}

String result(".");
while (current_dir_part != current_dir_parts.end()) {
result += "/..";
++current_dir_part;
}

while (path_part != path_parts.end()) {
result += "/" + *path_part;
++path_part;
}
return result;
}

} // anonymous

ClangCommand::ClangCommand(llvm::ArrayRef<const char*> args,
SharedPtr<llvm::opt::OptTable> opts)
: arg_list_([&] {
Expand Down Expand Up @@ -52,6 +84,8 @@ bool ClangCommand::FillFlags(base::proto::Flags* flags,
return true;
}

const String current_dir = base::GetCurrentDir();

flags->Clear();

llvm::opt::ArgStringList non_direct_list, non_cached_list, other_list;
Expand Down Expand Up @@ -143,8 +177,10 @@ bool ClangCommand::FillFlags(base::proto::Flags* flags,
replaced_command.replace(
pos, self_path.size(),
clang_path.substr(0, clang_path.find_last_of('/')));
const String relative_command =
GetRelativePath(current_dir, replaced_command);
non_direct_list.push_back(arg->getSpelling().data());
non_direct_list.push_back(tmp_list.MakeArgString(replaced_command));
non_direct_list.push_back(tmp_list.MakeArgString(relative_command));
LOG(VERBOSE) << "Replaced command: " << non_direct_list.back();
} else {
non_cached_list.push_back(arg->getSpelling().data());
Expand Down

0 comments on commit 9570a24

Please sign in to comment.