Skip to content

Commit 54023f8

Browse files
committed
[Basic] Introduce abortWithPrettyStackTraceMessage
Introduce a convenience for aborting while printing a given message to a frame of the pretty stack trace. Use this in the existing places where we're currently doing this.
1 parent aedb869 commit 54023f8

File tree

5 files changed

+50
-30
lines changed

5 files changed

+50
-30
lines changed

include/swift/Basic/PrettyStackTrace.h

+11
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ class PrettyStackTraceSwiftVersion : public llvm::PrettyStackTraceEntry {
5050
void print(llvm::raw_ostream &OS) const override;
5151
};
5252

53+
/// Aborts the program, printing a given message to a PrettyStackTrace frame
54+
/// before exiting.
55+
[[noreturn]]
56+
void abortWithPrettyStackTraceMessage(
57+
llvm::function_ref<void(llvm::raw_ostream &)> message);
58+
59+
/// Aborts the program, printing a given message to a PrettyStackTrace frame
60+
/// before exiting.
61+
[[noreturn]]
62+
void abortWithPrettyStackTraceMessage(llvm::StringRef message);
63+
5364
} // end namespace swift
5465

5566
#endif // SWIFT_BASIC_PRETTYSTACKTRACE_H

lib/AST/ASTScopePrinting.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/AST/Stmt.h"
3030
#include "swift/AST/TypeRepr.h"
3131
#include "swift/Basic/Assertions.h"
32+
#include "swift/Basic/PrettyStackTrace.h"
3233
#include "swift/Basic/STLExtras.h"
3334
#include "llvm/Support/Compiler.h"
3435
#include <algorithm>
@@ -71,15 +72,11 @@ void ASTScopeImpl::dumpOneScopeMapLocation(
7172

7273
void ASTScopeImpl::abortWithVerificationError(
7374
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();
75+
abortWithPrettyStackTraceMessage([&](auto &out) {
76+
out << "ASTScopeImpl verification error in source file '"
77+
<< getSourceFile()->getFilename() << "':\n";
78+
messageFn(out);
79+
});
8380
}
8481

8582
#pragma mark printing

lib/Basic/PrettyStackTrace.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/Basic/PrettyStackTrace.h"
1919
#include "swift/Basic/QuotedString.h"
2020
#include "swift/Basic/Version.h"
21+
#include "llvm/ADT/SmallString.h"
2122
#include "llvm/Support/MemoryBuffer.h"
2223
#include "llvm/Support/raw_ostream.h"
2324

@@ -38,3 +39,18 @@ void PrettyStackTraceFileContents::print(llvm::raw_ostream &out) const {
3839
void PrettyStackTraceSwiftVersion::print(llvm::raw_ostream &out) const {
3940
out << version::getSwiftFullVersion() << '\n';
4041
}
42+
43+
void swift::abortWithPrettyStackTraceMessage(
44+
llvm::function_ref<void(llvm::raw_ostream &)> message) {
45+
llvm::SmallString<0> errorStr;
46+
llvm::raw_svector_ostream out(errorStr);
47+
message(out);
48+
llvm::PrettyStackTraceString trace(errorStr.c_str());
49+
abort();
50+
}
51+
52+
void swift::abortWithPrettyStackTraceMessage(StringRef message) {
53+
auto messageStr = message.str();
54+
llvm::PrettyStackTraceString trace(messageStr.c_str());
55+
abort();
56+
}

lib/Serialization/ModuleFileSharedCore.cpp

+14-17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/Module.h"
2020
#include "swift/Basic/Assertions.h"
2121
#include "swift/Basic/LangOptions.h"
22+
#include "swift/Basic/PrettyStackTrace.h"
2223
#include "swift/Parse/ParseVersion.h"
2324
#include "swift/Serialization/SerializedModuleLoader.h"
2425
#include "swift/Strings.h"
@@ -697,23 +698,19 @@ std::string ModuleFileSharedCore::Dependency::getPrettyPrintedPath() const {
697698
}
698699

699700
void ModuleFileSharedCore::fatal(llvm::Error error) const {
700-
llvm::SmallString<0> errorStr;
701-
llvm::raw_svector_ostream out(errorStr);
702-
703-
out << "*** DESERIALIZATION FAILURE ***\n";
704-
out << "*** If any module named here was modified in the SDK, please delete the ***\n";
705-
out << "*** new swiftmodule files from the SDK and keep only swiftinterfaces. ***\n";
706-
outputDiagnosticInfo(out);
707-
out << "\n";
708-
if (error) {
709-
handleAllErrors(std::move(error), [&](const llvm::ErrorInfoBase &ei) {
710-
ei.log(out);
711-
out << "\n";
712-
});
713-
}
714-
715-
llvm::PrettyStackTraceString trace(errorStr.c_str());
716-
abort();
701+
abortWithPrettyStackTraceMessage([&](auto &out) {
702+
out << "*** DESERIALIZATION FAILURE ***\n";
703+
out << "*** If any module named here was modified in the SDK, please delete the ***\n";
704+
out << "*** new swiftmodule files from the SDK and keep only swiftinterfaces. ***\n";
705+
outputDiagnosticInfo(out);
706+
out << "\n";
707+
if (error) {
708+
handleAllErrors(std::move(error), [&](const llvm::ErrorInfoBase &ei) {
709+
ei.log(out);
710+
out << "\n";
711+
});
712+
}
713+
});
717714
}
718715

719716
void ModuleFileSharedCore::outputDiagnosticInfo(llvm::raw_ostream &os) const {

lib/Serialization/Serialization.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "swift/Basic/FileSystem.h"
4747
#include "swift/Basic/LLVMExtras.h"
4848
#include "swift/Basic/PathRemapper.h"
49+
#include "swift/Basic/PrettyStackTrace.h"
4950
#include "swift/Basic/STLExtras.h"
5051
#include "swift/Basic/Version.h"
5152
#include "swift/ClangImporter/ClangImporter.h"
@@ -5314,8 +5315,7 @@ void Serializer::writeASTBlockEntity(const Decl *D) {
53145315
SWIFT_DEFER {
53155316
// This is important enough to leave on in Release builds.
53165317
if (initialOffset == Out.GetCurrentBitNo()) {
5317-
llvm::PrettyStackTraceString message("failed to serialize anything");
5318-
abort();
5318+
abortWithPrettyStackTraceMessage("failed to serialize anything");
53195319
}
53205320
};
53215321

@@ -6141,8 +6141,7 @@ void Serializer::writeASTBlockEntity(Type ty) {
61416141
SWIFT_DEFER {
61426142
// This is important enough to leave on in Release builds.
61436143
if (initialOffset == Out.GetCurrentBitNo()) {
6144-
llvm::PrettyStackTraceString message("failed to serialize anything");
6145-
abort();
6144+
abortWithPrettyStackTraceMessage("failed to serialize anything");
61466145
}
61476146
};
61486147

0 commit comments

Comments
 (0)