]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp
Update clang to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Tooling / CompilationDatabase.cpp
1 //===--- CompilationDatabase.cpp - ----------------------------------------===//
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 contains implementations of the CompilationDatabase base class
11 //  and the FixedCompilationDatabase.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/Tooling/CompilationDatabase.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/DiagnosticOptions.h"
18 #include "clang/Driver/Action.h"
19 #include "clang/Driver/Compilation.h"
20 #include "clang/Driver/Driver.h"
21 #include "clang/Driver/DriverDiagnostic.h"
22 #include "clang/Driver/Job.h"
23 #include "clang/Frontend/TextDiagnosticPrinter.h"
24 #include "clang/Tooling/CompilationDatabasePluginRegistry.h"
25 #include "clang/Tooling/Tooling.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Option/Arg.h"
28 #include "llvm/Support/Host.h"
29 #include "llvm/Support/Path.h"
30 #include <sstream>
31 #include <system_error>
32 using namespace clang;
33 using namespace tooling;
34
35 LLVM_INSTANTIATE_REGISTRY(CompilationDatabasePluginRegistry)
36
37 CompilationDatabase::~CompilationDatabase() {}
38
39 std::unique_ptr<CompilationDatabase>
40 CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
41                                        std::string &ErrorMessage) {
42   llvm::raw_string_ostream ErrorStream(ErrorMessage);
43   for (CompilationDatabasePluginRegistry::iterator
44        It = CompilationDatabasePluginRegistry::begin(),
45        Ie = CompilationDatabasePluginRegistry::end();
46        It != Ie; ++It) {
47     std::string DatabaseErrorMessage;
48     std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
49     if (std::unique_ptr<CompilationDatabase> DB =
50             Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
51       return DB;
52     ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n";
53   }
54   return nullptr;
55 }
56
57 static std::unique_ptr<CompilationDatabase>
58 findCompilationDatabaseFromDirectory(StringRef Directory,
59                                      std::string &ErrorMessage) {
60   std::stringstream ErrorStream;
61   bool HasErrorMessage = false;
62   while (!Directory.empty()) {
63     std::string LoadErrorMessage;
64
65     if (std::unique_ptr<CompilationDatabase> DB =
66             CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage))
67       return DB;
68
69     if (!HasErrorMessage) {
70       ErrorStream << "No compilation database found in " << Directory.str()
71                   << " or any parent directory\n" << LoadErrorMessage;
72       HasErrorMessage = true;
73     }
74
75     Directory = llvm::sys::path::parent_path(Directory);
76   }
77   ErrorMessage = ErrorStream.str();
78   return nullptr;
79 }
80
81 std::unique_ptr<CompilationDatabase>
82 CompilationDatabase::autoDetectFromSource(StringRef SourceFile,
83                                           std::string &ErrorMessage) {
84   SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile));
85   StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
86
87   std::unique_ptr<CompilationDatabase> DB =
88       findCompilationDatabaseFromDirectory(Directory, ErrorMessage);
89
90   if (!DB)
91     ErrorMessage = ("Could not auto-detect compilation database for file \"" +
92                    SourceFile + "\"\n" + ErrorMessage).str();
93   return DB;
94 }
95
96 std::unique_ptr<CompilationDatabase>
97 CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir,
98                                              std::string &ErrorMessage) {
99   SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir));
100
101   std::unique_ptr<CompilationDatabase> DB =
102       findCompilationDatabaseFromDirectory(AbsolutePath, ErrorMessage);
103
104   if (!DB)
105     ErrorMessage = ("Could not auto-detect compilation database from directory \"" +
106                    SourceDir + "\"\n" + ErrorMessage).str();
107   return DB;
108 }
109
110 CompilationDatabasePlugin::~CompilationDatabasePlugin() {}
111
112 namespace {
113 // Helper for recursively searching through a chain of actions and collecting
114 // all inputs, direct and indirect, of compile jobs.
115 struct CompileJobAnalyzer {
116   void run(const driver::Action *A) {
117     runImpl(A, false);
118   }
119
120   SmallVector<std::string, 2> Inputs;
121
122 private:
123
124   void runImpl(const driver::Action *A, bool Collect) {
125     bool CollectChildren = Collect;
126     switch (A->getKind()) {
127     case driver::Action::CompileJobClass:
128       CollectChildren = true;
129       break;
130
131     case driver::Action::InputClass: {
132       if (Collect) {
133         const driver::InputAction *IA = cast<driver::InputAction>(A);
134         Inputs.push_back(IA->getInputArg().getSpelling());
135       }
136     } break;
137
138     default:
139       // Don't care about others
140       ;
141     }
142
143     for (const driver::Action *AI : A->inputs())
144       runImpl(AI, CollectChildren);
145   }
146 };
147
148 // Special DiagnosticConsumer that looks for warn_drv_input_file_unused
149 // diagnostics from the driver and collects the option strings for those unused
150 // options.
151 class UnusedInputDiagConsumer : public DiagnosticConsumer {
152 public:
153   UnusedInputDiagConsumer() : Other(nullptr) {}
154
155   // Useful for debugging, chain diagnostics to another consumer after
156   // recording for our own purposes.
157   UnusedInputDiagConsumer(DiagnosticConsumer *Other) : Other(Other) {}
158
159   void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
160                         const Diagnostic &Info) override {
161     if (Info.getID() == clang::diag::warn_drv_input_file_unused) {
162       // Arg 1 for this diagnostic is the option that didn't get used.
163       UnusedInputs.push_back(Info.getArgStdStr(0));
164     }
165     if (Other)
166       Other->HandleDiagnostic(DiagLevel, Info);
167   }
168
169   DiagnosticConsumer *Other;
170   SmallVector<std::string, 2> UnusedInputs;
171 };
172
173 // Unary functor for asking "Given a StringRef S1, does there exist a string
174 // S2 in Arr where S1 == S2?"
175 struct MatchesAny {
176   MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {}
177   bool operator() (StringRef S) {
178     for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I)
179       if (*I == S)
180         return true;
181     return false;
182   }
183 private:
184   ArrayRef<std::string> Arr;
185 };
186 } // namespace
187
188 /// \brief Strips any positional args and possible argv[0] from a command-line
189 /// provided by the user to construct a FixedCompilationDatabase.
190 ///
191 /// FixedCompilationDatabase requires a command line to be in this format as it
192 /// constructs the command line for each file by appending the name of the file
193 /// to be compiled. FixedCompilationDatabase also adds its own argv[0] to the
194 /// start of the command line although its value is not important as it's just
195 /// ignored by the Driver invoked by the ClangTool using the
196 /// FixedCompilationDatabase.
197 ///
198 /// FIXME: This functionality should probably be made available by
199 /// clang::driver::Driver although what the interface should look like is not
200 /// clear.
201 ///
202 /// \param[in] Args Args as provided by the user.
203 /// \return Resulting stripped command line.
204 ///          \li true if successful.
205 ///          \li false if \c Args cannot be used for compilation jobs (e.g.
206 ///          contains an option like -E or -version).
207 static bool stripPositionalArgs(std::vector<const char *> Args,
208                                 std::vector<std::string> &Result) {
209   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
210   UnusedInputDiagConsumer DiagClient;
211   DiagnosticsEngine Diagnostics(
212       IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()),
213       &*DiagOpts, &DiagClient, false);
214
215   // The clang executable path isn't required since the jobs the driver builds
216   // will not be executed.
217   std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
218       /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(),
219       Diagnostics));
220   NewDriver->setCheckInputsExist(false);
221
222   // This becomes the new argv[0]. The value is actually not important as it
223   // isn't used for invoking Tools.
224   Args.insert(Args.begin(), "clang-tool");
225
226   // By adding -c, we force the driver to treat compilation as the last phase.
227   // It will then issue warnings via Diagnostics about un-used options that
228   // would have been used for linking. If the user provided a compiler name as
229   // the original argv[0], this will be treated as a linker input thanks to
230   // insertng a new argv[0] above. All un-used options get collected by
231   // UnusedInputdiagConsumer and get stripped out later.
232   Args.push_back("-c");
233
234   // Put a dummy C++ file on to ensure there's at least one compile job for the
235   // driver to construct. If the user specified some other argument that
236   // prevents compilation, e.g. -E or something like -version, we may still end
237   // up with no jobs but then this is the user's fault.
238   Args.push_back("placeholder.cpp");
239
240   // Remove -no-integrated-as; it's not used for syntax checking,
241   // and it confuses targets which don't support this option.
242   Args.erase(std::remove_if(Args.begin(), Args.end(),
243                             MatchesAny(std::string("-no-integrated-as"))),
244              Args.end());
245
246   const std::unique_ptr<driver::Compilation> Compilation(
247       NewDriver->BuildCompilation(Args));
248
249   const driver::JobList &Jobs = Compilation->getJobs();
250
251   CompileJobAnalyzer CompileAnalyzer;
252
253   for (const auto &Cmd : Jobs) {
254     // Collect only for Assemble jobs. If we do all jobs we get duplicates
255     // since Link jobs point to Assemble jobs as inputs.
256     if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass)
257       CompileAnalyzer.run(&Cmd.getSource());
258   }
259
260   if (CompileAnalyzer.Inputs.empty()) {
261     // No compile jobs found.
262     // FIXME: Emit a warning of some kind?
263     return false;
264   }
265
266   // Remove all compilation input files from the command line. This is
267   // necessary so that getCompileCommands() can construct a command line for
268   // each file.
269   std::vector<const char *>::iterator End = std::remove_if(
270       Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
271
272   // Remove all inputs deemed unused for compilation.
273   End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs));
274
275   // Remove the -c add above as well. It will be at the end right now.
276   assert(strcmp(*(End - 1), "-c") == 0);
277   --End;
278
279   Result = std::vector<std::string>(Args.begin() + 1, End);
280   return true;
281 }
282
283 FixedCompilationDatabase *FixedCompilationDatabase::loadFromCommandLine(
284     int &Argc, const char *const *Argv, Twine Directory) {
285   const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--"));
286   if (DoubleDash == Argv + Argc)
287     return nullptr;
288   std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
289   Argc = DoubleDash - Argv;
290
291   std::vector<std::string> StrippedArgs;
292   if (!stripPositionalArgs(CommandLine, StrippedArgs))
293     return nullptr;
294   return new FixedCompilationDatabase(Directory, StrippedArgs);
295 }
296
297 FixedCompilationDatabase::
298 FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) {
299   std::vector<std::string> ToolCommandLine(1, "clang-tool");
300   ToolCommandLine.insert(ToolCommandLine.end(),
301                          CommandLine.begin(), CommandLine.end());
302   CompileCommands.emplace_back(Directory, StringRef(),
303                                std::move(ToolCommandLine),
304                                StringRef());
305 }
306
307 std::vector<CompileCommand>
308 FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const {
309   std::vector<CompileCommand> Result(CompileCommands);
310   Result[0].CommandLine.push_back(FilePath);
311   Result[0].Filename = FilePath;
312   return Result;
313 }
314
315 std::vector<std::string>
316 FixedCompilationDatabase::getAllFiles() const {
317   return std::vector<std::string>();
318 }
319
320 std::vector<CompileCommand>
321 FixedCompilationDatabase::getAllCompileCommands() const {
322   return std::vector<CompileCommand>();
323 }
324
325 namespace clang {
326 namespace tooling {
327
328 // This anchor is used to force the linker to link in the generated object file
329 // and thus register the JSONCompilationDatabasePlugin.
330 extern volatile int JSONAnchorSource;
331 static int LLVM_ATTRIBUTE_UNUSED JSONAnchorDest = JSONAnchorSource;
332
333 } // end namespace tooling
334 } // end namespace clang