blob: 6d9242811bd14b1a127d7efffee91c83e0a3975b [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- GlobalCompilationDatabase.cpp --------------------------*- C++-*-===//
2//
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//
8//===---------------------------------------------------------------------===//
9
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
Sam McCallecbeab02017-12-04 10:08:45 +000019tooling::CompileCommand
20GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000021 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
Sam McCallecbeab02017-12-04 10:08:45 +000022 llvm::sys::path::filename(File),
23 {"clang", File.str()},
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000024 /*Output=*/"");
25}
Ilya Biryukov38d79772017-05-16 09:38:59 +000026
Ilya Biryukove5128f72017-09-20 07:24:15 +000027DirectoryBasedGlobalCompilationDatabase::
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000028 DirectoryBasedGlobalCompilationDatabase(
Ilya Biryukov940901e2017-12-13 12:51:22 +000029 llvm::Optional<Path> CompileCommandsDir)
30 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
Ilya Biryukove5128f72017-09-20 07:24:15 +000031
Sam McCallecbeab02017-12-04 10:08:45 +000032llvm::Optional<tooling::CompileCommand>
33DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
Sam McCallc02ba722017-12-22 09:47:34 +000034 if (auto CDB = getCDBForFile(File)) {
Sam McCallecbeab02017-12-04 10:08:45 +000035 auto Candidates = CDB->getCompileCommands(File);
36 if (!Candidates.empty()) {
37 addExtraFlags(File, Candidates.front());
38 return std::move(Candidates.front());
39 }
Sam McCallc02ba722017-12-22 09:47:34 +000040 } else {
Sam McCalld1a7a372018-01-31 13:40:48 +000041 log("Failed to find compilation database for " + Twine(File));
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000042 }
Sam McCallecbeab02017-12-04 10:08:45 +000043 return llvm::None;
44}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000045
Sam McCallecbeab02017-12-04 10:08:45 +000046tooling::CompileCommand
47DirectoryBasedGlobalCompilationDatabase::getFallbackCommand(
48 PathRef File) const {
49 auto C = GlobalCompilationDatabase::getFallbackCommand(File);
50 addExtraFlags(File, C);
51 return C;
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000052}
53
54void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
55 PathRef File, std::vector<std::string> ExtraFlags) {
Sam McCallecbeab02017-12-04 10:08:45 +000056 std::lock_guard<std::mutex> Lock(Mutex);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000057 ExtraFlagsForFile[File] = std::move(ExtraFlags);
Ilya Biryukov38d79772017-05-16 09:38:59 +000058}
59
Sam McCallecbeab02017-12-04 10:08:45 +000060void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
61 PathRef File, tooling::CompileCommand &C) const {
62 std::lock_guard<std::mutex> Lock(Mutex);
63
64 auto It = ExtraFlagsForFile.find(File);
65 if (It == ExtraFlagsForFile.end())
66 return;
67
68 auto &Args = C.CommandLine;
69 assert(Args.size() >= 2 && "Expected at least [compiler, source file]");
70 // The last argument of CommandLine is the name of the input file.
71 // Add ExtraFlags before it.
72 Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
73}
74
Ilya Biryukov38d79772017-05-16 09:38:59 +000075tooling::CompilationDatabase *
Sam McCallc02ba722017-12-22 09:47:34 +000076DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
77 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
78 auto CachedIt = CompilationDatabases.find(Dir);
79 if (CachedIt != CompilationDatabases.end())
80 return CachedIt->second.get();
81 std::string Error = "";
82 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
83 auto Result = CDB.get();
84 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
85 return Result;
86}
Ilya Biryukov38d79772017-05-16 09:38:59 +000087
Sam McCallc02ba722017-12-22 09:47:34 +000088tooling::CompilationDatabase *
89DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
Ilya Biryukov38d79772017-05-16 09:38:59 +000090 namespace path = llvm::sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +000091 assert((path::is_absolute(File, path::Style::posix) ||
92 path::is_absolute(File, path::Style::windows)) &&
93 "path must be absolute");
94
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000095 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCallc02ba722017-12-22 09:47:34 +000096 if (CompileCommandsDir)
97 return getCDBInDirLocked(*CompileCommandsDir);
Ilya Biryukov38d79772017-05-16 09:38:59 +000098 for (auto Path = path::parent_path(File); !Path.empty();
Sam McCallc02ba722017-12-22 09:47:34 +000099 Path = path::parent_path(Path))
100 if (auto CDB = getCDBInDirLocked(Path))
101 return CDB;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000102 return nullptr;
103}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000104
105} // namespace clangd
106} // namespace clang