Commit 642a1732 authored by Hans Wennborg's avatar Hans Wennborg
Browse files

Merging r351788:

------------------------------------------------------------------------
r351788 | kadircet | 2019-01-22 10:10:20 +0100 (Tue, 22 Jan 2019) | 15 lines

[clangd] Filter out plugin related flags and move all commandline manipulations into OverlayCDB.

Summary:
Some projects make use of clang plugins when building, but clangd is
not aware of those plugins therefore can't work with the same compile command
arguments.

There were multiple places clangd performed commandline manipulations,
 this one also moves them all into OverlayCDB.

Reviewers: ilya-biryukov

Subscribers: klimek, sammccall, ioeric, MaskRay, jkorous, arphaman, cfe-commits

Differential Revision: https://reviews.llvm.org/D56841
------------------------------------------------------------------------

llvm-svn: 351860
parent ee231d02
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -290,7 +290,8 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
  if (UseDirBasedCDB)
    BaseCDB = llvm::make_unique<DirectoryBasedGlobalCompilationDatabase>(
        CompileCommandsDir);
  CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags);
  CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags,
              ClangdServerOpts.ResourceDir);
  Server.emplace(*CDB, FSProvider, static_cast<DiagnosticsConsumer &>(*this),
                 ClangdServerOpts);
  applyConfiguration(Params.initializationOptions.ConfigSettings);
