1 //===- ArgumentsAdjusters.cpp - Command line arguments adjuster -----------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains definitions of classes which implement ArgumentsAdjuster
10 // interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Tooling/ArgumentsAdjusters.h"
15 #include "clang/Basic/LLVM.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include <cstddef>
19 #include <vector>
20 
21 namespace clang {
22 namespace tooling {
23 
getDriverMode(const CommandLineArguments & Args)24 static StringRef getDriverMode(const CommandLineArguments &Args) {
25   for (const auto &Arg : Args) {
26     StringRef ArgRef = Arg;
27     if (ArgRef.consume_front("--driver-mode=")) {
28       return ArgRef;
29     }
30   }
31   return StringRef();
32 }
33 
34 /// Add -fsyntax-only option and drop options that triggers output generation.
getClangSyntaxOnlyAdjuster()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.
41         "-save-temps",
42         "--save-temps",
43     };
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);
49           }))
50         continue;
51 
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`
56       // option as well.
57       // FIXME: This should be added to most argument adjusters!
58       else if (!AdjustedArgs.empty() && AdjustedArgs.back() == "-Xclang")
59         AdjustedArgs.pop_back();
60 
61       if (Arg == "-fsyntax-only")
62         HasSyntaxOnly = true;
63     }
64     if (!HasSyntaxOnly)
65       AdjustedArgs =
66           getInsertArgumentAdjuster("-fsyntax-only")(AdjustedArgs, "");
67     return AdjustedArgs;
68   };
69 }
70 
getClangStripOutputAdjuster()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]);
78 
79       if (Arg == "-o") {
80         // Output is specified as -o foo. Skip the next argument too.
81         ++i;
82       }
83       // Else, the output is specified as -ofoo. Just do nothing.
84     }
85     return AdjustedArgs;
86   };
87 }
88 
getClangStripSerializeDiagnosticAdjuster()89 ArgumentsAdjuster getClangStripSerializeDiagnosticAdjuster() {
90   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
91     CommandLineArguments AdjustedArgs;
92     for (size_t i = 0, e = Args.size(); i < e; ++i) {
93       StringRef Arg = Args[i];
94       if (Arg == "--serialize-diagnostics") {
95         // Skip the diagnostic output argument.
96         ++i;
97         continue;
98       }
99       AdjustedArgs.push_back(Args[i]);
100     }
101     return AdjustedArgs;
102   };
103 }
104 
getClangStripDependencyFileAdjuster()105 ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
106   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
107     auto UsingClDriver = (getDriverMode(Args) == "cl");
108 
109     CommandLineArguments AdjustedArgs;
110     for (size_t i = 0, e = Args.size(); i < e; ++i) {
111       StringRef Arg = Args[i];
112 
113       // These flags take an argument: -MX foo. Skip the next argument also.
114       if (!UsingClDriver && (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")) {
115         ++i;
116         continue;
117       }
118       // When not using the cl driver mode, dependency file generation options
119       // begin with -M. These include -MM, -MF, -MG, -MP, -MT, -MQ, -MD, and
120       // -MMD.
121       if (!UsingClDriver && Arg.startswith("-M"))
122         continue;
123       // Under MSVC's cl driver mode, dependency file generation is controlled
124       // using /showIncludes
125       if (Arg.startswith("/showIncludes") || Arg.startswith("-showIncludes"))
126         continue;
127 
128       AdjustedArgs.push_back(Args[i]);
129     }
130     return AdjustedArgs;
131   };
132 }
133 
getInsertArgumentAdjuster(const CommandLineArguments & Extra,ArgumentInsertPosition Pos)134 ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
135                                             ArgumentInsertPosition Pos) {
136   return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
137     CommandLineArguments Return(Args);
138 
139     CommandLineArguments::iterator I;
140     if (Pos == ArgumentInsertPosition::END) {
141       I = std::find(Return.begin(), Return.end(), "--");
142     } else {
143       I = Return.begin();
144       ++I; // To leave the program name in place
145     }
146 
147     Return.insert(I, Extra.begin(), Extra.end());
148     return Return;
149   };
150 }
151 
getInsertArgumentAdjuster(const char * Extra,ArgumentInsertPosition Pos)152 ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
153                                             ArgumentInsertPosition Pos) {
154   return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
155 }
156 
combineAdjusters(ArgumentsAdjuster First,ArgumentsAdjuster Second)157 ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
158                                    ArgumentsAdjuster Second) {
159   if (!First)
160     return Second;
161   if (!Second)
162     return First;
163   return [First, Second](const CommandLineArguments &Args, StringRef File) {
164     return Second(First(Args, File), File);
165   };
166 }
167 
getStripPluginsAdjuster()168 ArgumentsAdjuster getStripPluginsAdjuster() {
169   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
170     CommandLineArguments AdjustedArgs;
171     for (size_t I = 0, E = Args.size(); I != E; I++) {
172       // According to https://clang.llvm.org/docs/ClangPlugins.html
173       // plugin arguments are in the form:
174       // -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
175       // -Xclang <arbitrary-argument>
176       if (I + 4 < E && Args[I] == "-Xclang" &&
177           (Args[I + 1] == "-load" || Args[I + 1] == "-plugin" ||
178            llvm::StringRef(Args[I + 1]).startswith("-plugin-arg-") ||
179            Args[I + 1] == "-add-plugin") &&
180           Args[I + 2] == "-Xclang") {
181         I += 3;
182         continue;
183       }
184       AdjustedArgs.push_back(Args[I]);
185     }
186     return AdjustedArgs;
187   };
188 }
189 
190 } // end namespace tooling
191 } // end namespace clang
192