clang 19.0.0git
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
34namespace clang {
35namespace 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
71
72protected:
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.
82 int &argc, const char **argv, llvm::cl::OptionCategory &Category,
83 llvm::cl::NumOccurrencesFlag OccurrencesFlag = llvm::cl::OneOrMore,
84 const char *Overview = nullptr);
85
86public:
87 /// A factory method that is similar to the above constructor, except
88 /// this returns an error instead exiting the program on error.
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.
96 return *Compilations;
97 }
98
99 /// Returns a list of source file paths to process.
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.
107
108 static const char *const HelpMessage;
109
110private:
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
124public:
126 std::unique_ptr<CompilationDatabase> Compilations)
127 : Compilations(std::move(Compilations)) {}
128
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
138private:
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
int Category
Definition: Format.cpp:2974
ArgumentsAdjustingCompilations(std::unique_ptr< CompilationDatabase > Compilations)
void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster)
std::vector< std::string > getAllFiles() const override
Returns the list of all files available in the compilation database.
std::vector< CompileCommand > getAllCompileCommands() const override
Returns all compile commands for all the files in the compilation database.
std::vector< CompileCommand > getCompileCommands(StringRef FilePath) const override
Returns all compile commands in which the specified file was compiled.
A parser for options common to all command-line Clang tools.
static llvm::Expected< CommonOptionsParser > create(int &argc, const char **argv, llvm::cl::OptionCategory &Category, llvm::cl::NumOccurrencesFlag OccurrencesFlag=llvm::cl::OneOrMore, 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.
const std::vector< std::string > & getSourcePathList() const
Returns a list of source file paths to process.
CompilationDatabase & getCompilations()
Returns a reference to the loaded compilations database.
CommonOptionsParser(int &argc, const char **argv, llvm::cl::OptionCategory &Category, llvm::cl::NumOccurrencesFlag OccurrencesFlag=llvm::cl::OneOrMore, const char *Overview=nullptr)
Parses command-line, initializes a compilation database.
Interface for compilation databases.
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
The JSON file list parser is used to communicate input to InstallAPI.
Definition: Format.h:5394