forked from jeremyhu/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[llvm-c] Expose LLVMContextGetDiagnostic{Handler,Context}
Differential Revision: http://reviews.llvm.org/D18820 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265773 91177308-0d34-0410-b5e6-96231b3b80d8
- Loading branch information
Jeroen Ketema
committed
Apr 8, 2016
1 parent
bd8d19a
commit 9640ae7
Showing
9 changed files
with
137 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
; RUN: llvm-as < %s | llvm-dis > %t.orig | ||
; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo | ||
; RUN: diff -w %t.orig %t.echo | ||
; RUN: llvm-as < %s | llvm-c-test --test-diagnostic-handler 2>&1 | FileCheck %s | ||
; CHECK: Diagnostic handler was not called while loading module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ endif () | |
|
||
add_llvm_tool(llvm-c-test | ||
calc.c | ||
diagnostic.c | ||
disassemble.c | ||
echo.cpp | ||
helpers.c | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//===-- diagnostic.cpp - tool for testing libLLVM and llvm-c API ----------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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"); | ||
break; | ||
case LLVMDSWarning: | ||
fprintf(stderr, "warning"); | ||
break; | ||
case LLVMDSRemark: | ||
fprintf(stderr, "remark"); | ||
break; | ||
case LLVMDSNote: | ||
fprintf(stderr, "note"); | ||
break; | ||
} | ||
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); | ||
LLVMDisposeMessage(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); | ||
} | ||
|
||
LLVMDisposeMemoryBuffer(MB); | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters