Skip to content

Commit b492ec5

Browse files
authored
[ErrorHandling] Add reportFatalInternalError + reportFatalUsageError (NFC) (#138251)
This implements the result of the discussion at: https://discourse.llvm.org/t/rfc-report-fatal-error-and-the-default-value-of-gencrashdialog/73587 There are two different use cases for report_fatal_error, so replace it with two functions reportFatalInternalError() and reportFatalUsageError(). The former indicates a bug in LLVM and generates a crash dialog. The latter does not. The names have been suggested by rnk and people seemed to like them. This replaces a lot of the usages that passed an explicit value for GenCrashDiag. I did not bulk replace remaining report_fatal_error usage -- they probably require case by case review for which function to use.
1 parent 3416d4f commit b492ec5

27 files changed

+119
-82
lines changed

clang/lib/AST/ExternalASTSource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ uint32_t ExternalASTSource::incrementGeneration(ASTContext &C) {
129129
// FIXME: Only bump the generation counter if the current generation number
130130
// has been observed?
131131
if (!++CurrentGeneration)
132-
llvm::report_fatal_error("generation counter overflowed", false);
132+
llvm::reportFatalUsageError("generation counter overflowed");
133133
}
134134

135135
return OldGeneration;

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ int main(int Argc, char **Argv) {
13991399
PassPlugins.setCallback([&](const std::string &PluginPath) {
14001400
auto Plugin = PassPlugin::Load(PluginPath);
14011401
if (!Plugin)
1402-
report_fatal_error(Plugin.takeError(), /*gen_crash_diag=*/false);
1402+
reportFatalUsageError(Plugin.takeError());
14031403
PluginList.emplace_back(Plugin.get());
14041404
});
14051405
cl::ParseCommandLineOptions(NewArgv.size(), &NewArgv[0]);

llvm/include/llvm/CodeGen/CodeGenTargetMachineImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ getEffectiveCodeModel(std::optional<CodeModel::Model> CM,
8282
if (CM) {
8383
// By default, targets do not support the tiny and kernel models.
8484
if (*CM == CodeModel::Tiny)
85-
report_fatal_error("Target does not support the tiny CodeModel", false);
85+
reportFatalUsageError("Target does not support the tiny CodeModel");
8686
if (*CM == CodeModel::Kernel)
87-
report_fatal_error("Target does not support the kernel CodeModel", false);
87+
reportFatalUsageError("Target does not support the kernel CodeModel");
8888
return *CM;
8989
}
9090
return Default;

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addRegAllocPass(
11171117
addPass(RAGreedyPass());
11181118
break;
11191119
default:
1120-
report_fatal_error("register allocator not supported yet", false);
1120+
reportFatalUsageError("register allocator not supported yet");
11211121
}
11221122
return;
11231123
}

llvm/include/llvm/Support/Error.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,10 +735,17 @@ template <class T> class [[nodiscard]] Expected {
735735
#endif
736736
};
737737

738-
/// Report a serious error, calling any installed error handler. See
739-
/// ErrorHandling.h.
738+
/// @deprecated Use reportFatalInternalError() or reportFatalUsageError()
739+
/// instead.
740740
[[noreturn]] void report_fatal_error(Error Err, bool gen_crash_diag = true);
741741

742+
/// Report a fatal error that indicates a bug in LLVM.
743+
/// See ErrorHandling.h for details.
744+
[[noreturn]] void reportFatalInternalError(Error Err);
745+
/// Report a fatal error that does not indicate a bug in LLVM.
746+
/// See ErrorHandling.h for details.
747+
[[noreturn]] void reportFatalUsageError(Error Err);
748+
742749
/// Report a fatal error if Err is a failure value.
743750
///
744751
/// This function can be used to wrap calls to fallible functions ONLY when it

llvm/include/llvm/Support/ErrorHandling.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,44 @@ namespace llvm {
5959
~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
6060
};
6161

62-
/// Reports a serious error, calling any installed error handler. These
63-
/// functions are intended to be used for error conditions which are outside
64-
/// the control of the compiler (I/O errors, invalid user input, etc.)
65-
///
66-
/// If no error handler is installed the default is to print the message to
67-
/// standard error, followed by a newline.
68-
/// After the error handler is called this function will call abort(), it
69-
/// does not return.
70-
/// NOTE: The std::string variant was removed to avoid a <string> dependency.
62+
/// @deprecated Use reportFatalInternalError() or reportFatalUsageError()
63+
/// instead.
7164
[[noreturn]] void report_fatal_error(const char *reason,
7265
bool gen_crash_diag = true);
7366
[[noreturn]] void report_fatal_error(StringRef reason,
7467
bool gen_crash_diag = true);
7568
[[noreturn]] void report_fatal_error(const Twine &reason,
7669
bool gen_crash_diag = true);
7770

71+
/// Report a fatal error that likely indicates a bug in LLVM. It serves a
72+
/// similar purpose as an assertion, but is always enabled, regardless of the
73+
/// value of NDEBUG.
74+
///
75+
/// This will call installed error handlers (or print the message by default)
76+
/// and then abort. This will produce a crash trace and *will* ask users to
77+
/// report an LLVM bug.
78+
[[noreturn]] void reportFatalInternalError(const char *reason);
79+
[[noreturn]] void reportFatalInternalError(StringRef reason);
80+
[[noreturn]] void reportFatalInternalError(const Twine &reason);
81+
82+
/// Report a fatal error that does not indicate a bug in LLVM.
83+
///
84+
/// This can be used in contexts where a proper error reporting mechanism
85+
/// (such as Error/Expected or DiagnosticInfo) is currently not supported, and
86+
/// would be too involved to introduce at the moment.
87+
///
88+
/// Examples where this function should be used instead of
89+
/// reportFatalInternalError() include invalid inputs or options, but also
90+
/// environment error conditions outside LLVM's control. It should also be used
91+
/// for known unsupported/unimplemented functionality.
92+
///
93+
/// This will call installed error handlers (or print the message by default)
94+
/// and then exit with code 1. It will not produce a crash trace and will
95+
/// *not* ask users to report an LLVM bug.
96+
[[noreturn]] void reportFatalUsageError(const char *reason);
97+
[[noreturn]] void reportFatalUsageError(StringRef reason);
98+
[[noreturn]] void reportFatalUsageError(const Twine &reason);
99+
78100
/// Installs a new bad alloc error handler that should be used whenever a
79101
/// bad alloc error, e.g. failing malloc/calloc, is encountered by LLVM.
80102
///

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static void RegisterPassPlugins(ArrayRef<std::string> PassPlugins,
195195
for (auto &PluginFN : PassPlugins) {
196196
auto PassPlugin = PassPlugin::Load(PluginFN);
197197
if (!PassPlugin)
198-
report_fatal_error(PassPlugin.takeError(), /*gen_crash_diag=*/false);
198+
reportFatalUsageError(PassPlugin.takeError());
199199
PassPlugin->registerPassBuilderCallbacks(PB);
200200
}
201201
}

llvm/lib/Support/Error.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ void report_fatal_error(Error Err, bool GenCrashDiag) {
174174
report_fatal_error(Twine(ErrMsg), GenCrashDiag);
175175
}
176176

177+
void reportFatalInternalError(Error Err) {
178+
report_fatal_error(std::move(Err), /*GenCrashDiag=*/true);
179+
}
180+
void reportFatalUsageError(Error Err) {
181+
report_fatal_error(std::move(Err), /*GenCrashDiag=*/false);
182+
}
183+
177184
} // end namespace llvm
178185

179186
LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err) {

llvm/lib/Support/ErrorHandling.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
126126
exit(1);
127127
}
128128

