blob: e7cbc7bcc868a436981a5a1ca7065a32f7883b7e [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
Haojian Wue1ced5c2019-01-07 12:35:02 +000020static std::string getFallbackClangPath() {
21 static int Dummy;
22 std::string ClangdExecutable =
23 llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy);
24 SmallString<128> ClangPath;
25 ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
26 llvm::sys::path::append(ClangPath, "clang");
27 return ClangPath.str();
28}
29
Sam McCallecbeab02017-12-04 10:08:45 +000030tooling::CompileCommand
31GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
Haojian Wue1ced5c2019-01-07 12:35:02 +000032 std::vector<std::string> Argv = {getFallbackClangPath()};
Sam McCall690dcf12018-04-20 11:35:17 +000033 // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
34 // Parsing as Objective C++ is friendly to more cases.
Sam McCallc008af62018-10-20 15:30:37 +000035 if (sys::path::extension(File) == ".h")
Sam McCall690dcf12018-04-20 11:35:17 +000036 Argv.push_back("-xobjective-c++-header");
37 Argv.push_back(File);
Sam McCallc008af62018-10-20 15:30:37 +000038 return tooling::CompileCommand(sys::path::parent_path(File),
39 sys::path::filename(File), 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::
Sam McCallc008af62018-10-20 15:30:37 +000044 DirectoryBasedGlobalCompilationDatabase(Optional<Path> CompileCommandsDir)
Ilya Biryukov940901e2017-12-13 12:51:22 +000045 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
Ilya Biryukove5128f72017-09-20 07:24:15 +000046
Sam McCall690dcf12018-04-20 11:35:17 +000047DirectoryBasedGlobalCompilationDatabase::
48 ~DirectoryBasedGlobalCompilationDatabase() = default;
49
Sam McCallc008af62018-10-20 15:30:37 +000050Optional<tooling::CompileCommand>
Sam McCall6e2d2a32018-11-26 09:51:50 +000051DirectoryBasedGlobalCompilationDatabase::getCompileCommand(
52 PathRef File, ProjectInfo *Project) const {
53 if (auto CDB = getCDBForFile(File, Project)) {
Sam McCallecbeab02017-12-04 10:08:45 +000054 auto Candidates = CDB->getCompileCommands(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +000055 if (!Candidates.empty()) {
Sam McCallecbeab02017-12-04 10:08:45 +000056 return std::move(Candidates.front());
Sam McCall6e2d2a32018-11-26 09:51:50 +000057 }
Sam McCallc02ba722017-12-22 09:47:34 +000058 } else {
Sam McCallbed58852018-07-11 10:35:11 +000059 log("Failed to find compilation database for {0}", File);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000060 }
Sam McCallc008af62018-10-20 15:30:37 +000061 return None;
Sam McCallecbeab02017-12-04 10:08:45 +000062}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000063
Sam McCall2bebc3d2018-11-20 10:56:03 +000064std::pair<tooling::CompilationDatabase *, /*Cached*/ bool>
Sam McCallc02ba722017-12-22 09:47:34 +000065DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
66 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
67 auto CachedIt = CompilationDatabases.find(Dir);
68 if (CachedIt != CompilationDatabases.end())
Sam McCall2bebc3d2018-11-20 10:56:03 +000069 return {CachedIt->second.get(), true};
Sam McCallc02ba722017-12-22 09:47:34 +000070 std::string Error = "";
71 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
72 auto Result = CDB.get();
73 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
Sam McCall2bebc3d2018-11-20 10:56:03 +000074 return {Result, false};
Sam McCallc02ba722017-12-22 09:47:34 +000075}
Ilya Biryukov38d79772017-05-16 09:38:59 +000076
Sam McCallc02ba722017-12-22 09:47:34 +000077tooling::CompilationDatabase *
Sam McCall6e2d2a32018-11-26 09:51:50 +000078DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
79 PathRef File, ProjectInfo *Project) const {
Sam McCallc008af62018-10-20 15:30:37 +000080 namespace path = sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +000081 assert((path::is_absolute(File, path::Style::posix) ||
82 path::is_absolute(File, path::Style::windows)) &&
83 "path must be absolute");
84
Sam McCall2bebc3d2018-11-20 10:56:03 +000085 tooling::CompilationDatabase *CDB = nullptr;
86 bool Cached = false;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000087 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCall2bebc3d2018-11-20 10:56:03 +000088 if (CompileCommandsDir) {
89 std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir);
Sam McCall6e2d2a32018-11-26 09:51:50 +000090 if (Project && CDB)
91 Project->SourceRoot = *CompileCommandsDir;
Sam McCall2bebc3d2018-11-20 10:56:03 +000092 } else {
93 for (auto Path = path::parent_path(File); !CDB && !Path.empty();
94 Path = path::parent_path(Path)) {
95 std::tie(CDB, Cached) = getCDBInDirLocked(Path);
Sam McCall6e2d2a32018-11-26 09:51:50 +000096 if (Project && CDB)
97 Project->SourceRoot = Path;
Sam McCall2bebc3d2018-11-20 10:56:03 +000098 }
99 }
Sam McCall422c8282018-11-26 16:00:11 +0000100 // FIXME: getAllFiles() may return relative paths, we need absolute paths.
101 // Hopefully the fix is to change JSONCompilationDatabase and the interface.
Sam McCall2bebc3d2018-11-20 10:56:03 +0000102 if (CDB && !Cached)
103 OnCommandChanged.broadcast(CDB->getAllFiles());
104 return CDB;
105}
106
107OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
108 std::vector<std::string> FallbackFlags)
109 : Base(Base), FallbackFlags(std::move(FallbackFlags)) {
110 if (Base)
111 BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
112 OnCommandChanged.broadcast(Changes);
113 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000114}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000115
Sam McCallc008af62018-10-20 15:30:37 +0000116Optional<tooling::CompileCommand>
Sam McCall6e2d2a32018-11-26 09:51:50 +0000117OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const {
Sam McCallc55d09a2018-11-02 13:09:36 +0000118 {
119 std::lock_guard<std::mutex> Lock(Mutex);
120 auto It = Commands.find(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +0000121 if (It != Commands.end()) {
122 if (Project)
123 Project->SourceRoot = "";
Sam McCallc55d09a2018-11-02 13:09:36 +0000124 return It->second;
Sam McCall6e2d2a32018-11-26 09:51:50 +0000125 }
Sam McCallc55d09a2018-11-02 13:09:36 +0000126 }
Sam McCall422c8282018-11-26 16:00:11 +0000127 return Base ? Base->getCompileCommand(File, Project) : None;
Alex Lorenzf8087862018-08-01 17:39:29 +0000128}
129
Sam McCallc55d09a2018-11-02 13:09:36 +0000130tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
131 auto Cmd = Base ? Base->getFallbackCommand(File)
132 : GlobalCompilationDatabase::getFallbackCommand(File);
133 std::lock_guard<std::mutex> Lock(Mutex);
134 Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
135 FallbackFlags.end());
136 return Cmd;
137}
138
139void OverlayCDB::setCompileCommand(
140 PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
Sam McCall2bebc3d2018-11-20 10:56:03 +0000141 {
142 std::unique_lock<std::mutex> Lock(Mutex);
143 if (Cmd)
144 Commands[File] = std::move(*Cmd);
145 else
146 Commands.erase(File);
147 }
148 OnCommandChanged.broadcast({File});
Alex Lorenzf8087862018-08-01 17:39:29 +0000149}
150
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000151} // namespace clangd
152} // namespace clang