Ignore:
Timestamp:
Sep 24, 2016, 10:26:59 AM (3 years ago)
Author:
cameron
Message:

Override LLVM error_handler for return code 2; convert ParseFailure? to LLVM fatal error.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r5156 r5161  
    88#include <vector>
    99#include <llvm/Support/CommandLine.h>
     10#include <llvm/Support/ErrorHandling.h>
     11#include <llvm/Support/Signals.h>
    1012#include <re/re_alt.h>
    1113#include <re/re_parser.h>
     
    1921#include <pablo/pablo_toolchain.h>
    2022#include <mutex>
     23
    2124
    2225#include <iostream> // MEEE
     
    4952         Gives you colored output + back-referencing capability."), cl::cat(EnhancedGrepOptions));
    5053
     54//
     55// Handler for errors reported through llvm::report_fatal_error.  Report
     56// and signal error code 2 (grep convention).
     57//
     58static void icgrep_error_handler(void *UserData, const std::string &Message,
     59                             bool GenCrashDiag) {
     60
     61    // Modified from LLVM's internal report_fatal_error logic.
     62    SmallVector<char, 64> Buffer;
     63    raw_svector_ostream OS(Buffer);
     64    OS << "icgrep ERROR: " << Message << "\n";
     65    StringRef MessageStr = OS.str();
     66    ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
     67    (void)written; // If something went wrong, we deliberately just give up.
     68
     69    // Run the interrupt handlers to make sure any special cleanups get done, in
     70    // particular that we remove files registered with RemoveFileOnSignal.
     71    llvm::sys::RunInterruptHandlers();
     72    exit(2);
     73}
    5174
    5275static std::string allREs;
     
    212235
    213236int main(int argc, char *argv[]) {
     237    llvm::install_fatal_error_handler(&icgrep_error_handler);
    214238    cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *>{&LegacyGrepOptions, &EnhancedGrepOptions, re::re_toolchain_flags(), pablo::pablo_toolchain_flags(), codegen::codegen_flags()});
    215239    cl::ParseCommandLineOptions(argc, argv);
     
    259283            const int rc = pthread_create(&threads[i], NULL, DoGrep, (void *)&grepEngine);
    260284            if (rc) {
    261                 throw std::runtime_error("Failed to create thread: code " + std::to_string(rc));
     285                llvm::report_fatal_error("Failed to create thread: code " + std::to_string(rc));
    262286            }
    263287        }
     
    267291            const int rc = pthread_join(threads[i], &status);
    268292            if (rc) {
    269                 throw std::runtime_error("Failed to join thread: code " + std::to_string(rc));
     293                llvm::report_fatal_error("Failed to join thread: code " + std::to_string(rc));
    270294            }
    271295        }
Note: See TracChangeset for help on using the changeset viewer.