llvm.org GIT mirror llvm / 9640ae7
[llvm-c] Expose LLVMContextGetDiagnostic{Handler,Context} Differential Revision: http://reviews.llvm.org/D18820 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265773 91177308-0d34-0410-b5e6-96231b3b80d8 Jeroen Ketema 4 years ago
9 changed file(s) with 137 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
429429 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
430430 LLVMDiagnosticHandler Handler,
431431 void *DiagnosticContext);
432
433 /**
434 * Get the diagnostic handler of this context.
435 */
436 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C);
437
438 /**
439 * Get the diagnostic context of this context.
440 */
441 void *LLVMContextGetDiagnosticContext(LLVMContextRef C);
432442
433443 /**
434444 * Set the yield callback function for this context.
8484 LLVMDiagnosticHandler Handler,
8585 void *DiagnosticContext) {
8686 unwrap(C)->setDiagnosticHandler(
87 LLVM_EXTENSION reinterpret_cast(Handler),
87 LLVM_EXTENSION reinterpret_cast(
88 Handler),
8889 DiagnosticContext);
90 }
91
92 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) {
93 return LLVM_EXTENSION reinterpret_cast(
94 unwrap(C)->getDiagnosticHandler());
95 }
96
97 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) {
98 return unwrap(C)->getDiagnosticContext();
8999 }
90100
91101 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
0 ; RUN: llvm-as < %s | llvm-dis > %t.orig
11 ; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo
22 ; RUN: diff -w %t.orig %t.echo
3 ; RUN: llvm-as < %s | llvm-c-test --test-diagnostic-handler 2>&1 | FileCheck %s
4 ; CHECK: Diagnostic handler was not called while loading module
77 ; RUN: not llvm-c-test --lazy-new-module-dump < %S/Inputs/invalid.ll.bc 2>&1 | FileCheck --check-prefix=NEW %s
88
99 NEW: Error with new bitcode parser: Unknown attribute kind (52)
10
11 ; RUN: llvm-c-test --test-diagnostic-handler < %S/Inputs/invalid.ll.bc 2>&1 | FileCheck --check-prefix=DIAGNOSTIC %s
12
13 DIAGNOSTIC: Executing diagnostic handler
14 DIAGNOSTIC: Diagnostic severity is of type error
15 DIAGNOSTIC: Diagnostic handler was called while loading module
3636
3737 add_llvm_tool(llvm-c-test
3838 calc.c
39 diagnostic.c
3940 disassemble.c
4041 echo.cpp
4142 helpers.c
0 //===-- diagnostic.cpp - tool for testing libLLVM and llvm-c API ----------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the --test-diagnostic-handler command in llvm-c-test.
10 //
11 // This command uses the C API to read a module with a custom diagnostic
12 // handler set to test the diagnostic handler functionality.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm-c-test.h"
17 #include "llvm-c/BitReader.h"
18 #include "llvm-c/Core.h"
19
20 #include
21
22 static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
23 fprintf(stderr, "Executing diagnostic handler\n");
24
25 fprintf(stderr, "Diagnostic severity is of type ");
26 switch (LLVMGetDiagInfoSeverity(DI)) {
27 case LLVMDSError:
28 fprintf(stderr, "error");
29 break;
30 case LLVMDSWarning:
31 fprintf(stderr, "warning");
32 break;
33 case LLVMDSRemark:
34 fprintf(stderr, "remark");
35 break;
36 case LLVMDSNote:
37 fprintf(stderr, "note");
38 break;
39 }
40 fprintf(stderr, "\n");
41
42 (*(int *)C) = 1;
43 }
44
45 static int handlerCalled = 0;
46
47 int llvm_test_diagnostic_handler(void) {
48 LLVMContextRef C = LLVMGetGlobalContext();
49 LLVMContextSetDiagnosticHandler(C, diagnosticHandler, &handlerCalled);
50
51 if (LLVMContextGetDiagnosticHandler(C) != diagnosticHandler) {
52 fprintf(stderr, "LLVMContext{Set,Get}DiagnosticHandler failed\n");
53 return 1;
54 }
55
56 int *DC = (int *)LLVMContextGetDiagnosticContext(C);
57 if (DC != &handlerCalled || *DC) {
58 fprintf(stderr, "LLVMContextGetDiagnosticContext failed\n");
59 return 1;
60 }
61
62 LLVMMemoryBufferRef MB;
63 char *msg = NULL;
64 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
65 fprintf(stderr, "Error reading file: %s\n", msg);
66 LLVMDisposeMessage(msg);
67 return 1;
68 }
69
70
71 LLVMModuleRef M;
72 int Ret = LLVMGetBitcodeModule2(MB, &M);
73 if (Ret) {
74 // We do not return if the bitcode was invalid, as we want to test whether
75 // the diagnostic handler was executed.
76 fprintf(stderr, "Error parsing bitcode: %s\n", msg);
77 }
78
79 LLVMDisposeMemoryBuffer(MB);
80
81 if (handlerCalled) {
82 fprintf(stderr, "Diagnostic handler was called while loading module\n");
83 } else {
84 fprintf(stderr, "Diagnostic handler was not called while loading module\n");
85 }
86
87 return 0;
88 }
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file implements the --echo commands in llvm-c-test.
9 // This file implements the --echo command in llvm-c-test.
1010 //
1111 // This command uses the C API to read a module and output an exact copy of it
1212 // as output. It is used to check that the resulting module matches the input
4848 // echo.c
4949 int llvm_echo(void);
5050
51 // diagnostic.c
52 int llvm_test_diagnostic_handler(void);
53
5154 #ifdef __cplusplus
5255 }
5356 #endif /* !defined(__cplusplus) */
2020 fprintf(stderr, "llvm-c-test command\n\n");
2121 fprintf(stderr, " Commands:\n");
2222 fprintf(stderr, " * --module-dump\n");
23 fprintf(stderr, " Read bytecode from stdin - print disassembly\n\n");
23 fprintf(stderr, " Read bitcode from stdin - print disassembly\n\n");
2424 fprintf(stderr, " * --lazy-module-dump\n");
2525 fprintf(stderr,
26 " Lazily read bytecode from stdin - print disassembly\n\n");
26 " Lazily read bitcode from stdin - print disassembly\n\n");
2727 fprintf(stderr, " * --new-module-dump\n");
28 fprintf(stderr, " Read bytecode from stdin - print disassembly\n\n");
28 fprintf(stderr, " Read bitcode from stdin - print disassembly\n\n");
2929 fprintf(stderr, " * --lazy-new-module-dump\n");
3030 fprintf(stderr,
31 " Lazily read bytecode from stdin - print disassembly\n\n");
31 " Lazily read bitcode from stdin - print disassembly\n\n");
3232 fprintf(stderr, " * --module-list-functions\n");
3333 fprintf(stderr,
34 " Read bytecode from stdin - list summary of functions\n\n");
34 " Read bitcode from stdin - list summary of functions\n\n");
3535 fprintf(stderr, " * --module-list-globals\n");
36 fprintf(stderr, " Read bytecode from stdin - list summary of globals\n\n");
36 fprintf(stderr, " Read bitcode from stdin - list summary of globals\n\n");
3737 fprintf(stderr, " * --targets-list\n");
3838 fprintf(stderr, " List available targets\n\n");
3939 fprintf(stderr, " * --object-list-sections\n");
4545 fprintf(stderr, " Read lines of triple, hex ascii machine code from stdin "
4646 "- print disassembly\n\n");
4747 fprintf(stderr, " * --calc\n");
48 fprintf(stderr, " * --echo\n");
49 fprintf(stderr,
50 " Read object file form stdin - and print it back out\n\n");
5148 fprintf(
5249 stderr,
5350 " Read lines of name, rpn from stdin - print generated module\n\n");
51 fprintf(stderr, " * --echo\n");
52 fprintf(stderr,
53 " Read bitcode file form stdin - print it back out\n\n");
54 fprintf(stderr, " * --test-diagnostic-handler\n");
55 fprintf(stderr,
56 " Read bitcode file form stdin with a diagnostic handler set\n\n");
5457 }
5558
5659 int main(int argc, char **argv) {
8689 return llvm_set_metadata();
8790 } else if (argc == 2 && !strcmp(argv[1], "--echo")) {
8891 return llvm_echo();
92 } else if (argc == 2 && !strcmp(argv[1], "--test-diagnostic-handler")) {
93 return llvm_test_diagnostic_handler();
8994 } else {
9095 print_usage();
9196 }