llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Atharva Potdar (atharva-potdar)

<details>
<summary>Changes</summary>

When building C++23/26 projects that use `import std;` with CMake and Ninja, 
`compile_commands.json` includes an entry for the standard library module unit 
outside the project tree (e.g., `/usr/share/libc++/v1/std.cppm`).

When `clangd` opens a translation unit in the workspace, 
`GlobalCompilationDatabase::getProjectModules()` finds the project's CDB by 
walking parent directories and creates a `ProjectModules` instance using it. 
`ProjectModules::getSourceForModuleName()` resolves `std` to 
`/usr/share/libc++/v1/std.cppm`, but `ModulesBuilder` then queries 
`GlobalCompilationDatabase::getCompileCommand()` and `getProjectInfo()` using 
that module unit path.

Without `--compile-commands-dir`, `DirectoryBasedGlobalCompilationDatabase` 
walks the ancestors of `/usr/share/libc++/v1/std.cppm`, finds no CDB, and fails 
with:
&gt; `Failed to build module std; due to No compile command for 
/usr/share/libc++/v1/std.cppm`

Similarly, `getProjectInfo()` on the system path returns `std::nullopt` instead 
of the consuming project's root, causing the module cache to fall back to the 
user cache directory instead of the project-local `.cache/clangd/modules`.

### Solution

Fall back to the `ProjectModules` CDB and the consuming translation unit's 
project root when resolving out-of-tree module units:

- Add `ProjectModules::getCompileCommand(PathRef)` to query the project's 
underlying CDB (applying `CommandMangler` if set).
- In `ModulesBuilder`, fall back to `MDB.getCompileCommand()` when 
`getCDB().getCompileCommand()` returns `std::nullopt`.
- Fall back to `CDB.getProjectInfo(RequiredSource)` when resolving the module 
cache root and GC target for out-of-tree module units.
- Guard `Mangler` in `OverlayCDB::getProjectModules()` with `if (Mangler)` to 
avoid invoking an empty `llvm::unique_function`.

---

### Reproduction &amp; Verification

Minimal CMake project (`CMakeLists.txt` + `main.cpp`):

```cmake
cmake_minimum_required(VERSION 3.31)

project(std_module_test LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_MODULE_STD 1)

add_executable(app main.cpp)
```

```cpp
import std;
int main() { std::println("Hello, modules!"); }
```

**Before (unpatched `clangd`, without running a prior `cmake --build`):**

```bash
rm -rf build .cache ~/.cache/clangd/modules
cmake -B build -G Ninja \
  -DCMAKE_CXX_COMPILER=clang++ \
  -DCMAKE_CXX_FLAGS="-stdlib=libc++"

clangd --experimental-modules-support --log=verbose --check=$PWD/main.cpp 
2&gt;&amp;1 \
  | grep -iE "No compile command|Built module|error|All checks"
```

```text
E[...] Failed to build module std; due to No compile command for 
/usr/share/libc++/v1/std.cppm
I[...] All checks completed, 1 errors
```

**After (with this patch):**

```bash
rm -rf build .cache ~/.cache/clangd/modules
cmake -B build -G Ninja \
  -DCMAKE_CXX_COMPILER=$LLVM_BUILD/bin/clang++ \
  -DCMAKE_CXX_FLAGS="-stdlib=libc++"

$LLVM_BUILD/bin/clangd --experimental-modules-support --log=verbose 
--check=$PWD/main.cpp 2&gt;&amp;1 \
  | grep -iE "No compile command|Built module|error|All checks"
```

```text
I[...] Built module std to 
.../testimport/.cache/clangd/modules/std.cppm-.../std-....pcm
I[...] All checks completed, 0 errors
```

**To build and test:**

```bash
cmake -S llvm -B build -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_EXPORT_COMPILE_COMMANDS=OFF \
  -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" \
  -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
  -DLLVM_TARGETS_TO_BUILD="X86" \
  -DLLVM_ENABLE_LLD=ON

ninja -C build clang clang-scan-deps clangd runtimes ClangdTests
cmake --install build/runtimes/runtimes-bins --component cxx-modules --prefix 
build

./build/tools/clang/tools/extra/clangd/unittests/ClangdTests \
  
--gtest_filter="*OverlayCDBTest.GetProjectModulesWithoutMangler*:*PrerequisiteModulesTests.OutOfTreeModuleUnit*"
```

