@@ -176,7 +176,7 @@ SILGenFunction::getSILDebugLocation(SILBuilder &B, SILLocation Loc,
176176 const SILDebugScope *Scope = B.getCurrentDebugScope ();
177177 if (!Scope)
178178 Scope = F.getDebugScope ();
179- if (auto *SILScope = getScopeOrNull (Loc)) {
179+ if (auto *SILScope = getScopeOrNull (Loc, ForMetaInstruction )) {
180180 Scope = SILScope;
181181 // Metainstructions such as a debug_value may break the flow of scopes and
182182 // should not change the state of the builder.
@@ -187,14 +187,16 @@ SILGenFunction::getSILDebugLocation(SILBuilder &B, SILLocation Loc,
187187 return SILDebugLocation (overriddenLoc, Scope);
188188}
189189
190- const SILDebugScope *SILGenFunction::getScopeOrNull (SILLocation Loc) {
191- if (Loc.getKind () == SILLocation::CleanupKind ||
192- Loc.getKind () == SILLocation::ImplicitReturnKind ||
193- // The source locations produced by the ResultBuilder transformation are
194- // all over the place.
195- Loc.isImplicit () ||
196- Loc.isAutoGenerated ())
197- return nullptr ;
190+ const SILDebugScope *SILGenFunction::getScopeOrNull (SILLocation Loc,
191+ bool ForMetaInstruction) {
192+ if (!ForMetaInstruction) {
193+ if (Loc.getKind () == SILLocation::CleanupKind ||
194+ Loc.getKind () == SILLocation::ImplicitReturnKind ||
195+ // The source locations produced by the ResultBuilder transformation are
196+ // all over the place.
197+ Loc.isImplicit () || Loc.isAutoGenerated ())
198+ return nullptr ;
199+ }
198200
199201 SourceLoc SLoc = Loc.getSourceLoc ();
200202 if (!SF || LastSourceLoc == SLoc)
@@ -218,35 +220,44 @@ const SILDebugScope *SILGenFunction::getOrCreateScope(SourceLoc SLoc) {
218220 return Scope;
219221}
220222
221- static std::pair<SILLocation, std::string>
222- getMacroName (GeneratedSourceInfo &Info) {
223+ namespace {
224+ struct MacroInfo {
225+ MacroInfo (SourceLoc SLoc) : Loc(SLoc) {}
226+ RegularLocation Loc;
227+ std::string Name;
228+ bool Freestanding = false ;
229+ };
230+ }
231+
232+ static MacroInfo getMacroInfo (GeneratedSourceInfo &Info) {
223233 SourceLoc MacroSLoc = Info.generatedSourceRange .getStart ();
224- RegularLocation Loc (MacroSLoc);
225- Mangle::ASTMangler mangler;
226- std::string Name = " __unknown_macro__" ;
234+ MacroInfo Result (MacroSLoc);
235+ Result.Name = " __unknown_macro__" ;
227236 if (!Info.astNode )
228- return {Loc, Name} ;
237+ return Result ;
229238
230239 // Keep this in sync with ASTMangler::appendMacroExpansionContext().
240+ Mangle::ASTMangler mangler;
231241 switch (Info.kind ) {
232242 case GeneratedSourceInfo::ExpressionMacroExpansion: {
233243 auto parent = ASTNode::getFromOpaqueValue (Info.astNode );
234244 if (auto expr =
235245 cast_or_null<MacroExpansionExpr>(parent.dyn_cast <Expr *>())) {
236- Loc = RegularLocation (expr);
237- Name = mangler.mangleMacroExpansion (expr);
246+ Result. Loc = RegularLocation (expr);
247+ Result. Name = mangler.mangleMacroExpansion (expr);
238248 } else {
239249 auto decl = cast<MacroExpansionDecl>(parent.get <Decl *>());
240- Loc = RegularLocation (decl);
241- Name = mangler.mangleMacroExpansion (decl);
250+ Result. Loc = RegularLocation (decl);
251+ Result. Name = mangler.mangleMacroExpansion (decl);
242252 }
243253 break ;
244254 }
245255 case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
246256 auto expansion = cast<MacroExpansionDecl>(
247257 ASTNode::getFromOpaqueValue (Info.astNode ).get <Decl *>());
248- Loc = RegularLocation (expansion);
249- Name = mangler.mangleMacroExpansion (expansion);
258+ Result.Loc = RegularLocation (expansion);
259+ Result.Name = mangler.mangleMacroExpansion (expansion);
260+ Result.Freestanding = true ;
250261 break ;
251262 }
252263 case GeneratedSourceInfo::AccessorMacroExpansion:
@@ -257,16 +268,17 @@ getMacroName(GeneratedSourceInfo &Info) {
257268 auto decl = ASTNode::getFromOpaqueValue (Info.astNode ).get <Decl *>();
258269 auto attr = Info.attachedMacroCustomAttr ;
259270 if (auto *macroDecl = decl->getResolvedMacro (attr)) {
260- Loc = RegularLocation (macroDecl);
261- Name = macroDecl->getBaseName ().userFacingName ();
271+ Result.Loc = RegularLocation (macroDecl);
272+ Result.Name = macroDecl->getBaseName ().userFacingName ();
273+ Result.Freestanding = true ;
262274 }
263275 break ;
264276 }
265277 case GeneratedSourceInfo::PrettyPrinted:
266278 case GeneratedSourceInfo::ReplacedFunctionBody:
267279 break ;
268280 }
269- return {Loc, Name} ;
281+ return Result ;
270282}
271283
272284const SILDebugScope *SILGenFunction::getMacroScope (SourceLoc SLoc) {
@@ -276,6 +288,15 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
276288 if (!GeneratedSourceInfo)
277289 return nullptr ;
278290
291+ // There is no good way to represent freestanding macros as inlined functions,
292+ // because entire function would need to be "inlined" into a top-level
293+ // declaration that isn't part of a real function. By not handling them here,
294+ // source locations will still point into the macro expansion buffer, but
295+ // debug info doesn't know what macro that buffer was expanded from.
296+ auto Macro = getMacroInfo (*GeneratedSourceInfo);
297+ if (Macro.Freestanding )
298+ return nullptr ;
299+
279300 SourceLoc OrigSLoc = GeneratedSourceInfo->originalSourceRange .getStart ();
280301 if (!OrigSLoc)
281302 return nullptr ;
@@ -296,22 +317,24 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
296317 /* yields*/
297318 {},
298319 /* Results*/ {}, None, SubstitutionMap (), SubstitutionMap (), ASTContext);
299- auto LocName = getMacroName (*GeneratedSourceInfo);
300- RegularLocation MacroLoc (LocName.first );
301- StringRef MacroName = ASTContext.getIdentifier (LocName.second ).str ();
320+ StringRef MacroName = ASTContext.getIdentifier (Macro.Name ).str ();
321+
302322 SILFunction *MacroFn = B.getOrCreateFunction (
303- MacroLoc , MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
323+ Macro. Loc , MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
304324 IsNotBare, IsNotTransparent, IsNotSerialized, IsNotDynamic,
305325 IsNotDistributed, IsNotRuntimeAccessible);
306326 // At the end of the chain OrigSLoc should be a macro expansion node.
307- const SILDebugScope *InlinedAt = getOrCreateScope (OrigSLoc);
327+ const SILDebugScope *InlinedAt = nullptr ;
328+ const SILDebugScope *OrigScope = getOrCreateScope (OrigSLoc);
329+ RegularLocation OrigLoc (OrigSLoc);
308330 // Inject an extra scope to hold the inlined call site.
309- if (InlinedAt)
310- InlinedAt =
311- new (SGM.M ) SILDebugScope (RegularLocation (OrigSLoc), nullptr , InlinedAt,
312- InlinedAt->InlinedCallSite );
331+ if (OrigScope)
332+ InlinedAt = new (SGM.M )
333+ SILDebugScope (Macro.Freestanding ? Macro.Loc : OrigLoc, nullptr ,
334+ OrigScope, OrigScope->InlinedCallSite );
335+
313336 const SILDebugScope *Scope =
314- new (SGM.M ) SILDebugScope (MacroLoc , MacroFn, nullptr , InlinedAt);
337+ new (SGM.M ) SILDebugScope (Macro. Loc , MacroFn, nullptr , InlinedAt);
315338
316339 InlinedScopeMap.insert ({BufferID, Scope});
317340 return Scope;
0 commit comments