blob: cfb9cc942c885256b7ccc5318611f0ed010f18a3 [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- GlobalCompilationDatabase.cpp --------------------------*- C++-*-===//
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#include "GlobalCompilationDatabase.h"
11#include "clang/Tooling/CompilationDatabase.h"
12#include "llvm/Support/FileSystem.h"
13#include "llvm/Support/Path.h"
Ilya Biryukove5128f72017-09-20 07:24:15 +000014#include "Logger.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000015
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000016namespace clang {
17namespace clangd {
18
19static void addExtraFlags(tooling::CompileCommand &Command,
20 const std::vector<std::string> &ExtraFlags) {
21 if (ExtraFlags.empty())
22 return;
23 assert(Command.CommandLine.size() >= 2 &&
24 "Expected a command line containing at least 2 arguments, the "
25 "compiler binary and the output file");
26 // The last argument of CommandLine is the name of the input file.
27 // Add ExtraFlags before it.
28 auto It = Command.CommandLine.end();
29 --It;
30 Command.CommandLine.insert(It, ExtraFlags.begin(), ExtraFlags.end());
31}
32
33tooling::CompileCommand getDefaultCompileCommand(PathRef File) {
34 std::vector<std::string> CommandLine{"clang", "-fsyntax-only", File.str()};
35 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
36 llvm::sys::path::filename(File), CommandLine,
37 /*Output=*/"");
38}
Ilya Biryukov38d79772017-05-16 09:38:59 +000039
Ilya Biryukove5128f72017-09-20 07:24:15 +000040DirectoryBasedGlobalCompilationDatabase::
41 DirectoryBasedGlobalCompilationDatabase(clangd::Logger &Logger)
42 : Logger(Logger) {}
43
Ilya Biryukov38d79772017-05-16 09:38:59 +000044std::vector<tooling::CompileCommand>
45DirectoryBasedGlobalCompilationDatabase::getCompileCommands(PathRef File) {
46 std::vector<tooling::CompileCommand> Commands;
47
48 auto CDB = getCompilationDatabase(File);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000049 if (CDB)
50 Commands = CDB->getCompileCommands(File);
51 if (Commands.empty())
52 Commands.push_back(getDefaultCompileCommand(File));
53
54 auto It = ExtraFlagsForFile.find(File);
55 if (It != ExtraFlagsForFile.end()) {
56 // Append the user-specified flags to the compile commands.
57 for (tooling::CompileCommand &Command : Commands)
58 addExtraFlags(Command, It->second);
59 }
60
61 return Commands;
62}
63
64void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
65 PathRef File, std::vector<std::string> ExtraFlags) {
66 ExtraFlagsForFile[File] = std::move(ExtraFlags);
Ilya Biryukov38d79772017-05-16 09:38:59 +000067}
68
69tooling::CompilationDatabase *
70DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(PathRef File) {
71 std::lock_guard<std::mutex> Lock(Mutex);
72
73 namespace path = llvm::sys::path;
74
75 assert((path::is_absolute(File, path::Style::posix) ||
76 path::is_absolute(File, path::Style::windows)) &&
77 "path must be absolute");
78
79 for (auto Path = path::parent_path(File); !Path.empty();
80 Path = path::parent_path(Path)) {
81
82 auto CachedIt = CompilationDatabases.find(Path);
83 if (CachedIt != CompilationDatabases.end())
84 return CachedIt->second.get();
Ilya Biryukove5128f72017-09-20 07:24:15 +000085
Ilya Biryukov38d79772017-05-16 09:38:59 +000086 std::string Error;
87 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Path, Error);
Ilya Biryukove5128f72017-09-20 07:24:15 +000088 if (!CDB)
Ilya Biryukov38d79772017-05-16 09:38:59 +000089 continue;
Ilya Biryukov38d79772017-05-16 09:38:59 +000090
91 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
Ilya Biryukove5128f72017-09-20 07:24:15 +000092 auto Result = CDB.get();
Ilya Biryukov38d79772017-05-16 09:38:59 +000093 CompilationDatabases.insert(std::make_pair(Path, std::move(CDB)));
Ilya Biryukove5128f72017-09-20 07:24:15 +000094 return Result;
Ilya Biryukov38d79772017-05-16 09:38:59 +000095 }
96
Ilya Biryukove5128f72017-09-20 07:24:15 +000097 Logger.log("Failed to find compilation database for " + Twine(File) + "\n");
Ilya Biryukov38d79772017-05-16 09:38:59 +000098 return nullptr;
99}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000100
101} // namespace clangd
102} // namespace clang