1 //===-- diagnostic.cpp - tool for testing libLLVM and llvm-c API ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the --test-diagnostic-handler command in llvm-c-test.
12 // This command uses the C API to read a module with a custom diagnostic
13 // handler set to test the diagnostic handler functionality.
15 //===----------------------------------------------------------------------===//
17 #include "llvm-c-test.h"
18 #include "llvm-c/BitReader.h"
19 #include "llvm-c/Core.h"
23 static void diagnosticHandler(LLVMDiagnosticInfoRef DI
, void *C
) {
24 fprintf(stderr
, "Executing diagnostic handler\n");
26 fprintf(stderr
, "Diagnostic severity is of type ");
27 switch (LLVMGetDiagInfoSeverity(DI
)) {
29 fprintf(stderr
, "error");
32 fprintf(stderr
, "warning");
35 fprintf(stderr
, "remark");
38 fprintf(stderr
, "note");
41 fprintf(stderr
, "\n");
46 static int handlerCalled
= 0;
48 int llvm_test_diagnostic_handler(void) {
49 LLVMContextRef C
= LLVMGetGlobalContext();
50 LLVMContextSetDiagnosticHandler(C
, diagnosticHandler
, &handlerCalled
);
52 if (LLVMContextGetDiagnosticHandler(C
) != diagnosticHandler
) {
53 fprintf(stderr
, "LLVMContext{Set,Get}DiagnosticHandler failed\n");
57 int *DC
= (int *)LLVMContextGetDiagnosticContext(C
);
58 if (DC
!= &handlerCalled
|| *DC
) {
59 fprintf(stderr
, "LLVMContextGetDiagnosticContext failed\n");
63 LLVMMemoryBufferRef MB
;
65 if (LLVMCreateMemoryBufferWithSTDIN(&MB
, &msg
)) {
66 fprintf(stderr
, "Error reading file: %s\n", msg
);
67 LLVMDisposeMessage(msg
);
73 int Ret
= LLVMGetBitcodeModule2(MB
, &M
);
75 // We do not return if the bitcode was invalid, as we want to test whether
76 // the diagnostic handler was executed.
77 fprintf(stderr
, "Error parsing bitcode: %s\n", msg
);
80 LLVMDisposeMemoryBuffer(MB
);
83 fprintf(stderr
, "Diagnostic handler was called while loading module\n");
85 fprintf(stderr
, "Diagnostic handler was not called while loading module\n");