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