From af77e6615d0f97500dce2b54cdc7ab05239a3b63 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 15 Jul 2018 22:49:32 -0700 Subject: [PATCH] CrashRecoveryContext --- src/clang_complete.cc | 5 +++-- src/clang_tu.cc | 6 +++--- src/clang_tu.h | 10 ---------- src/indexer.cc | 2 +- src/main.cc | 5 ++++- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/clang_complete.cc b/src/clang_complete.cc index bbac2320..15b0761e 100644 --- a/src/clang_complete.cc +++ b/src/clang_complete.cc @@ -5,11 +5,12 @@ #include "log.hh" #include "platform.h" -#include "clang/Frontend/CompilerInstance.h" -#include "clang/Frontend/FrontendDiagnostic.h" +#include +#include #include #include #include +#include #include using namespace clang; using namespace llvm; diff --git a/src/clang_tu.cc b/src/clang_tu.cc index 9e2b3ac2..64c4a4ec 100644 --- a/src/clang_tu.cc +++ b/src/clang_tu.cc @@ -100,7 +100,7 @@ std::unique_ptr ClangTranslationUnit::Create( /*UserFilesAreVolatile=*/true, false, ret->PCHCO->getRawReader().getFormat(), &ErrUnit)); }; - if (!RunSafely(CRC, parse)) { + if (!CRC.RunSafely(parse)) { LOG_S(ERROR) << "clang crashed for " << filepath << "\n" << StringJoin(args, " ") + " -fsyntax-only"; @@ -116,7 +116,7 @@ std::unique_ptr ClangTranslationUnit::Create( int ClangTranslationUnit::Reparse(llvm::CrashRecoveryContext &CRC, const WorkingFiles::Snapshot &snapshot) { int ret = 1; - auto parse = [&]() { ret = Unit->Reparse(PCHCO, GetRemapped(snapshot)); }; - (void)RunSafely(CRC, parse); + (void)CRC.RunSafely( + [&]() { ret = Unit->Reparse(PCHCO, GetRemapped(snapshot)); }); return ret; } diff --git a/src/clang_tu.h b/src/clang_tu.h index 741d9a40..a20ad74e 100644 --- a/src/clang_tu.h +++ b/src/clang_tu.h @@ -22,16 +22,6 @@ Range FromTokenRange(const clang::SourceManager &SM, const clang::LangOptions &L clang::SourceRange R, llvm::sys::fs::UniqueID *UniqueID = nullptr); -template -bool RunSafely(llvm::CrashRecoveryContext &CRC, Fn &&fn) { - const char *env = getenv("CCLS_CRASH_RECOVERY"); - if (env && strcmp(env, "0") == 0) { - fn(); - return true; - } - return CRC.RunSafely(fn); -} - struct ClangTranslationUnit { static std::unique_ptr Create(const std::string &filepath, const std::vector &args, diff --git a/src/indexer.cc b/src/indexer.cc index 1f11b90b..e047e1dc 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1153,7 +1153,7 @@ std::vector> Index( /*CaptureDiagnostics=*/true, 0, false, false, /*UserFilesAreVolatile=*/true); }; - if (!RunSafely(CRC, compile)) { + if (!CRC.RunSafely(compile)) { LOG_S(ERROR) << "clang crashed for " << file; return {}; } diff --git a/src/main.cc b/src/main.cc index 11c7bb24..63c5148b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -8,6 +8,7 @@ using namespace ccls; #include +#include #include #include #include @@ -55,7 +56,9 @@ int main(int argc, char** argv) { } pipeline::Init(); - idx::IndexInit(); + const char *env = getenv("CCLS_CRASH_RECOVERY"); + if (!env || strcmp(env, "0") != 0) + CrashRecoveryContext::Enable(); bool language_server = true;