Skip to content

Commit 6759dc7

Browse files
committed
Added Core module
1 parent b3e1018 commit 6759dc7

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

llvm-api/LLVM/Core/Context.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public class Context: ContextRef {
9595
}
9696

9797
/// Return a string representation of the DiagnosticInfo. Use
98-
/// LLVMDisposeMessage to free the string.
98+
/// `Core.disposeMessage` (`LLVMDisposeMessage`) to free the string.
9999
public static func getDiagInfoDescription(diagnosticInfo: DiagnosticInfoRef) -> String? {
100100
guard let cString = LLVMGetDiagInfoDescription(diagnosticInfo.diagnosticInfoRef) else { return nil }
101-
defer { LLVMDisposeMessage(cString) }
101+
defer { Core.disposeMessage(cString) }
102102
return String(cString: cString)
103103
}
104104

llvm-api/LLVM/Core/Core.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,82 @@
11
import CLLVM
22

3+
/// This modules provide an interface to libLLVMCore, which implements the LLVM intermediate representation as well
4+
/// as other related types and utilities.
5+
///
6+
/// Many exotic languages can interoperate with C code but have a harder time with C++ due to name mangling. So in addition to C,
7+
/// this interface enables tools written in such languages.
8+
/// - SeeAlso: https://llvm.org/doxygen/group__LLVMCCore.html
9+
enum Core {
10+
/// Return the major, minor, and patch version of LLVM
11+
/// The version components are returned via the function's three output
12+
/// parameters or skipped if a NULL pointer was supplied - return 0.
13+
public static func getVersion() -> (major: UInt32, minor: UInt32, patch: UInt32) {
14+
var major: UInt32 = 0
15+
var minor: UInt32 = 0
16+
var patch: UInt32 = 0
17+
18+
withUnsafeMutablePointer(to: &major) { majorPtr in
19+
withUnsafeMutablePointer(to: &minor) { minorPtr in
20+
withUnsafeMutablePointer(to: &patch) { patchPtr in
21+
LLVMGetVersion(majorPtr, minorPtr, patchPtr)
22+
}
23+
}
24+
}
25+
return (major, minor, patch)
26+
}
27+
28+
/// Create message string reference. Run [disposeMessage] to free message.
29+
/// It useful when LLVM wait as parameter LLVM-string
30+
///
31+
/// - SeeAlso: `disposeLLVMMessage`
32+
public static func createMessage(with message: String) -> UnsafeMutablePointer<CChar>? {
33+
return message.withCString { cString in
34+
LLVMCreateMessage(cString)
35+
}
36+
}
37+
38+
/// Dispose LLVM message
39+
public static func disposeMessage(_ message: UnsafeMutablePointer<CChar>?) {
40+
LLVMDisposeMessage(message)
41+
}
42+
43+
/// This function permanently loads the dynamic library at the given path.
44+
/// It is safe to call this function multiple times for the same library.
45+
public static func loadLibraryPermanently(filename: String) -> Bool {
46+
return filename.withCString { cString in
47+
LLVMLoadLibraryPermanently(cString) != 0
48+
}
49+
}
50+
51+
/// This function parses the given arguments using the LLVM command line parser.
52+
/// Note that the only stable thing about this function is its signature; you
53+
/// cannot rely on any particular set of command line arguments being interpreted
54+
/// the same way across LLVM versions.
55+
public static func parseCommandLineOptions(arguments: [String], overview: String) {
56+
let cArgs = arguments.map { $0.withCString(strdup) }
57+
defer { cArgs.forEach { free($0) } }
58+
overview.withCString { cOverview in
59+
let cArgsPointers = cArgs.map { UnsafePointer($0) }
60+
cArgsPointers.withUnsafeBufferPointer { cArgsPointersBuffer in
61+
LLVMParseCommandLineOptions(Int32(arguments.count), cArgsPointersBuffer.baseAddress, cOverview)
62+
}
63+
}
64+
}
65+
66+
/// This function will search through all previously loaded dynamic
67+
/// libraries for the symbol `symbolName`. If it is found, the address of
68+
/// that symbol is returned. If not, null is returned.
69+
public static func searchForAddressOfSymbol(symbolName: String) -> UnsafeMutableRawPointer? {
70+
return symbolName.withCString { cString in
71+
LLVMSearchForAddressOfSymbol(cString)
72+
}
73+
}
74+
75+
/// This functions permanently adds the symbol \p symbolName with the
76+
/// value `symbolValue`. These symbols are searched before any libraries.
77+
public static func addSymbol(symbolName: String, symbolValue: UnsafeMutableRawPointer) {
78+
symbolName.withCString { cString in
79+
LLVMAddSymbol(cString, symbolValue)
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)