Skip to content

Commit 6d89ed3

Browse files
committed
[ASTScope] Use PrettyStackTraceString to print verification errors
Use the same approach as `ModuleFileSharedCore::fatal`, ensuring the verification error ends up in the crash log.
1 parent 1980f72 commit 6d89ed3

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

include/swift/AST/ASTScope.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
267267
void dumpOneScopeMapLocation(std::pair<unsigned, unsigned> lineColumn);
268268

269269
private:
270-
llvm::raw_ostream &verificationError() const;
270+
[[noreturn]]
271+
void abortWithVerificationError(
272+
llvm::function_ref<void(llvm::raw_ostream &)> messageFn) const;
271273

272274
#pragma mark - Scope tree creation
273275
public:

lib/AST/ASTScopePrinting.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,17 @@ void ASTScopeImpl::dumpOneScopeMapLocation(
6969
}
7070
}
7171

72-
llvm::raw_ostream &ASTScopeImpl::verificationError() const {
73-
return llvm::errs() << "ASTScopeImpl verification error in source file '"
74-
<< getSourceFile()->getFilename() << "': ";
72+
void ASTScopeImpl::abortWithVerificationError(
73+
llvm::function_ref<void(llvm::raw_ostream &)> messageFn) const {
74+
llvm::SmallString<0> errorStr;
75+
llvm::raw_svector_ostream out(errorStr);
76+
77+
out << "ASTScopeImpl verification error in source file '"
78+
<< getSourceFile()->getFilename() << "':\n";
79+
messageFn(out);
80+
81+
llvm::PrettyStackTraceString trace(errorStr.c_str());
82+
abort();
7583
}
7684

7785
#pragma mark printing

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ void ASTScopeImpl::checkSourceRangeBeforeAddingChild(ASTScopeImpl *child,
7676
auto childCharRange = child->getCharSourceRangeOfScope(sourceMgr);
7777

7878
if (!containedInParent(childCharRange)) {
79-
auto &out = verificationError() << "child not contained in its parent:\n";
80-
child->print(out);
81-
out << "\n***Parent node***\n";
82-
this->print(out);
83-
abort();
79+
abortWithVerificationError([&](llvm::raw_ostream &out) {
80+
out << "child not contained in its parent:\n";
81+
child->print(out);
82+
out << "\n***Parent node***\n";
83+
this->print(out);
84+
});
8485
}
8586

8687
if (!storedChildren.empty()) {
@@ -89,13 +90,14 @@ void ASTScopeImpl::checkSourceRangeBeforeAddingChild(ASTScopeImpl *child,
8990
sourceMgr).End;
9091

9192
if (!sourceMgr.isAtOrBefore(endOfPreviousChild, childCharRange.Start)) {
92-
auto &out = verificationError() << "child overlaps previous child:\n";
93-
child->print(out);
94-
out << "\n***Previous child\n";
95-
previousChild->print(out);
96-
out << "\n***Parent node***\n";
97-
this->print(out);
98-
abort();
93+
abortWithVerificationError([&](llvm::raw_ostream &out) {
94+
out << "child overlaps previous child:\n";
95+
child->print(out);
96+
out << "\n***Previous child\n";
97+
previousChild->print(out);
98+
out << "\n***Parent node***\n";
99+
this->print(out);
100+
});
99101
}
100102
}
101103
}

0 commit comments

Comments
 (0)