blob: 055f92c47341eed65284e281e03c3cac35e3ec6f [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 McCall6e2d2a32018-11-26 09:51:50 +000041DirectoryBasedGlobalCompilationDatabase::getCompileCommand(
42 PathRef File, ProjectInfo *Project) const {
43 if (auto CDB = getCDBForFile(File, Project)) {
Sam McCallecbeab02017-12-04 10:08:45 +000044 auto Candidates = CDB->getCompileCommands(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +000045 if (!Candidates.empty()) {
Sam McCallecbeab02017-12-04 10:08:45 +000046 return std::move(Candidates.front());
Sam McCall6e2d2a32018-11-26 09:51:50 +000047 }
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 McCall2bebc3d2018-11-20 10:56:03 +000054std::pair<tooling::CompilationDatabase *, /*Cached*/ bool>
Sam McCallc02ba722017-12-22 09:47:34 +000055DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
56 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
57 auto CachedIt = CompilationDatabases.find(Dir);
58 if (CachedIt != CompilationDatabases.end())
Sam McCall2bebc3d2018-11-20 10:56:03 +000059 return {CachedIt->second.get(), true};
Sam McCallc02ba722017-12-22 09:47:34 +000060 std::string Error = "";
61 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
62 auto Result = CDB.get();
63 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
Sam McCall2bebc3d2018-11-20 10:56:03 +000064 return {Result, false};
Sam McCallc02ba722017-12-22 09:47:34 +000065}
Ilya Biryukov38d79772017-05-16 09:38:59 +000066
Sam McCallc02ba722017-12-22 09:47:34 +000067tooling::CompilationDatabase *
Sam McCall6e2d2a32018-11-26 09:51:50 +000068DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
69 PathRef File, ProjectInfo *Project) const {
Sam McCallc008af62018-10-20 15:30:37 +000070 namespace path = sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +000071 assert((path::is_absolute(File, path::Style::posix) ||
72 path::is_absolute(File, path::Style::windows)) &&
73 "path must be absolute");
74
Sam McCall2bebc3d2018-11-20 10:56:03 +000075 tooling::CompilationDatabase *CDB = nullptr;
76 bool Cached = false;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000077 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCall2bebc3d2018-11-20 10:56:03 +000078 if (CompileCommandsDir) {
79 std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir);
Sam McCall6e2d2a32018-11-26 09:51:50 +000080 if (Project && CDB)
81 Project->SourceRoot = *CompileCommandsDir;
Sam McCall2bebc3d2018-11-20 10:56:03 +000082 } else {
83 for (auto Path = path::parent_path(File); !CDB && !Path.empty();
84 Path = path::parent_path(Path)) {
85 std::tie(CDB, Cached) = getCDBInDirLocked(Path);
Sam McCall6e2d2a32018-11-26 09:51:50 +000086 if (Project && CDB)
87 Project->SourceRoot = Path;
Sam McCall2bebc3d2018-11-20 10:56:03 +000088 }
89 }
Sam McCall422c8282018-11-26 16:00:11 +000090 // FIXME: getAllFiles() may return relative paths, we need absolute paths.
91 // Hopefully the fix is to change JSONCompilationDatabase and the interface.
Sam McCall2bebc3d2018-11-20 10:56:03 +000092 if (CDB && !Cached)
93 OnCommandChanged.broadcast(CDB->getAllFiles());
94 return CDB;
95}
96
97OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
98 std::vector<std::string> FallbackFlags)
99 : Base(Base), FallbackFlags(std::move(FallbackFlags)) {
100 if (Base)
101 BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
102 OnCommandChanged.broadcast(Changes);
103 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000104}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000105
Sam McCallc008af62018-10-20 15:30:37 +0000106Optional<tooling::CompileCommand>
Sam McCall6e2d2a32018-11-26 09:51:50 +0000107OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const {
Sam McCallc55d09a2018-11-02 13:09:36 +0000108 {
109 std::lock_guard<std::mutex> Lock(Mutex);
110 auto It = Commands.find(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +0000111 if (It != Commands.end()) {
112 if (Project)
113 Project->SourceRoot = "";
Sam McCallc55d09a2018-11-02 13:09:36 +0000114 return It->second;
Sam McCall6e2d2a32018-11-26 09:51:50 +0000115 }
Sam McCallc55d09a2018-11-02 13:09:36 +0000116 }
Sam McCall422c8282018-11-26 16:00:11 +0000117 return Base ? Base->getCompileCommand(File, Project) : None;
Alex Lorenzf8087862018-08-01 17:39:29 +0000118}
119
Sam McCallc55d09a2018-11-02 13:09:36 +0000120tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
121 auto Cmd = Base ? Base->getFallbackCommand(File)
122 : GlobalCompilationDatabase::getFallbackCommand(File);
123 std::lock_guard<std::mutex> Lock(Mutex);
124 Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
125 FallbackFlags.end());
126 return Cmd;
127}
128
129void OverlayCDB::setCompileCommand(
130 PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
Sam McCall2bebc3d2018-11-20 10:56:03 +0000131 {
132 std::unique_lock<std::mutex> Lock(Mutex);
133 if (Cmd)
134 Commands[File] = std::move(*Cmd);
135 else
136 Commands.erase(File);
137 }
138 OnCommandChanged.broadcast({File});
Alex Lorenzf8087862018-08-01 17:39:29 +0000139}
140
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000141} // namespace clangd
142} // namespace clang