[clang-tools-extra] [clangd] Resolve out-of-tree module units via ProjectModules (PR #242615)
Atharva Potdar via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 00:02:23 PST 2026
================
@@ -94,17 +94,55 @@ std::string normalizePathForCache(PathRef Path) {
return maybeCaseFoldPath(Normalized);
}
+/// Returns the project source root used for the module cache of
+/// \p ModuleUnitFileName. The module unit's own project root is used only when
+/// the global CDB has an exact entry for it. If it has none, or only an
+/// interpolated (heuristic) command, as with an unrelated ancestor
+/// compile_commands.json or a system `std.cppm`, fall back to the consuming
+/// translation unit's (\p RequiredSource) project root.
+std::optional<std::string>
+getModuleProjectRoot(PathRef RequiredSource, PathRef ModuleUnitFileName,
+ const GlobalCompilationDatabase &CDB) {
+ auto Cmd = CDB.getCompileCommand(ModuleUnitFileName);
+ if (Cmd && Cmd->Heuristic.empty()) {
+ 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 compile command for a module unit. The global CDB wins only
+/// when it has an exact entry. If it has none, or only a heuristic guess
+/// (e.g. an unrelated ancestor compile_commands.json), prefer the consuming
+/// project's CDB, then fall back to whatever the global CDB gave us.
+std::optional<tooling::CompileCommand>
+getModuleUnitCompileCommand(PathRef ModuleUnitFileName,
+ const GlobalCompilationDatabase &CDB,
+ const ProjectModules &MDB) {
+ auto Cmd = CDB.getCompileCommand(ModuleUnitFileName);
+ if (Cmd && Cmd->Heuristic.empty())
----------------
atharva-potdar wrote:
I've updated this so MDB is checked first and the global CDB is only used as a fallback. That also handles the compile_flags.txt case which I've added a test for. I've checked that it fails without this change. Done in d03d89e
I left getModuleProjectRoot as is because when I tried the same ordering there, `PersistentModuleCacheIgnoresRequiredSourceForOnDiskPath` failed, so a stray compile_flags.txt can still affect the cache dir.
https://github.com/llvm/llvm-project/pull/242615
More information about the cfe-commits
mailing list