2018-03-15 05:05:51 +08:00
|
|
|
//===- ArgumentsAdjusters.cpp - Command line arguments adjuster -----------===//
|
2012-05-10 00:18:30 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-05-10 00:18:30 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains definitions of classes which implement ArgumentsAdjuster
|
|
|
|
// interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Tooling/ArgumentsAdjusters.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2019-11-13 20:44:40 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include <cstddef>
|
2019-11-13 20:44:40 +08:00
|
|
|
#include <vector>
|
2012-05-10 00:18:30 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
|
|
|
|
2019-11-13 20:44:40 +08:00
|
|
|
/// Add -fsyntax-only option and drop options that triggers output generation.
|
2014-12-04 01:53:02 +08:00
|
|
|
ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
|
2015-11-05 10:19:53 +08:00
|
|
|
return [](const CommandLineArguments &Args, StringRef /*unused*/) {
|
2014-12-04 01:53:02 +08:00
|
|
|
CommandLineArguments AdjustedArgs;
|
2019-07-02 18:45:53 +08:00
|
|
|
bool HasSyntaxOnly = false;
|
2020-02-17 22:37:12 +08:00
|
|
|
constexpr llvm::StringRef OutputCommands[] = {
|
2019-11-13 20:44:40 +08:00
|
|
|
// FIXME: Add other options that generate output.
|
|
|
|
"-save-temps",
|
|
|
|
"--save-temps",
|
|
|
|
};
|
2018-03-15 05:05:51 +08:00
|
|
|
for (size_t i = 0, e = Args.size(); i < e; ++i) {
|
2014-12-04 01:53:02 +08:00
|
|
|
StringRef Arg = Args[i];
|
2019-11-13 20:44:40 +08:00
|
|
|
// Skip output commands.
|
|
|
|
if (llvm::any_of(OutputCommands, [&Arg](llvm::StringRef OutputCommand) {
|
|
|
|
return Arg.startswith(OutputCommand);
|
|
|
|
}))
|
|
|
|
continue;
|
|
|
|
|
2014-12-04 01:53:02 +08:00
|
|
|
if (!Arg.startswith("-fcolor-diagnostics") &&
|
|
|
|
!Arg.startswith("-fdiagnostics-color"))
|
|
|
|
AdjustedArgs.push_back(Args[i]);
|
2019-07-02 18:45:53 +08:00
|
|
|
if (Arg == "-fsyntax-only")
|
|
|
|
HasSyntaxOnly = true;
|
2014-12-04 01:53:02 +08:00
|
|
|
}
|
2019-07-02 18:45:53 +08:00
|
|
|
if (!HasSyntaxOnly)
|
|
|
|
AdjustedArgs.push_back("-fsyntax-only");
|
2014-12-04 01:53:02 +08:00
|
|
|
return AdjustedArgs;
|
|
|
|
};
|
2012-05-10 00:18:30 +08:00
|
|
|
}
|
|
|
|
|
2014-12-04 01:53:02 +08:00
|
|
|
ArgumentsAdjuster getClangStripOutputAdjuster() {
|
2015-11-05 10:19:53 +08:00
|
|
|
return [](const CommandLineArguments &Args, StringRef /*unused*/) {
|
2014-12-04 01:53:02 +08:00
|
|
|
CommandLineArguments AdjustedArgs;
|
|
|
|
for (size_t i = 0, e = Args.size(); i < e; ++i) {
|
|
|
|
StringRef Arg = Args[i];
|
|
|
|
if (!Arg.startswith("-o"))
|
|
|
|
AdjustedArgs.push_back(Args[i]);
|
2013-06-06 19:52:19 +08:00
|
|
|
|
2014-12-04 01:53:02 +08:00
|
|
|
if (Arg == "-o") {
|
2017-07-07 05:02:52 +08:00
|
|
|
// Output is specified as -o foo. Skip the next argument too.
|
2014-12-04 01:53:02 +08:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
// Else, the output is specified as -ofoo. Just do nothing.
|
2013-06-06 19:52:19 +08:00
|
|
|
}
|
2014-12-04 01:53:02 +08:00
|
|
|
return AdjustedArgs;
|
|
|
|
};
|
2013-06-06 19:52:19 +08:00
|
|
|
}
|
|
|
|
|
2019-09-21 08:17:26 +08:00
|
|
|
ArgumentsAdjuster getClangStripSerializeDiagnosticAdjuster() {
|
|
|
|
return [](const CommandLineArguments &Args, StringRef /*unused*/) {
|
|
|
|
CommandLineArguments AdjustedArgs;
|
|
|
|
for (size_t i = 0, e = Args.size(); i < e; ++i) {
|
|
|
|
StringRef Arg = Args[i];
|
|
|
|
if (Arg == "--serialize-diagnostics") {
|
|
|
|
// Skip the diagnostic output argument.
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
AdjustedArgs.push_back(Args[i]);
|
|
|
|
}
|
|
|
|
return AdjustedArgs;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-07 05:02:52 +08:00
|
|
|
ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
|
|
|
|
return [](const CommandLineArguments &Args, StringRef /*unused*/) {
|
|
|
|
CommandLineArguments AdjustedArgs;
|
|
|
|
for (size_t i = 0, e = Args.size(); i < e; ++i) {
|
|
|
|
StringRef Arg = Args[i];
|
|
|
|
// All dependency-file options begin with -M. These include -MM,
|
|
|
|
// -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD.
|
2017-11-18 00:27:21 +08:00
|
|
|
if (!Arg.startswith("-M")) {
|
2017-07-07 05:02:52 +08:00
|
|
|
AdjustedArgs.push_back(Args[i]);
|
2017-11-18 00:27:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-07-07 05:02:52 +08:00
|
|
|
|
2017-11-18 00:27:21 +08:00
|
|
|
if (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")
|
|
|
|
// These flags take an argument: -MX foo. Skip the next argument also.
|
2017-07-07 05:02:52 +08:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return AdjustedArgs;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-12-04 01:53:02 +08:00
|
|
|
ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
|
|
|
|
ArgumentInsertPosition Pos) {
|
2015-11-05 10:19:53 +08:00
|
|
|
return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
|
2014-12-04 01:53:02 +08:00
|
|
|
CommandLineArguments Return(Args);
|
|
|
|
|
|
|
|
CommandLineArguments::iterator I;
|
|
|
|
if (Pos == ArgumentInsertPosition::END) {
|
|
|
|
I = Return.end();
|
|
|
|
} else {
|
|
|
|
I = Return.begin();
|
|
|
|
++I; // To leave the program name in place
|
|
|
|
}
|
|
|
|
|
|
|
|
Return.insert(I, Extra.begin(), Extra.end());
|
|
|
|
return Return;
|
|
|
|
};
|
|
|
|
}
|
2014-11-04 16:51:24 +08:00
|
|
|
|
2014-12-04 01:53:02 +08:00
|
|
|
ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
|
|
|
|
ArgumentInsertPosition Pos) {
|
|
|
|
return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
|
|
|
|
}
|
2014-11-04 16:51:24 +08:00
|
|
|
|
2014-12-04 01:53:02 +08:00
|
|
|
ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
|
|
|
|
ArgumentsAdjuster Second) {
|
2017-10-26 18:38:14 +08:00
|
|
|
if (!First)
|
|
|
|
return Second;
|
|
|
|
if (!Second)
|
|
|
|
return First;
|
2015-11-05 10:19:53 +08:00
|
|
|
return [First, Second](const CommandLineArguments &Args, StringRef File) {
|
|
|
|
return Second(First(Args, File), File);
|
2015-01-16 23:57:15 +08:00
|
|
|
};
|
2014-11-04 16:51:24 +08:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:00:31 +08:00
|
|
|
ArgumentsAdjuster getStripPluginsAdjuster() {
|
|
|
|
return [](const CommandLineArguments &Args, StringRef /*unused*/) {
|
|
|
|
CommandLineArguments AdjustedArgs;
|
|
|
|
for (size_t I = 0, E = Args.size(); I != E; I++) {
|
|
|
|
// According to https://clang.llvm.org/docs/ClangPlugins.html
|
|
|
|
// plugin arguments are in the form:
|
|
|
|
// -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
|
|
|
|
// -Xclang <arbitrary-argument>
|
|
|
|
if (I + 4 < E && Args[I] == "-Xclang" &&
|
|
|
|
(Args[I + 1] == "-load" || Args[I + 1] == "-plugin" ||
|
|
|
|
llvm::StringRef(Args[I + 1]).startswith("-plugin-arg-") ||
|
|
|
|
Args[I + 1] == "-add-plugin") &&
|
|
|
|
Args[I + 2] == "-Xclang") {
|
|
|
|
I += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
AdjustedArgs.push_back(Args[I]);
|
|
|
|
}
|
|
|
|
return AdjustedArgs;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-05-10 00:18:30 +08:00
|
|
|
} // end namespace tooling
|
|
|
|
} // end namespace clang
|