Index: include/llvm-c/Core.h =================================================================== --- include/llvm-c/Core.h +++ include/llvm-c/Core.h @@ -400,6 +400,16 @@ void *DiagnosticContext); /** + * Get the diagnostic handler of this context. + */ +LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C); + +/** + * Get the diagnostic context of this context. + */ +void *LLVMContextGetDiagnosticContext(LLVMContextRef C); + +/** * Set the yield callback function for this context. * * @see LLVMContext::setYieldCallback() Index: lib/IR/Core.cpp =================================================================== --- lib/IR/Core.cpp +++ lib/IR/Core.cpp @@ -85,10 +85,20 @@ LLVMDiagnosticHandler Handler, void *DiagnosticContext) { unwrap(C)->setDiagnosticHandler( - LLVM_EXTENSION reinterpret_cast(Handler), + LLVM_EXTENSION reinterpret_cast( + Handler), DiagnosticContext); } +LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) { + return LLVM_EXTENSION reinterpret_cast( + unwrap(C)->getDiagnosticHandler()); +} + +void *LLVMContextGetDiagnosticContext(LLVMContextRef C) { + return unwrap(C)->getDiagnosticContext(); +} + void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback, void *OpaqueHandle) { auto YieldCallback = Index: tools/llvm-c-test/echo.cpp =================================================================== --- tools/llvm-c-test/echo.cpp +++ tools/llvm-c-test/echo.cpp @@ -861,6 +861,27 @@ } } +static void diagnostic_handler(LLVMDiagnosticInfoRef DI, void * C) {} + +static int diagnostic_context = 42; + +static void test_diagnostic_handler(LLVMContextRef Ctx) { + LLVMDiagnosticHandler OldHandler = LLVMContextGetDiagnosticHandler(Ctx); + void *OldContext = LLVMContextGetDiagnosticContext(Ctx); + + LLVMContextSetDiagnosticHandler(Ctx, diagnostic_handler, &diagnostic_context); + + if (LLVMContextGetDiagnosticHandler(Ctx) != diagnostic_handler) + report_fatal_error("LLVMContext{Set,Get}DiagnosticHandler failed"); + + int *Context = (int *)LLVMContextGetDiagnosticContext(Ctx); + if (Context != &diagnostic_context || *Context != 42) + report_fatal_error("LLVMContextGetDiagnosticContext failed"); + + // Restore the original handler + LLVMContextSetDiagnosticHandler(Ctx, OldHandler, OldContext); +} + int llvm_echo(void) { LLVMEnablePrettyStackTrace(); @@ -870,6 +891,8 @@ LLVMContextRef Ctx = LLVMContextCreate(); LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx); + test_diagnostic_handler(Ctx); + // This whole switcharound is done because the C API has no way to // set the source_filename LLVMSetModuleIdentifier(M, "", 0);