[PATCH] D18820: [llvm-c] Expose LLVMContextGetDiagnostic{Handler, Context}

Jeroen Ketema via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 6 16:08:09 PDT 2016


jketema updated this revision to Diff 52865.
jketema added a comment.

Added test for diagnostic handler


http://reviews.llvm.org/D18820

Files:
  include/llvm-c/Core.h
  lib/IR/Core.cpp
  tools/llvm-c-test/echo.cpp

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);
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<LLVMContext::DiagnosticHandlerTy>(Handler),
+      LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(
+          Handler),
       DiagnosticContext);
 }
 
+LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
+  return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
+      unwrap(C)->getDiagnosticHandler());
+}
+
+void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
+  return unwrap(C)->getDiagnosticContext();
+}
+
 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
                                  void *OpaqueHandle) {
   auto YieldCallback =
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()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18820.52865.patch
Type: text/x-patch
Size: 2976 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160406/7c52266d/attachment.bin>


More information about the llvm-commits mailing list