blob: ca223f6b3440e8d1e548b17343d58c97d86ee153 [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 {
Sam McCall690dcf12018-04-20 11:35:17 +000021 std::vector<std::string> Argv = {"clang"};
22 // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
23 // Parsing as Objective C++ is friendly to more cases.
24 if (llvm::sys::path::extension(File) == ".h")
25 Argv.push_back("-xobjective-c++-header");
26 Argv.push_back(File);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000027 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
Sam McCallecbeab02017-12-04 10:08:45 +000028 llvm::sys::path::filename(File),
Sam McCall690dcf12018-04-20 11:35:17 +000029 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::
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000034 DirectoryBasedGlobalCompilationDatabase(
Ilya Biryukov940901e2017-12-13 12:51:22 +000035 llvm::Optional<Path> CompileCommandsDir)
36 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
Ilya Biryukove5128f72017-09-20 07:24:15 +000037
Sam McCall690dcf12018-04-20 11:35:17 +000038DirectoryBasedGlobalCompilationDatabase::
39 ~DirectoryBasedGlobalCompilationDatabase() = default;
40
Sam McCallecbeab02017-12-04 10:08:45 +000041llvm::Optional<tooling::CompileCommand>
42DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
Sam McCallc02ba722017-12-22 09:47:34 +000043 if (auto CDB = getCDBForFile(File)) {
Sam McCallecbeab02017-12-04 10:08:45 +000044 auto Candidates = CDB->getCompileCommands(File);
45 if (!Candidates.empty()) {
46 addExtraFlags(File, Candidates.front());
47 return std::move(Candidates.front());
48 }
Sam McCallc02ba722017-12-22 09:47:34 +000049 } else {
Sam McCalld1a7a372018-01-31 13:40:48 +000050 log("Failed to find compilation database for " + Twine(File));
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000051 }
Sam McCallecbeab02017-12-04 10:08:45 +000052 return llvm::None;
53}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000054
Sam McCallecbeab02017-12-04 10:08:45 +000055tooling::CompileCommand
56DirectoryBasedGlobalCompilationDatabase::getFallbackCommand(
57 PathRef File) const {
58 auto C = GlobalCompilationDatabase::getFallbackCommand(File);
59 addExtraFlags(File, C);
60 return C;
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000061}
62
Simon Marchi5178f922018-02-22 14:00:39 +000063void DirectoryBasedGlobalCompilationDatabase::setCompileCommandsDir(Path P) {
64 std::lock_guard<std::mutex> Lock(Mutex);
65 CompileCommandsDir = P;
66 CompilationDatabases.clear();
67}
68
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000069void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
70 PathRef File, std::vector<std::string> ExtraFlags) {
Sam McCallecbeab02017-12-04 10:08:45 +000071 std::lock_guard<std::mutex> Lock(Mutex);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000072 ExtraFlagsForFile[File] = std::move(ExtraFlags);
Ilya Biryukov38d79772017-05-16 09:38:59 +000073}
74
Sam McCallecbeab02017-12-04 10:08:45 +000075void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
76 PathRef File, tooling::CompileCommand &C) const {
77 std::lock_guard<std::mutex> Lock(Mutex);
78
79 auto It = ExtraFlagsForFile.find(File);
80 if (It == ExtraFlagsForFile.end())
81 return;
82
83 auto &Args = C.CommandLine;
84 assert(Args.size() >= 2 && "Expected at least [compiler, source file]");
85 // The last argument of CommandLine is the name of the input file.
86 // Add ExtraFlags before it.
87 Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
88}
89
Ilya Biryukov38d79772017-05-16 09:38:59 +000090tooling::CompilationDatabase *
Sam McCallc02ba722017-12-22 09:47:34 +000091DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
92 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
93 auto CachedIt = CompilationDatabases.find(Dir);
94 if (CachedIt != CompilationDatabases.end())
95 return CachedIt->second.get();
96 std::string Error = "";
97 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
Sam McCall307c4832018-04-09 15:22:08 +000098 if (CDB)
99 CDB = tooling::inferMissingCompileCommands(std::move(CDB));
Sam McCallc02ba722017-12-22 09:47:34 +0000100 auto Result = CDB.get();
101 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
102 return Result;
103}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000104
Sam McCallc02ba722017-12-22 09:47:34 +0000105tooling::CompilationDatabase *
106DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000107 namespace path = llvm::sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000108 assert((path::is_absolute(File, path::Style::posix) ||
109 path::is_absolute(File, path::Style::windows)) &&
110 "path must be absolute");
111
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000112 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCallc02ba722017-12-22 09:47:34 +0000113 if (CompileCommandsDir)
114 return getCDBInDirLocked(*CompileCommandsDir);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000115 for (auto Path = path::parent_path(File); !Path.empty();
Sam McCallc02ba722017-12-22 09:47:34 +0000116 Path = path::parent_path(Path))
117 if (auto CDB = getCDBInDirLocked(Path))
118 return CDB;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000119 return nullptr;
120}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000121
122} // namespace clangd
123} // namespace clang