aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2021-12-08 14:47:46 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2021-12-08 14:47:46 +0000
commit526323b0494be90d2dbaba296284f0646a800fb5 (patch)
tree7d4a9ce39cab29f3b40fc2f6507fa0b93828636d
parentb09c721b95e08f3e0446a3d201dae8ee930be4b0 (diff)
parent53fcd849fcf5acd1d3896981903ad0b66d82e279 (diff)
downloadinterceptor-526323b0494be90d2dbaba296284f0646a800fb5.tar.gz
Merge "interceptor_analysis supports --relative option."
-rw-r--r--analysis.cc35
1 files changed, 25 insertions, 10 deletions
diff --git a/analysis.cc b/analysis.cc
index 6420f7b..4e1d790 100644
--- a/analysis.cc
+++ b/analysis.cc
@@ -41,9 +41,9 @@ struct Options {
fs::path command_log;
OutputFormat output_format = OutputFormat::TEXT;
fs::path output;
-
// options for compdb
bool compdb_use_commands = false;
+ bool compdb_relative = false;
};
static Options parse_arguments(int argc, char* argv[]) {
@@ -53,18 +53,23 @@ static Options parse_arguments(int argc, char* argv[]) {
{"command-log", required_argument, nullptr, 'l'},
{"output-type", required_argument, nullptr, 't'},
{"output", required_argument, nullptr, 'o'},
+ {"relative", no_argument, nullptr, 'r'},
{nullptr, 0, nullptr, 0},
};
const auto usage = [&]() {
std::cerr << "usage: " << argv[0] << '\n'
<< " -l|--command-log filename\n"
<< " -o|--output filename\n"
- << " [-t|--output-type (text|compdb|compdb_commands)]\n";
+ << " [-t|--output-type (text|compdb|compdb_commands)]\n"
+ << " [-r|--relative]\n"
+ << "\n"
+ << "-r is used with -t compdb. If -r is specified, directories\n"
+ << "are relative to the root_directory.";
exit(EX_USAGE);
};
while (true) {
int ix;
- int c = getopt_long(argc, argv, "-l:t:o:", opts, &ix);
+ int c = getopt_long(argc, argv, "-l:t:o:r", opts, &ix);
if (c == -1) {
break;
}
@@ -87,6 +92,9 @@ static Options parse_arguments(int argc, char* argv[]) {
case 'o':
result.output = fs::absolute(optarg);
break;
+ case 'r':
+ result.compdb_relative = true;
+ break;
default:
usage();
}
@@ -129,7 +137,8 @@ void text_to_file(const interceptor::Log& log, const fs::path& output) {
}
}
-void compdb_to_file(const interceptor::Log& log, const fs::path& output, bool use_commands) {
+void compdb_to_file(const interceptor::Log& log, const fs::path& output,
+ const Options& options) {
static const std::unordered_set<std::string_view> kCompileExtensions = {
".c", ".cc", ".cpp", ".cxx", ".S",
};
@@ -181,12 +190,18 @@ void compdb_to_file(const interceptor::Log& log, const fs::path& output, bool us
// ok, now we have a new command
auto& compile_command = *compdb.add_commands();
- compile_command.set_directory(fs::path(log.root_directory()) / command.current_directory());
+ fs::path directory;
+ if (options.compdb_relative) {
+ directory = command.current_directory();
+ } else {
+ directory = fs::path(log.root_directory()) / command.current_directory();
+ }
+ compile_command.set_directory(directory);
compile_command.set_file(input);
if (!single_output.empty()) {
compile_command.set_output(single_output);
}
- if (use_commands) {
+ if (options.compdb_use_commands) {
compile_command.set_command(interceptor::command_line(command));
} else {
*compile_command.mutable_arguments() = {command.arguments().cbegin(),
@@ -203,9 +218,9 @@ void compdb_to_file(const interceptor::Log& log, const fs::path& output, bool us
}
std::string out_str;
- auto options = google::protobuf::util::JsonPrintOptions{};
- options.add_whitespace = true;
- google::protobuf::util::MessageToJsonString(compdb, &out_str, options);
+ auto json_options = google::protobuf::util::JsonPrintOptions{};
+ json_options.add_whitespace = true;
+ google::protobuf::util::MessageToJsonString(compdb, &out_str, json_options);
// this would emit {"command":[yadayada]}, but we want only [yadayada]
// the additional characters come from options.add_whitespace
@@ -229,7 +244,7 @@ int main(int argc, char* argv[]) {
text_to_file(log, options.output);
break;
case OutputFormat::COMPDB:
- compdb_to_file(log, options.output, options.compdb_use_commands);
+ compdb_to_file(log, options.output, options);
break;
}
}