@@ -258,11 +258,11 @@ static bool DiagReservedModuleName(Sema &S, const IdentifierInfo *II,
258258Sema::DeclGroupPtrTy
259259Sema::ActOnModuleDecl (SourceLocation StartLoc, SourceLocation ModuleLoc,
260260 ModuleDeclKind MDK, ModuleIdPath Path,
261- ModuleIdPath Partition, ModuleImportState &ImportState) {
261+ ModuleIdPath Partition, ModuleImportState &ImportState,
262+ bool IntroducerIsFirstPPToken) {
262263 assert (getLangOpts ().CPlusPlusModules &&
263264 " should only have module decl in standard C++ modules" );
264265
265- bool IsFirstDecl = ImportState == ModuleImportState::FirstDecl;
266266 bool SeenGMF = ImportState == ModuleImportState::GlobalFragment;
267267 // If any of the steps here fail, we count that as invalidating C++20
268268 // module state;
@@ -328,14 +328,11 @@ Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
328328 SeenGMF == (bool )this ->TheGlobalModuleFragment ) &&
329329 " mismatched global module state" );
330330
331- // In C++20, the module-declaration must be the first declaration if there
332- // is no global module fragment.
333- if (getLangOpts ().CPlusPlusModules && !IsFirstDecl && !SeenGMF) {
331+ // In C++20, A module directive may only appear as the first preprocessing
332+ // tokens in a file (excluding the global module fragment.) .
333+ if (getLangOpts ().CPlusPlusModules && !IntroducerIsFirstPPToken && !SeenGMF) {
334334 Diag (ModuleLoc, diag::err_module_decl_not_at_start);
335- SourceLocation BeginLoc =
336- ModuleScopes.empty ()
337- ? SourceMgr.getLocForStartOfFile (SourceMgr.getMainFileID ())
338- : ModuleScopes.back ().BeginLoc ;
335+ SourceLocation BeginLoc = PP.getMainFileFirstPPToken ().getLocation ();
339336 if (BeginLoc.isValid ()) {
340337 Diag (BeginLoc, diag::note_global_module_introducer_missing)
341338 << FixItHint::CreateInsertion (BeginLoc, " module;\n " );
0 commit comments