clang  10.0.0git
ArgumentsAdjusters.cpp
Go to the documentation of this file.
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 
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.
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 (Arg == "-fsyntax-only")
46  HasSyntaxOnly = true;
47  }
48  if (!HasSyntaxOnly)
49  AdjustedArgs.push_back("-fsyntax-only");
50  return AdjustedArgs;
51  };
52 }
53 
55  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
56  CommandLineArguments AdjustedArgs;
57  for (size_t i = 0, e = Args.size(); i < e; ++i) {
58  StringRef Arg = Args[i];
59  if (!Arg.startswith("-o"))
60  AdjustedArgs.push_back(Args[i]);
61 
62  if (Arg == "-o") {
63  // Output is specified as -o foo. Skip the next argument too.
64  ++i;
65  }
66  // Else, the output is specified as -ofoo. Just do nothing.
67  }
68  return AdjustedArgs;
69  };
70 }
71 
73  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
74  CommandLineArguments AdjustedArgs;
75  for (size_t i = 0, e = Args.size(); i < e; ++i) {
76  StringRef Arg = Args[i];
77  if (Arg == "--serialize-diagnostics") {
78  // Skip the diagnostic output argument.
79  ++i;
80  continue;
81  }
82  AdjustedArgs.push_back(Args[i]);
83  }
84  return AdjustedArgs;
85  };
86 }
87 
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  // All dependency-file options begin with -M. These include -MM,
94  // -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD.
95  if (!Arg.startswith("-M")) {
96  AdjustedArgs.push_back(Args[i]);
97  continue;
98  }
99 
100  if (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")
101  // These flags take an argument: -MX foo. Skip the next argument also.
102  ++i;
103  }
104  return AdjustedArgs;
105  };
106 }
107 
110  return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
111  CommandLineArguments Return(Args);
112 
113  CommandLineArguments::iterator I;
114  if (Pos == ArgumentInsertPosition::END) {
115  I = Return.end();
116  } else {
117  I = Return.begin();
118  ++I; // To leave the program name in place
119  }
120 
121  Return.insert(I, Extra.begin(), Extra.end());
122  return Return;
123  };
124 }
125 
128  return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
129 }
130 
132  ArgumentsAdjuster Second) {
133  if (!First)
134  return Second;
135  if (!Second)
136  return First;
137  return [First, Second](const CommandLineArguments &Args, StringRef File) {
138  return Second(First(Args, File), File);
139  };
140 }
141 
143  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
144  CommandLineArguments AdjustedArgs;
145  for (size_t I = 0, E = Args.size(); I != E; I++) {
146  // According to https://clang.llvm.org/docs/ClangPlugins.html
147  // plugin arguments are in the form:
148  // -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
149  // -Xclang <arbitrary-argument>
150  if (I + 4 < E && Args[I] == "-Xclang" &&
151  (Args[I + 1] == "-load" || Args[I + 1] == "-plugin" ||
152  llvm::StringRef(Args[I + 1]).startswith("-plugin-arg-") ||
153  Args[I + 1] == "-add-plugin") &&
154  Args[I + 2] == "-Xclang") {
155  I += 3;
156  continue;
157  }
158  AdjustedArgs.push_back(Args[I]);
159  }
160  return AdjustedArgs;
161  };
162 }
163 
164 } // end namespace tooling
165 } // end namespace clang
std::vector< std::string > CommandLineArguments
A sequence of command line arguments.
ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra, ArgumentInsertPosition Pos)
Gets an argument adjuster which inserts Extra arguments in the specified position.
ArgumentsAdjuster getClangStripDependencyFileAdjuster()
Gets an argument adjuster which removes dependency-file related command line arguments.
ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First, ArgumentsAdjuster Second)
Gets an argument adjuster which adjusts the arguments in sequence with the First adjuster and then wi...
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
ArgumentsAdjuster getClangStripOutputAdjuster()
Gets an argument adjuster which removes output-related command line arguments.
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
Dataflow Directional Tag Classes.
ArgumentsAdjuster getClangSyntaxOnlyAdjuster()
Gets an argument adjuster that converts input command line arguments to the "syntax check only" varia...
ArgumentsAdjuster getClangStripSerializeDiagnosticAdjuster()
Gets an argument adjuster which removes command line arguments related to diagnostic serialization...
ArgumentsAdjuster getStripPluginsAdjuster()
Gets an argument adjuster which strips plugin related command line arguments.