blob: 6670606a5e0477b836904181ebe76aaa4431aea6 [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- GlobalCompilationDatabase.cpp ---------------------------*- C++-*-===//
Ilya Biryukov38d79772017-05-16 09:38:59 +00002//
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//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00008//===----------------------------------------------------------------------===//
Ilya Biryukov38d79772017-05-16 09:38:59 +00009
10#include "GlobalCompilationDatabase.h"
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000011#include "Logger.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000012#include "clang/Tooling/CompilationDatabase.h"
13#include "llvm/Support/FileSystem.h"
14#include "llvm/Support/Path.h"
15
Sam McCallc008af62018-10-20 15:30:37 +000016using namespace llvm;
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000017namespace clang {
18namespace clangd {
19
Sam McCallecbeab02017-12-04 10:08:45 +000020tooling::CompileCommand
21GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
Sam McCall690dcf12018-04-20 11:35:17 +000022 std::vector<std::string> Argv = {"clang"};
23 // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
24 // Parsing as Objective C++ is friendly to more cases.
Sam McCallc008af62018-10-20 15:30:37 +000025 if (sys::path::extension(File) == ".h")
Sam McCall690dcf12018-04-20 11:35:17 +000026 Argv.push_back("-xobjective-c++-header");
27 Argv.push_back(File);
Sam McCallc008af62018-10-20 15:30:37 +000028 return tooling::CompileCommand(sys::path::parent_path(File),
29 sys::path::filename(File), std::move(Argv),
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000030 /*Output=*/"");
31}
Ilya Biryukov38d79772017-05-16 09:38:59 +000032
Ilya Biryukove5128f72017-09-20 07:24:15 +000033DirectoryBasedGlobalCompilationDatabase::
Sam McCallc008af62018-10-20 15:30:37 +000034 DirectoryBasedGlobalCompilationDatabase(Optional<Path> CompileCommandsDir)
Ilya Biryukov940901e2017-12-13 12:51:22 +000035 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
Ilya Biryukove5128f72017-09-20 07:24:15 +000036
Sam McCall690dcf12018-04-20 11:35:17 +000037DirectoryBasedGlobalCompilationDatabase::
38 ~DirectoryBasedGlobalCompilationDatabase() = default;
39
Sam McCallc008af62018-10-20 15:30:37 +000040Optional<tooling::CompileCommand>
Sam McCallecbeab02017-12-04 10:08:45 +000041DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
Sam McCallc02ba722017-12-22 09:47:34 +000042 if (auto CDB = getCDBForFile(File)) {
Sam McCallecbeab02017-12-04 10:08:45 +000043 auto Candidates = CDB->getCompileCommands(File);
44 if (!Candidates.empty()) {
45 addExtraFlags(File, Candidates.front());
46 return std::move(Candidates.front());
47 }
Sam McCallc02ba722017-12-22 09:47:34 +000048 } else {
Sam McCallbed58852018-07-11 10:35:11 +000049 log("Failed to find compilation database for {0}", File);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000050 }
Sam McCallc008af62018-10-20 15:30:37 +000051 return None;
Sam McCallecbeab02017-12-04 10:08:45 +000052}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000053
Sam McCallecbeab02017-12-04 10:08:45 +000054tooling::CompileCommand
55DirectoryBasedGlobalCompilationDatabase::getFallbackCommand(
56 PathRef File) const {
57 auto C = GlobalCompilationDatabase::getFallbackCommand(File);
58 addExtraFlags(File, C);
59 return C;
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000060}
61
Simon Marchi5178f922018-02-22 14:00:39 +000062void DirectoryBasedGlobalCompilationDatabase::setCompileCommandsDir(Path P) {
63 std::lock_guard<std::mutex> Lock(Mutex);
64 CompileCommandsDir = P;
65 CompilationDatabases.clear();
66}
67
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000068void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
69 PathRef File, std::vector<std::string> ExtraFlags) {
Sam McCallecbeab02017-12-04 10:08:45 +000070 std::lock_guard<std::mutex> Lock(Mutex);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000071 ExtraFlagsForFile[File] = std::move(ExtraFlags);
Ilya Biryukov38d79772017-05-16 09:38:59 +000072}
73
Sam McCallecbeab02017-12-04 10:08:45 +000074void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
75 PathRef File, tooling::CompileCommand &C) const {
76 std::lock_guard<std::mutex> Lock(Mutex);
77
78 auto It = ExtraFlagsForFile.find(File);
79 if (It == ExtraFlagsForFile.end())
80 return;
81
82 auto &Args = C.CommandLine;
83 assert(Args.size() >= 2 && "Expected at least [compiler, source file]");
84 // The last argument of CommandLine is the name of the input file.
85 // Add ExtraFlags before it.
86 Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
87}
88
Ilya Biryukov38d79772017-05-16 09:38:59 +000089tooling::CompilationDatabase *
Sam McCallc02ba722017-12-22 09:47:34 +000090DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
91 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
92 auto CachedIt = CompilationDatabases.find(Dir);
93 if (CachedIt != CompilationDatabases.end())
94 return CachedIt->second.get();
95 std::string Error = "";
96 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
97 auto Result = CDB.get();
98 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
99 return Result;
100}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000101
Sam McCallc02ba722017-12-22 09:47:34 +0000102tooling::CompilationDatabase *
103DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
Sam McCallc008af62018-10-20 15:30:37 +0000104 namespace path = sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000105 assert((path::is_absolute(File, path::Style::posix) ||
106 path::is_absolute(File, path::Style::windows)) &&
107 "path must be absolute");
108
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000109 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCallc02ba722017-12-22 09:47:34 +0000110 if (CompileCommandsDir)
111 return getCDBInDirLocked(*CompileCommandsDir);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000112 for (auto Path = path::parent_path(File); !Path.empty();
Sam McCallc02ba722017-12-22 09:47:34 +0000113 Path = path::parent_path(Path))
114 if (auto CDB = getCDBInDirLocked(Path))
115 return CDB;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000116 return nullptr;
117}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000118
Ilya Biryukovb10ef472018-06-13 09:20:41 +0000119CachingCompilationDb::CachingCompilationDb(
120 const GlobalCompilationDatabase &InnerCDB)
121 : InnerCDB(InnerCDB) {}
122
Sam McCallc008af62018-10-20 15:30:37 +0000123Optional<tooling::CompileCommand>
Ilya Biryukovb10ef472018-06-13 09:20:41 +0000124CachingCompilationDb::getCompileCommand(PathRef File) const {
125 std::unique_lock<std::mutex> Lock(Mut);
126 auto It = Cached.find(File);
127 if (It != Cached.end())
128 return It->second;
129
130 Lock.unlock();
Sam McCallc008af62018-10-20 15:30:37 +0000131 Optional<tooling::CompileCommand> Command = InnerCDB.getCompileCommand(File);
Ilya Biryukovb10ef472018-06-13 09:20:41 +0000132 Lock.lock();
133 return Cached.try_emplace(File, std::move(Command)).first->getValue();
134}
135
136tooling::CompileCommand
137CachingCompilationDb::getFallbackCommand(PathRef File) const {
138 return InnerCDB.getFallbackCommand(File);
139}
140
141void CachingCompilationDb::invalidate(PathRef File) {
142 std::unique_lock<std::mutex> Lock(Mut);
143 Cached.erase(File);
144}
145
146void CachingCompilationDb::clear() {
147 std::unique_lock<std::mutex> Lock(Mut);
148 Cached.clear();
149}
150
Sam McCallc008af62018-10-20 15:30:37 +0000151Optional<tooling::CompileCommand>
Alex Lorenzf8087862018-08-01 17:39:29 +0000152InMemoryCompilationDb::getCompileCommand(PathRef File) const {
153 std::lock_guard<std::mutex> Lock(Mutex);
154 auto It = Commands.find(File);
155 if (It == Commands.end())
156 return None;
157 return It->second;
158}
159
160bool InMemoryCompilationDb::setCompilationCommandForFile(
161 PathRef File, tooling::CompileCommand CompilationCommand) {
162 std::unique_lock<std::mutex> Lock(Mutex);
163 auto ItInserted = Commands.insert(std::make_pair(File, CompilationCommand));
164 if (ItInserted.second)
165 return true;
166 ItInserted.first->setValue(std::move(CompilationCommand));
167 return false;
168}
169
170void InMemoryCompilationDb::invalidate(PathRef File) {
171 std::unique_lock<std::mutex> Lock(Mutex);
172 Commands.erase(File);
173}
174
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000175} // namespace clangd
176} // namespace clang