clang  8.0.0
ArgumentsAdjusters.cpp
Go to the documentation of this file.
1 //===- ArgumentsAdjusters.cpp - Command line arguments adjuster -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains definitions of classes which implement ArgumentsAdjuster
11 // interface.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "clang/Basic/LLVM.h"
17 #include "llvm/ADT/StringRef.h"
18 #include <cstddef>
19 
20 namespace clang {
21 namespace tooling {
22 
23 /// Add -fsyntax-only option to the command line arguments.
25  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
26  CommandLineArguments AdjustedArgs;
27  for (size_t i = 0, e = Args.size(); i < e; ++i) {
28  StringRef Arg = Args[i];
29  // FIXME: Remove options that generate output.
30  if (!Arg.startswith("-fcolor-diagnostics") &&
31  !Arg.startswith("-fdiagnostics-color"))
32  AdjustedArgs.push_back(Args[i]);
33  }
34  AdjustedArgs.push_back("-fsyntax-only");
35  return AdjustedArgs;
36  };
37 }
38 
40  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
41  CommandLineArguments AdjustedArgs;
42  for (size_t i = 0, e = Args.size(); i < e; ++i) {
43  StringRef Arg = Args[i];
44  if (!Arg.startswith("-o"))
45  AdjustedArgs.push_back(Args[i]);
46 
47  if (Arg == "-o") {
48  // Output is specified as -o foo. Skip the next argument too.
49  ++i;
50  }
51  // Else, the output is specified as -ofoo. Just do nothing.
52  }
53  return AdjustedArgs;
54  };
55 }
56 
58  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
59  CommandLineArguments AdjustedArgs;
60  for (size_t i = 0, e = Args.size(); i < e; ++i) {
61  StringRef Arg = Args[i];
62  // All dependency-file options begin with -M. These include -MM,
63  // -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD.
64  if (!Arg.startswith("-M")) {
65  AdjustedArgs.push_back(Args[i]);
66  continue;
67  }
68 
69  if (Arg == "-MF" || Arg == "-MT" || Arg == "-MQ")
70  // These flags take an argument: -MX foo. Skip the next argument also.
71  ++i;
72  }
73  return AdjustedArgs;
74  };
75 }
76 
79  return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) {
80  CommandLineArguments Return(Args);
81 
82  CommandLineArguments::iterator I;
83  if (Pos == ArgumentInsertPosition::END) {
84  I = Return.end();
85  } else {
86  I = Return.begin();
87  ++I; // To leave the program name in place
88  }
89 
90  Return.insert(I, Extra.begin(), Extra.end());
91  return Return;
92  };
93 }
94 
97  return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
98 }
99 
101  ArgumentsAdjuster Second) {
102  if (!First)
103  return Second;
104  if (!Second)
105  return First;
106  return [First, Second](const CommandLineArguments &Args, StringRef File) {
107  return Second(First(Args, File), File);
108  };
109 }
110 
112  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
113  CommandLineArguments AdjustedArgs;
114  for (size_t I = 0, E = Args.size(); I != E; I++) {
115  // According to https://clang.llvm.org/docs/ClangPlugins.html
116  // plugin arguments are in the form:
117  // -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
118  // -Xclang <arbitrary-argument>
119  if (I + 4 < E && Args[I] == "-Xclang" &&
120  (Args[I + 1] == "-load" || Args[I + 1] == "-plugin" ||
121  llvm::StringRef(Args[I + 1]).startswith("-plugin-arg-") ||
122  Args[I + 1] == "-add-plugin") &&
123  Args[I + 2] == "-Xclang") {
124  I += 3;
125  continue;
126  }
127  AdjustedArgs.push_back(Args[I]);
128  }
129  return AdjustedArgs;
130  };
131 }
132 
133 } // end namespace tooling
134 } // 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 getStripPluginsAdjuster()
Gets an argument adjuster which strips plugin related command line arguments.