blob: c2fff7b20f37bb2f43ae5d8748fe890d98803a2d [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
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000016namespace clang {
17namespace clangd {
18
Haojian Wue1ced5c2019-01-07 12:35:02 +000019static std::string getFallbackClangPath() {
20 static int Dummy;
21 std::string ClangdExecutable =
22 llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy);
23 SmallString<128> ClangPath;
24 ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
25 llvm::sys::path::append(ClangPath, "clang");
26 return ClangPath.str();
27}
28
Sam McCallecbeab02017-12-04 10:08:45 +000029tooling::CompileCommand
30GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
Haojian Wue1ced5c2019-01-07 12:35:02 +000031 std::vector<std::string> Argv = {getFallbackClangPath()};
Sam McCall690dcf12018-04-20 11:35:17 +000032 // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
33 // Parsing as Objective C++ is friendly to more cases.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000034 if (llvm::sys::path::extension(File) == ".h")
Sam McCall690dcf12018-04-20 11:35:17 +000035 Argv.push_back("-xobjective-c++-header");
36 Argv.push_back(File);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000037 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
38 llvm::sys::path::filename(File),
39 std::move(Argv),
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000040 /*Output=*/"");
41}
Ilya Biryukov38d79772017-05-16 09:38:59 +000042
Ilya Biryukove5128f72017-09-20 07:24:15 +000043DirectoryBasedGlobalCompilationDatabase::
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000044 DirectoryBasedGlobalCompilationDatabase(
45 llvm::Optional<Path> CompileCommandsDir)
Ilya Biryukov940901e2017-12-13 12:51:22 +000046 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
Ilya Biryukove5128f72017-09-20 07:24:15 +000047
Sam McCall690dcf12018-04-20 11:35:17 +000048DirectoryBasedGlobalCompilationDatabase::
49 ~DirectoryBasedGlobalCompilationDatabase() = default;
50
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000051llvm::Optional<tooling::CompileCommand>
Sam McCall6e2d2a32018-11-26 09:51:50 +000052DirectoryBasedGlobalCompilationDatabase::getCompileCommand(
53 PathRef File, ProjectInfo *Project) const {
54 if (auto CDB = getCDBForFile(File, Project)) {
Sam McCallecbeab02017-12-04 10:08:45 +000055 auto Candidates = CDB->getCompileCommands(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +000056 if (!Candidates.empty()) {
Sam McCallecbeab02017-12-04 10:08:45 +000057 return std::move(Candidates.front());
Sam McCall6e2d2a32018-11-26 09:51:50 +000058 }
Sam McCallc02ba722017-12-22 09:47:34 +000059 } else {
Sam McCallbed58852018-07-11 10:35:11 +000060 log("Failed to find compilation database for {0}", File);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000061 }
Sam McCallc008af62018-10-20 15:30:37 +000062 return None;
Sam McCallecbeab02017-12-04 10:08:45 +000063}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000064
Sam McCall2bebc3d2018-11-20 10:56:03 +000065std::pair<tooling::CompilationDatabase *, /*Cached*/ bool>
Sam McCallc02ba722017-12-22 09:47:34 +000066DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
67 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
68 auto CachedIt = CompilationDatabases.find(Dir);
69 if (CachedIt != CompilationDatabases.end())
Sam McCall2bebc3d2018-11-20 10:56:03 +000070 return {CachedIt->second.get(), true};
Sam McCallc02ba722017-12-22 09:47:34 +000071 std::string Error = "";
72 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
73 auto Result = CDB.get();
74 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
Sam McCall2bebc3d2018-11-20 10:56:03 +000075 return {Result, false};
Sam McCallc02ba722017-12-22 09:47:34 +000076}
Ilya Biryukov38d79772017-05-16 09:38:59 +000077
Sam McCallc02ba722017-12-22 09:47:34 +000078tooling::CompilationDatabase *
Sam McCall6e2d2a32018-11-26 09:51:50 +000079DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
80 PathRef File, ProjectInfo *Project) const {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000081 namespace path = llvm::sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +000082 assert((path::is_absolute(File, path::Style::posix) ||
83 path::is_absolute(File, path::Style::windows)) &&
84 "path must be absolute");
85
Sam McCall2bebc3d2018-11-20 10:56:03 +000086 tooling::CompilationDatabase *CDB = nullptr;
87 bool Cached = false;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000088 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCall2bebc3d2018-11-20 10:56:03 +000089 if (CompileCommandsDir) {
90 std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir);
Sam McCall6e2d2a32018-11-26 09:51:50 +000091 if (Project && CDB)
92 Project->SourceRoot = *CompileCommandsDir;
Sam McCall2bebc3d2018-11-20 10:56:03 +000093 } else {
94 for (auto Path = path::parent_path(File); !CDB && !Path.empty();
95 Path = path::parent_path(Path)) {
96 std::tie(CDB, Cached) = getCDBInDirLocked(Path);
Sam McCall6e2d2a32018-11-26 09:51:50 +000097 if (Project && CDB)
98 Project->SourceRoot = Path;
Sam McCall2bebc3d2018-11-20 10:56:03 +000099 }
100 }
Sam McCall422c8282018-11-26 16:00:11 +0000101 // FIXME: getAllFiles() may return relative paths, we need absolute paths.
102 // Hopefully the fix is to change JSONCompilationDatabase and the interface.
Sam McCall2bebc3d2018-11-20 10:56:03 +0000103 if (CDB && !Cached)
104 OnCommandChanged.broadcast(CDB->getAllFiles());
105 return CDB;
106}
107
108OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
109 std::vector<std::string> FallbackFlags)
110 : Base(Base), FallbackFlags(std::move(FallbackFlags)) {
111 if (Base)
112 BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
113 OnCommandChanged.broadcast(Changes);
114 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000115}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000116
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000117llvm::Optional<tooling::CompileCommand>
Sam McCall6e2d2a32018-11-26 09:51:50 +0000118OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const {
Sam McCallc55d09a2018-11-02 13:09:36 +0000119 {
120 std::lock_guard<std::mutex> Lock(Mutex);
121 auto It = Commands.find(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +0000122 if (It != Commands.end()) {
123 if (Project)
124 Project->SourceRoot = "";
Sam McCallc55d09a2018-11-02 13:09:36 +0000125 return It->second;
Sam McCall6e2d2a32018-11-26 09:51:50 +0000126 }
Sam McCallc55d09a2018-11-02 13:09:36 +0000127 }
Sam McCall422c8282018-11-26 16:00:11 +0000128 return Base ? Base->getCompileCommand(File, Project) : None;
Alex Lorenzf8087862018-08-01 17:39:29 +0000129}
130
Sam McCallc55d09a2018-11-02 13:09:36 +0000131tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
132 auto Cmd = Base ? Base->getFallbackCommand(File)
133 : GlobalCompilationDatabase::getFallbackCommand(File);
134 std::lock_guard<std::mutex> Lock(Mutex);
135 Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
136 FallbackFlags.end());
137 return Cmd;
138}
139
140void OverlayCDB::setCompileCommand(
141 PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
Sam McCall2bebc3d2018-11-20 10:56:03 +0000142 {
143 std::unique_lock<std::mutex> Lock(Mutex);
144 if (Cmd)
145 Commands[File] = std::move(*Cmd);
146 else
147 Commands.erase(File);
148 }
149 OnCommandChanged.broadcast({File});
Alex Lorenzf8087862018-08-01 17:39:29 +0000150}
151
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000152} // namespace clangd
153} // namespace clang