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 
24 /// Add -fsyntax-only option and drop options that triggers output generation.
getClangSyntaxOnlyAdjuster()25 ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
26   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
27     CommandLineArguments AdjustedArgs;
28     bool HasSyntaxOnly = false;
29     const std::vector<llvm::StringRef> OutputCommands = {
30         // FIXME: Add other options that generate output.
31         "-save-temps",
32         "--save-temps",
33     };
34     for (size_t i = 0, e = Args.size(); i < e; ++i) {
35       StringRef Arg = Args[i];
36       // Skip output commands.
37       if (llvm::any_of(OutputCommands, [&Arg](llvm::StringRef OutputCommand) {
38             return Arg.startswith(OutputCommand);
39           }))
40         continue;
41 
42       if (!Arg.startswith("-fcolor-diagnostics") &&
43           !Arg.startswith("-fdiagnostics-color"))
44         AdjustedArgs.push_back(Args[i]);
45       // If we strip a color option, make sure we strip any preceeding `-Xclang`
46       // option as well.
47       // FIXME: This should be added to most argument adjusters!
48       else if (!AdjustedArgs.empty() && AdjustedArgs.back() == "-Xclang")
49         AdjustedArgs.pop_back();
50 
51       if (Arg == "-fsyntax-only")
52         HasSyntaxOnly = true;
53     }
54     if (!HasSyntaxOnly)
55       AdjustedArgs.push_back("-fsyntax-only");
56     return AdjustedArgs;
57   };
58 }
59 
getClangStripOutputAdjuster()60 ArgumentsAdjuster getClangStripOutputAdjuster() {
61   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
62     CommandLineArguments AdjustedArgs;
63     for (size_t i = 0, e = Args.size(); i < e; ++i) {
64       StringRef Arg = Args[i];
65       if (!Arg.startswith("-o"))
66         AdjustedArgs.push_back(Args[i]);
67 
68       if (Arg == "-o") {
69         // Output is specified as -o foo. Skip the next argument too.
70         ++i;
71       }
72       // Else, the output is specified as -ofoo. Just do nothing.
73     }
74     return AdjustedArgs;
75   };
76 }
77 
getClangStripSerializeDiagnosticAdjuster()78 ArgumentsAdjuster getClangStripSerializeDiagnosticAdjuster() {
79   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
80     CommandLineArguments AdjustedArgs;
81     for (size_t i = 0, e = Args.size(); i < e; ++i) {
82       StringRef Arg = Args[i];
83       if (Arg == "--serialize-diagnostics") {
84         // Skip the diagnostic output argument.
85         ++i;
86         continue;
87       }
88       AdjustedArgs.push_back(Args[i]);
89     }
90     return AdjustedArgs;
91   };
92 }
93 
getClangStripDependencyFileAdjuster()94 ArgumentsAdjuster getClangStripDependencyFileAdjuster() {
95   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
96     CommandLineArguments AdjustedArgs;
97     for (size_t i = 0, e = Args.size(); i < e; ++i) {
98       StringRef Arg = Args[i];
99       // All dependency-file options begin with -M. These include -MM,
100       // -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD.
101       if (!Arg.startswith("-M")) {
102         AdjustedArgs.push_back(Args[i]);
103         continue;
104       }
105 
106       if (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")
107         // These flags take an argument: -MX foo. Skip the next argument also.
108         ++i;
109     }
110     return AdjustedArgs;
111   };
112 }
113 
getInsertArgumentAdjuster(const CommandLineArguments & Extra,ArgumentInsertPosition Pos)114 ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
115                                             ArgumentInsertPosition Pos) {
116   return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
117     CommandLineArguments Return(Args);
118 
119     CommandLineArguments::iterator I;
120     if (Pos == ArgumentInsertPosition::END) {
121       I = Return.end();
122     } else {
123       I = Return.begin();
124       ++I; // To leave the program name in place
125     }
126 
127     Return.insert(I, Extra.begin(), Extra.end());
128     return Return;
129   };
130 }
131 
getInsertArgumentAdjuster(const char * Extra,ArgumentInsertPosition Pos)132 ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
133                                             ArgumentInsertPosition Pos) {
134   return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
135 }
136 
combineAdjusters(ArgumentsAdjuster First,ArgumentsAdjuster Second)137 ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
138                                    ArgumentsAdjuster Second) {
139   if (!First)
140     return Second;
141   if (!Second)
142     return First;
143   return [First, Second](const CommandLineArguments &Args, StringRef File) {
144     return Second(First(Args, File), File);
145   };
146 }
147 
getStripPluginsAdjuster()148 ArgumentsAdjuster getStripPluginsAdjuster() {
149   return [](const CommandLineArguments &Args, StringRef /*unused*/) {
150     CommandLineArguments AdjustedArgs;
151     for (size_t I = 0, E = Args.size(); I != E; I++) {
152       // According to https://clang.llvm.org/docs/ClangPlugins.html
153       // plugin arguments are in the form:
154       // -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
155       // -Xclang <arbitrary-argument>
156       if (I + 4 < E && Args[I] == "-Xclang" &&
157           (Args[I + 1] == "-load" || Args[I + 1] == "-plugin" ||
158            llvm::StringRef(Args[I + 1]).startswith("-plugin-arg-") ||
159            Args[I + 1] == "-add-plugin") &&
160           Args[I + 2] == "-Xclang") {
161         I += 3;
162         continue;
163       }
164       AdjustedArgs.push_back(Args[I]);
165     }
166     return AdjustedArgs;
167   };
168 }
169 
170 } // end namespace tooling
171 } // end namespace clang
172