+1 −12
Original line number Diff line number Diff line
@@ -38,11 +38,6 @@ namespace clang {
namespace clangd {
namespace {

std::string getStandardResourceDir() {
  static int Dummy; // Just an address in this process.
  return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
}

class RefactoringResultCollector final
    : public tooling::RefactoringResultConsumer {
public:
@@ -108,8 +103,6 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
                           DiagnosticsConsumer &DiagConsumer,
                           const Options &Opts)
    : CDB(CDB), FSProvider(FSProvider),
      ResourceDir(Opts.ResourceDir ? *Opts.ResourceDir
                                   : getStandardResourceDir()),
      DynamicIdx(Opts.BuildDynamicSymbolIndex
                     ? new FileIndex(Opts.HeavyweightDynamicSymbolIndex)
                     : nullptr),
@@ -137,7 +130,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
    AddIndex(Opts.StaticIndex);
  if (Opts.BackgroundIndex) {
    BackgroundIdx = llvm::make_unique<BackgroundIndex>(
        Context::current().clone(), ResourceDir, FSProvider, CDB,
        Context::current().clone(), FSProvider, CDB,
        BackgroundIndexStorage::createDiskBackedStorageFactory(),
        Opts.BackgroundIndexRebuildPeriodMs);
    AddIndex(BackgroundIdx.get());
@@ -462,10 +455,6 @@ tooling::CompileCommand ClangdServer::getCompileCommand(PathRef File) {
  llvm::Optional<tooling::CompileCommand> C = CDB.getCompileCommand(File);
  if (!C) // FIXME: Suppress diagnostics? Let the user know?
    C = CDB.getFallbackCommand(File);

  // Inject the resource dir.
  // FIXME: Don't overwrite it if it's already there.
  C->CommandLine.push_back("-resource-dir=" + ResourceDir);
  return std::move(*C);
}

+37 −4
Original line number Diff line number Diff line
@@ -9,12 +9,36 @@

#include "GlobalCompilationDatabase.h"
#include "Logger.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Tooling/ArgumentsAdjusters.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/ADT/Optional.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"

namespace clang {
namespace clangd {
namespace {

void adjustArguments(tooling::CompileCommand &Cmd,
                     llvm::StringRef ResourceDir) {
  // Strip plugin related command line arguments. Clangd does
  // not support plugins currently. Therefore it breaks if
  // compiler tries to load plugins.
  Cmd.CommandLine =
      tooling::getStripPluginsAdjuster()(Cmd.CommandLine, Cmd.Filename);
  // Inject the resource dir.
  // FIXME: Don't overwrite it if it's already there.
  if (!ResourceDir.empty())
    Cmd.CommandLine.push_back(("-resource-dir=" + ResourceDir).str());
}

std::string getStandardResourceDir() {
  static int Dummy; // Just an address in this process.
  return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
}

} // namespace

static std::string getFallbackClangPath() {
  static int Dummy;
@@ -106,8 +130,11 @@ DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
}

OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
                       std::vector<std::string> FallbackFlags)
    : Base(Base), FallbackFlags(std::move(FallbackFlags)) {
                       std::vector<std::string> FallbackFlags,
                       llvm::Optional<std::string> ResourceDir)
    : Base(Base), ResourceDir(ResourceDir ? std::move(*ResourceDir)
                                          : getStandardResourceDir()),
      FallbackFlags(std::move(FallbackFlags)) {
  if (Base)
    BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
      OnCommandChanged.broadcast(Changes);
@@ -116,16 +143,22 @@ OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,

llvm::Optional<tooling::CompileCommand>
OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const {
  llvm::Optional<tooling::CompileCommand> Cmd;
  {
    std::lock_guard<std::mutex> Lock(Mutex);
    auto It = Commands.find(File);
    if (It != Commands.end()) {
      if (Project)
        Project->SourceRoot = "";
      return It->second;
      Cmd = It->second;
    }
  }
  return Base ? Base->getCompileCommand(File, Project) : None;
  if (!Cmd && Base)
    Cmd = Base->getCompileCommand(File, Project);
  if (!Cmd)
    return llvm::None;
  adjustArguments(*Cmd, ResourceDir);
  return Cmd;
}

tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
+4 −1
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@

#include "Function.h"
#include "Path.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringMap.h"
#include <memory>
#include <mutex>
@@ -98,7 +99,8 @@ public:
  // Base may be null, in which case no entries are inherited.
  // FallbackFlags are added to the fallback compile command.
  OverlayCDB(const GlobalCompilationDatabase *Base,
             std::vector<std::string> FallbackFlags = {});
             std::vector<std::string> FallbackFlags = {},
             llvm::Optional<std::string> ResourceDir = llvm::None);

  llvm::Optional<tooling::CompileCommand>
  getCompileCommand(PathRef File, ProjectInfo * = nullptr) const override;
@@ -113,6 +115,7 @@ private:
  mutable std::mutex Mutex;
  llvm::StringMap<tooling::CompileCommand> Commands; /* GUARDED_BY(Mut) */
  const GlobalCompilationDatabase *Base;
  std::string ResourceDir;
  std::vector<std::string> FallbackFlags;
  CommandChanged::Subscription BaseChanged;
};
+4 −6
Original line number Diff line number Diff line
@@ -127,13 +127,12 @@ llvm::SmallString<128> getAbsolutePath(const tooling::CompileCommand &Cmd) {
} // namespace

BackgroundIndex::BackgroundIndex(
    Context BackgroundContext, llvm::StringRef ResourceDir,
    const FileSystemProvider &FSProvider, const GlobalCompilationDatabase &CDB,
    Context BackgroundContext, const FileSystemProvider &FSProvider,
    const GlobalCompilationDatabase &CDB,
    BackgroundIndexStorage::Factory IndexStorageFactory,
    size_t BuildIndexPeriodMs, size_t ThreadPoolSize)
    : SwapIndex(llvm::make_unique<MemIndex>()), ResourceDir(ResourceDir),
      FSProvider(FSProvider), CDB(CDB),
      BackgroundContext(std::move(BackgroundContext)),
    : SwapIndex(llvm::make_unique<MemIndex>()), FSProvider(FSProvider),
      CDB(CDB), BackgroundContext(std::move(BackgroundContext)),
      BuildIndexPeriodMs(BuildIndexPeriodMs),
      SymbolsUpdatedSinceLastIndex(false),
      IndexStorageFactory(std::move(IndexStorageFactory)),
@@ -230,7 +229,6 @@ void BackgroundIndex::enqueue(tooling::CompileCommand Cmd,
                              BackgroundIndexStorage *Storage) {
  enqueueTask(Bind(
                  [this, Storage](tooling::CompileCommand Cmd) {
                    Cmd.CommandLine.push_back("-resource-dir=" + ResourceDir);
                    // We can't use llvm::StringRef here since we are going to
                    // move from Cmd during the call below.
                    const std::string FileName = Cmd.Filename;
Loading