https://github.com/atharva-potdar updated https://github.com/llvm/llvm-project/pull/242615
>From fad5a9bf2c25d85e45b91fc6fe2f5431c052e0a6 Mon Sep 17 00:00:00 2001 From: atharva-potdar <[email protected]> Date: Sat, 14 Nov 2026 10:21:40 +0530 Subject: [PATCH] [clangd] Resolve out-of-tree module units via ProjectModules 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 "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. Fall back to the ProjectModules CDB and the consuming file's project root: - 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 calling an empty llvm::unique_function. --- .../clangd/GlobalCompilationDatabase.cpp | 11 +- clang-tools-extra/clangd/ModulesBuilder.cpp | 71 ++++-- clang-tools-extra/clangd/ProjectModules.cpp | 27 +++ clang-tools-extra/clangd/ProjectModules.h | 8 + .../GlobalCompilationDatabaseTests.cpp | 18 ++ .../unittests/PrerequisiteModulesTest.cpp | 223 ++++++++++++++++++ 6 files changed, 335 insertions(+), 23 deletions(-) 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([ + { + "directory": "{0}", + "command": "clang++ -std=c++20 --precompile {1}", + "file": "{1}" + }, + { + "directory": "{0}", + "command": "clang++ -std=c++20 -c {2}", + "file": "{2}" + } +])json", + SlashBuild, SlashDepMod, SlashSeed); + } + auto SeedInfo = SeedBuilder.buildPrerequisiteModulesFor(SeedMainPath, FS); + ASSERT_TRUE(SeedInfo); + HeaderSearchOptions SeedHS(ProjectDir); + SeedInfo->adjustHeaderSearchOptions(SeedHS); + ASSERT_EQ(SeedHS.PrebuiltModuleFiles.count("Dep"), 1u); + ASSERT_FALSE(llvm::sys::fs::copy_file(SeedHS.PrebuiltModuleFiles["Dep"], + DepPcmPath)); + } + + // Now update external/M.cppm to import Dep and pass -fmodule-file=Dep=... on + // M.cppm's compile command in compile_commands.json. + { + std::error_code EC; + llvm::raw_fd_ostream OS(ExtModPath, EC); + ASSERT_FALSE(EC); + OS << "export module M;\nimport Dep;\nexport int extVal = depVal + 35;\n"; + } + { + std::string SlashBuild = llvm::sys::path::convert_to_slash(BuildDir); + std::string SlashDepMod = llvm::sys::path::convert_to_slash(DepModPath); + std::string SlashExtMod = llvm::sys::path::convert_to_slash(ExtModPath); + std::string SlashMain = llvm::sys::path::convert_to_slash(MainPath); + std::string SlashDepPcm = llvm::sys::path::convert_to_slash(DepPcmPath); + std::string SlashMPcm = llvm::sys::path::convert_to_slash(MPcmPath); + 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 -o {4} {1}", + "file": "{1}" + }, + { + "directory": "{0}", + "command": "clang++ -std=c++20 -fmodule-file=Dep={4} --precompile -o {5} {2}", + "file": "{2}" + }, + { + "directory": "{0}", + "command": "clang++ -std=c++20 -fmodule-file=M={5} -c {3}", + "file": "{3}" + } +])json", + SlashBuild, SlashDepMod, SlashExtMod, SlashMain, SlashDepPcm, + SlashMPcm); + } + + DirectoryBasedGlobalCompilationDatabase DirCDB2(FS); + OverlayCDB CDB2(&DirCDB2); + ModulesBuilder BuilderWithPrebuilt(CDB2); + auto PrebuiltInfo = + BuilderWithPrebuilt.buildPrerequisiteModulesFor(MainPath, FS); + ASSERT_TRUE(PrebuiltInfo); + + HeaderSearchOptions PrebuiltHS(ProjectDir); + PrebuiltInfo->adjustHeaderSearchOptions(PrebuiltHS); + ASSERT_EQ(PrebuiltHS.PrebuiltModuleFiles.count("Dep"), 1u); + EXPECT_EQ(PrebuiltHS.PrebuiltModuleFiles["Dep"], std::string(DepPcmPath)); +} + TEST_F(PrerequisiteModulesTests, ModuleImportThroughInclude) { MockDirectoryCompilationDatabase CDB(TestDir, FS); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
