Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / llvm / tools / llvm-c-test / diagnostic.c
blob0f9b48b2aa5510c871c713c341b2d09535e935f2
1 //===-- diagnostic.cpp - tool for testing libLLVM and llvm-c API ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the --test-diagnostic-handler command in llvm-c-test.
11 // This command uses the C API to read a module with a custom diagnostic
12 // handler set to test the diagnostic handler functionality.
14 //===----------------------------------------------------------------------===//
16 #include "llvm-c-test.h"
17 #include "llvm-c/BitReader.h"
18 #include "llvm-c/Core.h"
20 #include <stdio.h>
22 static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
23 fprintf(stderr, "Executing diagnostic handler\n");
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;
40 fprintf(stderr, "\n");
42 (*(int *)C) = 1;
45 static int handlerCalled = 0;
47 int llvm_test_diagnostic_handler(void) {
48 LLVMContextRef C = LLVMGetGlobalContext();
49 LLVMContextSetDiagnosticHandler(C, diagnosticHandler, &handlerCalled);
51 if (LLVMContextGetDiagnosticHandler(C) != diagnosticHandler) {
52 fprintf(stderr, "LLVMContext{Set,Get}DiagnosticHandler failed\n");
53 return 1;
56 int *DC = (int *)LLVMContextGetDiagnosticContext(C);
57 if (DC != &handlerCalled || *DC) {
58 fprintf(stderr, "LLVMContextGetDiagnosticContext failed\n");
59 return 1;
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;
71 LLVMModuleRef M;
72 int Ret = LLVMGetBitcodeModule2(MB, &M);
73 if (Ret)
74 LLVMDisposeMemoryBuffer(MB);
76 if (handlerCalled) {
77 fprintf(stderr, "Diagnostic handler was called while loading module\n");
78 } else {
79 fprintf(stderr, "Diagnostic handler was not called while loading module\n");
82 return 0;