From d9cea8d3a8fff86672174780312674871729578c Mon Sep 17 00:00:00 2001 From: Iain Sandoe Date: Wed, 2 Mar 2022 20:18:10 +0000 Subject: [C++20][Modules][HU 4/5] Handle pre-processed header units. We wish to support emitting a pre-processed output for an importable header unit, that can be consumed to produce the same header units as the original source. This means that ee need to find the original filename used to produce the re-preprocessed output, so that it can be assigned as the module name. This is peeked from the first line of the pre-processed source when the action sets up the files. Differential Revision: https://reviews.llvm.org/D121098 --- clang/lib/Sema/SemaModule.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'clang/lib/Sema/SemaModule.cpp') diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp index e28de8c..893b913 100644 --- a/clang/lib/Sema/SemaModule.cpp +++ b/clang/lib/Sema/SemaModule.cpp @@ -109,10 +109,18 @@ void Sema::HandleStartOfHeaderUnit() { const_cast(getLangOpts()).CurrentModule = HUName.str(); } - auto &Map = PP.getHeaderSearchInfo().getModuleMap(); // TODO: Make the C++20 header lookup independent. - Module::Header H{getLangOpts().CurrentModule, getLangOpts().CurrentModule, - SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())}; + // When the input is pre-processed source, we need a file ref to the original + // file for the header map. + auto F = SourceMgr.getFileManager().getFile(HUName); + // For the sake of error recovery (if someone has moved the original header + // after creating the pre-processed output) fall back to obtaining the file + // ref for the input file, which must be present. + if (!F) + F = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); + assert(F && "failed to find the header unit source?"); + Module::Header H{HUName.str(), HUName.str(), *F}; + auto &Map = PP.getHeaderSearchInfo().getModuleMap(); Module *Mod = Map.createHeaderUnit(StartOfTU, HUName, H); assert(Mod && "module creation should not fail"); ModuleScopes.push_back({}); // No GMF -- cgit v1.1