clang 20.0.0git
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | List of all members
clang::tooling::CommonOptionsParser Class Reference

A parser for options common to all command-line Clang tools. More...

#include "clang/Tooling/CommonOptionsParser.h"

Public Member Functions

CompilationDatabasegetCompilations ()
 Returns a reference to the loaded compilations database.
 
const std::vector< std::string > & getSourcePathList () const
 Returns a list of source file paths to process.
 
ArgumentsAdjuster getArgumentsAdjuster ()
 Returns the argument adjuster calculated from "--extra-arg" and.
 

Static Public Member Functions

static llvm::Expected< CommonOptionsParsercreate (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 exiting the program on error.
 

Static Public Attributes

static const char *const HelpMessage
 

Protected Member Functions

 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.
 

Detailed Description

A parser for options common to all command-line Clang tools.

Parses a common subset of command-line arguments, locates and loads a compilation commands database and runs a tool with user-specified action. It also contains a help message for the common command-line options.

An example of usage:

#include "llvm/Support/CommandLine.h"
using namespace clang::tooling;
using namespace llvm;
static cl::OptionCategory MyToolCategory("my-tool options");
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...\n");
int main(int argc, const char **argv) {
auto ExpectedParser =
CommonOptionsParser::create(argc, argv, MyToolCategory);
if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError();
return 1;
}
CommonOptionsParser& OptionsParser = ExpectedParser.get();
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(
newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
}
Utility to run a FrontendAction over a set of files.
Definition: Tooling.h:311
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...
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.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30

Definition at line 70 of file CommonOptionsParser.h.

Constructor & Destructor Documentation

◆ CommonOptionsParser()

clang::tooling::CommonOptionsParser::CommonOptionsParser ( int argc,
const char **  argv,
llvm::cl::OptionCategory &  Category,
llvm::cl::NumOccurrencesFlag  OccurrencesFlag = llvm::cl::OneOrMore,
const char *  Overview = nullptr 
)
protected

Parses command-line, initializes a compilation database.

This constructor can change argc and argv contents, e.g. consume command-line options used for creating FixedCompilationDatabase.

All options not belonging to Category become hidden.

It also allows calls to set the required number of positional parameters.

Member Function Documentation

◆ create()

llvm::Expected< CommonOptionsParser > CommonOptionsParser::create ( int argc,
const char **  argv,
llvm::cl::OptionCategory &  Category,
llvm::cl::NumOccurrencesFlag  OccurrencesFlag = llvm::cl::OneOrMore,
const char *  Overview = nullptr 
)
static

A factory method that is similar to the above constructor, except this returns an error instead exiting the program on error.

Definition at line 156 of file CommonOptionsParser.cpp.

References Category.

Referenced by clang::tooling::internal::createExecutorFromCommandLineArgsImpl().

◆ getArgumentsAdjuster()

ArgumentsAdjuster clang::tooling::CommonOptionsParser::getArgumentsAdjuster ( )
inline

Returns the argument adjuster calculated from "--extra-arg" and.

Definition at line 106 of file CommonOptionsParser.h.

◆ getCompilations()

CompilationDatabase & clang::tooling::CommonOptionsParser::getCompilations ( )
inline

Returns a reference to the loaded compilations database.

Definition at line 95 of file CommonOptionsParser.h.

◆ getSourcePathList()

const std::vector< std::string > & clang::tooling::CommonOptionsParser::getSourcePathList ( ) const
inline

Returns a list of source file paths to process.

Definition at line 100 of file CommonOptionsParser.h.

Referenced by clang::tooling::AllTUsToolExecutorPlugin::create(), and clang::tooling::StandaloneToolExecutorPlugin::create().

Member Data Documentation

◆ HelpMessage

const char *const CommonOptionsParser::HelpMessage
static
Initial value:
=
"\n"
"-p <build-path> is used to read a compile command database.\n"
"\n"
"\tFor example, it can be a CMake build directory in which a file named\n"
"\tcompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n"
"\tCMake option to get this output). When no build path is specified,\n"
"\ta search for compile_commands.json will be attempted through all\n"
"\tparent paths of the first input file . See:\n"
"\thttps://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n"
"\texample of setting up Clang Tooling on a source tree.\n"
"\n"
"<source0> ... specify the paths of source files. These paths are\n"
"\tlooked up in the compile command database. If the path of a file is\n"
"\tabsolute, it needs to point into CMake's source tree. If the path is\n"
"\trelative, the current working directory needs to be in the CMake\n"
"\tsource tree and the file must be in a subdirectory of the current\n"
"\tworking directory. \"./\" prefixes in the relative files will be\n"
"\tautomatically removed, but the rest of a relative path must be a\n"
"\tsuffix of a path in the compile command database.\n"
"\n"

Definition at line 108 of file CommonOptionsParser.h.


The documentation for this class was generated from the following files: