blob: 95e7ce7ba692165b6f6d7c7970da226c64174f70 [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- GlobalCompilationDatabase.cpp ---------------------------*- C++-*-===//
Ilya Biryukov38d79772017-05-16 09:38:59 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ilya Biryukov38d79772017-05-16 09:38:59 +00006//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00007//===----------------------------------------------------------------------===//
Ilya Biryukov38d79772017-05-16 09:38:59 +00008
9#include "GlobalCompilationDatabase.h"
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000010#include "Logger.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000011#include "clang/Tooling/CompilationDatabase.h"
12#include "llvm/Support/FileSystem.h"
13#include "llvm/Support/Path.h"
14
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000015namespace clang {
16namespace clangd {
17
Haojian Wue1ced5c2019-01-07 12:35:02 +000018static std::string getFallbackClangPath() {
19 static int Dummy;
20 std::string ClangdExecutable =
21 llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy);
22 SmallString<128> ClangPath;
23 ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
24 llvm::sys::path::append(ClangPath, "clang");
25 return ClangPath.str();
26}
27
Sam McCallecbeab02017-12-04 10:08:45 +000028tooling::CompileCommand
29GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
Haojian Wue1ced5c2019-01-07 12:35:02 +000030 std::vector<std::string> Argv = {getFallbackClangPath()};
Sam McCall690dcf12018-04-20 11:35:17 +000031 // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
32 // Parsing as Objective C++ is friendly to more cases.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000033 if (llvm::sys::path::extension(File) == ".h")
Sam McCall690dcf12018-04-20 11:35:17 +000034 Argv.push_back("-xobjective-c++-header");
35 Argv.push_back(File);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000036 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
37 llvm::sys::path::filename(File),
38 std::move(Argv),
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000039 /*Output=*/"");
40}
Ilya Biryukov38d79772017-05-16 09:38:59 +000041
Ilya Biryukove5128f72017-09-20 07:24:15 +000042DirectoryBasedGlobalCompilationDatabase::
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000043 DirectoryBasedGlobalCompilationDatabase(
44 llvm::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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000050llvm::Optional<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 {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000080 namespace path = llvm::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
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000116llvm::Optional<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