1243791Sdim//===- CommonOptionsParser.h - common options for clang tools -*- C++ -*-=====// 2243791Sdim// 3243791Sdim// The LLVM Compiler Infrastructure 4243791Sdim// 5243791Sdim// This file is distributed under the University of Illinois Open Source 6243791Sdim// License. See LICENSE.TXT for details. 7243791Sdim// 8243791Sdim//===----------------------------------------------------------------------===// 9243791Sdim// 10243791Sdim// This file implements the CommonOptionsParser class used to parse common 11243791Sdim// command-line options for clang tools, so that they can be run as separate 12243791Sdim// command-line applications with a consistent common interface for handling 13243791Sdim// compilation database and input files. 14243791Sdim// 15243791Sdim// It provides a common subset of command-line options, common algorithm 16243791Sdim// for locating a compilation database and source files, and help messages 17243791Sdim// for the basic command-line interface. 18243791Sdim// 19243791Sdim// It creates a CompilationDatabase and reads common command-line options. 20243791Sdim// 21243791Sdim// This class uses the Clang Tooling infrastructure, see 22243791Sdim// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html 23243791Sdim// for details on setting it up with LLVM source tree. 24243791Sdim// 25243791Sdim//===----------------------------------------------------------------------===// 26243791Sdim 27243791Sdim#ifndef LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H 28243791Sdim#define LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H 29243791Sdim 30243791Sdim#include "clang/Tooling/CompilationDatabase.h" 31243791Sdim 32243791Sdimnamespace clang { 33243791Sdimnamespace tooling { 34243791Sdim/// \brief A parser for options common to all command-line Clang tools. 35243791Sdim/// 36243791Sdim/// Parses a common subset of command-line arguments, locates and loads a 37243791Sdim/// compilation commands database and runs a tool with user-specified action. It 38243791Sdim/// also contains a help message for the common command-line options. 39243791Sdim/// 40243791Sdim/// An example of usage: 41243791Sdim/// \code 42243791Sdim/// #include "clang/Frontend/FrontendActions.h" 43243791Sdim/// #include "clang/Tooling/CommonOptionsParser.h" 44243791Sdim/// #include "llvm/Support/CommandLine.h" 45243791Sdim/// 46243791Sdim/// using namespace clang::tooling; 47243791Sdim/// using namespace llvm; 48243791Sdim/// 49243791Sdim/// static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); 50243791Sdim/// static cl::extrahelp MoreHelp("\nMore help text..."); 51243791Sdim/// static cl:opt<bool> YourOwnOption(...); 52243791Sdim/// ... 53243791Sdim/// 54243791Sdim/// int main(int argc, const char **argv) { 55243791Sdim/// CommonOptionsParser OptionsParser(argc, argv); 56249423Sdim/// ClangTool Tool(OptionsParser.getCompilations(), 57249423Sdim/// OptionsParser.getSourcePathListi()); 58243791Sdim/// return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>()); 59243791Sdim/// } 60243791Sdim/// \endcode 61243791Sdimclass CommonOptionsParser { 62243791Sdimpublic: 63243791Sdim /// \brief Parses command-line, initializes a compilation database. 64243791Sdim /// This constructor can change argc and argv contents, e.g. consume 65243791Sdim /// command-line options used for creating FixedCompilationDatabase. 66243791Sdim /// This constructor exits program in case of error. 67243791Sdim CommonOptionsParser(int &argc, const char **argv); 68243791Sdim 69243791Sdim /// Returns a reference to the loaded compilations database. 70249423Sdim CompilationDatabase &getCompilations() { 71243791Sdim return *Compilations; 72243791Sdim } 73243791Sdim 74243791Sdim /// Returns a list of source file paths to process. 75249423Sdim std::vector<std::string> getSourcePathList() { 76243791Sdim return SourcePathList; 77243791Sdim } 78243791Sdim 79243791Sdim static const char *const HelpMessage; 80243791Sdim 81243791Sdimprivate: 82249423Sdim OwningPtr<CompilationDatabase> Compilations; 83243791Sdim std::vector<std::string> SourcePathList; 84243791Sdim}; 85243791Sdim 86243791Sdim} // namespace tooling 87243791Sdim} // namespace clang 88243791Sdim 89243791Sdim#endif // LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H 90