118 lines
4.4 KiB
C++
118 lines
4.4 KiB
C++
//===- CommonOptionsParser.h - common options for clang tools -*- C++ -*-=====//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements the CommonOptionsParser class used to parse common
|
|
// command-line options for clang tools, so that they can be run as separate
|
|
// command-line applications with a consistent common interface for handling
|
|
// compilation database and input files.
|
|
//
|
|
// It provides a common subset of command-line options, common algorithm
|
|
// for locating a compilation database and source files, and help messages
|
|
// for the basic command-line interface.
|
|
//
|
|
// It creates a CompilationDatabase and reads common command-line options.
|
|
//
|
|
// This class uses the Clang Tooling infrastructure, see
|
|
// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
|
|
// for details on setting it up with LLVM source tree.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
|
|
#define LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H
|
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
namespace clang {
|
|
namespace tooling {
|
|
/// \brief 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:
|
|
/// \code
|
|
/// #include "clang/Frontend/FrontendActions.h"
|
|
/// #include "clang/Tooling/CommonOptionsParser.h"
|
|
/// #include "clang/Tooling/Tooling.h"
|
|
/// #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...");
|
|
/// static cl::opt<bool> YourOwnOption(...);
|
|
/// ...
|
|
///
|
|
/// int main(int argc, const char **argv) {
|
|
/// CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
|
|
/// ClangTool Tool(OptionsParser.getCompilations(),
|
|
/// OptionsParser.getSourcePathList());
|
|
/// return Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get());
|
|
/// }
|
|
/// \endcode
|
|
class CommonOptionsParser {
|
|
public:
|
|
/// \brief 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 \p Category become hidden.
|
|
///
|
|
/// This constructor exits program in case of error.
|
|
CommonOptionsParser(int &argc, const char **argv,
|
|
llvm::cl::OptionCategory &Category,
|
|
const char *Overview = nullptr)
|
|
: CommonOptionsParser(argc, argv, Category, llvm::cl::OneOrMore,
|
|
Overview) {}
|
|
|
|
/// \brief 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 \p Category become hidden.
|
|
///
|
|
/// I also allows calls to set the required number of positional parameters.
|
|
///
|
|
/// This constructor exits program in case of error.
|
|
CommonOptionsParser(int &argc, const char **argv,
|
|
llvm::cl::OptionCategory &Category,
|
|
llvm::cl::NumOccurrencesFlag OccurrencesFlag,
|
|
const char *Overview = nullptr);
|
|
|
|
/// Returns a reference to the loaded compilations database.
|
|
CompilationDatabase &getCompilations() {
|
|
return *Compilations;
|
|
}
|
|
|
|
/// Returns a list of source file paths to process.
|
|
const std::vector<std::string> &getSourcePathList() const {
|
|
return SourcePathList;
|
|
}
|
|
|
|
static const char *const HelpMessage;
|
|
|
|
private:
|
|
std::unique_ptr<CompilationDatabase> Compilations;
|
|
std::vector<std::string> SourcePathList;
|
|
std::vector<std::string> ExtraArgsBefore;
|
|
std::vector<std::string> ExtraArgsAfter;
|
|
};
|
|
|
|
} // namespace tooling
|
|
} // namespace clang
|
|
|
|
#endif // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H
|