Skip to content

Commit 8834f6c

Browse files
authored
Merge pull request #80496 from hamishknight/message-in-a-crash-log
[Mangler] Include verification errors in the crash log
2 parents a20b568 + abf8a81 commit 8834f6c

File tree

6 files changed

+62
-38
lines changed

6 files changed

+62
-38
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/Mangler.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "swift/Basic/Mangler.h"
1414
#include "swift/Basic/Assertions.h"
15+
#include "swift/Basic/PrettyStackTrace.h"
1516
#include "swift/Demangling/Demangler.h"
1617
#include "swift/Demangling/ManglingMacros.h"
1718
#include "swift/Demangling/Punycode.h"
@@ -201,22 +202,25 @@ void Mangler::verify(StringRef nameStr, ManglingFlavor Flavor) {
201202
Demangler Dem;
202203
NodePointer Root = Dem.demangleSymbol(nameStr);
203204
if (!Root || treeContains(Root, Node::Kind::Suffix)) {
204-
llvm::errs() << "Can't demangle: " << nameStr << '\n';
205-
abort();
205+
abortWithPrettyStackTraceMessage([&](auto &out) {
206+
out << "Can't demangle: " << nameStr;
207+
});
206208
}
207209
auto mangling = mangleNode(Root, Flavor);
208210
if (!mangling.isSuccess()) {
209-
llvm::errs() << "Can't remangle: " << nameStr << '\n';
210-
abort();
211+
abortWithPrettyStackTraceMessage([&](auto &out) {
212+
out << "Can't remangle: " << nameStr;
213+
});
211214
}
212215
std::string Remangled = mangling.result();
213216
if (Remangled == nameStr)
214217
return;
215218

216-
llvm::errs() << "Remangling failed:\n"
217-
"original = " << nameStr << "\n"
218-
"remangled = " << Remangled << "\n";
219-
abort();
219+
abortWithPrettyStackTraceMessage([&](auto &out) {
220+
out << "Remangling failed:\n";
221+
out << "original = " << nameStr << "\n";
222+
out << "remangled = " << Remangled;
223+
});
220224
}
221225

222226
void Mangler::appendIdentifier(StringRef ident, bool allowRawIdentifiers) {

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"
@@ -694,23 +695,19 @@ std::string ModuleFileSharedCore::Dependency::getPrettyPrintedPath() const {
694695
}
695696

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

716713
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"
@@ -5309,8 +5310,7 @@ void Serializer::writeASTBlockEntity(const Decl *D) {
53095310
SWIFT_DEFER {
53105311
// This is important enough to leave on in Release builds.
53115312
if (initialOffset == Out.GetCurrentBitNo()) {
5312-
llvm::PrettyStackTraceString message("failed to serialize anything");
5313-
abort();
5313+
abortWithPrettyStackTraceMessage("failed to serialize anything");
53145314
}
53155315
};
53165316

@@ -6136,8 +6136,7 @@ void Serializer::writeASTBlockEntity(Type ty) {
61366136
SWIFT_DEFER {
61376137
// This is important enough to leave on in Release builds.
61386138
if (initialOffset == Out.GetCurrentBitNo()) {
6139-
llvm::PrettyStackTraceString message("failed to serialize anything");
6140-
abort();
6139+
abortWithPrettyStackTraceMessage("failed to serialize anything");
61416140
}
61426141
};
61436142

0 commit comments

Comments
 (0)