1*f4a2713aSLionel Sambuc //===--- CommonOptionsParser.cpp - common options for clang tools ---------===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This file implements the CommonOptionsParser class used to parse common 11*f4a2713aSLionel Sambuc // command-line options for clang tools, so that they can be run as separate 12*f4a2713aSLionel Sambuc // command-line applications with a consistent common interface for handling 13*f4a2713aSLionel Sambuc // compilation database and input files. 14*f4a2713aSLionel Sambuc // 15*f4a2713aSLionel Sambuc // It provides a common subset of command-line options, common algorithm 16*f4a2713aSLionel Sambuc // for locating a compilation database and source files, and help messages 17*f4a2713aSLionel Sambuc // for the basic command-line interface. 18*f4a2713aSLionel Sambuc // 19*f4a2713aSLionel Sambuc // It creates a CompilationDatabase and reads common command-line options. 20*f4a2713aSLionel Sambuc // 21*f4a2713aSLionel Sambuc // This class uses the Clang Tooling infrastructure, see 22*f4a2713aSLionel Sambuc // http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html 23*f4a2713aSLionel Sambuc // for details on setting it up with LLVM source tree. 24*f4a2713aSLionel Sambuc // 25*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 26*f4a2713aSLionel Sambuc 27*f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h" 28*f4a2713aSLionel Sambuc #include "clang/Tooling/CommonOptionsParser.h" 29*f4a2713aSLionel Sambuc #include "clang/Tooling/Tooling.h" 30*f4a2713aSLionel Sambuc 31*f4a2713aSLionel Sambuc using namespace clang::tooling; 32*f4a2713aSLionel Sambuc using namespace llvm; 33*f4a2713aSLionel Sambuc 34*f4a2713aSLionel Sambuc const char *const CommonOptionsParser::HelpMessage = 35*f4a2713aSLionel Sambuc "\n" 36*f4a2713aSLionel Sambuc "-p <build-path> is used to read a compile command database.\n" 37*f4a2713aSLionel Sambuc "\n" 38*f4a2713aSLionel Sambuc "\tFor example, it can be a CMake build directory in which a file named\n" 39*f4a2713aSLionel Sambuc "\tcompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n" 40*f4a2713aSLionel Sambuc "\tCMake option to get this output). When no build path is specified,\n" 41*f4a2713aSLionel Sambuc "\ta search for compile_commands.json will be attempted through all\n" 42*f4a2713aSLionel Sambuc "\tparent paths of the first input file . See:\n" 43*f4a2713aSLionel Sambuc "\thttp://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n" 44*f4a2713aSLionel Sambuc "\texample of setting up Clang Tooling on a source tree.\n" 45*f4a2713aSLionel Sambuc "\n" 46*f4a2713aSLionel Sambuc "<source0> ... specify the paths of source files. These paths are\n" 47*f4a2713aSLionel Sambuc "\tlooked up in the compile command database. If the path of a file is\n" 48*f4a2713aSLionel Sambuc "\tabsolute, it needs to point into CMake's source tree. If the path is\n" 49*f4a2713aSLionel Sambuc "\trelative, the current working directory needs to be in the CMake\n" 50*f4a2713aSLionel Sambuc "\tsource tree and the file must be in a subdirectory of the current\n" 51*f4a2713aSLionel Sambuc "\tworking directory. \"./\" prefixes in the relative files will be\n" 52*f4a2713aSLionel Sambuc "\tautomatically removed, but the rest of a relative path must be a\n" 53*f4a2713aSLionel Sambuc "\tsuffix of a path in the compile command database.\n" 54*f4a2713aSLionel Sambuc "\n"; 55*f4a2713aSLionel Sambuc 56*f4a2713aSLionel Sambuc CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, 57*f4a2713aSLionel Sambuc const char *Overview) { 58*f4a2713aSLionel Sambuc static cl::opt<std::string> BuildPath( 59*f4a2713aSLionel Sambuc "p", cl::desc("Build path"), cl::Optional); 60*f4a2713aSLionel Sambuc 61*f4a2713aSLionel Sambuc static cl::list<std::string> SourcePaths( 62*f4a2713aSLionel Sambuc cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore); 63*f4a2713aSLionel Sambuc 64*f4a2713aSLionel Sambuc Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc, 65*f4a2713aSLionel Sambuc argv)); 66*f4a2713aSLionel Sambuc cl::ParseCommandLineOptions(argc, argv, Overview); 67*f4a2713aSLionel Sambuc SourcePathList = SourcePaths; 68*f4a2713aSLionel Sambuc if (!Compilations) { 69*f4a2713aSLionel Sambuc std::string ErrorMessage; 70*f4a2713aSLionel Sambuc if (!BuildPath.empty()) { 71*f4a2713aSLionel Sambuc Compilations.reset(CompilationDatabase::autoDetectFromDirectory( 72*f4a2713aSLionel Sambuc BuildPath, ErrorMessage)); 73*f4a2713aSLionel Sambuc } else { 74*f4a2713aSLionel Sambuc Compilations.reset(CompilationDatabase::autoDetectFromSource( 75*f4a2713aSLionel Sambuc SourcePaths[0], ErrorMessage)); 76*f4a2713aSLionel Sambuc } 77*f4a2713aSLionel Sambuc if (!Compilations) 78*f4a2713aSLionel Sambuc llvm::report_fatal_error(ErrorMessage); 79*f4a2713aSLionel Sambuc } 80*f4a2713aSLionel Sambuc } 81