Skip to content

Commit 7982e4b

Browse files
committed
[AST] Handle null in printDeclDescription
Sink down the null Decl printing into `printDeclDescription` such that all callers benefit from it. This is an attempt at fixing at least the secondary crash in rdar://113491294, it does not address the underlying crash.
1 parent df2e2ee commit 7982e4b

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

Diff for: include/swift/AST/PrettyStackTrace.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class PrettyStackTraceLocation : public llvm::PrettyStackTraceEntry {
5959
};
6060

6161
void printDeclDescription(llvm::raw_ostream &out, const Decl *D,
62-
const ASTContext &Context, bool addNewline = true);
62+
bool addNewline = true);
6363

6464
/// PrettyStackTraceDecl - Observe that we are processing a specific
6565
/// declaration.

Diff for: lib/AST/PrettyStackTrace.cpp

+18-21
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,25 @@ using namespace swift;
3535

3636
void PrettyStackTraceDecl::print(llvm::raw_ostream &out) const {
3737
out << "While " << Action << ' ';
38-
if (!TheDecl) {
39-
out << "NULL declaration!\n";
40-
return;
41-
}
42-
printDeclDescription(out, TheDecl, TheDecl->getASTContext());
38+
printDeclDescription(out, TheDecl);
4339
}
4440

4541
void PrettyStackTraceDeclAndSubst::print(llvm::raw_ostream &out) const {
4642
out << "While " << action << ' ';
47-
if (!decl) {
48-
out << "NULL declaration!\n";
49-
return;
50-
}
51-
printDeclDescription(out, decl, decl->getASTContext());
43+
printDeclDescription(out, decl);
5244

5345
out << "with substitution map: ";
5446
subst.dump(out);
5547
}
5648

5749

5850
void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
59-
const ASTContext &Context, bool addNewline) {
51+
bool addNewline) {
52+
if (!D) {
53+
out << "NULL declaration!";
54+
if (addNewline) out << '\n';
55+
return;
56+
}
6057
SourceLoc loc = D->getStartLoc();
6158
bool hasPrintedName = false;
6259
if (auto *named = dyn_cast<ValueDecl>(D)) {
@@ -114,7 +111,7 @@ void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
114111

115112
if (loc.isValid()) {
116113
out << " (at ";
117-
loc.print(out, Context.SourceMgr);
114+
loc.print(out, D->getASTContext().SourceMgr);
118115
out << ')';
119116
} else {
120117
out << " (in module '" << D->getModuleContext()->getName() << "')";
@@ -124,25 +121,25 @@ void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
124121

125122
void PrettyStackTraceAnyFunctionRef::print(llvm::raw_ostream &out) const {
126123
out << "While " << Action << ' ';
127-
auto &Context = TheRef.getAsDeclContext()->getASTContext();
128-
if (auto *AFD = TheRef.getAbstractFunctionDecl())
129-
printDeclDescription(out, AFD, Context);
130-
else {
124+
if (auto *AFD = TheRef.getAbstractFunctionDecl()) {
125+
printDeclDescription(out, AFD);
126+
} else {
131127
auto *ACE = TheRef.getAbstractClosureExpr();
132-
printExprDescription(out, ACE, Context);
128+
printExprDescription(out, ACE, ACE->getASTContext());
133129
}
134130
}
135131

136132
void PrettyStackTraceFreestandingMacroExpansion::print(
137133
llvm::raw_ostream &out) const {
138134
out << "While " << Action << ' ';
139-
auto &Context = Expansion->getDeclContext()->getASTContext();
140135
switch (Expansion->getFreestandingMacroKind()) {
141-
case FreestandingMacroKind::Expr:
136+
case FreestandingMacroKind::Expr: {
137+
auto &Context = Expansion->getDeclContext()->getASTContext();
142138
printExprDescription(out, cast<MacroExpansionExpr>(Expansion), Context);
143139
break;
140+
}
144141
case FreestandingMacroKind::Decl:
145-
printDeclDescription(out, cast<MacroExpansionDecl>(Expansion), Context);
142+
printDeclDescription(out, cast<MacroExpansionDecl>(Expansion));
146143
break;
147144
}
148145
}
@@ -277,7 +274,7 @@ void swift::printConformanceDescription(llvm::raw_ostream &out,
277274
}
278275

279276
out << "protocol conformance to ";
280-
printDeclDescription(out, conformance->getProtocol(), ctxt, /*newline*/false);
277+
printDeclDescription(out, conformance->getProtocol(), /*newline*/false);
281278
out << " for ";
282279
printTypeDescription(out, conformance->getType(), ctxt, addNewline);
283280
}

Diff for: lib/SIL/Utils/PrettyStackTrace.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void swift::printSILLocationDescription(llvm::raw_ostream &out,
4040
ASTContext &Context) {
4141
if (loc.isASTNode()) {
4242
if (auto decl = loc.getAsASTNode<Decl>()) {
43-
printDeclDescription(out, decl, Context);
43+
printDeclDescription(out, decl);
4444
} else if (auto expr = loc.getAsASTNode<Expr>()) {
4545
printExprDescription(out, expr, Context);
4646
} else if (auto stmt = loc.getAsASTNode<Stmt>()) {

0 commit comments

Comments
 (0)