Skip to content

Commit d672f75

Browse files
committed
[Basic] Rename abortWithPrettyStackTraceMessage to ABORT
Turn it into a wrapping macro that includes the file location, and move into `Assertions.h`.
1 parent ffb474e commit d672f75

File tree

8 files changed

+90
-56
lines changed

8 files changed

+90
-56
lines changed

include/swift/Basic/Assertions.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef SWIFT_BASIC_ASSERTIONS_H
1818
#define SWIFT_BASIC_ASSERTIONS_H
1919

20+
#include "swift/Basic/LLVM.h"
21+
2022
// Only for use in this header
2123
#if __has_builtin(__builtin_expect)
2224
#define ASSERT_UNLIKELY(expression) (__builtin_expect(!!(expression), 0))
@@ -183,4 +185,33 @@ extern int CONDITIONAL_ASSERT_Global_enable_flag;
183185
#define SWIFT_ASSERT_ONLY_DECL DEBUG_ASSERT_DECL
184186
#define SWIFT_ASSERT_ONLY DEBUG_ASSERT_EXPR
185187

188+
// ================================ Abort ======================================
189+
190+
/// Implementation for \c ABORT, not to be used directly.
191+
[[noreturn]]
192+
void _ABORT(const char *file, int line, const char *func,
193+
llvm::function_ref<void(llvm::raw_ostream &)> message);
194+
195+
/// Implementation for \c ABORT, not to be used directly.
196+
[[noreturn]]
197+
void _ABORT(const char *file, int line, const char *func,
198+
llvm::StringRef message);
199+
200+
// Aborts the program, printing a given message to a PrettyStackTrace frame
201+
// before exiting. This should be preferred over manually logging to stderr and
202+
// `abort()`'ing since that won't be picked up by the crash reporter.
203+
//
204+
// There are two different forms of ABORT:
205+
//
206+
// ```
207+
// ABORT("abort with string");
208+
//
209+
// ABORT([&](auto &out) {
210+
// out << "abort with arbitrary stream";
211+
// node.dump(out);
212+
// });
213+
// ```
214+
//
215+
#define ABORT(arg) _ABORT(_FILENAME_FOR_ASSERT, __LINE__, __func__, (arg))
216+
186217
#endif // SWIFT_BASIC_ASSERTIONS_H

include/swift/Basic/PrettyStackTrace.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,6 @@ 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. This should be preferred over manually logging to stderr and
55-
/// aborting since that won't be picked up by the crash reporter.
56-
[[noreturn]]
57-
void abortWithPrettyStackTraceMessage(
58-
llvm::function_ref<void(llvm::raw_ostream &)> message);
59-
60-
/// Aborts the program, printing a given message to a PrettyStackTrace frame
61-
/// before exiting. This should be preferred over manually logging to stderr and
62-
/// aborting since that won't be picked up by the crash reporter.
63-
[[noreturn]]
64-
void abortWithPrettyStackTraceMessage(llvm::StringRef message);
65-
6653
} // end namespace swift
6754

6855
#endif // SWIFT_BASIC_PRETTYSTACKTRACE_H

