#include "clang/Tooling/ArgumentsAdjusters.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include <cstddef>
#include <vector>
namespace clang {
namespace tooling {
static StringRef getDriverMode(const CommandLineArguments &Args) {
for (const auto &Arg : Args) {
StringRef ArgRef = Arg;
if (ArgRef.consume_front("--driver-mode=")) {
return ArgRef;
}
}
return StringRef();
}
ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
return [](const CommandLineArguments &Args, StringRef ) {
CommandLineArguments AdjustedArgs;
bool HasSyntaxOnly = false;
constexpr llvm::StringRef OutputCommands[] = {
"-save-temps",
"--save-temps",
};
for (size_t i = 0, e = Args.size(); i < e; ++i) {
StringRef Arg = Args[i];
if (llvm::any_of(OutputCommands, [&Arg](llvm::StringRef OutputCommand) {
return Arg.startswith(OutputCommand);
}))
continue;
if (!Arg.startswith("-fcolor-diagnostics") &&
!Arg.startswith("-fdiagnostics-color"))
AdjustedArgs.push_back(Args[i]);
else if (!AdjustedArgs.empty() && AdjustedArgs.back() == "-Xclang")
AdjustedArgs.pop_back();
if (Arg == "-fsyntax-only")
HasSyntaxOnly = true;
}
if (!HasSyntaxOnly)
AdjustedArgs =
getInsertArgumentAdjuster("-fsyntax-only")(AdjustedArgs, "");
return AdjustedArgs;
};
}
ArgumentsAdjuster getClangStripOutputAdjuster() {
return [](const CommandLineArguments &Args, StringRef ) {
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]);
if (Arg == "-o") {
++i;
}
}
return AdjustedArgs;
};
}
ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
return [](const CommandLineArguments &Args, StringRef ) {
auto UsingClDriver = (getDriverMode(Args) == "cl");
CommandLineArguments AdjustedArgs;
for (size_t i = 0, e = Args.size(); i < e; ++i) {
StringRef Arg = Args[i];
if (!UsingClDriver && (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")) {
++i;
continue;
}
if (!UsingClDriver && Arg.startswith("-M"))
continue;
if (Arg.startswith("/showIncludes") || Arg.startswith("-showIncludes"))
continue;
AdjustedArgs.push_back(Args[i]);
}
return AdjustedArgs;
};
}
ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
ArgumentInsertPosition Pos) {
return [Extra, Pos](const CommandLineArguments &Args, StringRef ) {
CommandLineArguments Return(Args);
CommandLineArguments::iterator I;
if (Pos == ArgumentInsertPosition::END) {
I = std::find(Return.begin(), Return.end(), "--");
} else {
I = Return.begin();
++I; }
Return.insert(I, Extra.begin(), Extra.end());
return Return;
};
}
ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
ArgumentInsertPosition Pos) {
return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
}
ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
ArgumentsAdjuster Second) {
if (!First)
return Second;
if (!Second)
return First;
return [First, Second](const CommandLineArguments &Args, StringRef File) {
return Second(First(Args, File), File);
};
}
ArgumentsAdjuster getStripPluginsAdjuster() {
return [](const CommandLineArguments &Args, StringRef ) {
CommandLineArguments AdjustedArgs;
for (size_t I = 0, E = Args.size(); I != E; I++) {
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;
};
}
} }