CLING DiagnosticRenderer::emitDiagnostiC BAD ACCESS

I have following code to attach textdiagnosticprinter to cling. It results in crash at DiagnosticRenderer::emitDiagnostic. Will appreciate any help to understand what I am doing wrong here.


        auto compiler = state.interp->getCI();
    // get Diagnostics Engine and connect to our diagnostic consumer
    auto& de = compiler->getDiagnostics();
    auto& langOptions = compiler->getLangOpts() ;
    std::string str;
    llvm::raw_string_ostream os(str);
    clang::DiagnosticOptions &diagnosticOptions =compiler->getDiagnosticOpts();
    clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =
        new clang::TextDiagnosticPrinter(os, &diagnosticOptions, true);
	de.setClient(pTextDiagnosticPrinter, true);

(lldb) bt

  • thread #1, queue = ‘’, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    • frame #0: 0x000000010039c91c cling-cliclang::DiagnosticRenderer::emitDiagnostic(clang::FullSourceLoc, clang::DiagnosticsEngine::Level, llvm::StringRef, llvm::ArrayRef<clang::CharSourceRange>, llvm::ArrayRef<clang::FixItHint>, llvm::PointerUnion<clang::Diagnostic const*, clang::StoredDiagnostic const*>) + 76 frame #1: 0x00000001003e1513 cling-cliclang::TextDiagnosticPrinter::HandleDiagnostic(clang::DiagnosticsEngine::Level, clang::Diagnostic const&) + 1331
      frame #2: 0x00000001024ffd5e cling-cliclang::DiagnosticIDs::ProcessDiag(clang::DiagnosticsEngine&) const + 382 frame #3: 0x00000001024f97e9 cling-cliclang::DiagnosticsEngine::EmitCurrentDiagnostic(bool) + 89

Never mind, resolved the issue. I was missing call to BeginSourceFile of TextDiagnosticPrinter. It initialize the TextDiagnostic utility

void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO,
const Preprocessor *PP) {
// Build the TextDiagnostic utility.
TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts));

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.