lib/AST/ASTScopePrinting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void ASTScopeImpl::dumpOneScopeMapLocation(
7272

7373
void ASTScopeImpl::abortWithVerificationError(
7474
llvm::function_ref<void(llvm::raw_ostream &)> messageFn) const {
75-
abortWithPrettyStackTraceMessage([&](auto &out) {
75+
ABORT([&](auto &out) {
7676
out << "ASTScopeImpl verification error in source file '"
7777
<< getSourceFile()->getFilename() << "':\n";
7878
messageFn(out);

lib/Basic/Assertions.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#include "llvm/ADT/SmallString.h"
1718
#include "llvm/Support/CommandLine.h"
19+
#include "llvm/Support/PrettyStackTrace.h"
1820
#include "llvm/Support/raw_ostream.h"
1921
#include "swift/Basic/Assertions.h"
2022
#include <iostream>
@@ -52,6 +54,9 @@ static void ASSERT_help() {
5254
llvm::errs() << " Continue after any failed assertion\n\n";
5355
}
5456

57+
[[noreturn]]
58+
static inline void _abortWithMessage(llvm::StringRef message);
59+
5560
void ASSERT_failure(const char *expr, const char *filename, int line, const char *func) {
5661
// Find the last component of `filename`
5762
// Needed on Windows MSVC, which lacks __FILE_NAME__
@@ -86,3 +91,50 @@ void ASSERT_failure(const char *expr, const char *filename, int line, const char
8691
int CONDITIONAL_ASSERT_enabled() {
8792
return (CONDITIONAL_ASSERT_Global_enable_flag != 0);
8893
}
94+
95+
// MARK: ABORT
96+
97+
namespace {
98+
/// Similar to PrettyStackTraceString, but formats multi-line strings for
99+
/// the stack trace.
100+
class PrettyStackTraceMultilineString : public llvm::PrettyStackTraceEntry {
101+
llvm::StringRef Str;
102+
103+
public:
104+
PrettyStackTraceMultilineString(llvm::StringRef str) : Str(str) {}
105+
void print(llvm::raw_ostream &OS) const override {
106+
// For each line, add a leading character and indentation to better match
107+
// the formatting of the stack trace.
108+
for (auto c : Str.rtrim('\n')) {
109+
OS << c;
110+
if (c == '\n')
111+
OS << "| \t";
112+
}
113+
OS << '\n';
114+
}
115+
};
116+
} // end anonymous namespace
117+
118+
static void _abortWithMessage(llvm::StringRef message) {
119+
// Use a pretty stack trace to ensure the message gets picked up the
120+
// crash reporter.
121+
PrettyStackTraceMultilineString trace(message);
122+
123+
abort();
124+
}
125+
126+
void _ABORT(const char *file, int line, const char *func,
127+
llvm::function_ref<void(llvm::raw_ostream &)> message) {
128+
llvm::SmallString<0> errorStr;
129+
llvm::raw_svector_ostream out(errorStr);
130+
out << "Abort: " << "function " << func << " at "
131+
<< file << ":" << line << "\n";
132+
message(out);
133+
134+
_abortWithMessage(errorStr);
135+
}
136+
137+
void _ABORT(const char *file, int line, const char *func,
138+
llvm::StringRef message) {
139+
_ABORT(file, line, func, [&](auto &out) { out << message; });
140+
}

lib/Basic/Mangler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,21 @@ void Mangler::verify(StringRef nameStr, ManglingFlavor Flavor) {
202202
Demangler Dem;
203203
NodePointer Root = Dem.demangleSymbol(nameStr);
204204
if (!Root || treeContains(Root, Node::Kind::Suffix)) {
205-
abortWithPrettyStackTraceMessage([&](auto &out) {
205+
ABORT([&](auto &out) {
206206
out << "Can't demangle: " << nameStr;
207207
});
208208
}
209209
auto mangling = mangleNode(Root, Flavor);
210210
if (!mangling.isSuccess()) {
211-
abortWithPrettyStackTraceMessage([&](auto &out) {
211+
ABORT([&](auto &out) {
212212
out << "Can't remangle: " << nameStr;
213213
});
214214
}
215215
std::string Remangled = mangling.result();
216216
if (Remangled == nameStr)
217217
return;
218218

219-
abortWithPrettyStackTraceMessage([&](auto &out) {
219+
ABORT([&](auto &out) {
220220
out << "Remangling failed:\n";
221221
out << "original = " << nameStr << "\n";
222222
out << "remangled = " << Remangled;

lib/Basic/PrettyStackTrace.cpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,3 @@ void PrettyStackTraceFileContents::print(llvm::raw_ostream &out) const {
3939
void PrettyStackTraceSwiftVersion::print(llvm::raw_ostream &out) const {
4040
out << version::getSwiftFullVersion() << '\n';
4141
}
42-
43-
namespace {
44-
/// Similar to PrettyStackTraceString, but formats multi-line strings for
45-
/// the stack trace.
46-
class PrettyStackTraceMultilineString : public llvm::PrettyStackTraceEntry {
47-
StringRef Str;
48-
49-
public:
50-
PrettyStackTraceMultilineString(StringRef str) : Str(str) {}
51-
void print(raw_ostream &OS) const override {
52-
// For each line, add a leading character and indentation to better match
53-
// the formatting of the stack trace.
54-
for (auto c : Str.rtrim('\n')) {
55-
OS << c;
56-
if (c == '\n')
57-
OS << "| \t";
58-
}
59-
OS << '\n';
60-
}
61-
};
62-
} // end anonymous namespace
63-
64-
void swift::abortWithPrettyStackTraceMessage(
65-
llvm::function_ref<void(llvm::raw_ostream &)> message) {
66-
llvm::SmallString<0> errorStr;
67-
llvm::raw_svector_ostream out(errorStr);
68-
message(out);
69-
70-
PrettyStackTraceMultilineString trace(errorStr);
71-
abort();
72-
}
73-
74-
void swift::abortWithPrettyStackTraceMessage(StringRef message) {
75-
PrettyStackTraceMultilineString trace(message);
76-
abort();
77-
}

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ std::string ModuleFileSharedCore::Dependency::getPrettyPrintedPath() const {
695695
}
696696

697697
void ModuleFileSharedCore::fatal(llvm::Error error) const {
698-
abortWithPrettyStackTraceMessage([&](auto &out) {
698+
ABORT([&](auto &out) {
699699
out << "*** DESERIALIZATION FAILURE ***\n";
700700
out << "*** If any module named here was modified in the SDK, please delete the ***\n";
701701
out << "*** new swiftmodule files from the SDK and keep only swiftinterfaces. ***\n";

lib/Serialization/Serialization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5337,7 +5337,7 @@ void Serializer::writeASTBlockEntity(const Decl *D) {
53375337
SWIFT_DEFER {
53385338
// This is important enough to leave on in Release builds.
53395339
if (initialOffset == Out.GetCurrentBitNo()) {
5340-
abortWithPrettyStackTraceMessage("failed to serialize anything");
5340+
ABORT("failed to serialize anything");
53415341
}
53425342
};
53435343

@@ -6164,7 +6164,7 @@ void Serializer::writeASTBlockEntity(Type ty) {
61646164
SWIFT_DEFER {
61656165
// This is important enough to leave on in Release builds.
61666166
if (initialOffset == Out.GetCurrentBitNo()) {
6167-
abortWithPrettyStackTraceMessage("failed to serialize anything");
6167+
ABORT("failed to serialize anything");
61686168
}
61696169
};
61706170

0 commit comments

Comments
 (0)