summaryrefslogtreecommitdiffstats
path: root/clangd/GlobalCompilationDatabase.cpp
blob: 2d3757dd825d48ed3ef07bafb2b526ac88f64535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//===--- GlobalCompilationDatabase.cpp --------------------------*- C++-*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===---------------------------------------------------------------------===//

#include "GlobalCompilationDatabase.h"
#include "Logger.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"

namespace clang {
namespace clangd {

tooling::CompileCommand
GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
  return tooling::CompileCommand(llvm::sys::path::parent_path(File),
                                 llvm::sys::path::filename(File),
                                 {"clang", File.str()},
                                 /*Output=*/"");
}

DirectoryBasedGlobalCompilationDatabase::
    DirectoryBasedGlobalCompilationDatabase(
        llvm::Optional<Path> CompileCommandsDir)
    : CompileCommandsDir(std::move(CompileCommandsDir)) {}

llvm::Optional<tooling::CompileCommand>
DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
  if (auto CDB = getCompilationDatabase(File)) {
    auto Candidates = CDB->getCompileCommands(File);
    if (!Candidates.empty()) {
      addExtraFlags(File, Candidates.front());
      return std::move(Candidates.front());
    }
  }
  return llvm::None;
}

tooling::CompileCommand
DirectoryBasedGlobalCompilationDatabase::getFallbackCommand(
    PathRef File) const {
  auto C = GlobalCompilationDatabase::getFallbackCommand(File);
  addExtraFlags(File, C);
  return C;
}

void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
    PathRef File, std::vector<std::string> ExtraFlags) {
  std::lock_guard<std::mutex> Lock(Mutex);
  ExtraFlagsForFile[File] = std::move(ExtraFlags);
}

void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
    PathRef File, tooling::CompileCommand &C) const {
  std::lock_guard<std::mutex> Lock(Mutex);

  auto It = ExtraFlagsForFile.find(File);
  if (It == ExtraFlagsForFile.end())
    return;

  auto &Args = C.CommandLine;
  assert(Args.size() >= 2 && "Expected at least [compiler, source file]");
  // The last argument of CommandLine is the name of the input file.
  // Add ExtraFlags before it.
  Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
}

tooling::CompilationDatabase *
DirectoryBasedGlobalCompilationDatabase::tryLoadDatabaseFromPath(
    PathRef File) const {

  namespace path = llvm::sys::path;
  auto CachedIt = CompilationDatabases.find(File);

  assert((path::is_absolute(File, path::Style::posix) ||
          path::is_absolute(File, path::Style::windows)) &&
         "path must be absolute");

  if (CachedIt != CompilationDatabases.end())
    return CachedIt->second.get();
  std::string Error = "";
  auto CDB = tooling::CompilationDatabase::loadFromDirectory(File, Error);
  if (CDB) {
    auto Result = CDB.get();
    CompilationDatabases.insert(std::make_pair(File, std::move(CDB)));
    return Result;
  }

  return nullptr;
}

tooling::CompilationDatabase *
DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(
    PathRef File) const {
  std::lock_guard<std::mutex> Lock(Mutex);

  namespace path = llvm::sys::path;
  if (CompileCommandsDir.hasValue()) {
    tooling::CompilationDatabase *ReturnValue =
        tryLoadDatabaseFromPath(CompileCommandsDir.getValue());
    if (ReturnValue == nullptr) {
      // FIXME(ibiryukov): pass a proper Context here.
      log(Context::empty(), "Failed to find compilation database for " +
                                Twine(File) + "in overriden directory " +
                                CompileCommandsDir.getValue());
    }
    return ReturnValue;
  }

  for (auto Path = path::parent_path(File); !Path.empty();
       Path = path::parent_path(Path)) {
    auto CDB = tryLoadDatabaseFromPath(Path);
    if (!CDB)
      continue;
    // FIXME(ibiryukov): Invalidate cached compilation databases on changes
    return CDB;
  }

  // FIXME(ibiryukov): pass a proper Context here.
  log(Context::empty(),
      "Failed to find compilation database for " + Twine(File));
  return nullptr;
}

} // namespace clangd
} // namespace clang