Skip to content

Commit 19e4a6a

Browse files
committed
Added a few more enums
1 parent 731b30e commit 19e4a6a

File tree

4 files changed

+85
-18
lines changed

4 files changed

+85
-18
lines changed

Sources/Clang/Cursor.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,19 @@ extension Cursor {
248248
platforms: platforms)
249249
}
250250

251+
/// Returns the storage class for a function or variable declaration.
252+
public var storageClass: StorageClass? {
253+
return StorageClass(clang: clang_Cursor_getStorageClass(asClang()))
254+
}
255+
256+
/// Returns the access control level for the referenced object.
257+
/// If the cursor refers to a C++ declaration, its access control level
258+
/// within its parent scope is returned. Otherwise, if the cursor refers to
259+
/// a base specifier or access specifier, the specifier itself is returned.
260+
public var accessSpecifier: CXXAccessSpecifierKind? {
261+
return CXXAccessSpecifierKind(clang: clang_getCXXAccessSpecifier(asClang()))
262+
}
263+
251264
/// Given a cursor that represents a documentable entity (e.g.,
252265
/// declaration), return the associated parsed comment
253266
public var fullComment: FullComment? {
@@ -321,3 +334,46 @@ public enum TemplateArgumentKind {
321334
}
322335
}
323336
}
337+
338+
/// Represents the C++ access control level to a base class for a cursor.
339+
public enum CXXAccessSpecifierKind {
340+
case `public`
341+
case protected
342+
case `private`
343+
344+
init?(clang: CX_CXXAccessSpecifier) {
345+
switch clang {
346+
case CX_CXXInvalidAccessSpecifier: return nil
347+
case CX_CXXPublic: self = .public
348+
case CX_CXXProtected: self = .protected
349+
case CX_CXXPrivate: self = .private
350+
default: fatalError("invalid CX_CXXAccessSpecifier \(clang)")
351+
}
352+
}
353+
}
354+
355+
/// Represents the storage classes as declared in the source. CX_SC_Invalid was
356+
/// added for the case that the passed cursor in not a declaration.
357+
public enum StorageClass {
358+
case none
359+
case extern
360+
case `static`
361+
case privateExtern
362+
case openCLWorkGroupLocal
363+
case auto
364+
case register
365+
366+
init?(clang: CX_StorageClass) {
367+
switch clang {
368+
case CX_SC_Invalid: return nil
369+
case CX_SC_None: self = .none
370+
case CX_SC_Extern: self = .extern
371+
case CX_SC_Static: self = .static
372+
case CX_SC_PrivateExtern: self = .privateExtern
373+
case CX_SC_OpenCLWorkGroupLocal: self = .openCLWorkGroupLocal
374+
case CX_SC_Auto: self = .auto
375+
case CX_SC_Register: self = .register
376+
default: fatalError("invalid CX_StorageClass \(clang)")
377+
}
378+
}
379+
}

Sources/Clang/Cursors.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,21 @@ public struct EnumConstantDecl: ClangCursorBacked {
9797
let clang: CXCursor
9898

9999
/// Retrieve the integer value of an enum constant declaration as an `Int`.
100-
var value: Int {
100+
public var value: Int {
101101
return Int(clang_getEnumConstantDeclValue(clang))
102102
}
103103

104104
/// Retrieve the integer value of an enum constant declaration as a `UInt`.
105-
var unsignedValue: UInt {
105+
public var unsignedValue: UInt {
106106
return UInt(clang_getEnumConstantDeclUnsignedValue(clang))
107107
}
108108
}
109109

110+
/// An access specifier.
111+
public struct CXXAccessSpecifier: ClangCursorBacked {
112+
let clang: CXCursor
113+
}
114+
110115
public struct EnumDecl: ClangCursorBacked {
111116
let clang: CXCursor
112117

@@ -285,22 +290,14 @@ public struct ObjCDynamicDecl: ClangCursorBacked {
285290
let clang: CXCursor
286291
}
287292

288-
/// An access specifier.
289-
public struct CXXAccessSpecifier: ClangCursorBacked {
290-
let clang: CXCursor
291-
}
292-
293-
/// An access specifier.
294293
public struct ObjCSuperClassRef: ClangCursorBacked {
295294
let clang: CXCursor
296295
}
297296

298-
/// An access specifier.
299297
public struct ObjCProtocolRef: ClangCursorBacked {
300298
let clang: CXCursor
301299
}
302300

303-
/// An access specifier.
304301
public struct ObjCClassRef: ClangCursorBacked {
305302
let clang: CXCursor
306303
}

Sources/Clang/TranslationUnit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ public class TranslationUnit {
112112
/// Creates a `TranslationUnit` by parsing the file at the specified path,
113113
/// passing optional command line arguments and options to clang.
114114
///
115-
/// - Parameters:
115+
/// - parameters:
116116
/// - index: The index
117117
/// - filename: The path you're going to parse
118118
/// - args: Optional command-line arguments to pass to clang
119119
/// - options: Options for how to handle the parsed file
120-
/// - throws: `ClangError` if the translation unit could not
121-
/// be created successfully.
120+
/// - throws: `ClangError` if the translation unit could not be created
121+
/// successfully.
122122
public init(index: Index, filename: String,
123123
commandLineArgs args: [String] = [],
124124
options: TranslationUnitOptions = []) throws {

Tests/ClangTests/ClangTests.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,16 @@ func convert(_ comment: FullComment, indent: Int = 0) -> [String] {
214214
.map { "/// \($0)" }
215215
}
216216

217-
217+
extension TranslationUnit {
218+
func findComment(symbol: String) -> FullComment? {
219+
for child in cursor.children() {
220+
if "\(child)".contains(symbol) {
221+
return child.fullComment
222+
}
223+
}
224+
return nil
225+
}
226+
}
218227

219228
class ClangTests: XCTestCase {
220229
func testExample() {
@@ -227,8 +236,8 @@ class ClangTests: XCTestCase {
227236
"-I/usr/local/opt/llvm/include"
228237
])
229238
let typesToMake: [String: (type: String, prefix: String, suffix: String)] = [
230-
"CXTranslationUnit_Flags": (type: "TranslationUnitOptions",
231-
prefix: "CXTranslationUnit_",
239+
"CX_StorageClass": (type: "StorageClass",
240+
prefix: "CX_SC_",
232241
suffix: "")
233242
]
234243
for child in tu.cursor.children() {
@@ -239,9 +248,14 @@ class ClangTests: XCTestCase {
239248
// generateSwiftOptionSet(forEnum: enumDecl, prefix: "CXTranslationUnit_", name: "TranslationUnitOptions")
240249
// }
241250
guard let enumDecl = child as? EnumDecl else { continue }
251+
// print(enumDecl)
252+
// for constant in enumDecl.constants() {
253+
// print(" \(constant)")
254+
// }
255+
// print()
242256
if let values = typesToMake["\(enumDecl)"] {
243-
generateSwiftOptionSet(forEnum: enumDecl, prefix: values.prefix, name: values.type)
244-
// generateSwiftEnum(forEnum: enumDecl, prefix: values.prefix, name: values.type)
257+
// generateSwiftOptionSet(forEnum: enumDecl, prefix: values.prefix, name: values.type)
258+
generateSwiftEnum(forEnum: enumDecl, prefix: values.prefix, name: values.type)
245259
// generateStructs(forEnum: enumDecl, type: values.type,
246260
// prefix: values.prefix, suffix: values.suffix)
247261
}

0 commit comments

Comments
 (0)