Skip to content

Commit b3e1018

Browse files
committed
Refactoring for LLVM-Core module
1 parent f9b3d2c commit b3e1018

22 files changed

+55
-26
lines changed
Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Context: ContextRef {
1919
public var contextRef: LLVMContextRef { llvm }
2020

2121
/// Diagnostic handler type
22-
public typealias DiagnosticHandler = @convention(c) (LLVMDiagnosticInfoRef, UnsafeMutableRawPointer?) -> Void
22+
public typealias DiagnosticHandler = @convention(c) (LLVMDiagnosticInfoRef?, UnsafeMutableRawPointer?) -> Void
2323

2424
public typealias YieldCallback = @convention(c) (LLVMContextRef?, UnsafeMutableRawPointer?) -> Void
2525

@@ -48,16 +48,12 @@ public class Context: ContextRef {
4848

4949
/// Get the diagnostic handler of current context.
5050
public var getDiagnosticHandler: DiagnosticHandler? {
51-
if let handler = LLVMContextGetDiagnosticHandler(contextRef) {
52-
return unsafeBitCast(handler, to: DiagnosticHandler.self)
53-
} else {
54-
return nil
55-
}
51+
LLVMContextGetDiagnosticHandler(llvm)
5652
}
5753

5854
/// Set the diagnostic handler for current context.
59-
public func setDiagnosticHandler(handler: LLVMDiagnosticHandler?, diagnosticContext: UnsafeMutableRawPointer?) {
60-
LLVMContextSetDiagnosticHandler(contextRef, handler, diagnosticContext)
55+
public func setDiagnosticHandler(handler: DiagnosticHandler?, diagnosticContext: UnsafeMutableRawPointer?) {
56+
LLVMContextSetDiagnosticHandler(llvm, handler.self, diagnosticContext)
6157
}
6258

6359
/// Retrieve whether the given context is set to discard all value names.
@@ -100,14 +96,15 @@ public class Context: ContextRef {
10096

10197
/// Return a string representation of the DiagnosticInfo. Use
10298
/// LLVMDisposeMessage to free the string.
103-
public func getDiagInfoDescription(diagnosticInfo: DiagnosticInfoRef) -> String? {
99+
public static func getDiagInfoDescription(diagnosticInfo: DiagnosticInfoRef) -> String? {
104100
guard let cString = LLVMGetDiagInfoDescription(diagnosticInfo.diagnosticInfoRef) else { return nil }
101+
defer { LLVMDisposeMessage(cString) }
105102
return String(cString: cString)
106103
}
107104

108105
/// Return an enum LLVMDiagnosticSeverity.
109-
public func getDiagInfoSeverity(diagnosticInfo: DiagnosticInfoRef) -> LLVMDiagnosticSeverity {
110-
LLVMGetDiagInfoSeverity(diagnosticInfo.diagnosticInfoRef)
106+
public static func getDiagInfoSeverity(diagnosticInfo: DiagnosticInfoRef) -> DiagnosticSeverity? {
107+
DiagnosticSeverity(from: LLVMGetDiagInfoSeverity(diagnosticInfo.diagnosticInfoRef))
111108
}
112109

113110
/// Get Metadata KindId by name in current Context.
@@ -118,7 +115,7 @@ public class Context: ContextRef {
118115
}
119116
}
120117

121-
public func getMDKindID(name: String) -> UInt32 {
118+
public static func getMDKindID(name: String) -> UInt32 {
122119
name.withCString { cString in
123120
LLVMGetMDKindID(cString, UInt32(name.utf8.count))
124121
}
@@ -133,13 +130,14 @@ public class Context: ContextRef {
133130
///
134131
/// NB: Attribute names and/or id are subject to change without
135132
/// going through the C API deprecation cycle.
136-
public func getEnumAttributeKindForName(name: String) -> UInt32 {
133+
public static func getEnumAttributeKindForName(name: String) -> UInt32 {
137134
name.withCString { cString in
138135
LLVMGetEnumAttributeKindForName(cString, name.utf8.count)
139136
}
140137
}
141138

142-
public func getLastEnumAttributeKind() -> UInt32 {
139+
/// Get last enum attribute
140+
public static func getLastEnumAttributeKind() -> UInt32 {
143141
LLVMGetLastEnumAttributeKind()
144142
}
145143

@@ -154,12 +152,12 @@ public class Context: ContextRef {
154152
}
155153

156154
/// Get the unique id corresponding to the enum attribute passed as argument.
157-
public func getEnumAttributeKind(attributeRef: AttributeRef) -> UInt32 {
155+
public static func getEnumAttributeKind(attributeRef: AttributeRef) -> UInt32 {
158156
LLVMGetEnumAttributeKind(attributeRef.attributeRef)
159157
}
160158

161159
/// Get the enum attribute's value. 0 is returned if none exists.
162-
public func getEnumAttributeValue(attributeRef: AttributeRef) -> UInt64 {
160+
public static func getEnumAttributeValue(attributeRef: AttributeRef) -> UInt64 {
163161
LLVMGetEnumAttributeValue(attributeRef.attributeRef)
164162
}
165163

@@ -170,7 +168,7 @@ public class Context: ContextRef {
170168
}
171169

172170
/// Get the type attribute's value.
173-
public func getTypeAttributeValue(attributeRef: AttributeRef) -> TypeRef? {
171+
public static func getTypeAttributeValue(attributeRef: AttributeRef) -> TypeRef? {
174172
guard let typeRef = LLVMGetTypeAttributeValue(attributeRef.attributeRef) else { return nil }
175173
return Types(llvm: typeRef)
176174
}
@@ -186,12 +184,8 @@ public class Context: ContextRef {
186184
return Attribute(attributeRef: attributeRef)
187185
}
188186

189-
//===============================
190-
// ###############################
191-
//===============================
192-
193187
/// Get the string attribute's kind.
194-
public func getStringAttributeKind(attributeRef: AttributeRef, length: UInt32) -> String? {
188+
public static func getStringAttributeKind(attributeRef: AttributeRef, length: UInt32) -> String? {
195189
var mutLength = length
196190
guard let cString = withUnsafeMutablePointer(to: &mutLength, { lengthPtr in
197191
LLVMGetStringAttributeKind(attributeRef.attributeRef, lengthPtr)
@@ -200,7 +194,7 @@ public class Context: ContextRef {
200194
}
201195

202196
/// Get the string attribute's value.
203-
public func getStringAttributeValue(attributeRef: AttributeRef, length: UInt32) -> String? {
197+
public static func getStringAttributeValue(attributeRef: AttributeRef, length: UInt32) -> String? {
204198
var mutLength = length
205199
guard let cString = withUnsafeMutablePointer(to: &mutLength, { lengthPtr in
206200
LLVMGetStringAttributeValue(attributeRef.attributeRef, lengthPtr)
@@ -209,17 +203,17 @@ public class Context: ContextRef {
209203
}
210204

211205
/// Check for the types of attributes.
212-
public func isEnumAttribute(attributeRef: AttributeRef) -> Bool {
206+
public static func isEnumAttribute(attributeRef: AttributeRef) -> Bool {
213207
return LLVMIsEnumAttribute(attributeRef.attributeRef) != 0
214208
}
215209

216210
/// Check for the types of attributes.
217-
public func isStringAttribute(attributeRef: AttributeRef) -> Bool {
211+
public static func isStringAttribute(attributeRef: AttributeRef) -> Bool {
218212
return LLVMIsStringAttribute(attributeRef.attributeRef) != 0
219213
}
220214

221215
/// Check for the types of attributes.
222-
public func isTypeAttribute(attributeRef: AttributeRef) -> Bool {
216+
public static func isTypeAttribute(attributeRef: AttributeRef) -> Bool {
223217
return LLVMIsTypeAttribute(attributeRef.attributeRef) != 0
224218
}
225219

llvm-api/LLVM/Core/Core.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import CLLVM
2+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import CLLVM
2+
3+
/// Diagnostic functionality
4+
public struct Diagnostic: DiagnosticInfoRef {
5+
private var llvm: LLVMDiagnosticInfoRef
6+
public var diagnosticInfoRef: LLVMDiagnosticInfoRef { llvm }
7+
8+
/// Init DIagnosticInfo
9+
public init(llvm: LLVMDiagnosticInfoRef) {
10+
self.llvm = llvm
11+
}
12+
13+
/// Return a string representation of the DiagnosticInfo.
14+
public var getDiagInfoDescription: String? {
15+
Context.getDiagInfoDescription(diagnosticInfo: self)
16+
}
17+
18+
/// Return an enum LLVMDiagnosticSeverity.
19+
public var getDiagInfoSeverity: DiagnosticSeverity? {
20+
Context.getDiagInfoSeverity(diagnosticInfo: self)
21+
}
22+
}
23+
24+
public enum DiagnosticSeverity: Int {
25+
case error = 0
26+
case warning = 1
27+
case remark = 2
28+
case note = 3
29+
30+
public init?(from cSeverity: LLVMDiagnosticSeverity) {
31+
self.init(rawValue: Int(cSeverity.rawValue))
32+
}
33+
}

0 commit comments

Comments
 (0)