CrashRecoveryContext

This commit is contained in:
Fangrui Song 2018-07-15 22:49:32 -07:00
parent 0f63be2418
commit af77e6615d
5 changed files with 11 additions and 17 deletions

View File

@ -5,11 +5,12 @@
#include "log.hh" #include "log.hh"
#include "platform.h" #include "platform.h"
#include "clang/Frontend/CompilerInstance.h" #include <clang/Frontend/CompilerInstance.h>
#include "clang/Frontend/FrontendDiagnostic.h" #include <clang/Frontend/FrontendDiagnostic.h>
#include <clang/Sema/CodeCompleteConsumer.h> #include <clang/Sema/CodeCompleteConsumer.h>
#include <llvm/ADT/Twine.h> #include <llvm/ADT/Twine.h>
#include <llvm/Config/llvm-config.h> #include <llvm/Config/llvm-config.h>
#include <llvm/Support/CrashRecoveryContext.h>
#include <llvm/Support/Threading.h> #include <llvm/Support/Threading.h>
using namespace clang; using namespace clang;
using namespace llvm; using namespace llvm;

View File

@ -100,7 +100,7 @@ std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Create(
/*UserFilesAreVolatile=*/true, false, /*UserFilesAreVolatile=*/true, false,
ret->PCHCO->getRawReader().getFormat(), &ErrUnit)); ret->PCHCO->getRawReader().getFormat(), &ErrUnit));
}; };
if (!RunSafely(CRC, parse)) { if (!CRC.RunSafely(parse)) {
LOG_S(ERROR) LOG_S(ERROR)
<< "clang crashed for " << filepath << "\n" << "clang crashed for " << filepath << "\n"
<< StringJoin(args, " ") + " -fsyntax-only"; << StringJoin(args, " ") + " -fsyntax-only";
@ -116,7 +116,7 @@ std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Create(
int ClangTranslationUnit::Reparse(llvm::CrashRecoveryContext &CRC, int ClangTranslationUnit::Reparse(llvm::CrashRecoveryContext &CRC,
const WorkingFiles::Snapshot &snapshot) { const WorkingFiles::Snapshot &snapshot) {
int ret = 1; int ret = 1;
auto parse = [&]() { ret = Unit->Reparse(PCHCO, GetRemapped(snapshot)); }; (void)CRC.RunSafely(
(void)RunSafely(CRC, parse); [&]() { ret = Unit->Reparse(PCHCO, GetRemapped(snapshot)); });
return ret; return ret;
} }

View File

@ -22,16 +22,6 @@ Range FromTokenRange(const clang::SourceManager &SM, const clang::LangOptions &L
clang::SourceRange R, clang::SourceRange R,
llvm::sys::fs::UniqueID *UniqueID = nullptr); llvm::sys::fs::UniqueID *UniqueID = nullptr);
template <typename Fn>
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 { struct ClangTranslationUnit {
static std::unique_ptr<ClangTranslationUnit> static std::unique_ptr<ClangTranslationUnit>
Create(const std::string &filepath, const std::vector<std::string> &args, Create(const std::string &filepath, const std::vector<std::string> &args,

View File

@ -1153,7 +1153,7 @@ std::vector<std::unique_ptr<IndexFile>> Index(
/*CaptureDiagnostics=*/true, 0, false, false, /*CaptureDiagnostics=*/true, 0, false, false,
/*UserFilesAreVolatile=*/true); /*UserFilesAreVolatile=*/true);
}; };
if (!RunSafely(CRC, compile)) { if (!CRC.RunSafely(compile)) {
LOG_S(ERROR) << "clang crashed for " << file; LOG_S(ERROR) << "clang crashed for " << file;
return {}; return {};
} }

View File

@ -8,6 +8,7 @@
using namespace ccls; using namespace ccls;
#include <llvm/Support/CommandLine.h> #include <llvm/Support/CommandLine.h>
#include <llvm/Support/CrashRecoveryContext.h>
#include <llvm/Support/Process.h> #include <llvm/Support/Process.h>
#include <llvm/Support/Program.h> #include <llvm/Support/Program.h>
#include <llvm/Support/Signals.h> #include <llvm/Support/Signals.h>
@ -55,7 +56,9 @@ int main(int argc, char** argv) {
} }
pipeline::Init(); pipeline::Init();
idx::IndexInit(); const char *env = getenv("CCLS_CRASH_RECOVERY");
if (!env || strcmp(env, "0") != 0)
CrashRecoveryContext::Enable();
bool language_server = true; bool language_server = true;