[llvm] r265773 - [llvm-c] Expose LLVMContextGetDiagnostic{Handler, Context}
Jeroen Ketema via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 8 02:19:05 PDT 2016
Author: jketema
Date: Fri Apr 8 04:19:02 2016
New Revision: 265773
URL: http://llvm.org/viewvc/llvm-project?rev=265773&view=rev
Log:
[llvm-c] Expose LLVMContextGetDiagnostic{Handler,Context}
Differential Revision: http://reviews.llvm.org/D18820
Added:
llvm/trunk/tools/llvm-c-test/diagnostic.c
Modified:
llvm/trunk/include/llvm-c/Core.h
llvm/trunk/lib/IR/Core.cpp
llvm/trunk/test/Bindings/llvm-c/empty.ll
llvm/trunk/test/Bindings/llvm-c/invalid-bitcode.test
llvm/trunk/tools/llvm-c-test/CMakeLists.txt
llvm/trunk/tools/llvm-c-test/echo.cpp
llvm/trunk/tools/llvm-c-test/llvm-c-test.h
llvm/trunk/tools/llvm-c-test/main.c
Modified: llvm/trunk/include/llvm-c/Core.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Core.h?rev=265773&r1=265772&r2=265773&view=diff
==============================================================================
--- llvm/trunk/include/llvm-c/Core.h (original)
+++ llvm/trunk/include/llvm-c/Core.h Fri Apr 8 04:19:02 2016
@@ -432,6 +432,16 @@ void LLVMContextSetDiagnosticHandler(LLV
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()
Modified: llvm/trunk/lib/IR/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Core.cpp?rev=265773&r1=265772&r2=265773&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Core.cpp (original)
+++ llvm/trunk/lib/IR/Core.cpp Fri Apr 8 04:19:02 2016
@@ -85,10 +85,20 @@ void LLVMContextSetDiagnosticHandler(LLV
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 =
Modified: llvm/trunk/test/Bindings/llvm-c/empty.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bindings/llvm-c/empty.ll?rev=265773&r1=265772&r2=265773&view=diff
==============================================================================
--- llvm/trunk/test/Bindings/llvm-c/empty.ll (original)
+++ llvm/trunk/test/Bindings/llvm-c/empty.ll Fri Apr 8 04:19:02 2016
@@ -1,3 +1,5 @@
; RUN: llvm-as < %s | llvm-dis > %t.orig
; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo
; RUN: diff -w %t.orig %t.echo
+; RUN: llvm-as < %s | llvm-c-test --test-diagnostic-handler 2>&1 | FileCheck %s
+; CHECK: Diagnostic handler was not called while loading module
Modified: llvm/trunk/test/Bindings/llvm-c/invalid-bitcode.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bindings/llvm-c/invalid-bitcode.test?rev=265773&r1=265772&r2=265773&view=diff
==============================================================================
--- llvm/trunk/test/Bindings/llvm-c/invalid-bitcode.test (original)
+++ llvm/trunk/test/Bindings/llvm-c/invalid-bitcode.test Fri Apr 8 04:19:02 2016
@@ -8,3 +8,9 @@ CHECK: Error parsing bitcode: Unknown at
; RUN: not llvm-c-test --lazy-new-module-dump < %S/Inputs/invalid.ll.bc 2>&1 | FileCheck --check-prefix=NEW %s
NEW: Error with new bitcode parser: Unknown attribute kind (52)
+
+; RUN: llvm-c-test --test-diagnostic-handler < %S/Inputs/invalid.ll.bc 2>&1 | FileCheck --check-prefix=DIAGNOSTIC %s
+
+DIAGNOSTIC: Executing diagnostic handler
+DIAGNOSTIC: Diagnostic severity is of type error
+DIAGNOSTIC: Diagnostic handler was called while loading module
Modified: llvm/trunk/tools/llvm-c-test/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/CMakeLists.txt?rev=265773&r1=265772&r2=265773&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/CMakeLists.txt (original)
+++ llvm/trunk/tools/llvm-c-test/CMakeLists.txt Fri Apr 8 04:19:02 2016
@@ -37,6 +37,7 @@ endif ()
add_llvm_tool(llvm-c-test
calc.c
+ diagnostic.c
disassemble.c
echo.cpp
helpers.c
Added: llvm/trunk/tools/llvm-c-test/diagnostic.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/diagnostic.c?rev=265773&view=auto
==============================================================================
--- llvm/trunk/tools/llvm-c-test/diagnostic.c (added)
+++ llvm/trunk/tools/llvm-c-test/diagnostic.c Fri Apr 8 04:19:02 2016
@@ -0,0 +1,89 @@
+//===-- diagnostic.cpp - tool for testing libLLVM and llvm-c API ----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the --test-diagnostic-handler command in llvm-c-test.
+//
+// This command uses the C API to read a module with a custom diagnostic
+// handler set to test the diagnostic handler functionality.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm-c-test.h"
+#include "llvm-c/BitReader.h"
+#include "llvm-c/Core.h"
+
+#include <stdio.h>
+
+static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
+ fprintf(stderr, "Executing diagnostic handler\n");
+
+ fprintf(stderr, "Diagnostic severity is of type ");
+ switch (LLVMGetDiagInfoSeverity(DI)) {
+ case LLVMDSError:
+ fprintf(stderr, "error");
+ break;
+ case LLVMDSWarning:
+ fprintf(stderr, "warning");
+ break;
+ case LLVMDSRemark:
+ fprintf(stderr, "remark");
+ break;
+ case LLVMDSNote:
+ fprintf(stderr, "note");
+ break;
+ }
+ fprintf(stderr, "\n");
+
+ (*(int *)C) = 1;
+}
+
+static int handlerCalled = 0;
+
+int llvm_test_diagnostic_handler(void) {
+ LLVMContextRef C = LLVMGetGlobalContext();
+ LLVMContextSetDiagnosticHandler(C, diagnosticHandler, &handlerCalled);
+
+ if (LLVMContextGetDiagnosticHandler(C) != diagnosticHandler) {
+ fprintf(stderr, "LLVMContext{Set,Get}DiagnosticHandler failed\n");
+ return 1;
+ }
+
+ int *DC = (int *)LLVMContextGetDiagnosticContext(C);
+ if (DC != &handlerCalled || *DC) {
+ fprintf(stderr, "LLVMContextGetDiagnosticContext failed\n");
+ return 1;
+ }
+
+ LLVMMemoryBufferRef MB;
+ char *msg = NULL;
+ if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
+ fprintf(stderr, "Error reading file: %s\n", msg);
+ LLVMDisposeMessage(msg);
+ return 1;
+ }
+
+
+ LLVMModuleRef M;
+ int Ret = LLVMGetBitcodeModule2(MB, &M);
+ if (Ret) {
+ // We do not return if the bitcode was invalid, as we want to test whether
+ // the diagnostic handler was executed.
+ fprintf(stderr, "Error parsing bitcode: %s\n", msg);
+ }
+
+ LLVMDisposeMemoryBuffer(MB);
+
+ if (handlerCalled) {
+ fprintf(stderr, "Diagnostic handler was called while loading module\n");
+ } else {
+ fprintf(stderr, "Diagnostic handler was not called while loading module\n");
+ }
+
+ return 0;
+}
Modified: llvm/trunk/tools/llvm-c-test/echo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/echo.cpp?rev=265773&r1=265772&r2=265773&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/echo.cpp (original)
+++ llvm/trunk/tools/llvm-c-test/echo.cpp Fri Apr 8 04:19:02 2016
@@ -7,7 +7,7 @@
//
//===----------------------------------------------------------------------===//
//
-// This file implements the --echo commands in llvm-c-test.
+// This file implements the --echo command in llvm-c-test.
//
// This command uses the C API to read a module and output an exact copy of it
// as output. It is used to check that the resulting module matches the input
Modified: llvm/trunk/tools/llvm-c-test/llvm-c-test.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/llvm-c-test.h?rev=265773&r1=265772&r2=265773&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/llvm-c-test.h (original)
+++ llvm/trunk/tools/llvm-c-test/llvm-c-test.h Fri Apr 8 04:19:02 2016
@@ -49,6 +49,9 @@ int llvm_targets_list(void);
// echo.c
int llvm_echo(void);
+// diagnostic.c
+int llvm_test_diagnostic_handler(void);
+
#ifdef __cplusplus
}
#endif /* !defined(__cplusplus) */
Modified: llvm/trunk/tools/llvm-c-test/main.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/main.c?rev=265773&r1=265772&r2=265773&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/main.c (original)
+++ llvm/trunk/tools/llvm-c-test/main.c Fri Apr 8 04:19:02 2016
@@ -21,20 +21,20 @@ static void print_usage(void) {
fprintf(stderr, "llvm-c-test command\n\n");
fprintf(stderr, " Commands:\n");
fprintf(stderr, " * --module-dump\n");
- fprintf(stderr, " Read bytecode from stdin - print disassembly\n\n");
+ fprintf(stderr, " Read bitcode from stdin - print disassembly\n\n");
fprintf(stderr, " * --lazy-module-dump\n");
fprintf(stderr,
- " Lazily read bytecode from stdin - print disassembly\n\n");
+ " Lazily read bitcode from stdin - print disassembly\n\n");
fprintf(stderr, " * --new-module-dump\n");
- fprintf(stderr, " Read bytecode from stdin - print disassembly\n\n");
+ fprintf(stderr, " Read bitcode from stdin - print disassembly\n\n");
fprintf(stderr, " * --lazy-new-module-dump\n");
fprintf(stderr,
- " Lazily read bytecode from stdin - print disassembly\n\n");
+ " Lazily read bitcode from stdin - print disassembly\n\n");
fprintf(stderr, " * --module-list-functions\n");
fprintf(stderr,
- " Read bytecode from stdin - list summary of functions\n\n");
+ " Read bitcode from stdin - list summary of functions\n\n");
fprintf(stderr, " * --module-list-globals\n");
- fprintf(stderr, " Read bytecode from stdin - list summary of globals\n\n");
+ fprintf(stderr, " Read bitcode from stdin - list summary of globals\n\n");
fprintf(stderr, " * --targets-list\n");
fprintf(stderr, " List available targets\n\n");
fprintf(stderr, " * --object-list-sections\n");
@@ -46,12 +46,15 @@ static void print_usage(void) {
fprintf(stderr, " Read lines of triple, hex ascii machine code from stdin "
"- print disassembly\n\n");
fprintf(stderr, " * --calc\n");
- fprintf(stderr, " * --echo\n");
- fprintf(stderr,
- " Read object file form stdin - and print it back out\n\n");
fprintf(
stderr,
" Read lines of name, rpn from stdin - print generated module\n\n");
+ fprintf(stderr, " * --echo\n");
+ fprintf(stderr,
+ " Read bitcode file form stdin - print it back out\n\n");
+ fprintf(stderr, " * --test-diagnostic-handler\n");
+ fprintf(stderr,
+ " Read bitcode file form stdin with a diagnostic handler set\n\n");
}
int main(int argc, char **argv) {
@@ -87,6 +90,8 @@ int main(int argc, char **argv) {
return llvm_set_metadata();
} else if (argc == 2 && !strcmp(argv[1], "--echo")) {
return llvm_echo();
+ } else if (argc == 2 && !strcmp(argv[1], "--test-diagnostic-handler")) {
+ return llvm_test_diagnostic_handler();
} else {
print_usage();
}
More information about the llvm-commits
mailing list