clang  6.0.0svn
CommonOptionsParser.h
Go to the documentation of this file.
1 //===- CommonOptionsParser.h - common options for clang tools -*- C++ -*-=====//
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 implements the CommonOptionsParser class used to parse common
11 // command-line options for clang tools, so that they can be run as separate
12 // command-line applications with a consistent common interface for handling
13 // compilation database and input files.
14 //
15 // It provides a common subset of command-line options, common algorithm
16 // for locating a compilation database and source files, and help messages
17 // for the basic command-line interface.
18 //
19 // It creates a CompilationDatabase and reads common command-line options.
20 //
21 // This class uses the Clang Tooling infrastructure, see
22 // http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
23 // for details on setting it up with LLVM source tree.
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #ifndef LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
28 #define LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
29 
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Error.h"
34 
35 namespace clang {
36 namespace tooling {
37 /// \brief A parser for options common to all command-line Clang tools.
38 ///
39 /// Parses a common subset of command-line arguments, locates and loads a
40 /// compilation commands database and runs a tool with user-specified action. It
41 /// also contains a help message for the common command-line options.
42 ///
43 /// An example of usage:
44 /// \code
45 /// #include "clang/Frontend/FrontendActions.h"
46 /// #include "clang/Tooling/CommonOptionsParser.h"
47 /// #include "clang/Tooling/Tooling.h"
48 /// #include "llvm/Support/CommandLine.h"
49 ///
50 /// using namespace clang::tooling;
51 /// using namespace llvm;
52 ///
53 /// static cl::OptionCategory MyToolCategory("My tool options");
54 /// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
55 /// static cl::extrahelp MoreHelp("\nMore help text...");
56 /// static cl::opt<bool> YourOwnOption(...);
57 /// ...
58 ///
59 /// int main(int argc, const char **argv) {
60 /// CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
61 /// ClangTool Tool(OptionsParser.getCompilations(),
62 /// OptionsParser.getSourcePathList());
63 /// return Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get());
64 /// }
65 /// \endcode
67 public:
68  /// \brief Parses command-line, initializes a compilation database.
69  ///
70  /// This constructor can change argc and argv contents, e.g. consume
71  /// command-line options used for creating FixedCompilationDatabase.
72  ///
73  /// All options not belonging to \p Category become hidden.
74  ///
75  /// This constructor exits program in case of error.
76  CommonOptionsParser(int &argc, const char **argv,
77  llvm::cl::OptionCategory &Category,
78  const char *Overview = nullptr)
79  : CommonOptionsParser(argc, argv, Category, llvm::cl::OneOrMore,
80  Overview) {}
81 
82  /// \brief Parses command-line, initializes a compilation database.
83  ///
84  /// This constructor can change argc and argv contents, e.g. consume
85  /// command-line options used for creating FixedCompilationDatabase.
86  ///
87  /// All options not belonging to \p Category become hidden.
88  ///
89  /// It also allows calls to set the required number of positional parameters.
90  CommonOptionsParser(int &argc, const char **argv,
91  llvm::cl::OptionCategory &Category,
92  llvm::cl::NumOccurrencesFlag OccurrencesFlag,
93  const char *Overview = nullptr);
94 
95  /// \brief A factory method that is similar to the above constructor, except
96  /// this returns an error instead exiting the program on error.
98  create(int &argc, const char **argv, llvm::cl::OptionCategory &Category,
99  llvm::cl::NumOccurrencesFlag OccurrencesFlag,
100  const char *Overview = nullptr);
101 
102  /// Returns a reference to the loaded compilations database.
104  return *Compilations;
105  }
106 
107  /// Returns a list of source file paths to process.
108  const std::vector<std::string> &getSourcePathList() const {
109  return SourcePathList;
110  }
111 
112  /// Returns the argument adjuster calculated from "--extra-arg" and
113  //"--extra-arg-before" options.
115 
116  static const char *const HelpMessage;
117 
118 private:
119  CommonOptionsParser() = default;
120 
121  llvm::Error init(int &argc, const char **argv,
122  llvm::cl::OptionCategory &Category,
123  llvm::cl::NumOccurrencesFlag OccurrencesFlag,
124  const char *Overview);
125 
126  std::unique_ptr<CompilationDatabase> Compilations;
127  std::vector<std::string> SourcePathList;
128  ArgumentsAdjuster Adjuster;
129 };
130 
132 public:
134  std::unique_ptr<CompilationDatabase> Compilations)
135  : Compilations(std::move(Compilations)) {}
136 
137  void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster);
138 
139  std::vector<CompileCommand>
140  getCompileCommands(StringRef FilePath) const override;
141 
142  std::vector<std::string> getAllFiles() const override;
143 
144  std::vector<CompileCommand> getAllCompileCommands() const override;
145 
146 private:
147  std::unique_ptr<CompilationDatabase> Compilations;
148  std::vector<ArgumentsAdjuster> Adjusters;
149 
150  std::vector<CompileCommand>
151  adjustCommands(std::vector<CompileCommand> Commands) const;
152 };
153 
154 } // namespace tooling
155 } // namespace clang
156 
157 #endif // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
Definition: Format.h:1900
int Category
Definition: Format.cpp:1348
ArgumentsAdjustingCompilations(std::unique_ptr< CompilationDatabase > Compilations)
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
CommonOptionsParser(int &argc, const char **argv, llvm::cl::OptionCategory &Category, const char *Overview=nullptr)
Parses command-line, initializes a compilation database.
Interface for compilation databases.
A parser for options common to all command-line Clang tools.
Dataflow Directional Tag Classes.
static llvm::Expected< CommonOptionsParser > create(int &argc, const char **argv, llvm::cl::OptionCategory &Category, llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview=nullptr)
A factory method that is similar to the above constructor, except this returns an error instead exiti...
ArgumentsAdjuster getArgumentsAdjuster()
Returns the argument adjuster calculated from "--extra-arg" and.
CompilationDatabase & getCompilations()
Returns a reference to the loaded compilations database.
const std::vector< std::string > & getSourcePathList() const
Returns a list of source file paths to process.