blob: d759097e8e962806af628785a243b7c23341b8ac [file] [log] [blame]
//===-- diagnostic.cpp - tool for testing libLLVM and llvm-c API ----------===//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// 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");
case LLVMDSWarning:
fprintf(stderr, "warning");
case LLVMDSRemark:
fprintf(stderr, "remark");
case LLVMDSNote:
fprintf(stderr, "note");
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);
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);
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;