@@ -271,7 +271,6 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
271
271
if (auto enclosingSF = SF->getEnclosingSourceFile ()) {
272
272
SourceLoc parentLoc;
273
273
auto macroRole = SF->getFulfilledMacroRole ();
274
- auto expansion = SF->getMacroExpansion ();
275
274
276
275
// Determine the parent source location based on the macro role.
277
276
AbstractFunctionDecl *bodyForDecl = nullptr ;
@@ -283,35 +282,24 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
283
282
case MacroRole::MemberAttribute:
284
283
case MacroRole::Conformance:
285
284
case MacroRole::Extension:
286
- parentLoc = expansion. getStartLoc ();
287
- break ;
285
+ case MacroRole::Member:
286
+ case MacroRole::Peer:
288
287
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 ();
292
292
break ;
293
293
}
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 ();
295
300
parentLoc = expansion.getEndLoc ();
296
301
bodyForDecl = cast<AbstractFunctionDecl>(expansion.get <Decl *>());
297
302
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 ;
315
303
}
316
304
}
317
305
0 commit comments