]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Tooling/CommonOptionsParser.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Tooling / CommonOptionsParser.cpp
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/ArgumentsAdjusters.h"
29 #include "clang/Tooling/CommonOptionsParser.h"
30 #include "clang/Tooling/Tooling.h"
31
32 using namespace clang::tooling;
33 using namespace llvm;
34
35 const char *const CommonOptionsParser::HelpMessage =
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"
42     "\ta search for compile_commands.json will be attempted through all\n"
43     "\tparent paths of the first input file . See:\n"
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
57 namespace {
58 class ArgumentsAdjustingCompilations : public CompilationDatabase {
59 public:
60   ArgumentsAdjustingCompilations(
61       std::unique_ptr<CompilationDatabase> Compilations)
62       : Compilations(std::move(Compilations)) {}
63
64   void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) {
65     Adjusters.push_back(std::move(Adjuster));
66   }
67
68   std::vector<CompileCommand>
69   getCompileCommands(StringRef FilePath) const override {
70     return adjustCommands(Compilations->getCompileCommands(FilePath));
71   }
72
73   std::vector<std::string> getAllFiles() const override {
74     return Compilations->getAllFiles();
75   }
76
77   std::vector<CompileCommand> getAllCompileCommands() const override {
78     return adjustCommands(Compilations->getAllCompileCommands());
79   }
80
81 private:
82   std::unique_ptr<CompilationDatabase> Compilations;
83   std::vector<ArgumentsAdjuster> Adjusters;
84
85   std::vector<CompileCommand>
86   adjustCommands(std::vector<CompileCommand> Commands) const {
87     for (CompileCommand &Command : Commands)
88       for (const auto &Adjuster : Adjusters)
89         Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename);
90     return Commands;
91   }
92 };
93 } // namespace
94
95 CommonOptionsParser::CommonOptionsParser(
96     int &argc, const char **argv, cl::OptionCategory &Category,
97     llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
98   static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
99
100   static cl::opt<std::string> BuildPath("p", cl::desc("Build path"),
101                                         cl::Optional, cl::cat(Category));
102
103   static cl::list<std::string> SourcePaths(
104       cl::Positional, cl::desc("<source0> [... <sourceN>]"), OccurrencesFlag,
105       cl::cat(Category));
106
107   static cl::list<std::string> ArgsAfter(
108       "extra-arg",
109       cl::desc("Additional argument to append to the compiler command line"),
110       cl::cat(Category));
111
112   static cl::list<std::string> ArgsBefore(
113       "extra-arg-before",
114       cl::desc("Additional argument to prepend to the compiler command line"),
115       cl::cat(Category));
116
117   cl::HideUnrelatedOptions(Category);
118
119   std::string ErrorMessage;
120   Compilations =
121       FixedCompilationDatabase::loadFromCommandLine(argc, argv, ErrorMessage);
122   if (!Compilations && !ErrorMessage.empty())
123     llvm::errs() << ErrorMessage;
124   cl::ParseCommandLineOptions(argc, argv, Overview);
125   cl::PrintOptionValues();
126
127   SourcePathList = SourcePaths;
128   if ((OccurrencesFlag == cl::ZeroOrMore || OccurrencesFlag == cl::Optional) &&
129       SourcePathList.empty())
130     return;
131   if (!Compilations) {
132     if (!BuildPath.empty()) {
133       Compilations =
134           CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage);
135     } else {
136       Compilations = CompilationDatabase::autoDetectFromSource(SourcePaths[0],
137                                                                ErrorMessage);
138     }
139     if (!Compilations) {
140       llvm::errs() << "Error while trying to load a compilation database:\n"
141                    << ErrorMessage << "Running without flags.\n";
142       Compilations.reset(
143           new FixedCompilationDatabase(".", std::vector<std::string>()));
144     }
145   }
146   auto AdjustingCompilations =
147       llvm::make_unique<ArgumentsAdjustingCompilations>(
148           std::move(Compilations));
149   AdjustingCompilations->appendArgumentsAdjuster(
150       getInsertArgumentAdjuster(ArgsBefore, ArgumentInsertPosition::BEGIN));
151   AdjustingCompilations->appendArgumentsAdjuster(
152       getInsertArgumentAdjuster(ArgsAfter, ArgumentInsertPosition::END));
153   Compilations = std::move(AdjustingCompilations);
154 }