&gt; **Note on testing directly from an LLVM build tree:** Because `--prefix 
build` installs `std.cppm` inside `$LLVM_BUILD/share/libc++/v1/std.cppm`, if 
`$LLVM_BUILD/compile_commands.json` is present on disk, 
`DirectoryBasedGlobalCompilationDatabase` will walk up from `std.cppm`, find 
LLVM's own CDB, and heuristically interpolate an LLVM C++17 compile command 
instead of returning `std::nullopt`. Building with 
`-DCMAKE_EXPORT_COMPILE_COMMANDS=OFF` (or moving 
`$LLVM_BUILD/compile_commands.json` aside while testing uninstalled `clang++`) 
avoids this; normal installed toolchains are unaffected.

---

Patch is 22.64 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/242615.diff


6 Files Affected:

- (modified) clang-tools-extra/clangd/GlobalCompilationDatabase.cpp (+7-4) 
- (modified) clang-tools-extra/clangd/ModulesBuilder.cpp (+52-19) 
- (modified) clang-tools-extra/clangd/ProjectModules.cpp (+27) 
- (modified) clang-tools-extra/clangd/ProjectModules.h (+8) 
- (modified) 
clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp (+18) 
- (modified) clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp 
(+223) 


``````````diff
diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp 
b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
index b7bb7f426d6150..553236c7d33603 100644
--- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
+++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
@@ -856,10 +856,13 @@ OverlayCDB::getProjectModules(PathRef File) const {
     log("Failed to get compilation Database for {0}", File);
     return {};
   }
-  MDB->setCommandMangler([&Mangler = Mangler](tooling::CompileCommand &Command,
-                                              PathRef CommandPath) {
-    Mangler(Command, CommandPath);
-  });
+  if (Mangler) {
+    MDB->setCommandMangler(
+        [&Mangler = Mangler](tooling::CompileCommand &Command,
+                             PathRef CommandPath) {
+          Mangler(Command, CommandPath);
+        });
+  }
   return MDB;
 }
 
diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp 
b/clang-tools-extra/clangd/ModulesBuilder.cpp
index 72bb5b836d2012..f75f34f6eb1dac 100644
--- a/clang-tools-extra/clangd/ModulesBuilder.cpp
+++ b/clang-tools-extra/clangd/ModulesBuilder.cpp
@@ -94,17 +94,33 @@ std::string normalizePathForCache(PathRef Path) {
   return maybeCaseFoldPath(Normalized);
 }
 
+/// Returns the project source root owning \p ModuleUnitFileName, falling back
+/// to the consuming translation unit's (\p RequiredSource) project root when
+/// the module unit resides outside the workspace tree (e.g. system 
`std.cppm`).
+std::optional<std::string>
+getModuleProjectRoot(PathRef RequiredSource, PathRef ModuleUnitFileName,
+                     const GlobalCompilationDatabase &CDB) {
+  if (auto PI = CDB.getProjectInfo(ModuleUnitFileName);
+      PI && !PI->SourceRoot.empty())
+    return PI->SourceRoot;
+  if (auto PI = CDB.getProjectInfo(RequiredSource);
+      PI && !PI->SourceRoot.empty())
+    return PI->SourceRoot;
+  return std::nullopt;
+}
+
 /// Returns the root directory used for persistent module cache storage.
-/// Prefer a project-local cache so different clangd sessions working on the
+/// Prefer a project-local cache (resolved from the module unit or the
+/// consuming translation unit) so different clangd sessions working on the
 /// same source tree can reuse BMIs. Fall back to the user cache directory, and
 /// finally to a non-ephemeral temp directory when no better cache root exists.
 llvm::SmallString<256>
