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" |
| 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 | 6fbe982 | 2012-08-24 00:39:14 +0000 | [diff] [blame] | 34 | const char *const CommonOptionsParser::HelpMessage = |
Alexander Kornienko | d7166b0 | 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 | b1f67db | 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 | d7166b0 | 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 | |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 56 | CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv) { |
Alexander Kornienko | ffb155f | 2012-08-28 22:15:41 +0000 | [diff] [blame] | 57 | static cl::opt<std::string> BuildPath( |
| 58 | "p", cl::desc("Build path"), cl::Optional); |
| 59 | |
| 60 | static cl::list<std::string> SourcePaths( |
| 61 | cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore); |
| 62 | |
Alexander Kornienko | d7166b0 | 2012-08-22 20:52:52 +0000 | [diff] [blame] | 63 | Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc, |
| 64 | argv)); |
| 65 | cl::ParseCommandLineOptions(argc, argv); |
| 66 | SourcePathList = SourcePaths; |
| 67 | if (!Compilations) { |
| 68 | std::string ErrorMessage; |
| 69 | if (!BuildPath.empty()) { |
| 70 | Compilations.reset(CompilationDatabase::autoDetectFromDirectory( |
| 71 | BuildPath, ErrorMessage)); |
| 72 | } else { |
| 73 | Compilations.reset(CompilationDatabase::autoDetectFromSource( |
| 74 | SourcePaths[0], ErrorMessage)); |
| 75 | } |
| 76 | if (!Compilations) |
| 77 | llvm::report_fatal_error(ErrorMessage); |
| 78 | } |
| 79 | } |