xref: /freebsd/contrib/llvm-project/clang/include/clang/Tooling/CommonOptionsParser.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
29 #include "clang/Tooling/ArgumentsAdjusters.h"
30 #include "clang/Tooling/CompilationDatabase.h"
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 ///
56 /// int main(int argc, const char **argv) {
57 ///   auto ExpectedParser =
58 ///       CommonOptionsParser::create(argc, argv, MyToolCategory);
59 ///   if (!ExpectedParser) {
60 ///     llvm::errs() << ExpectedParser.takeError();
61 ///     return 1;
62 ///   }
63 ///   CommonOptionsParser& OptionsParser = ExpectedParser.get();
64 ///   ClangTool Tool(OptionsParser.getCompilations(),
65 ///                  OptionsParser.getSourcePathList());
66 ///   return Tool.run(
67 ///       newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
68 /// }
69 /// \endcode
70 class CommonOptionsParser {
71 
72 protected:
73   /// Parses command-line, initializes a compilation database.
74   ///
75   /// This constructor can change argc and argv contents, e.g. consume
76   /// command-line options used for creating FixedCompilationDatabase.
77   ///
78   /// All options not belonging to \p Category become hidden.
79   ///
80   /// It also allows calls to set the required number of positional parameters.
81   CommonOptionsParser(
82       int &argc, const char **argv, llvm::cl::OptionCategory &Category,
83       llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
84       const char *Overview = nullptr);
85 
86 public:
87   /// A factory method that is similar to the above constructor, except
88   /// this returns an error instead exiting the program on error.
89   static llvm::Expected<CommonOptionsParser>
90   create(int &argc, const char **argv, llvm::cl::OptionCategory &Category,
91          llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
92          const char *Overview = nullptr);
93 
94   /// Returns a reference to the loaded compilations database.
getCompilations()95   CompilationDatabase &getCompilations() {
96     return *Compilations;
97   }
98 
99   /// Returns a list of source file paths to process.
getSourcePathList()100   const std::vector<std::string> &getSourcePathList() const {
101     return SourcePathList;
102   }
103 
104   /// Returns the argument adjuster calculated from "--extra-arg" and
105   //"--extra-arg-before" options.
getArgumentsAdjuster()106   ArgumentsAdjuster getArgumentsAdjuster() { return Adjuster; }
107 
108   static const char *const HelpMessage;
109 
110 private:
111   CommonOptionsParser() = default;
112 
113   llvm::Error init(int &argc, const char **argv,
114                    llvm::cl::OptionCategory &Category,
115                    llvm::cl::NumOccurrencesFlag OccurrencesFlag,
116                    const char *Overview);
117 
118   std::unique_ptr<CompilationDatabase> Compilations;
119   std::vector<std::string> SourcePathList;
120   ArgumentsAdjuster Adjuster;
121 };
122 
123 class ArgumentsAdjustingCompilations : public CompilationDatabase {
124 public:
ArgumentsAdjustingCompilations(std::unique_ptr<CompilationDatabase> Compilations)125   ArgumentsAdjustingCompilations(
126       std::unique_ptr<CompilationDatabase> Compilations)
127       : Compilations(std::move(Compilations)) {}
128 
129   void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster);
130 
131   std::vector<CompileCommand>
132   getCompileCommands(StringRef FilePath) const override;
133 
134   std::vector<std::string> getAllFiles() const override;
135 
136   std::vector<CompileCommand> getAllCompileCommands() const override;
137 
138 private:
139   std::unique_ptr<CompilationDatabase> Compilations;
140   std::vector<ArgumentsAdjuster> Adjusters;
141 
142   std::vector<CompileCommand>
143   adjustCommands(std::vector<CompileCommand> Commands) const;
144 };
145 
146 }  // namespace tooling
147 }  // namespace clang
148 
149 #endif // LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
150