Skip to content

Commit 11f2ceb

Browse files
committed
Extended context with Diagnostc Handler
1 parent f34abd5 commit 11f2ceb

File tree

10 files changed

+46
-23
lines changed

10 files changed

+46
-23
lines changed

Package.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import PackageDescription
66
let package = Package(
77
name: "llvm-codegen",
88
products: [
9-
.executable(name: "llvm-codegen", targets: ["llvm-codegen"])
9+
.executable(name: "llvm-codegen", targets: ["llvm-codegen"]),
1010
],
1111
dependencies: [
12-
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0")
12+
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0"),
1313
],
1414
targets: [
1515
.executableTarget(
@@ -19,7 +19,7 @@ let package = Package(
1919
.product(
2020
name: "ArgumentParser",
2121
package: "swift-argument-parser"
22-
)
22+
),
2323
],
2424
path: "llvm-codegen/cli"
2525
),
@@ -28,14 +28,14 @@ let package = Package(
2828
path: "llvm-codegen/CLLVM",
2929
pkgConfig: "CLLVM",
3030
providers: [
31-
.brew(["llvm"])
31+
.brew(["llvm"]),
3232
]
3333
),
3434
.target(
3535
name: "LLVM",
3636
dependencies: ["CLLVM"],
3737
path: "llvm-codegen/LLVM"
38-
)
38+
),
3939
],
4040
cxxLanguageStandard: .cxx20
4141
)

llvm-codegen/LLVM/AddressSpace.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ public struct AddressSpace: Equatable {
5959
/// Creates and initializes an address space with the given identifier.
6060
/// - Parameter identifier: The raw, integral address space identifier.
6161
public init(_ identifier: UInt32) {
62-
self.rawValue = identifier
62+
rawValue = identifier
6363
}
6464
}

