CommonOptionsParser.cpp revision 280031
191100Sdes//===--- CommonOptionsParser.cpp - common options for clang tools ---------===// 291100Sdes// 391100Sdes// The LLVM Compiler Infrastructure 491100Sdes// 591100Sdes// This file is distributed under the University of Illinois Open Source 691100Sdes// License. See LICENSE.TXT for details. 791100Sdes// 891100Sdes//===----------------------------------------------------------------------===// 991100Sdes// 1091100Sdes// This file implements the CommonOptionsParser class used to parse common 1191100Sdes// command-line options for clang tools, so that they can be run as separate 1291100Sdes// command-line applications with a consistent common interface for handling 1391100Sdes// compilation database and input files. 1491100Sdes// 1591100Sdes// It provides a common subset of command-line options, common algorithm 1691100Sdes// for locating a compilation database and source files, and help messages 1791100Sdes// for the basic command-line interface. 1891100Sdes// 1991100Sdes// It creates a CompilationDatabase and reads common command-line options. 2091100Sdes// 2191100Sdes// This class uses the Clang Tooling infrastructure, see 2291100Sdes// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html 2391100Sdes// for details on setting it up with LLVM source tree. 2491100Sdes// 2591100Sdes//===----------------------------------------------------------------------===// 2691100Sdes 2791100Sdes#include "llvm/Support/CommandLine.h" 2891100Sdes#include "clang/Tooling/ArgumentsAdjusters.h" 2991100Sdes#include "clang/Tooling/CommonOptionsParser.h" 3091100Sdes#include "clang/Tooling/Tooling.h" 3191100Sdes 3291100Sdesusing namespace clang::tooling; 3391100Sdesusing namespace llvm; 3491100Sdes 3591100Sdesconst char *const CommonOptionsParser::HelpMessage = 3691100Sdes "\n" 3791100Sdes "-p <build-path> is used to read a compile command database.\n" 3891100Sdes "\n" 3991100Sdes "\tFor example, it can be a CMake build directory in which a file named\n" 4091100Sdes "\tcompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n" 4191100Sdes "\tCMake option to get this output). When no build path is specified,\n" 4291100Sdes "\ta search for compile_commands.json will be attempted through all\n" 4391100Sdes "\tparent paths of the first input file . See:\n" 4491100Sdes "\thttp://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n" 4591100Sdes "\texample of setting up Clang Tooling on a source tree.\n" 4691100Sdes "\n" 4791100Sdes "<source0> ... specify the paths of source files. These paths are\n" 4891100Sdes "\tlooked up in the compile command database. If the path of a file is\n" 4991100Sdes "\tabsolute, it needs to point into CMake's source tree. If the path is\n" 5091100Sdes "\trelative, the current working directory needs to be in the CMake\n" 5191100Sdes "\tsource tree and the file must be in a subdirectory of the current\n" 5291100Sdes "\tworking directory. \"./\" prefixes in the relative files will be\n" 5391100Sdes "\tautomatically removed, but the rest of a relative path must be a\n" 5491100Sdes "\tsuffix of a path in the compile command database.\n" 5591100Sdes "\n"; 5691100Sdes 5791100Sdesclass ArgumentsAdjustingCompilations : public CompilationDatabase { 5891100Sdespublic: 5991100Sdes ArgumentsAdjustingCompilations( 6091100Sdes std::unique_ptr<CompilationDatabase> Compilations) 6191100Sdes : Compilations(std::move(Compilations)) {} 6291100Sdes 6391100Sdes void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) { 6491100Sdes Adjusters.push_back(Adjuster); 6591100Sdes } 6691100Sdes 67 std::vector<CompileCommand> 68 getCompileCommands(StringRef FilePath) const override { 69 return adjustCommands(Compilations->getCompileCommands(FilePath)); 70 } 71 72 std::vector<std::string> getAllFiles() const override { 73 return Compilations->getAllFiles(); 74 } 75 76 std::vector<CompileCommand> getAllCompileCommands() const override { 77 return adjustCommands(Compilations->getAllCompileCommands()); 78 } 79 80private: 81 std::unique_ptr<CompilationDatabase> Compilations; 82 std::vector<ArgumentsAdjuster> Adjusters; 83 84 std::vector<CompileCommand> 85 adjustCommands(std::vector<CompileCommand> Commands) const { 86 for (CompileCommand &Command : Commands) 87 for (const auto &Adjuster : Adjusters) 88 Command.CommandLine = Adjuster(Command.CommandLine); 89 return Commands; 90 } 91}; 92 93CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, 94 cl::OptionCategory &Category, 95 const char *Overview) { 96 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); 97 98 static cl::opt<std::string> BuildPath("p", cl::desc("Build path"), 99 cl::Optional, cl::cat(Category)); 100 101 static cl::list<std::string> SourcePaths( 102 cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore, 103 cl::cat(Category)); 104 105 static cl::list<std::string> ArgsAfter( 106 "extra-arg", 107 cl::desc("Additional argument to append to the compiler command line"), 108 cl::cat(Category)); 109 110 static cl::list<std::string> ArgsBefore( 111 "extra-arg-before", 112 cl::desc("Additional argument to prepend to the compiler command line"), 113 cl::cat(Category)); 114 115 // Hide unrelated options. 116 StringMap<cl::Option*> Options; 117 cl::getRegisteredOptions(Options); 118 for (StringMap<cl::Option *>::iterator I = Options.begin(), E = Options.end(); 119 I != E; ++I) { 120 if (I->second->Category != &Category && I->first() != "help" && 121 I->first() != "version") 122 I->second->setHiddenFlag(cl::ReallyHidden); 123 } 124 125 Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc, 126 argv)); 127 cl::ParseCommandLineOptions(argc, argv, Overview); 128 SourcePathList = SourcePaths; 129 if (!Compilations) { 130 std::string ErrorMessage; 131 if (!BuildPath.empty()) { 132 Compilations = 133 CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage); 134 } else { 135 Compilations = CompilationDatabase::autoDetectFromSource(SourcePaths[0], 136 ErrorMessage); 137 } 138 if (!Compilations) 139 llvm::report_fatal_error(ErrorMessage); 140 } 141 auto AdjustingCompilations = 142 llvm::make_unique<ArgumentsAdjustingCompilations>( 143 std::move(Compilations)); 144 AdjustingCompilations->appendArgumentsAdjuster( 145 getInsertArgumentAdjuster(ArgsBefore, ArgumentInsertPosition::BEGIN)); 146 AdjustingCompilations->appendArgumentsAdjuster( 147 getInsertArgumentAdjuster(ArgsAfter, ArgumentInsertPosition::END)); 148 Compilations = std::move(AdjustingCompilations); 149} 150