diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.h b/clang-tools-extra/clangd/GlobalCompilationDatabase.h --- a/clang-tools-extra/clangd/GlobalCompilationDatabase.h +++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.h @@ -119,7 +119,6 @@ getQueryDriverDatabase(llvm::ArrayRef QueryDriverGlobs, std::unique_ptr Base); - /// Wraps another compilation database, and supports overriding the commands /// using an in-memory mapping. class OverlayCDB : public GlobalCompilationDatabase { @@ -134,6 +133,10 @@ llvm::Optional getCompileCommand(PathRef File) const override; tooling::CompileCommand getFallbackCommand(PathRef File) const override; + /// Note that project info is gathered purely from the inner compilation + /// database. This is to ensure consistency, as a translation unit with an + /// overriden command might yield a different project compared to headers + /// included in it (or before and after the override arrives). llvm::Optional getProjectInfo(PathRef File) const override; /// Sets or clears the compilation command for a particular file. diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp --- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp +++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp @@ -298,15 +298,8 @@ } llvm::Optional OverlayCDB::getProjectInfo(PathRef File) const { - { - std::lock_guard Lock(Mutex); - auto It = Commands.find(removeDots(File)); - if (It != Commands.end()) - return ProjectInfo{}; - } if (Base) return Base->getProjectInfo(File); - return llvm::None; } } // namespace clangd diff --git a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp --- a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp +++ b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp @@ -313,9 +313,22 @@ llvm::sys::path::append(File, "blabla", "..", "a.cc"); EXPECT_TRUE(DB.getCompileCommand(File)); - EXPECT_TRUE(DB.getProjectInfo(File)); + EXPECT_FALSE(DB.getProjectInfo(File)); } +TEST_F(OverlayCDBTest, GetProjectInfo) { + OverlayCDB DB(Base.get()); + Path File = testPath("foo.cc"); + Path Header = testPath("foo.h"); + + EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot()); + EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot()); + + // Shouldn't change after an override. + DB.setCompileCommand(File, tooling::CompileCommand()); + EXPECT_EQ(DB.getProjectInfo(File)->SourceRoot, testRoot()); + EXPECT_EQ(DB.getProjectInfo(Header)->SourceRoot, testRoot()); +} } // namespace } // namespace clangd } // namespace clang