Skip to content

Commit 42ebee8

Browse files
committed
AST: Improve TypeRefinementContext debug descriptions.
When printing the source ranges for nodes that represent an expansion within a source file, print the range from the original source file and include a description of the kind of expansion.
1 parent ac38469 commit 42ebee8

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

include/swift/Basic/SourceManager.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class GeneratedSourceInfo {
5555
enum Kind {
5656
#define MACRO_ROLE(Name, Description) Name##MacroExpansion,
5757
#include "swift/Basic/MacroRoles.def"
58+
#undef MACRO_ROLE
5859

5960
/// A new function body that is replacing an existing function body.
6061
ReplacedFunctionBody,
@@ -66,6 +67,23 @@ class GeneratedSourceInfo {
6667
DefaultArgument,
6768
} kind;
6869

70+
static StringRef kindToString(GeneratedSourceInfo::Kind kind) {
71+
switch (kind) {
72+
#define MACRO_ROLE(Name, Description) \
73+
case Name##MacroExpansion: \
74+
return #Name "MacroExpansion";
75+
#include "swift/Basic/MacroRoles.def"
76+
#undef MACRO_ROLE
77+
case ReplacedFunctionBody:
78+
return "ReplacedFunctionBody";
79+
case PrettyPrinted:
80+
return "PrettyPrinted";
81+
case DefaultArgument:
82+
return "DefaultArgument";
83+
}
84+
llvm_unreachable("Invalid kind");
85+
}
86+
6987
/// The source range in the enclosing buffer where this source was generated.
7088
///
7189
/// This could point at a macro expansion or at the implicit location at

lib/AST/TypeRefinementContext.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,24 @@ void TypeRefinementContext::print(raw_ostream &OS, SourceManager &SrcMgr,
404404
auto R = getSourceRange();
405405
if (R.isValid()) {
406406
OS << " src_range=";
407-
R.print(OS, SrcMgr, /*PrintText=*/false);
407+
408+
if (getReason() != Reason::Root) {
409+
R.print(OS, SrcMgr, /*PrintText=*/false);
410+
} else if (auto info = SrcMgr.getGeneratedSourceInfo(
411+
Node.getAsSourceFile()->getBufferID())) {
412+
info->originalSourceRange.print(OS, SrcMgr, /*PrintText=*/false);
413+
} else {
414+
OS << "<unknown>";
415+
}
416+
}
417+
418+
if (getReason() == Reason::Root) {
419+
if (auto info = SrcMgr.getGeneratedSourceInfo(
420+
Node.getAsSourceFile()->getBufferID())) {
421+
OS << " generated_kind=" << GeneratedSourceInfo::kindToString(info->kind);
422+
} else {
423+
OS << " file=" << Node.getAsSourceFile()->getFilename().str();
424+
}
408425
}
409426

410427
if (!ExplicitAvailabilityInfo.isAlwaysAvailable())

0 commit comments

Comments
 (0)