@@ -271,7 +271,6 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
271271 if (auto enclosingSF = SF->getEnclosingSourceFile ()) {
272272 SourceLoc parentLoc;
273273 auto macroRole = SF->getFulfilledMacroRole ();
274- auto expansion = SF->getMacroExpansion ();
275274
276275 // Determine the parent source location based on the macro role.
277276 AbstractFunctionDecl *bodyForDecl = nullptr ;
@@ -283,35 +282,24 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
283282 case MacroRole::MemberAttribute:
284283 case MacroRole::Conformance:
285284 case MacroRole::Extension:
286- parentLoc = expansion. getStartLoc ();
287- break ;
285+ case MacroRole::Member:
286+ case MacroRole::Peer:
288287 case MacroRole::Preamble: {
289- // Preamble macro roles start at the beginning of the macro body.
290- auto func = cast<AbstractFunctionDecl>(expansion.get <Decl *>());
291- parentLoc = func->getMacroExpandedBody ()->getStartLoc ();
288+ auto &ctx = SF->getASTContext ();
289+ auto generatedSourceInfo =
290+ *ctx.SourceMgr .getGeneratedSourceInfo (*SF->getBufferID ());
291+ parentLoc = generatedSourceInfo.originalSourceRange .getEnd ();
292292 break ;
293293 }
294- case MacroRole::Body:
294+ case MacroRole::Body: {
295+ // Use the end location of the function decl itself as the parentLoc
296+ // for the new function body scope. This is different from the end
297+ // location of the original source range, which is after the end of the
298+ // function decl.
299+ auto expansion = SF->getMacroExpansion ();
295300 parentLoc = expansion.getEndLoc ();
296301 bodyForDecl = cast<AbstractFunctionDecl>(expansion.get <Decl *>());
297302 break ;
298- case MacroRole::Peer: {
299- ASTContext &ctx = SF->getASTContext ();
300- SourceManager &sourceMgr = ctx.SourceMgr ;
301- auto generatedSourceInfo =
302- *sourceMgr.getGeneratedSourceInfo (*SF->getBufferID ());
303-
304- ASTNode node = ASTNode::getFromOpaqueValue (generatedSourceInfo.astNode );
305- parentLoc = Lexer::getLocForEndOfToken (sourceMgr, node.getEndLoc ());
306- break ;
307- }
308- case MacroRole::Member: {
309- // For synthesized member macros, take the end loc of the
310- // enclosing declaration (before the closing brace), because
311- // the macro expansion is inside this scope.
312- auto *decl = expansion.getAsDeclContext ()->getAsDecl ();
313- parentLoc = decl->getEndLoc ();
314- break ;
315303 }
316304 }
317305
0 commit comments