Home | History | Annotate | Download | only in llvm-c-test
      1 //===-- diagnostic.cpp - tool for testing libLLVM and llvm-c API ----------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the --test-diagnostic-handler command in llvm-c-test.
     11 //
     12 // This command uses the C API to read a module with a custom diagnostic
     13 // handler set to test the diagnostic handler functionality.
     14 //
     15 //===----------------------------------------------------------------------===//
     16 
     17 #include "llvm-c-test.h"
     18 #include "llvm-c/BitReader.h"
     19 #include "llvm-c/Core.h"
     20 
     21 #include <stdio.h>
     22 
     23 static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
     24   fprintf(stderr, "Executing diagnostic handler\n");
     25 
     26   fprintf(stderr, "Diagnostic severity is of type ");
     27   switch (LLVMGetDiagInfoSeverity(DI)) {
     28   case LLVMDSError:
     29     fprintf(stderr, "error");
     30     break;
     31   case LLVMDSWarning:
     32     fprintf(stderr, "warning");
     33     break;
     34   case LLVMDSRemark:
     35     fprintf(stderr, "remark");
     36     break;
     37   case LLVMDSNote:
     38     fprintf(stderr, "note");
     39     break;
     40   }
     41   fprintf(stderr, "\n");
     42 
     43   (*(int *)C) = 1;
     44 }
     45 
     46 static int handlerCalled = 0;
     47 
     48 int llvm_test_diagnostic_handler(void) {
     49   LLVMContextRef C = LLVMGetGlobalContext();
     50   LLVMContextSetDiagnosticHandler(C, diagnosticHandler, &handlerCalled);
     51 
     52   if (LLVMContextGetDiagnosticHandler(C) != diagnosticHandler) {
     53     fprintf(stderr, "LLVMContext{Set,Get}DiagnosticHandler failed\n");
     54     return 1;
     55   }
     56 
     57   int *DC = (int *)LLVMContextGetDiagnosticContext(C);
     58   if (DC != &handlerCalled || *DC) {
     59     fprintf(stderr, "LLVMContextGetDiagnosticContext failed\n");
     60     return 1;
     61   }
     62 
     63   LLVMMemoryBufferRef MB;
     64   char *msg = NULL;
     65   if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
     66     fprintf(stderr, "Error reading file: %s\n", msg);
     67     LLVMDisposeMessage(msg);
     68     return 1;
     69   }
     70 
     71 
     72   LLVMModuleRef M;
     73   int Ret = LLVMGetBitcodeModule2(MB, &M);
     74   if (Ret) {
     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);
     78   }
     79 
     80   LLVMDisposeMemoryBuffer(MB);
     81 
     82   if (handlerCalled) {
     83     fprintf(stderr, "Diagnostic handler was called while loading module\n");
     84   } else {
     85     fprintf(stderr, "Diagnostic handler was not called while loading module\n");
     86   }
     87 
     88   return 0;
     89 }
     90