Alexander Kornienko | 0138726 | 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" |
| 28 | #include "clang/Tooling/CommonOptionsParser.h" |
| 29 | #include "clang/Tooling/Tooling.h" |
| 30 | |
| 31 | using namespace clang::tooling; |
| 32 | using namespace llvm; |
| 33 | |
Alexander Kornienko | 4bcd58b | 2012-08-24 00:39:14 +0000 | [diff] [blame] | 34 | const char *const CommonOptionsParser::HelpMessage = |
Alexander Kornienko | 0138726 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 35 | "\n" |
| 36 | "-p <build-path> is used to read a compile command database.\n" |
| 37 | "\n" |
| 38 | "\tFor example, it can be a CMake build directory in which a file named\n" |
| 39 | "\tcompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n" |
| 40 | "\tCMake option to get this output). When no build path is specified,\n" |
Edwin Vane | 524741f | 2012-12-14 18:58:25 +0000 | [diff] [blame] | 41 | "\ta search for compile_commands.json will be attempted through all\n" |
| 42 | "\tparent paths of the first input file . See:\n" |
Alexander Kornienko | 0138726 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 43 | "\thttp://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an\n" |
| 44 | "\texample of setting up Clang Tooling on a source tree.\n" |
| 45 | "\n" |
| 46 | "<source0> ... specify the paths of source files. These paths are\n" |
| 47 | "\tlooked up in the compile command database. If the path of a file is\n" |
| 48 | "\tabsolute, it needs to point into CMake's source tree. If the path is\n" |
| 49 | "\trelative, the current working directory needs to be in the CMake\n" |
| 50 | "\tsource tree and the file must be in a subdirectory of the current\n" |
| 51 | "\tworking directory. \"./\" prefixes in the relative files will be\n" |
| 52 | "\tautomatically removed, but the rest of a relative path must be a\n" |
| 53 | "\tsuffix of a path in the compile command database.\n" |
| 54 | "\n"; |
| 55 | |
Johannes Altmanninger | 5d2fd55 | 2017-08-18 16:21:08 +0000 | [diff] [blame] | 56 | void ArgumentsAdjustingCompilations::appendArgumentsAdjuster( |
| 57 | ArgumentsAdjuster Adjuster) { |
| 58 | Adjusters.push_back(std::move(Adjuster)); |
| 59 | } |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 60 | |
Johannes Altmanninger | 5d2fd55 | 2017-08-18 16:21:08 +0000 | [diff] [blame] | 61 | std::vector<CompileCommand> ArgumentsAdjustingCompilations::getCompileCommands( |
| 62 | StringRef FilePath) const { |
| 63 | return adjustCommands(Compilations->getCompileCommands(FilePath)); |
| 64 | } |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 65 | |
Johannes Altmanninger | 5d2fd55 | 2017-08-18 16:21:08 +0000 | [diff] [blame] | 66 | std::vector<std::string> |
| 67 | ArgumentsAdjustingCompilations::getAllFiles() const { |
| 68 | return Compilations->getAllFiles(); |
| 69 | } |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 70 | |
Johannes Altmanninger | 5d2fd55 | 2017-08-18 16:21:08 +0000 | [diff] [blame] | 71 | std::vector<CompileCommand> |
| 72 | ArgumentsAdjustingCompilations::getAllCompileCommands() const { |
| 73 | return adjustCommands(Compilations->getAllCompileCommands()); |
| 74 | } |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 75 | |
Johannes Altmanninger | 5d2fd55 | 2017-08-18 16:21:08 +0000 | [diff] [blame] | 76 | std::vector<CompileCommand> ArgumentsAdjustingCompilations::adjustCommands( |
| 77 | std::vector<CompileCommand> Commands) const { |
| 78 | for (CompileCommand &Command : Commands) |
| 79 | for (const auto &Adjuster : Adjusters) |
| 80 | Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename); |
| 81 | return Commands; |
| 82 | } |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 83 | |
Alexander Kornienko | 3474b55 | 2015-08-17 10:01:42 +0000 | [diff] [blame] | 84 | CommonOptionsParser::CommonOptionsParser( |
| 85 | int &argc, const char **argv, cl::OptionCategory &Category, |
| 86 | llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) { |
Alex Lorenz | 3d712c4 | 2017-09-14 13:16:14 +0000 | [diff] [blame^] | 87 | static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden, |
| 88 | cl::sub(*cl::AllSubCommands)); |
Alexander Kornienko | b5e774e | 2013-12-12 09:59:42 +0000 | [diff] [blame] | 89 | |
| 90 | static cl::opt<std::string> BuildPath("p", cl::desc("Build path"), |
Alex Lorenz | 3d712c4 | 2017-09-14 13:16:14 +0000 | [diff] [blame^] | 91 | cl::Optional, cl::cat(Category), |
| 92 | cl::sub(*cl::AllSubCommands)); |
Alexander Kornienko | 9ed8c4e | 2012-08-28 22:15:41 +0000 | [diff] [blame] | 93 | |
| 94 | static cl::list<std::string> SourcePaths( |
Alexander Kornienko | 3474b55 | 2015-08-17 10:01:42 +0000 | [diff] [blame] | 95 | cl::Positional, cl::desc("<source0> [... <sourceN>]"), OccurrencesFlag, |
Alex Lorenz | 3d712c4 | 2017-09-14 13:16:14 +0000 | [diff] [blame^] | 96 | cl::cat(Category), cl::sub(*cl::AllSubCommands)); |
Alexander Kornienko | b5e774e | 2013-12-12 09:59:42 +0000 | [diff] [blame] | 97 | |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 98 | static cl::list<std::string> ArgsAfter( |
| 99 | "extra-arg", |
| 100 | cl::desc("Additional argument to append to the compiler command line"), |
Alex Lorenz | 3d712c4 | 2017-09-14 13:16:14 +0000 | [diff] [blame^] | 101 | cl::cat(Category), cl::sub(*cl::AllSubCommands)); |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 102 | |
| 103 | static cl::list<std::string> ArgsBefore( |
| 104 | "extra-arg-before", |
| 105 | cl::desc("Additional argument to prepend to the compiler command line"), |
Alex Lorenz | 3d712c4 | 2017-09-14 13:16:14 +0000 | [diff] [blame^] | 106 | cl::cat(Category), cl::sub(*cl::AllSubCommands)); |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 107 | |
Chris Bieneman | 0a9f607 | 2015-01-21 23:26:11 +0000 | [diff] [blame] | 108 | cl::HideUnrelatedOptions(Category); |
Alexander Kornienko | 9ed8c4e | 2012-08-28 22:15:41 +0000 | [diff] [blame] | 109 | |
Serge Pavlov | c46064c | 2017-05-24 11:57:37 +0000 | [diff] [blame] | 110 | std::string ErrorMessage; |
| 111 | Compilations = |
| 112 | FixedCompilationDatabase::loadFromCommandLine(argc, argv, ErrorMessage); |
| 113 | if (!Compilations && !ErrorMessage.empty()) |
| 114 | llvm::errs() << ErrorMessage; |
Manuel Klimek | 7a69851 | 2013-07-19 12:20:59 +0000 | [diff] [blame] | 115 | cl::ParseCommandLineOptions(argc, argv, Overview); |
Alexander Kornienko | c1694c6 | 2016-02-23 10:29:02 +0000 | [diff] [blame] | 116 | cl::PrintOptionValues(); |
| 117 | |
Alexander Kornienko | 0138726 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 118 | SourcePathList = SourcePaths; |
Alexander Kornienko | 3474b55 | 2015-08-17 10:01:42 +0000 | [diff] [blame] | 119 | if ((OccurrencesFlag == cl::ZeroOrMore || OccurrencesFlag == cl::Optional) && |
| 120 | SourcePathList.empty()) |
| 121 | return; |
Alexander Kornienko | 0138726 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 122 | if (!Compilations) { |
Alexander Kornienko | 0138726 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 123 | if (!BuildPath.empty()) { |
David Blaikie | cdba84c | 2014-08-08 16:06:15 +0000 | [diff] [blame] | 124 | Compilations = |
| 125 | CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage); |
Alexander Kornienko | 0138726 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 126 | } else { |
David Blaikie | cdba84c | 2014-08-08 16:06:15 +0000 | [diff] [blame] | 127 | Compilations = CompilationDatabase::autoDetectFromSource(SourcePaths[0], |
| 128 | ErrorMessage); |
Alexander Kornienko | 0138726 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 129 | } |
Manuel Klimek | 6003384 | 2015-12-03 10:38:53 +0000 | [diff] [blame] | 130 | if (!Compilations) { |
| 131 | llvm::errs() << "Error while trying to load a compilation database:\n" |
| 132 | << ErrorMessage << "Running without flags.\n"; |
| 133 | Compilations.reset( |
| 134 | new FixedCompilationDatabase(".", std::vector<std::string>())); |
| 135 | } |
Alexander Kornienko | 0138726 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 136 | } |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 137 | auto AdjustingCompilations = |
| 138 | llvm::make_unique<ArgumentsAdjustingCompilations>( |
| 139 | std::move(Compilations)); |
| 140 | AdjustingCompilations->appendArgumentsAdjuster( |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 141 | getInsertArgumentAdjuster(ArgsBefore, ArgumentInsertPosition::BEGIN)); |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 142 | AdjustingCompilations->appendArgumentsAdjuster( |
Alexander Kornienko | 74e1c46 | 2014-12-03 17:53:02 +0000 | [diff] [blame] | 143 | getInsertArgumentAdjuster(ArgsAfter, ArgumentInsertPosition::END)); |
Alexander Kornienko | 6168609 | 2014-11-04 08:51:24 +0000 | [diff] [blame] | 144 | Compilations = std::move(AdjustingCompilations); |
Alexander Kornienko | 0138726 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 145 | } |