129+
void llvm::reportFatalInternalError(const char *reason) {
130+
report_fatal_error(reason, /*GenCrashDiag=*/true);
131+
}
132+
void llvm::reportFatalInternalError(StringRef reason) {
133+
report_fatal_error(reason, /*GenCrashDiag=*/true);
134+
}
135+
void llvm::reportFatalInternalError(const Twine &reason) {
136+
report_fatal_error(reason, /*GenCrashDiag=*/true);
137+
}
138+
void llvm::reportFatalUsageError(const char *reason) {
139+
report_fatal_error(reason, /*GenCrashDiag=*/false);
140+
}
141+
void llvm::reportFatalUsageError(StringRef reason) {
142+
report_fatal_error(reason, /*GenCrashDiag=*/false);
143+
}
144+
void llvm::reportFatalUsageError(const Twine &reason) {
145+
report_fatal_error(reason, /*GenCrashDiag=*/false);
146+
}
147+
129148
void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler,
130149
void *user_data) {
131150
#if LLVM_ENABLE_THREADS == 1

llvm/lib/Support/raw_ostream.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,8 @@ raw_fd_ostream::~raw_fd_ostream() {
678678
// has_error() and clear the error flag with clear_error() before
679679
// destructing raw_ostream objects which may have errors.
680680
if (has_error())
681-
report_fatal_error(Twine("IO failure on output stream: ") +
682-
error().message(),
683-
/*gen_crash_diag=*/false);
681+
reportFatalUsageError(Twine("IO failure on output stream: ") +
682+
error().message());
684683
}
685684

686685
#if defined(_WIN32)

0 commit comments

Comments
 (0)