[LLVM][Alignment] Introduce Alignment In Attributes
[llvm-core.git] / tools / llvm-c-test / diagnostic.c
blobd759097e8e962806af628785a243b7c23341b8ac
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 // 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);
79 LLVMDisposeMemoryBuffer(MB);
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");
87 return 0;