llvm-codegen/LLVM/Context.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class Context: ContextRef {
1818
/// Retrieves the underlying LLVM type object.
1919
public var contextRef: LLVMContextRef { llvm }
2020

21+
/// Diagnostic handler type
22+
public typealias DiagnosticHandler = @convention(c) (LLVMDiagnosticInfoRef, UnsafeMutableRawPointer?) -> Void
23+
2124
/// Retrieves the global context instance.
2225
///
2326
/// The global context is an particularly convenient instance managed by LLVM
@@ -41,14 +44,34 @@ public class Context: ContextRef {
4144
self.llvm = llvm
4245
}
4346

47+
/// Get the diagnostic handler of current context.
48+
public var getContextDiagnosticHandler: DiagnosticHandler? {
49+
if let handler = LLVMContextGetDiagnosticHandler(contextRef) {
50+
return unsafeBitCast(handler, to: DiagnosticHandler.self)
51+
} else {
52+
return nil
53+
}
54+
}
55+
56+
/// Set the diagnostic handler for current context.
57+
public func setContextDiagnosticHandler(handler: LLVMDiagnosticHandler?, diagnosticContext: UnsafeMutableRawPointer?) {
58+
LLVMContextSetDiagnosticHandler(contextRef, handler, diagnosticContext)
59+
}
60+
4461
/// Returns whether the given context is set to discard all value names.
4562
///
4663
/// If true, only the names of GlobalValue objects will be available in
4764
/// the IR. This can be used to save memory and processing time, especially
4865
/// in release environments.
4966
public var discardValueNames: Bool {
50-
get { return LLVMContextShouldDiscardValueNames(llvm) != 0 }
51-
set { LLVMContextSetDiscardValueNames(llvm, newValue.llvm) }
67+
get {
68+
// Retrieve whether the given context is set to discard all value names.
69+
return LLVMContextShouldDiscardValueNames(llvm) != 0
70+
}
71+
set {
72+
// Set whether the given context discards all value names.
73+
LLVMContextSetDiscardValueNames(llvm, newValue.llvm)
74+
}
5275
}
5376

5477
/// Deinitialize this value and dispose of its resources.

llvm-codegen/LLVM/Functions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public struct Function: ValueRef {
114114
public var getEntryBasicBlock: BasicBlockRef? { BasicBlock.getEntryBasicBlock(funcValueRef: self) }
115115

116116
/// Append the given basic block to the basic block list for the current function.
117-
public func appendExistingBasicBlock(funcValueRef: ValueRef, blockRef: BasicBlockRef) { BasicBlock.appendExistingBasicBlock(funcValueRef: self, blockRef: blockRef)
117+
public func appendExistingBasicBlock(funcValueRef _: ValueRef, blockRef: BasicBlockRef) { BasicBlock.appendExistingBasicBlock(funcValueRef: self, blockRef: blockRef)
118118
}
119119

120120
/// Append named basic block to the end of a function in Context.

llvm-codegen/LLVM/Types/Array.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct ArrayType: TypeRef {
2929
public init(elementType: TypeRef, count: UInt32) {
3030
self.elementType = elementType
3131
self.count = .x32(count)
32-
self.llvm = LLVMArrayType(elementType.typeRef, count)
32+
llvm = LLVMArrayType(elementType.typeRef, count)
3333
}
3434

3535
/// Creates an array type from an underlying element type and count.
@@ -38,14 +38,14 @@ public struct ArrayType: TypeRef {
3838
public init(elementType: TypeRef, count: UInt64) {
3939
self.elementType = elementType
4040
self.count = .x64(count)
41-
self.llvm = LLVMArrayType2(elementType.typeRef, count)
41+
llvm = LLVMArrayType2(elementType.typeRef, count)
4242
}
4343

4444
/// Init with predefined `TypeRef`
4545
public init(typeRef: TypeRef) {
46-
self.elementType = ArrayType.getElementType(typeRef: typeRef)!
47-
self.count = .x64(ArrayType.getArrayLength2(typeRef: typeRef))
48-
self.llvm = typeRef.typeRef
46+
elementType = ArrayType.getElementType(typeRef: typeRef)!
47+
count = .x64(ArrayType.getArrayLength2(typeRef: typeRef))
48+
llvm = typeRef.typeRef
4949
}
5050

5151
/// Get the length of an array type for 32 bits array size.

llvm-codegen/LLVM/Types/Function.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct FunctionType: TypeRef {
1616
/// For parent classes init with predefined `FunctionType` in Context
1717
public init(returnType: TypeRef, parameterTypes: [TypeRef], isVariadic: Bool = false) {
1818
var mutableParamTypes = parameterTypes.map { $0.typeRef as Optional }
19-
self.llvm = mutableParamTypes.withUnsafeMutableBufferPointer { paramsBuffer in
19+
llvm = mutableParamTypes.withUnsafeMutableBufferPointer { paramsBuffer in
2020
LLVMFunctionType(returnType.typeRef, paramsBuffer.baseAddress, UInt32(parameterTypes.count), isVariadic.llvm)!
2121
}
2222
self.returnType = returnType

llvm-codegen/LLVM/Types/Other/TargetExt.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct TargetExtType: TypeRef {
1414
public init(in context: Context, name: String, typeParams: [TypeRef], intParams: [UInt32]) {
1515
var mutableTypeParams = typeParams.map { $0.typeRef as Optional }
1616
var mutableIntParams = intParams
17-
self.llvm = name.withCString { cName in
17+
llvm = name.withCString { cName in
1818
mutableTypeParams.withUnsafeMutableBufferPointer { typeParamsBuffer in
1919
mutableIntParams.withUnsafeMutableBufferPointer { intParamsBuffer in
2020
LLVMTargetExtTypeInContext(context.contextRef, cName,

llvm-codegen/LLVM/Types/Types.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public struct Types: TypeRef {
214214

215215
/// Init `Types` by `TypeRef`
216216
public init(typeRef: TypeRef) {
217-
self.llvm = typeRef.typeRef
217+
llvm = typeRef.typeRef
218218
}
219219

220220
/// Init `Types` by `LLVMTypeRef`

llvm-codegen/LLVM/Types/Vector.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ public class VectorType: TypeRef {
2121
public init(elementType: TypeRef, count: UInt32) {
2222
self.elementType = elementType
2323
self.count = count
24-
self.llvm = LLVMVectorType(elementType.typeRef, count)
24+
llvm = LLVMVectorType(elementType.typeRef, count)
2525
}
2626

2727
/// Init with predefined `TypeRef`
2828
public init(typeRef: TypeRef) {
29-
self.elementType = VectorType.getElementType(typeRef: typeRef)!
30-
self.count = VectorType.getVectorSize(typeRef: typeRef)
31-
self.llvm = typeRef.typeRef
29+
elementType = VectorType.getElementType(typeRef: typeRef)!
30+
count = VectorType.getVectorSize(typeRef: typeRef)
31+
llvm = typeRef.typeRef
3232
}
3333

3434
/// Init for pre-init vector for depended class
3535
init(for vecTy: LLVMTypeRef, elementType: TypeRef, count: UInt32) {
3636
self.elementType = elementType
3737
self.count = count
38-
self.llvm = vecTy
38+
llvm = vecTy
3939
}
4040

4141
/// Get the (possibly scalable) number of elements in the current vector type.

llvm-codegen/LLVM/Values/Values.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public struct Values: ValueRef {
88

99
/// Init `Values` by `ValueRef`
1010
public init(valueRef: ValueRef) {
11-
self.llvm = valueRef.valueRef
11+
llvm = valueRef.valueRef
1212
}
1313

1414
/// Init `Values` by `LLVMValueRef`

0 commit comments

Comments
 (0)