From e463b69736da8b0a950ecd937cf990401bdfcdeb Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Tue, 5 Oct 2021 10:51:28 +0100 Subject: [PATCH] [Support] Change fatal_error_handler_t to take a const char* instead of std::string https://commondatastorage.googleapis.com/chromium-browser-clang/llvm-include-analysis.html Excessive use of the header has a massive impact on compile time; its most commonly included via the ErrorHandling.h header, which has to be included in many key headers, impacting many source files that have no need for std::string. As an initial step toward removing the include from ErrorHandling.h, this patch proposes to update the fatal_error_handler_t handler to just take a raw const char* instead. The next step will be to remove the report_fatal_error std::string variant, which will involve a lot of cleanup and better use of Twine/StringRef. Differential Revision: https://reviews.llvm.org/D111049 --- clang/tools/clang-repl/ClangRepl.cpp | 2 +- clang/tools/driver/cc1_main.cpp | 2 +- clang/tools/driver/cc1as_main.cpp | 2 +- clang/tools/libclang/FatalErrorHandler.cpp | 4 ++-- llvm/include/llvm/Support/ErrorHandling.h | 4 ++-- llvm/lib/Support/ErrorHandling.cpp | 6 +++--- llvm/tools/llvm-as-fuzzer/llvm-as-fuzzer.cpp | 2 +- llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp | 2 +- llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp | 2 +- 9 files changed, 13 insertions(+), 13 deletions(-) diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp index ba6bb11abc8670..8e50418f705c25 100644 --- a/clang/tools/clang-repl/ClangRepl.cpp +++ b/clang/tools/clang-repl/ClangRepl.cpp @@ -32,7 +32,7 @@ static llvm::cl::list OptInputs(llvm::cl::Positional, llvm::cl::ZeroOrMore, llvm::cl::desc("[code to run]")); -static void LLVMErrorHandler(void *UserData, const std::string &Message, +static void LLVMErrorHandler(void *UserData, const char *Message, bool GenCrashDiag) { auto &Diags = *static_cast(UserData); diff --git a/clang/tools/driver/cc1_main.cpp b/clang/tools/driver/cc1_main.cpp index 396d6ff529f318..13fb80fbad3002 100644 --- a/clang/tools/driver/cc1_main.cpp +++ b/clang/tools/driver/cc1_main.cpp @@ -57,7 +57,7 @@ using namespace llvm::opt; // Main driver //===----------------------------------------------------------------------===// -static void LLVMErrorHandler(void *UserData, const std::string &Message, +static void LLVMErrorHandler(void *UserData, const char *Message, bool GenCrashDiag) { DiagnosticsEngine &Diags = *static_cast(UserData); diff --git a/clang/tools/driver/cc1as_main.cpp b/clang/tools/driver/cc1as_main.cpp index 6549b132f6166e..6816fd20e43ecf 100644 --- a/clang/tools/driver/cc1as_main.cpp +++ b/clang/tools/driver/cc1as_main.cpp @@ -550,7 +550,7 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, return Failed; } -static void LLVMErrorHandler(void *UserData, const std::string &Message, +static void LLVMErrorHandler(void *UserData, const char *Message, bool GenCrashDiag) { DiagnosticsEngine &Diags = *static_cast(UserData); diff --git a/clang/tools/libclang/FatalErrorHandler.cpp b/clang/tools/libclang/FatalErrorHandler.cpp index ef21569637f037..73864754c06557 100644 --- a/clang/tools/libclang/FatalErrorHandler.cpp +++ b/clang/tools/libclang/FatalErrorHandler.cpp @@ -11,11 +11,11 @@ #include "llvm/Support/ErrorHandling.h" #include -static void aborting_fatal_error_handler(void *, const std::string &reason, +static void aborting_fatal_error_handler(void *, const char *reason, bool) { // Write the result out to stderr avoiding errs() because raw_ostreams can // call report_fatal_error. - fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str()); + fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason); ::abort(); } diff --git a/llvm/include/llvm/Support/ErrorHandling.h b/llvm/include/llvm/Support/ErrorHandling.h index 81cac477cb69b9..260c76fadc95aa 100644 --- a/llvm/include/llvm/Support/ErrorHandling.h +++ b/llvm/include/llvm/Support/ErrorHandling.h @@ -18,12 +18,12 @@ #include namespace llvm { -class StringRef; + class StringRef; class Twine; /// An error handler callback. typedef void (*fatal_error_handler_t)(void *user_data, - const std::string& reason, + const char *reason, bool gen_crash_diag); /// install_fatal_error_handler - Installs a new error handler to be used diff --git a/llvm/lib/Support/ErrorHandling.cpp b/llvm/lib/Support/ErrorHandling.cpp index ce6344284f0661..b15c36f5b9e57a 100644 --- a/llvm/lib/Support/ErrorHandling.cpp +++ b/llvm/lib/Support/ErrorHandling.cpp @@ -105,7 +105,7 @@ void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { } if (handler) { - handler(handlerData, Reason.str(), GenCrashDiag); + handler(handlerData, Reason.str().c_str(), GenCrashDiag); } else { // Blast the result out to stderr. We don't try hard to make sure this // succeeds (e.g. handling EINTR) and we can't use errs() here because @@ -218,11 +218,11 @@ void llvm::llvm_unreachable_internal(const char *msg, const char *file, #endif } -static void bindingsErrorHandler(void *user_data, const std::string& reason, +static void bindingsErrorHandler(void *user_data, const char *reason, bool gen_crash_diag) { LLVMFatalErrorHandler handler = LLVM_EXTENSION reinterpret_cast(user_data); - handler(reason.c_str()); + handler(reason); } void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) { diff --git a/llvm/tools/llvm-as-fuzzer/llvm-as-fuzzer.cpp b/llvm/tools/llvm-as-fuzzer/llvm-as-fuzzer.cpp index ec3ccbd17363de..119bf6f8d92d84 100644 --- a/llvm/tools/llvm-as-fuzzer/llvm-as-fuzzer.cpp +++ b/llvm/tools/llvm-as-fuzzer/llvm-as-fuzzer.cpp @@ -30,7 +30,7 @@ static jmp_buf JmpBuf; namespace { -void MyFatalErrorHandler(void *user_data, const std::string& reason, +void MyFatalErrorHandler(void *user_data, const char *reason, bool gen_crash_diag) { // Don't bother printing reason, just return to the test function, // since a fatal error represents a successful parse (i.e. it correctly diff --git a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp index 1d93299b9e81ec..f8300955964e42 100644 --- a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp +++ b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp @@ -107,7 +107,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { return 0; } -static void handleLLVMFatalError(void *, const std::string &Message, bool) { +static void handleLLVMFatalError(void *, const char *Message, bool) { // TODO: Would it be better to call into the fuzzer internals directly? dbgs() << "LLVM ERROR: " << Message << "\n" << "Aborting to trigger fuzzer exit handling.\n"; diff --git a/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp b/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp index d737f63dfd3671..7abb5d7b78d907 100644 --- a/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp +++ b/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp @@ -170,7 +170,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { return 0; } -static void handleLLVMFatalError(void *, const std::string &Message, bool) { +static void handleLLVMFatalError(void *, const char *Message, bool) { // TODO: Would it be better to call into the fuzzer internals directly? dbgs() << "LLVM ERROR: " << Message << "\n" << "Aborting to trigger fuzzer exit handling.\n";