-getModuleCacheRoot(PathRef ModuleUnitFileName,
+getModuleCacheRoot(PathRef RequiredSource, PathRef ModuleUnitFileName,
                    const GlobalCompilationDatabase &CDB) {
   llvm::SmallString<256> Result;
-  if (auto PI = CDB.getProjectInfo(ModuleUnitFileName);
-      PI && !PI->SourceRoot.empty()) {
-    Result = PI->SourceRoot;
+  if (auto ProjectRoot =
+          getModuleProjectRoot(RequiredSource, ModuleUnitFileName, CDB)) {
+    Result = *ProjectRoot;
     llvm::sys::path::append(Result, ".cache", "clangd", "modules");
     return Result;
   }
@@ -123,9 +139,10 @@ getModuleCacheRoot(PathRef ModuleUnitFileName,
 /// module cache. Placing locks beside the cache ensures all builders sharing
 /// the cache also synchronize through the same lock namespace.
 llvm::SmallString<256>
-getModuleCacheLocksDirectory(PathRef ModuleUnitFileName,
+getModuleCacheLocksDirectory(PathRef RequiredSource, PathRef 
ModuleUnitFileName,
                              const GlobalCompilationDatabase &CDB) {
-  llvm::SmallString<256> Result = getModuleCacheRoot(ModuleUnitFileName, CDB);
+  llvm::SmallString<256> Result =
+      getModuleCacheRoot(RequiredSource, ModuleUnitFileName, CDB);
   llvm::sys::path::append(Result, ".locks");
   return Result;
 }
@@ -162,10 +179,11 @@ std::string getCompileCommandStringHash(const 
tooling::CompileCommand &Cmd) {
 /// basename alongside the source-hash, and the command-hash keeps incompatible
 /// command lines apart.
 llvm::SmallString<256>
-getModuleFilesDirectory(PathRef ModuleUnitFileName,
+getModuleFilesDirectory(PathRef RequiredSource, PathRef ModuleUnitFileName,
                         const tooling::CompileCommand &Cmd,
                         const GlobalCompilationDatabase &CDB) {
-  llvm::SmallString<256> Result = getModuleCacheRoot(ModuleUnitFileName, CDB);
+  llvm::SmallString<256> Result =
+      getModuleCacheRoot(RequiredSource, ModuleUnitFileName, CDB);
   llvm::sys::path::append(Result,
                           getModuleUnitSourceDirectoryName(ModuleUnitFileName),
                           getCompileCommandStringHash(Cmd));
@@ -175,10 +193,10 @@ getModuleFilesDirectory(PathRef ModuleUnitFileName,
 /// Returns the lock file path guarding publication of BMIs for a module unit
 /// source. Builders targeting the same source-hash serialize through this 
path.
 llvm::SmallString<256>
-getModuleSourceHashLockPath(PathRef ModuleUnitFileName,
+getModuleSourceHashLockPath(PathRef RequiredSource, PathRef ModuleUnitFileName,
                             const GlobalCompilationDatabase &CDB) {
   llvm::SmallString<256> Result =
-      getModuleCacheLocksDirectory(ModuleUnitFileName, CDB);
+      getModuleCacheLocksDirectory(RequiredSource, ModuleUnitFileName, CDB);
   llvm::sys::path::append(Result,
                           getModuleUnitSourcePathHash(ModuleUnitFileName));
   return Result;
@@ -269,10 +287,11 @@ llvm::Error ensureLockAnchorFileExists(PathRef LockPath) {
 class ScopedModuleSourceLock {
 public:
   static llvm::Expected<ScopedModuleSourceLock>
-  acquire(PathRef ModuleUnitFileName, const GlobalCompilationDatabase &CDB) {
+  acquire(PathRef RequiredSource, PathRef ModuleUnitFileName,
+          const GlobalCompilationDatabase &CDB) {
     constexpr auto LockWaitInterval = std::chrono::seconds(10);
     llvm::SmallString<256> LockPath =
-        getModuleSourceHashLockPath(ModuleUnitFileName, CDB);
+        getModuleSourceHashLockPath(RequiredSource, ModuleUnitFileName, CDB);
     if (llvm::Error Err = ensureLockAnchorFileExists(LockPath))
       return std::move(Err);
 
@@ -966,6 +985,11 @@ class CachingProjectModules : public ProjectModules {
     return MDB->getModuleNameState(ModuleName);
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    return MDB->getCompileCommand(File);
+  }
+
   std::string getSourceForModuleName(llvm::StringRef ModuleName,
                                      PathRef RequiredSrcFile) override {
     auto ModuleState = MDB->getModuleNameState(ModuleName);
@@ -1152,6 +1176,7 @@ class ModulesBuilder::ModulesBuilderImpl {
   /// Try to get prebuilt module files from the compilation database.
   void getPrebuiltModuleFile(StringRef ModuleName, PathRef ModuleUnitFileName,
                              const ThreadsafeFS &TFS,
+                             const CachingProjectModules &MDB,
                              ReusablePrerequisiteModules &BuiltModuleFiles);
 
   /// Runs GC once for the cache root owning a project root.
@@ -1190,11 +1215,14 @@ void ModulesBuilder::ModulesBuilderImpl::
 
 void ModulesBuilder::ModulesBuilderImpl::getPrebuiltModuleFile(
     StringRef ModuleName, PathRef ModuleUnitFileName, const ThreadsafeFS &TFS,
+    const CachingProjectModules &MDB,
     ReusablePrerequisiteModules &BuiltModuleFiles) {
   if (!ReusePrebuiltModuleFile)
     return;
 
   auto Cmd = getCDB().getCompileCommand(ModuleUnitFileName);
+  if (!Cmd)
+    Cmd = MDB.getCompileCommand(ModuleUnitFileName);
   if (!Cmd)
     return;
 
@@ -1257,7 +1285,8 @@ llvm::Error 
ModulesBuilder::ModulesBuilderImpl::getOrBuildModuleFile(
   /// Try to get prebuilt module files from the compilation database first. 
This
   /// helps to avoid building the module files that are already built by the
   /// compiler.
-  getPrebuiltModuleFile(ModuleName, ModuleUnitFileName, TFS, BuiltModuleFiles);
+  getPrebuiltModuleFile(ModuleName, ModuleUnitFileName, TFS, MDB,
+                        BuiltModuleFiles);
 
   // Get Required modules in topological order.
   auto ReqModuleNames = getAllRequiredModules(RequiredSource, MDB, ModuleName);
@@ -1268,20 +1297,24 @@ llvm::Error 
ModulesBuilder::ModulesBuilderImpl::getOrBuildModuleFile(
     std::string ReqFileName =
         MDB.getSourceForModuleName(ReqModuleName, RequiredSource);
     auto Cmd = getCDB().getCompileCommand(ReqFileName);
+    if (!Cmd)
+      Cmd = MDB.getCompileCommand(ReqFileName);
     if (!Cmd)
       return llvm::createStringError(
           llvm::formatv("No compile command for {0}", ReqFileName));
-    if (auto PI = getCDB().getProjectInfo(ReqFileName);
-        PI && !PI->SourceRoot.empty())
-      garbageCollectModuleCacheForProjectRoot(PI->SourceRoot);
+    if (auto ProjectRoot =
+            getModuleProjectRoot(RequiredSource, ReqFileName, getCDB()))
+      garbageCollectModuleCacheForProjectRoot(*ProjectRoot);
 
     const std::string CommandHash = getCompileCommandStringHash(*Cmd);
     const std::string PublishedModuleFilePath = getPublishedModuleFilePath(
-        ReqModuleName, getModuleFilesDirectory(ReqFileName, *Cmd, getCDB()));
+        ReqModuleName,
+        getModuleFilesDirectory(RequiredSource, ReqFileName, *Cmd, getCDB()));
 
     // Keep the source-scoped lock while probing and validating cached BMIs so
     // stale-file replacement and final publication stay serialized.
-    auto SourceLock = ScopedModuleSourceLock::acquire(ReqFileName, getCDB());
+    auto SourceLock =
+        ScopedModuleSourceLock::acquire(RequiredSource, ReqFileName, getCDB());
     if (!SourceLock)
       return SourceLock.takeError();
 
diff --git a/clang-tools-extra/clangd/ProjectModules.cpp 
b/clang-tools-extra/clangd/ProjectModules.cpp
index 4ea38f4177fe9b..4b57d220bc7a6c 100644
--- a/clang-tools-extra/clangd/ProjectModules.cpp
+++ b/clang-tools-extra/clangd/ProjectModules.cpp
@@ -196,6 +196,14 @@ class ModuleDependencyScanner {
   getRequiredModules(PathRef File,
                      const ProjectModules::CommandMangler &Mangler);
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef FilePath,
+                    const ProjectModules::CommandMangler &Mangler) const {
+    if (!CDB)
+      return std::nullopt;
+    return getCompileCommandForFile(*CDB, FilePath, Mangler);
+  }
+
 private:
   std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
 
@@ -344,6 +352,11 @@ class ScanningAllProjectModules : public ProjectModules {
     return ModuleNameState::Unknown;
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    return Scanner.getCompileCommand(File, Mangler);
+  }
+
 private:
   ModuleDependencyScanner Scanner;
   CommandMangler Mangler;
@@ -429,6 +442,13 @@ class CompileCommandsProjectModules : public 
ProjectModules {
     return SourceIt->second;
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    if (!CDB)
+      return std::nullopt;
+    return getCompileCommandForFile(*CDB, File, Mangler);
+  }
+
   void setCommandMangler(CommandMangler Mangler) override {
     this->Mangler = std::move(Mangler);
     ProducerCommandsIndexed = false;
@@ -567,6 +587,13 @@ class CompoundProjectModules : public ProjectModules {
     return Scanning->getModuleNameState(ModuleName);
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    if (auto Cmd = CompileCommands->getCompileCommand(File))
+      return Cmd;
+    return Scanning->getCompileCommand(File);
+  }
+
   void setCommandMangler(CommandMangler Mangler) override {
     this->Mangler = std::move(Mangler);
     auto ForwardMangler = [this](tooling::CompileCommand &Command,
diff --git a/clang-tools-extra/clangd/ProjectModules.h 
b/clang-tools-extra/clangd/ProjectModules.h
index fbf658bafe1500..76e43f6bac7fc5 100644
--- a/clang-tools-extra/clangd/ProjectModules.h
+++ b/clang-tools-extra/clangd/ProjectModules.h
@@ -15,6 +15,7 @@
 #include "clang/Tooling/CompilationDatabase.h"
 
 #include <memory>
+#include <optional>
 
 namespace clang {
 namespace clangd {
@@ -55,6 +56,13 @@ class ProjectModules {
     return ModuleNameState::Unknown;
   }
 
+  /// Returns a compile command for \p File from this project's underlying
+  /// compilation database, with any configured CommandMangler applied.
+  virtual std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const {
+    return std::nullopt;
+  }
+
   virtual void setCommandMangler(CommandMangler Mangler) {}
 
   virtual ~ProjectModules() = default;
diff --git 
a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp 
b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp
index 39ab1446e980b1..c380582f994f5c 100644
--- a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp
+++ b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp
@@ -14,6 +14,7 @@
 #include "support/Path.h"
 #include "support/ThreadsafeFS.h"
 #include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/JSONCompilationDatabase.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
@@ -92,6 +93,15 @@ class OverlayCDBTest : public ::testing::Test {
     std::optional<ProjectInfo> getProjectInfo(PathRef File) const override {
       return ProjectInfo{testRoot()};
     }
+
+    std::unique_ptr<ProjectModules>
+    getProjectModules(PathRef File) const override {
+      MockFS TFS;
+      std::string Error;
+      auto JSONCDB = tooling::JSONCompilationDatabase::loadFromBuffer(
+          testRoot(), "[]", Error, tooling::JSONCommandLineSyntax::AutoDetect);
+      return clang::clangd::getProjectModules(std::move(JSONCDB), TFS);
+    }
   };
 
 protected:
@@ -179,6 +189,14 @@ TEST_F(OverlayCDBTest, Adjustments) {
                                            "-DFallback", "-DAdjust_baz.cc"));
 }
 
+TEST_F(OverlayCDBTest, GetProjectModulesWithoutMangler) {
+  OverlayCDB CDB(Base.get());
+  auto MDB = CDB.getProjectModules(testPath("foo.cc"));
+  ASSERT_NE(MDB, nullptr);
+  // Invoking ProjectModules queries must not crash when Mangler is empty.
+  EXPECT_EQ(MDB->getCompileCommand(testPath("foo.cc")), std::nullopt);
+}
+
 TEST_F(OverlayCDBTest, ExpandedResponseFiles) {
   SmallString<1024> Path;
   int FD;
diff --git a/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp 
b/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
index e878e2e0fa637b..8472821cf13412 100644
--- a/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
+++ b/clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
@@ -14,6 +14,7 @@
 #include "Annotations.h"
 #include "CodeComplete.h"
 #include "Compiler.h"
+#include "GlobalCompilationDatabase.h"
 #include "ModulesBuilder.h"
 #include "Preamble.h"
 #include "ProjectModules.h"
@@ -68,6 +69,11 @@ class GlobalScanningCounterProjectModules : public 
ProjectModules {
     return Underlying->getModuleNameState(ModuleName);
   }
 
+  std::optional<tooling::CompileCommand>
+  getCompileCommand(PathRef File) const override {
+    return Underlying->getCompileCommand(File);
+  }
+
 private:
   std::unique_ptr<ProjectModules> Underlying;
   std::atomic<unsigned> &Count;
@@ -1649,6 +1655,223 @@ export int BValue = MValue;
   EXPECT_TRUE(StringRef(HSB.PrebuiltModuleFiles["M"]).contains("M.cppm-"));
 }
 
+TEST_F(PrerequisiteModulesTests,
+       OutOfTreeModuleUnitResolvedViaConsumerCDBAndCachedInProject) {
+  SmallString<256> ProjectDir(TestDir);
+  llvm::sys::path::append(ProjectDir, "project");
+  SmallString<256> BuildDir(ProjectDir);
+  llvm::sys::path::append(BuildDir, "build");
+  SmallString<256> ExternalDir(TestDir);
+  llvm::sys::path::append(ExternalDir, "external");
+
+  ASSERT_FALSE(llvm::sys::fs::create_directories(BuildDir));
+  ASSERT_FALSE(llvm::sys::fs::create_directories(ExternalDir));
+
+  SmallString<256> ExtModPath(ExternalDir);
+  llvm::sys::path::append(ExtModPath, "M.cppm");
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(ExtModPath, EC);
+    ASSERT_FALSE(EC);
+    OS << "export module M;\nexport int extVal = 42;\n";
+  }
+
+  SmallString<256> MainPath(ProjectDir);
+  llvm::sys::path::append(MainPath, "main.cpp");
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(MainPath, EC);
+    ASSERT_FALSE(EC);
+    OS << "import M;\nint main() { return extVal; }\n";
+  }
+
+  SmallString<256> MPcmPath(BuildDir);
+  llvm::sys::path::append(MPcmPath, "M.pcm");
+
+  SmallString<256> CDBPath(BuildDir);
+  llvm::sys::path::append(CDBPath, "compile_commands.json");
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(CDBPath, EC);
+    ASSERT_FALSE(EC);
+    std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
+    std::string SlashExtMod = llvm::sys::path::convert_to_slash(ExtModPath);
+    std::string SlashMain = llvm::sys::path::convert_to_slash(MainPath);
+    std::string SlashPcm = llvm::sys::path::convert_to_slash(MPcmPath);
+    OS << llvm::formatv(
+        R"json([
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 --precompile -o {3} {1}",
+    "file": "{1}"
+  },
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 -fmodule-file=M={3} -c {2}",
+    "file": "{2}"
+  }
+])json",
+        SlashBuild, SlashExtMod, SlashMain, SlashPcm);
+  }
+
+  DirectoryBasedGlobalCompilationDatabase DirCDB(FS);
+  OverlayCDB CDB(&DirCDB);
+
+  // Out-of-tree module unit has no ancestor CDB of its own.
+  EXPECT_FALSE(CDB.getCompileCommand(ExtModPath));
+
+  ModulesBuilder Builder(CDB);
+  auto Info = Builder.buildPrerequisiteModulesFor(MainPath, FS);
+  ASSERT_TRUE(Info);
+
+  HeaderSearchOptions HS(ProjectDir);
+  Info->adjustHeaderSearchOptions(HS);
+  ASSERT_EQ(HS.PrebuiltModuleFiles.count("M"), 1u);
+  std::string BuiltPCMPath = HS.PrebuiltModuleFiles["M"];
+  EXPECT_TRUE(llvm::sys::fs::exists(BuiltPCMPath));
+
+  // Because the out-of-tree module unit has no project root, its persistent 
BMI
+  // cache must fall back to the consuming translation unit's project root.
+  SmallString<256> ExpectedCacheRoot(ProjectDir);
+  llvm::sys::path::append(ExpectedCacheRoot, ".cache", "clangd", "modules");
+  EXPECT_TRUE(StringRef(BuiltPCMPath).starts_with(ExpectedCacheRoot))
+      << "Actual BMI path: " << BuiltPCMPath
+      << "\nExpected prefix: " << ExpectedCacheRoot;
+
+  ParseInputs MainInputs = getInputs("project/main.cpp", CDB);
+  MainInputs.ModulesManager = &Builder;
+  auto CI = buildCompilerInvocation(MainInputs, DiagConsumer);
+  ASSERT_TRUE(CI);
+  EXPECT_TRUE(Info->canReuse(*CI, FS.view(ProjectDir)));
+
+  // Also verify getPrebuiltModuleFile() when an out-of-tree module unit
+  // itself consumes a prebuilt BMI via -fmodule-file= in 
compile_commands.json.
+  SmallString<256> DepModPath(ExternalDir);
+  llvm::sys::path::append(DepModPath, "Dep.cppm");
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(DepModPath, EC);
+    ASSERT_FALSE(EC);
+    OS << "export module Dep;\nexport int depVal = 7;\n";
+  }
+
+  SmallString<256> DepPcmPath(BuildDir);
+  llvm::sys::path::append(DepPcmPath, "Dep.pcm");
+
+  // Build Dep.pcm once so a valid prebuilt BMI exists on disk.
+  {
+    std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
+    std::string SlashDepMod = llvm::sys::path::convert_to_slash(DepModPath);
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(CDBPath, EC);
+    ASSERT_FALSE(EC);
+    OS << llvm::formatv(
+        R"json([
+  {
+    "directory": "{0}",
+    "command": "clang++ -std=c++20 --precompile {1}",
+    "file": "{1}"
+  }
+])json",
+        SlashBuild, SlashDepMod);
+  }
+  {
+    DirectoryBasedGlobalCompilationDatabase SeedDirCDB(FS);
+    OverlayCDB SeedCDB(&SeedDirCDB);
+    ModulesBuilder SeedBuilder(SeedCDB);
+    // Build a temporary consumer of Dep to produce a valid BMI on disk.
+    SmallString<256> SeedMainPath(ProjectDir);
+    llvm::sys::path::append(SeedMainPath, "seed.cpp");
+    {
+      std::error_code EC;
+      llvm::raw_fd_ostream OS(SeedMainPath, EC);
+      ASSERT_FALSE(EC);
+      OS << "import Dep;\n";
+    }
+    {
+      std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir);
+      std::string SlashDepMod = llvm::sys::path::convert_to_slash(DepModPath);
+      std::string SlashSeed = llvm::sys::path::convert_to_slash(SeedMainPath);
+      std::error_code EC;
+      llvm::raw_fd_ostream OS(CDBPath, EC);
+      ASSERT_FALSE(EC);
+      OS << llvm::formatv(
+          R"json([
+  {
...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/242615
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to