Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 1 | //===--- CommonOptionsParser.cpp - common options for clang tools ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the CommonOptionsParser class used to parse common |
| 11 | // command-line options for clang tools, so that they can be run as separate |
| 12 | // command-line applications with a consistent common interface for handling |
| 13 | // compilation database and input files. |
| 14 | // |
| 15 | // It provides a common subset of command-line options, common algorithm |
| 16 | // for locating a compilation database and source files, and help messages |
| 17 | // for the basic command-line interface. |
| 18 | // |
| 19 | // It creates a CompilationDatabase and reads common command-line options. |
| 20 | // |
| 21 | // This class uses the Clang Tooling infrastructure, see |
| 22 | // http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html |
| 23 | // for details on setting it up with LLVM source tree. |
| 24 | // |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | #include "llvm/Support/CommandLine.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 28 | #include "clang/Tooling/ArgumentsAdjusters.h" |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 29 | #include "clang/Tooling/CommonOptionsParser.h" |
| 30 | #include "clang/Tooling/Tooling.h" |
| 31 | |
| 32 | using namespace clang::tooling; |
| 33 | using namespace llvm; |
| 34 | |
Alexander Kornienko | 6fbe982 | 2012-08-24 00:39:14 +0000 | [diff] [blame] | 35 | const char *const CommonOptionsParser::HelpMessage = |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 36 | "\n" |
| 37 | "-p <build-path> is used to read a compile command database.\n" |
| 38 | "\n" |
| 39 | "\tFor example, it can be a CMake build directory in which a file named\n" |
| 40 | "\tcompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n" |
| 41 | "\tCMake option to get this output). When no build path is specified,\n" |
Edwin Vane | b1f67db | 2012-12-14 18:58:25 +0000 | [diff] [blame] | 42 | "\ta search for compile_commands.json will be attempted through all\n" |
| 43 | "\tparent paths of the first input file . See:\n" |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 44 | "\thttp://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n" |
| 45 | "\texample of setting up Clang Tooling on a source tree.\n" |
| 46 | "\n" |
| 47 | "<source0> ... specify the paths of source files. These paths are\n" |
| 48 | "\tlooked up in the compile command database. If the path of a file is\n" |
| 49 | "\tabsolute, it needs to point into CMake's source tree. If the path is\n" |
| 50 | "\trelative, the current working directory needs to be in the CMake\n" |
| 51 | "\tsource tree and the file must be in a subdirectory of the current\n" |
| 52 | "\tworking directory. \"./\" prefixes in the relative files will be\n" |
| 53 | "\tautomatically removed, but the rest of a relative path must be a\n" |
| 54 | "\tsuffix of a path in the compile command database.\n" |
| 55 | "\n"; |
| 56 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 57 | class ArgumentsAdjustingCompilations : public CompilationDatabase { |
| 58 | public: |
| 59 | ArgumentsAdjustingCompilations( |
| 60 | std::unique_ptr<CompilationDatabase> Compilations) |
| 61 | : Compilations(std::move(Compilations)) {} |
| 62 | |
| 63 | void appendArgumentsAdjuster(std::unique_ptr<ArgumentsAdjuster> Adjuster) { |
| 64 | Adjusters.push_back(std::move(Adjuster)); |
| 65 | } |
| 66 | |
| 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 | |
| 80 | private: |
| 81 | std::unique_ptr<CompilationDatabase> Compilations; |
| 82 | std::vector<std::unique_ptr<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->Adjust(Command.CommandLine); |
| 89 | return Commands; |
| 90 | } |
| 91 | }; |
| 92 | |
Manuel Klimek | 8b1f2f9 | 2013-07-19 12:20:59 +0000 | [diff] [blame] | 93 | CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 94 | cl::OptionCategory &Category, |
Manuel Klimek | 8b1f2f9 | 2013-07-19 12:20:59 +0000 | [diff] [blame] | 95 | const char *Overview) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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)); |
Alexander Kornienko | ffb155f | 2012-08-28 22:15:41 +0000 | [diff] [blame] | 100 | |
| 101 | static cl::list<std::string> SourcePaths( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 102 | cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore, |
| 103 | cl::cat(Category)); |
| 104 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 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 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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 | } |
Alexander Kornienko | ffb155f | 2012-08-28 22:15:41 +0000 | [diff] [blame] | 124 | |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 125 | Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc, |
| 126 | argv)); |
Manuel Klimek | 8b1f2f9 | 2013-07-19 12:20:59 +0000 | [diff] [blame] | 127 | cl::ParseCommandLineOptions(argc, argv, Overview); |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 128 | SourcePathList = SourcePaths; |
| 129 | if (!Compilations) { |
| 130 | std::string ErrorMessage; |
| 131 | if (!BuildPath.empty()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 132 | Compilations = |
| 133 | CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage); |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 134 | } else { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 135 | Compilations = CompilationDatabase::autoDetectFromSource(SourcePaths[0], |
| 136 | ErrorMessage); |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 137 | } |
| 138 | if (!Compilations) |
| 139 | llvm::report_fatal_error(ErrorMessage); |
| 140 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame^] | 141 | auto AdjustingCompilations = |
| 142 | llvm::make_unique<ArgumentsAdjustingCompilations>( |
| 143 | std::move(Compilations)); |
| 144 | AdjustingCompilations->appendArgumentsAdjuster( |
| 145 | llvm::make_unique<InsertArgumentAdjuster>(ArgsBefore, |
| 146 | InsertArgumentAdjuster::BEGIN)); |
| 147 | AdjustingCompilations->appendArgumentsAdjuster( |
| 148 | llvm::make_unique<InsertArgumentAdjuster>(ArgsAfter, |
| 149 | InsertArgumentAdjuster::END)); |
| 150 | Compilations = std::move(AdjustingCompilations); |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 151 | } |