blob: acfd5953a168296870952dc33e5debb43602d2e1 [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
Ilya Biryukov38d79772017-05-16 09:38:59 +000052tooling::CompilationDatabase *
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())
57 return CachedIt->second.get();
58 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)));
62 return Result;
63}
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
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000072 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCallc02ba722017-12-22 09:47:34 +000073 if (CompileCommandsDir)
74 return getCDBInDirLocked(*CompileCommandsDir);
Ilya Biryukov38d79772017-05-16 09:38:59 +000075 for (auto Path = path::parent_path(File); !Path.empty();
Sam McCallc02ba722017-12-22 09:47:34 +000076 Path = path::parent_path(Path))
77 if (auto CDB = getCDBInDirLocked(Path))
78 return CDB;
Ilya Biryukov38d79772017-05-16 09:38:59 +000079 return nullptr;
80}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000081
Sam McCallc008af62018-10-20 15:30:37 +000082Optional<tooling::CompileCommand>
Alex Lorenzf8087862018-08-01 17:39:29 +000083InMemoryCompilationDb::getCompileCommand(PathRef File) const {
84 std::lock_guard<std::mutex> Lock(Mutex);
85 auto It = Commands.find(File);
86 if (It == Commands.end())
87 return None;
88 return It->second;
89}
90
91bool InMemoryCompilationDb::setCompilationCommandForFile(
92 PathRef File, tooling::CompileCommand CompilationCommand) {
93 std::unique_lock<std::mutex> Lock(Mutex);
94 auto ItInserted = Commands.insert(std::make_pair(File, CompilationCommand));
95 if (ItInserted.second)
96 return true;
97 ItInserted.first->setValue(std::move(CompilationCommand));
98 return false;
99}
100
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000101} // namespace clangd
102} // namespace clang