• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.push_back("-fsyntax-only");
66     return AdjustedArgs;
67   };
68 }
69 
getClangStripOutputAdjuster()70 ArgumentsAdjuster getClangStripOutputAdjuster() {
71   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
72     CommandLineArguments AdjustedArgs;
73     for (size_t i = 0, e = Args.size(); i < e; ++i) {
74       StringRef Arg = Args[i];
75       if (!Arg.startswith("-o"))
76         AdjustedArgs.push_back(Args[i]);
77 
78       if (Arg == "-o") {
79         // Output is specified as -o foo. Skip the next argument too.
80         ++i;
81       }
82       // Else, the output is specified as -ofoo. Just do nothing.
83     }
84     return AdjustedArgs;
85   };
86 }
87 
getClangStripSerializeDiagnosticAdjuster()88 ArgumentsAdjuster getClangStripSerializeDiagnosticAdjuster() {
89   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
90     CommandLineArguments AdjustedArgs;
91     for (size_t i = 0, e = Args.size(); i < e; ++i) {
92       StringRef Arg = Args[i];
93       if (Arg == "--serialize-diagnostics") {
94         // Skip the diagnostic output argument.
95         ++i;
96         continue;
97       }
98       AdjustedArgs.push_back(Args[i]);
99     }
100     return AdjustedArgs;
101   };
102 }
103 
getClangStripDependencyFileAdjuster()104 ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
105   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
106     auto UsingClDriver = (getDriverMode(Args) == "cl");
107 
108     CommandLineArguments AdjustedArgs;
109     for (size_t i = 0, e = Args.size(); i < e; ++i) {
110       StringRef Arg = Args[i];
111 
112       // These flags take an argument: -MX foo. Skip the next argument also.
113       if (!UsingClDriver && (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")) {
114         ++i;
115         continue;
116       }
117       // When not using the cl driver mode, dependency file generation options
118       // begin with -M. These include -MM, -MF, -MG, -MP, -MT, -MQ, -MD, and
119       // -MMD.
120       if (!UsingClDriver && Arg.startswith("-M"))
121         continue;
122       // Under MSVC's cl driver mode, dependency file generation is controlled
123       // using /showIncludes
124       if (Arg.startswith("/showIncludes") || Arg.startswith("-showIncludes"))
125         continue;
126 
127       AdjustedArgs.push_back(Args[i]);
128     }
129     return AdjustedArgs;
130   };
131 }
132 
getInsertArgumentAdjuster(const CommandLineArguments & Extra,ArgumentInsertPosition Pos)133 ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
134                                             ArgumentInsertPosition Pos) {
135   return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
136     CommandLineArguments Return(Args);
137 
138     CommandLineArguments::iterator I;
139     if (Pos == ArgumentInsertPosition::END) {
140       I = Return.end();
141     } else {
142       I = Return.begin();
143       ++I; // To leave the program name in place
144     }
145 
146     Return.insert(I, Extra.begin(), Extra.end());
147     return Return;
148   };
149 }
150 
getInsertArgumentAdjuster(const char * Extra,ArgumentInsertPosition Pos)151 ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
152                                             ArgumentInsertPosition Pos) {
153   return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
154 }
155 
combineAdjusters(ArgumentsAdjuster First,ArgumentsAdjuster Second)156 ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
157                                    ArgumentsAdjuster Second) {
158   if (!First)
159     return Second;
160   if (!Second)
161     return First;
162   return [First, Second](const CommandLineArguments &Args, StringRef File) {
163     return Second(First(Args, File), File);
164   };
165 }
166 
getStripPluginsAdjuster()167 ArgumentsAdjuster getStripPluginsAdjuster() {
168   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
169     CommandLineArguments AdjustedArgs;
170     for (size_t I = 0, E = Args.size(); I != E; I++) {
171       // According to https://clang.llvm.org/docs/ClangPlugins.html
172       // plugin arguments are in the form:
173       // -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
174       // -Xclang <arbitrary-argument>
175       if (I + 4 < E && Args[I] == "-Xclang" &&
176           (Args[I + 1] == "-load" || Args[I + 1] == "-plugin" ||
177            llvm::StringRef(Args[I + 1]).startswith("-plugin-arg-") ||
178            Args[I + 1] == "-add-plugin") &&
179           Args[I + 2] == "-Xclang") {
180         I += 3;
181         continue;
182       }
183       AdjustedArgs.push_back(Args[I]);
184     }
185     return AdjustedArgs;
186   };
187 }
188 
189 } // end namespace tooling
190 } // end namespace clang
191