blob: f7c964cb4ae6f43dc5e7c49f438ff03a6b9d01a1 [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
Simon Marchi5178f922018-02-22 14:00:39 +000054void DirectoryBasedGlobalCompilationDatabase::setCompileCommandsDir(Path P) {
55 std::lock_guard<std::mutex> Lock(Mutex);
56 CompileCommandsDir = P;
57 CompilationDatabases.clear();
58}
59
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000060void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
61 PathRef File, std::vector<std::string> ExtraFlags) {
Sam McCallecbeab02017-12-04 10:08:45 +000062 std::lock_guard<std::mutex> Lock(Mutex);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000063 ExtraFlagsForFile[File] = std::move(ExtraFlags);
Ilya Biryukov38d79772017-05-16 09:38:59 +000064}
65
Sam McCallecbeab02017-12-04 10:08:45 +000066void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
67 PathRef File, tooling::CompileCommand &C) const {
68 std::lock_guard<std::mutex> Lock(Mutex);
69
70 auto It = ExtraFlagsForFile.find(File);
71 if (It == ExtraFlagsForFile.end())
72 return;
73
74 auto &Args = C.CommandLine;
75 assert(Args.size() >= 2 && "Expected at least [compiler, source file]");
76 // The last argument of CommandLine is the name of the input file.
77 // Add ExtraFlags before it.
78 Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
79}
80
Ilya Biryukov38d79772017-05-16 09:38:59 +000081tooling::CompilationDatabase *
Sam McCallc02ba722017-12-22 09:47:34 +000082DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
83 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
84 auto CachedIt = CompilationDatabases.find(Dir);
85 if (CachedIt != CompilationDatabases.end())
86 return CachedIt->second.get();
87 std::string Error = "";
88 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
Sam McCall307c4832018-04-09 15:22:08 +000089 if (CDB)
90 CDB = tooling::inferMissingCompileCommands(std::move(CDB));
Sam McCallc02ba722017-12-22 09:47:34 +000091 auto Result = CDB.get();
92 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
93 return Result;
94}
Ilya Biryukov38d79772017-05-16 09:38:59 +000095
Sam McCallc02ba722017-12-22 09:47:34 +000096tooling::CompilationDatabase *
97DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
Ilya Biryukov38d79772017-05-16 09:38:59 +000098 namespace path = llvm::sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +000099 assert((path::is_absolute(File, path::Style::posix) ||
100 path::is_absolute(File, path::Style::windows)) &&
101 "path must be absolute");
102
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000103 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCallc02ba722017-12-22 09:47:34 +0000104 if (CompileCommandsDir)
105 return getCDBInDirLocked(*CompileCommandsDir);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000106 for (auto Path = path::parent_path(File); !Path.empty();
Sam McCallc02ba722017-12-22 09:47:34 +0000107 Path = path::parent_path(Path))
108 if (auto CDB = getCDBInDirLocked(Path))
109 return CDB;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000110 return nullptr;
111}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000112
113} // namespace clangd
114} // namespace clang