1 //===- ArgumentsAdjusters.cpp - Command line arguments adjuster -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains definitions of classes which implement ArgumentsAdjuster
12 //===----------------------------------------------------------------------===//
14 #include "clang/Tooling/ArgumentsAdjusters.h"
15 #include "clang/Basic/LLVM.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
24 static StringRef
getDriverMode(const CommandLineArguments
&Args
) {
25 for (const auto &Arg
: Args
) {
26 StringRef ArgRef
= Arg
;
27 if (ArgRef
.consume_front("--driver-mode=")) {
34 /// Add -fsyntax-only option and drop options that triggers output generation.
35 ArgumentsAdjuster
getClangSyntaxOnlyAdjuster() {
36 return [](const CommandLineArguments
&Args
, StringRef
/*unused*/) {
37 CommandLineArguments AdjustedArgs
;
38 bool HasSyntaxOnly
= false;
39 constexpr llvm::StringRef OutputCommands
[] = {
40 // FIXME: Add other options that generate output.
44 for (size_t i
= 0, e
= Args
.size(); i
< e
; ++i
) {
45 StringRef Arg
= Args
[i
];
46 // Skip output commands.
47 if (llvm::any_of(OutputCommands
, [&Arg
](llvm::StringRef OutputCommand
) {
48 return Arg
.startswith(OutputCommand
);
52 if (!Arg
.startswith("-fcolor-diagnostics") &&
53 !Arg
.startswith("-fdiagnostics-color"))
54 AdjustedArgs
.push_back(Args
[i
]);
55 // If we strip a color option, make sure we strip any preceeding `-Xclang`
57 // FIXME: This should be added to most argument adjusters!
58 else if (!AdjustedArgs
.empty() && AdjustedArgs
.back() == "-Xclang")
59 AdjustedArgs
.pop_back();
61 if (Arg
== "-fsyntax-only")
66 getInsertArgumentAdjuster("-fsyntax-only")(AdjustedArgs
, "");
71 ArgumentsAdjuster
getClangStripOutputAdjuster() {
72 return [](const CommandLineArguments
&Args
, StringRef
/*unused*/) {
73 CommandLineArguments AdjustedArgs
;
74 for (size_t i
= 0, e
= Args
.size(); i
< e
; ++i
) {
75 StringRef Arg
= Args
[i
];
76 if (!Arg
.startswith("-o"))
77 AdjustedArgs
.push_back(Args
[i
]);
80 // Output is specified as -o foo. Skip the next argument too.
83 // Else, the output is specified as -ofoo. Just do nothing.
89 ArgumentsAdjuster
getClangStripDependencyFileAdjuster() {
90 return [](const CommandLineArguments
&Args
, StringRef
/*unused*/) {
91 auto UsingClDriver
= (getDriverMode(Args
) == "cl");
93 CommandLineArguments AdjustedArgs
;
94 for (size_t i
= 0, e
= Args
.size(); i
< e
; ++i
) {
95 StringRef Arg
= Args
[i
];
97 // These flags take an argument: -MX foo. Skip the next argument also.
98 if (!UsingClDriver
&& (Arg
== "-MF" || Arg
== "-MT" || Arg
== "-MQ")) {
102 // When not using the cl driver mode, dependency file generation options
103 // begin with -M. These include -MM, -MF, -MG, -MP, -MT, -MQ, -MD, and
105 if (!UsingClDriver
&& Arg
.startswith("-M"))
107 // Under MSVC's cl driver mode, dependency file generation is controlled
108 // using /showIncludes
109 if (Arg
.startswith("/showIncludes") || Arg
.startswith("-showIncludes"))
112 AdjustedArgs
.push_back(Args
[i
]);
118 ArgumentsAdjuster
getInsertArgumentAdjuster(const CommandLineArguments
&Extra
,
119 ArgumentInsertPosition Pos
) {
120 return [Extra
, Pos
](const CommandLineArguments
&Args
, StringRef
/*unused*/) {
121 CommandLineArguments
Return(Args
);
123 CommandLineArguments::iterator I
;
124 if (Pos
== ArgumentInsertPosition::END
) {
125 I
= llvm::find(Return
, "--");
128 ++I
; // To leave the program name in place
131 Return
.insert(I
, Extra
.begin(), Extra
.end());
136 ArgumentsAdjuster
getInsertArgumentAdjuster(const char *Extra
,
137 ArgumentInsertPosition Pos
) {
138 return getInsertArgumentAdjuster(CommandLineArguments(1, Extra
), Pos
);
141 ArgumentsAdjuster
combineAdjusters(ArgumentsAdjuster First
,
142 ArgumentsAdjuster Second
) {
147 return [First
, Second
](const CommandLineArguments
&Args
, StringRef File
) {
148 return Second(First(Args
, File
), File
);
152 ArgumentsAdjuster
getStripPluginsAdjuster() {
153 return [](const CommandLineArguments
&Args
, StringRef
/*unused*/) {
154 CommandLineArguments AdjustedArgs
;
155 for (size_t I
= 0, E
= Args
.size(); I
!= E
; I
++) {
156 // According to https://clang.llvm.org/docs/ClangPlugins.html
157 // plugin arguments are in the form:
158 // -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
159 // -Xclang <arbitrary-argument>
160 if (I
+ 4 < E
&& Args
[I
] == "-Xclang" &&
161 (Args
[I
+ 1] == "-load" || Args
[I
+ 1] == "-plugin" ||
162 llvm::StringRef(Args
[I
+ 1]).startswith("-plugin-arg-") ||
163 Args
[I
+ 1] == "-add-plugin") &&
164 Args
[I
+ 2] == "-Xclang") {
168 AdjustedArgs
.push_back(Args
[I
]);
174 } // end namespace tooling
175 } // end namespace clang