blob: a21ea5231dc0d08cc588cf162fde076735e2f902 [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);
Sam McCall2eb6b402018-11-02 13:06:55 +000044 if (!Candidates.empty())
Sam McCallecbeab02017-12-04 10:08:45 +000045 return std::move(Candidates.front());
Sam McCallc02ba722017-12-22 09:47:34 +000046 } else {
Sam McCallbed58852018-07-11 10:35:11 +000047 log("Failed to find compilation database for {0}", File);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000048 }
Sam McCallc008af62018-10-20 15:30:37 +000049 return None;
Sam McCallecbeab02017-12-04 10:08:45 +000050}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000051
Sam McCall2bebc3d2018-11-20 10:56:03 +000052std::pair<tooling::CompilationDatabase *, /*Cached*/ bool>
Sam McCallc02ba722017-12-22 09:47:34 +000053DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
54 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
55 auto CachedIt = CompilationDatabases.find(Dir);
56 if (CachedIt != CompilationDatabases.end())
Sam McCall2bebc3d2018-11-20 10:56:03 +000057 return {CachedIt->second.get(), true};
Sam McCallc02ba722017-12-22 09:47:34 +000058 std::string Error = "";
59 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
60 auto Result = CDB.get();
61 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
Sam McCall2bebc3d2018-11-20 10:56:03 +000062 return {Result, false};
Sam McCallc02ba722017-12-22 09:47:34 +000063}
Ilya Biryukov38d79772017-05-16 09:38:59 +000064
Sam McCallc02ba722017-12-22 09:47:34 +000065tooling::CompilationDatabase *
66DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
Sam McCallc008af62018-10-20 15:30:37 +000067 namespace path = sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +000068 assert((path::is_absolute(File, path::Style::posix) ||
69 path::is_absolute(File, path::Style::windows)) &&
70 "path must be absolute");
71
Sam McCall2bebc3d2018-11-20 10:56:03 +000072 tooling::CompilationDatabase *CDB = nullptr;
73 bool Cached = false;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000074 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCall2bebc3d2018-11-20 10:56:03 +000075 if (CompileCommandsDir) {
76 std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir);
77 } else {
78 for (auto Path = path::parent_path(File); !CDB && !Path.empty();
79 Path = path::parent_path(Path)) {
80 std::tie(CDB, Cached) = getCDBInDirLocked(Path);
81 }
82 }
83 if (CDB && !Cached)
84 OnCommandChanged.broadcast(CDB->getAllFiles());
85 return CDB;
86}
87
88OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
89 std::vector<std::string> FallbackFlags)
90 : Base(Base), FallbackFlags(std::move(FallbackFlags)) {
91 if (Base)
92 BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
93 OnCommandChanged.broadcast(Changes);
94 });
Ilya Biryukov38d79772017-05-16 09:38:59 +000095}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000096
Sam McCallc008af62018-10-20 15:30:37 +000097Optional<tooling::CompileCommand>
Sam McCallc55d09a2018-11-02 13:09:36 +000098OverlayCDB::getCompileCommand(PathRef File) const {
99 {
100 std::lock_guard<std::mutex> Lock(Mutex);
101 auto It = Commands.find(File);
102 if (It != Commands.end())
103 return It->second;
104 }
105 return Base ? Base->getCompileCommand(File) : None;
Alex Lorenzf8087862018-08-01 17:39:29 +0000106}
107
Sam McCallc55d09a2018-11-02 13:09:36 +0000108tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
109 auto Cmd = Base ? Base->getFallbackCommand(File)
110 : GlobalCompilationDatabase::getFallbackCommand(File);
111 std::lock_guard<std::mutex> Lock(Mutex);
112 Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
113 FallbackFlags.end());
114 return Cmd;
115}
116
117void OverlayCDB::setCompileCommand(
118 PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
Sam McCall2bebc3d2018-11-20 10:56:03 +0000119 {
120 std::unique_lock<std::mutex> Lock(Mutex);
121 if (Cmd)
122 Commands[File] = std::move(*Cmd);
123 else
124 Commands.erase(File);
125 }
126 OnCommandChanged.broadcast({File});
Alex Lorenzf8087862018-08-01 17:39:29 +0000127}
128
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000129} // namespace clangd
130} // namespace clang