@@ -232,8 +232,16 @@ struct MacroInfo {
232232};
233233}
234234
235+ static DeclContext *getInnermostFunctionContext (DeclContext *DC) {
236+ for (; DC; DC = DC->getParent ())
237+ if (DC->getContextKind () == DeclContextKind::AbstractFunctionDecl)
238+ return DC;
239+ return nullptr ;
240+ }
241+
235242// / Return location of the macro expansion and the macro name.
236- static MacroInfo getMacroInfo (GeneratedSourceInfo &Info) {
243+ static MacroInfo getMacroInfo (GeneratedSourceInfo &Info,
244+ DeclContext *FunctionDC) {
237245 MacroInfo Result (Info.generatedSourceRange .getStart (),
238246 Info.originalSourceRange .getStart ());
239247 if (!Info.astNode )
@@ -252,6 +260,13 @@ static MacroInfo getMacroInfo(GeneratedSourceInfo &Info) {
252260 Result.ExpansionLoc = RegularLocation (decl);
253261 Result.Name = mangler.mangleMacroExpansion (decl);
254262 }
263+ // If the parent function of the macro expansion expression is not the
264+ // current function, then the macro expanded to a closure or nested
265+ // function. As far as the generated SIL is concerned this is the same as a
266+ // function generated from a freestanding macro expansion.
267+ DeclContext *MacroContext = getInnermostFunctionContext (Info.declContext );
268+ if (MacroContext != FunctionDC)
269+ Result.Freestanding = true ;
255270 break ;
256271 }
257272 case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
@@ -295,7 +310,7 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
295310 // declaration that isn't part of a real function. By not handling them here,
296311 // source locations will still point into the macro expansion buffer, but
297312 // debug info doesn't know what macro that buffer was expanded from.
298- auto Macro = getMacroInfo (*GeneratedSourceInfo);
313+ auto Macro = getMacroInfo (*GeneratedSourceInfo, FunctionDC );
299314 if (Macro.Freestanding )
300315 return nullptr ;
301316
0 commit comments