1 //===- ArgumentsAdjusters.cpp - Command line arguments adjuster -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains definitions of classes which implement ArgumentsAdjuster
13 //===----------------------------------------------------------------------===//
15 #include "clang/Tooling/ArgumentsAdjusters.h"
16 #include "clang/Basic/LLVM.h"
17 #include "llvm/ADT/StringRef.h"
23 /// Add -fsyntax-only option to the command line arguments.
24 ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
25 return [](const CommandLineArguments &Args, StringRef /*unused*/) {
26 CommandLineArguments AdjustedArgs;
27 for (size_t i = 0, e = Args.size(); i < e; ++i) {
28 StringRef Arg = Args[i];
29 // FIXME: Remove options that generate output.
30 if (!Arg.startswith("-fcolor-diagnostics") &&
31 !Arg.startswith("-fdiagnostics-color"))
32 AdjustedArgs.push_back(Args[i]);
34 AdjustedArgs.push_back("-fsyntax-only");
39 ArgumentsAdjuster getClangStripOutputAdjuster() {
40 return [](const CommandLineArguments &Args, StringRef /*unused*/) {
41 CommandLineArguments AdjustedArgs;
42 for (size_t i = 0, e = Args.size(); i < e; ++i) {
43 StringRef Arg = Args[i];
44 if (!Arg.startswith("-o"))
45 AdjustedArgs.push_back(Args[i]);
48 // Output is specified as -o foo. Skip the next argument too.
51 // Else, the output is specified as -ofoo. Just do nothing.
57 ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
58 return [](const CommandLineArguments &Args, StringRef /*unused*/) {
59 CommandLineArguments AdjustedArgs;
60 for (size_t i = 0, e = Args.size(); i < e; ++i) {
61 StringRef Arg = Args[i];
62 // All dependency-file options begin with -M. These include -MM,
63 // -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD.
64 if (!Arg.startswith("-M")) {
65 AdjustedArgs.push_back(Args[i]);
69 if (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")
70 // These flags take an argument: -MX foo. Skip the next argument also.
77 ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
78 ArgumentInsertPosition Pos) {
79 return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
80 CommandLineArguments Return(Args);
82 CommandLineArguments::iterator I;
83 if (Pos == ArgumentInsertPosition::END) {
87 ++I; // To leave the program name in place
90 Return.insert(I, Extra.begin(), Extra.end());
95 ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
96 ArgumentInsertPosition Pos) {
97 return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
100 ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
101 ArgumentsAdjuster Second) {
106 return [First, Second](const CommandLineArguments &Args, StringRef File) {
107 return Second(First(Args, File), File);
111 ArgumentsAdjuster getStripPluginsAdjuster() {
112 return [](const CommandLineArguments &Args, StringRef /*unused*/) {
113 CommandLineArguments AdjustedArgs;
114 for (size_t I = 0, E = Args.size(); I != E; I++) {
115 // According to https://clang.llvm.org/docs/ClangPlugins.html
116 // plugin arguments are in the form:
117 // -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
118 // -Xclang <arbitrary-argument>
119 if (I + 4 < E && Args[I] == "-Xclang" &&
120 (Args[I + 1] == "-load" || Args[I + 1] == "-plugin" ||
121 llvm::StringRef(Args[I + 1]).startswith("-plugin-arg-") ||
122 Args[I + 1] == "-add-plugin") &&
123 Args[I + 2] == "-Xclang") {
127 AdjustedArgs.push_back(Args[I]);
133 } // end namespace tooling
134 } // end namespace clang