Skip to content

Commit 731b30e

Browse files
committed
Added generation for OptionSets and generated a few low-hanging fruit.
1 parent 4d72cdd commit 731b30e

File tree

8 files changed

+321
-73
lines changed

8 files changed

+321
-73
lines changed

Sources/Clang/CTypes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public struct RecordType: ClangTypeBacked {
2121
}
2222
return Int(val)
2323
}
24-
24+
2525
/// Gathers and returns all the fields of this record.
2626
func fields() -> [Cursor] {
2727
let fields = Box([Cursor]())

Sources/Clang/Cursors.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,6 @@ public struct ClassDecl: RecordDecl {
9696
public struct EnumConstantDecl: ClangCursorBacked {
9797
let clang: CXCursor
9898

99-
/// Retrieve the integer type of an enum declaration.
100-
var integerType: CType {
101-
return convertType(clang_getEnumDeclIntegerType(clang))!
102-
}
103-
10499
/// Retrieve the integer value of an enum constant declaration as an `Int`.
105100
var value: Int {
106101
return Int(clang_getEnumConstantDeclValue(clang))
@@ -118,6 +113,11 @@ public struct EnumDecl: ClangCursorBacked {
118113
func constants() -> [EnumConstantDecl] {
119114
return children() as! [EnumConstantDecl]
120115
}
116+
117+
/// Retrieve the integer type of an enum declaration.
118+
var integerType: CType {
119+
return convertType(clang_getEnumDeclIntegerType(clang))!
120+
}
121121
}
122122

123123
protocol TypeAliasCursor: ClangCursorBacked {}

Sources/Clang/Diagnostic.swift

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,88 @@ public enum DiagnosticSeverity {
3131
}
3232
}
3333
}
34+
35+
/// Describes the kind of error that occurred (if any) in a call to
36+
/// loadDiagnostics
37+
public enum LoadDiagError: Error {
38+
/// Indicates that an unknown error occurred while attempting to deserialize
39+
/// diagnostics.
40+
case unknown
41+
42+
/// Indicates that the file containing the serialized diagnostics could not be
43+
/// opened.
44+
case cannotLoad
45+
46+
/// Indicates that the serialized diagnostics file is invalid or corrupt.
47+
case invalidFile
48+
49+
init?(clang: CXLoadDiag_Error) {
50+
switch clang {
51+
case CXLoadDiag_None: return nil
52+
case CXLoadDiag_Unknown: self = .unknown
53+
case CXLoadDiag_CannotLoad: self = .cannotLoad
54+
case CXLoadDiag_InvalidFile: self = .invalidFile
55+
default: fatalError("invalid CXLoadDiag_Error \(clang)")
56+
}
57+
}
58+
}
59+
60+
/// Options to control the display of diagnostics.
61+
/// The values in this enum are meant to be combined to customize the
62+
/// behavior of
63+
/// `clang_formatDiagnostic().`
64+
public struct DiagnosticDisplayOptions: OptionSet {
65+
public typealias RawValue = CXDiagnosticDisplayOptions.RawValue
66+
public let rawValue: RawValue
67+
/// Creates a new DiagnosticDisplayOptions from a raw integer value.
68+
public init(rawValue: RawValue) {
69+
self.rawValue = rawValue
70+
}
71+
72+
/// Display the source-location information where the diagnostic was located.
73+
/// When set, diagnostics will be prefixed by the file, line, and (optionally)
74+
/// column to which the diagnostic refers. For example,
75+
/// ```
76+
/// test.c:28: warning: extra tokens at end of #endif directive
77+
/// ```
78+
/// This option corresponds to the clang flag
79+
/// `-fshow-source-location.`
80+
public static let sourceLocation = DiagnosticDisplayOptions(rawValue:
81+
CXDiagnostic_DisplaySourceLocation.rawValue)
82+
83+
/// If displaying the source-location information of the diagnostic, also
84+
/// include the column number.
85+
/// This option corresponds to the clang flag
86+
/// `-fshow-column.`
87+
public static let column = DiagnosticDisplayOptions(rawValue:
88+
CXDiagnostic_DisplayColumn.rawValue)
89+
90+
/// If displaying the source-location information of the diagnostic, also
91+
/// include information about source ranges in a machine-parsable format.
92+
/// This option corresponds to the clang flag
93+
/// `-fdiagnostics-print-source-range-info.`
94+
public static let sourceRanges = DiagnosticDisplayOptions(rawValue:
95+
CXDiagnostic_DisplaySourceRanges.rawValue)
96+
97+
/// Display the option name associated with this diagnostic, if any.
98+
/// The option name displayed (e.g., -Wconversion) will be placed in brackets
99+
/// after the diagnostic text. This option corresponds to the clang flag
100+
/// `-fdiagnostics-show-option.`
101+
public static let option = DiagnosticDisplayOptions(rawValue:
102+
CXDiagnostic_DisplayOption.rawValue)
103+
104+
/// Display the category number associated with this diagnostic, if any.
105+
/// The category number is displayed within brackets after the diagnostic
106+
/// text.
107+
/// This option corresponds to the clang flag
108+
/// `-fdiagnostics-show-category=id.`
109+
public static let categoryId = DiagnosticDisplayOptions(rawValue:
110+
CXDiagnostic_DisplayCategoryId.rawValue)
111+
112+
/// Display the category name associated with this diagnostic, if any.
113+
/// The category name is displayed within brackets after the diagnostic text.
114+
/// This option corresponds to the clang flag
115+
/// `-fdiagnostics-show-category=name.`
116+
public static let categoryName = DiagnosticDisplayOptions(rawValue:
117+
CXDiagnostic_DisplayCategoryName.rawValue)
118+
}

Sources/Clang/File.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import cclang
2+
import Foundation
3+
4+
/// Represents a file ID that's unique to each file in a translation unit.
5+
public struct UniqueFileID: Equatable {
6+
let clang: CXFileUniqueID
7+
8+
public static func ==(lhs: UniqueFileID, rhs: UniqueFileID) -> Bool {
9+
return lhs.clang.data == rhs.clang.data
10+
}
11+
}
12+
13+
/// A particular source file that is part of a translation unit.
14+
public struct File: Equatable {
15+
let clang: CXFile
16+
17+
/// Retrieve the complete file and path name of the given file.
18+
public var name: String {
19+
return clang_getFileName(clang).asSwift()
20+
}
21+
22+
/// Retrieve the last modification time of the given file.
23+
public var lastModified: Date {
24+
return Date(timeIntervalSince1970: TimeInterval(clang_getFileTime(clang)))
25+
}
26+
27+
/// Retrieves the unique identifier for this file.
28+
/// If it failed, returns `nil`.
29+
public var uniqueID: UniqueFileID? {
30+
var id = CXFileUniqueID()
31+
guard clang_getFileUniqueID(clang, &id) != 0 else {
32+
return nil
33+
}
34+
return UniqueFileID(clang: id)
35+
}
36+
37+
/// Determines if two files are equal.
38+
public static func ==(lhs: File, rhs: File) -> Bool {
39+
return clang_File_isEqual(lhs.clang, rhs.clang) != 0
40+
}
41+
}

Sources/Clang/Index.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,51 @@ public class Index {
99
displayDiagnostics.asClang())
1010
}
1111

12+
/// The general options associated with an Index.
13+
var globalOptions: GlobalOptions {
14+
get {
15+
return GlobalOptions(rawValue: clang_CXIndex_getGlobalOptions(clang))
16+
}
17+
set {
18+
clang_CXIndex_setGlobalOptions(clang, newValue.rawValue)
19+
}
20+
}
21+
1222
deinit {
1323
clang_disposeIndex(clang)
1424
}
1525
}
26+
27+
/// Global options used to inform the Index.
28+
public struct GlobalOptions: OptionSet {
29+
public typealias RawValue = CXGlobalOptFlags.RawValue
30+
public let rawValue: RawValue
31+
32+
/// Creates a new GlobalOptions from a raw integer value.
33+
public init(rawValue: RawValue) {
34+
self.rawValue = rawValue
35+
}
36+
37+
/// Used to indicate that no special CXIndex options are needed.
38+
public static let none = GlobalOptions(rawValue:
39+
CXGlobalOpt_None.rawValue)
40+
41+
/// Used to indicate that threads that libclang creates for indexing purposes
42+
/// should use background priority.
43+
/// Affects #clang_indexSourceFile, #clang_indexTranslationUnit,
44+
/// #clang_parseTranslationUnit, #clang_saveTranslationUnit.
45+
public static let threadBackgroundPriorityForIndexing = GlobalOptions(rawValue:
46+
CXGlobalOpt_ThreadBackgroundPriorityForIndexing.rawValue)
47+
48+
/// Used to indicate that threads that libclang creates for editing purposes
49+
/// should use background priority.
50+
/// Affects #clang_reparseTranslationUnit, #clang_codeCompleteAt,
51+
/// #clang_annotateTokens
52+
public static let threadBackgroundPriorityForEditing = GlobalOptions(rawValue:
53+
CXGlobalOpt_ThreadBackgroundPriorityForEditing.rawValue)
54+
55+
/// Used to indicate that all threads that libclang creates should use
56+
/// background priority.
57+
public static let threadBackgroundPriorityForAll = GlobalOptions(rawValue:
58+
CXGlobalOpt_ThreadBackgroundPriorityForAll.rawValue)
59+
}

Sources/Clang/Token.swift

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,6 @@ public struct Token {
2121
}
2222
}
2323

24-
/// Represents a file ID that's unique to each file in a translation unit.
25-
public struct UniqueFileID: Equatable {
26-
let clang: CXFileUniqueID
27-
28-
public static func ==(lhs: UniqueFileID, rhs: UniqueFileID) -> Bool {
29-
return lhs.clang.data == rhs.clang.data
30-
}
31-
}
32-
33-
/// A particular source file that is part of a translation unit.
34-
public struct File: Equatable {
35-
let clang: CXFile
36-
37-
/// Retrieve the complete file and path name of the given file.
38-
public var name: String {
39-
return clang_getFileName(clang).asSwift()
40-
}
41-
42-
/// Retrieve the last modification time of the given file.
43-
public var lastModified: Date {
44-
return Date(timeIntervalSince1970: TimeInterval(clang_getFileTime(clang)))
45-
}
46-
47-
/// Retrieves the unique identifier for this file.
48-
/// If it failed, returns `nil`.
49-
public var uniqueID: UniqueFileID? {
50-
var id = CXFileUniqueID()
51-
guard clang_getFileUniqueID(clang, &id) != 0 else {
52-
return nil
53-
}
54-
return UniqueFileID(clang: id)
55-
}
56-
57-
public static func ==(lhs: File, rhs: File) -> Bool {
58-
return clang_File_isEqual(lhs.clang, rhs.clang) != 0
59-
}
60-
}
61-
6224
public struct SourceLocation {
6325
let clang: CXSourceLocation
6426

Sources/Clang/TranslationUnit.swift

Lines changed: 98 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,104 @@
11
import cclang
22

3+
/// Flags that control the creation of translation units.
4+
/// The enumerators in this enumeration type are meant to be bitwise ORed
5+
/// together to specify which options should be used when constructing the
6+
/// translation unit.
37
public struct TranslationUnitOptions: OptionSet {
4-
public typealias RawValue = UInt32
5-
public let rawValue: UInt32
6-
7-
public init(rawValue: UInt32) { self.rawValue = rawValue }
8-
9-
public static let detailedPreprocessingRecord =
10-
TranslationUnitOptions(rawValue: CXTranslationUnit_DetailedPreprocessingRecord.rawValue)
11-
public static let incomplete =
12-
TranslationUnitOptions(rawValue: CXTranslationUnit_Incomplete.rawValue)
13-
public static let precompiledPreamble =
14-
TranslationUnitOptions(rawValue: CXTranslationUnit_PrecompiledPreamble.rawValue)
15-
public static let cacheCompletionResults =
16-
TranslationUnitOptions(rawValue: CXTranslationUnit_CacheCompletionResults.rawValue)
17-
public static let forSerialization =
18-
TranslationUnitOptions(rawValue: CXTranslationUnit_ForSerialization.rawValue)
19-
public static let cxxChainedPCH =
20-
TranslationUnitOptions(rawValue: CXTranslationUnit_CXXChainedPCH.rawValue)
21-
public static let skipFunctionBodies =
22-
TranslationUnitOptions(rawValue: CXTranslationUnit_SkipFunctionBodies.rawValue)
23-
public static let includeBriefCommentsInCodeCompletion =
24-
TranslationUnitOptions(rawValue: CXTranslationUnit_IncludeBriefCommentsInCodeCompletion.rawValue)
25-
public static let createPreambleOnFirstParse =
26-
TranslationUnitOptions(rawValue: CXTranslationUnit_CreatePreambleOnFirstParse.rawValue)
27-
public static let keepGoing =
28-
TranslationUnitOptions(rawValue: CXTranslationUnit_KeepGoing.rawValue)
8+
public typealias RawValue = CXTranslationUnit_Flags.RawValue
9+
public let rawValue: RawValue
10+
11+
/// Creates a new TranslationUnitOptions from a raw integer value.
12+
public init(rawValue: RawValue) {
13+
self.rawValue = rawValue
14+
}
15+
16+
/// Used to indicate that no special translation-unit options are needed.
17+
public static let none = TranslationUnitOptions(rawValue:
18+
CXTranslationUnit_None.rawValue)
19+
20+
/// Used to indicate that the parser should construct a "detailed"
21+
/// preprocessing record, including all macro definitions and instantiations.
22+
/// Constructing a detailed preprocessing record requires more memory and time
23+
/// to parse, since the information contained in the record is usually not
24+
/// retained. However, it can be useful for applications that require more
25+
/// detailed information about the behavior of the preprocessor.
26+
public static let detailedPreprocessingRecord = TranslationUnitOptions(rawValue:
27+
CXTranslationUnit_DetailedPreprocessingRecord.rawValue)
28+
29+
/// Used to indicate that the translation unit is incomplete.
30+
/// When a translation unit is considered "incomplete", semantic analysis that
31+
/// is typically performed at the end of the translation unit will be
32+
/// suppressed. For example, this suppresses the completion of tentative
33+
/// declarations in C and of instantiation of implicitly-instantiation
34+
/// function templates in C++. This option is typically used when parsing a
35+
/// header with the intent of producing a precompiled header.
36+
public static let incomplete = TranslationUnitOptions(rawValue:
37+
CXTranslationUnit_Incomplete.rawValue)
38+
39+
/// Used to indicate that the translation unit should be built with an
40+
/// implicit precompiled header for the preamble.
41+
/// An implicit precompiled header is used as an optimization when a
42+
/// particular translation unit is likely to be reparsed many times
43+
/// when the sources aren't changing that often. In this case, an
44+
/// implicit precompiled header will be built containing all of the
45+
/// initial includes at the top of the main file (what we refer to as
46+
/// the "preamble" of the file). In subsequent parses, if the
47+
/// preamble or the files in it have not changed,
48+
/// `clang_reparseTranslationUnit()`
49+
/// will re-use the implicit
50+
/// precompiled header to improve parsing performance.
51+
public static let precompiledPreamble = TranslationUnitOptions(rawValue:
52+
CXTranslationUnit_PrecompiledPreamble.rawValue)
53+
54+
/// Used to indicate that the translation unit should cache some
55+
/// code-completion results with each reparse of the source file.
56+
/// Caching of code-completion results is a performance optimization that
57+
/// introduces some overhead to reparsing but improves the performance of
58+
/// code-completion operations.
59+
public static let cacheCompletionResults = TranslationUnitOptions(rawValue:
60+
CXTranslationUnit_CacheCompletionResults.rawValue)
61+
62+
/// This option is typically used when parsing a header with the intent of
63+
/// producing a precompiled header.
64+
/// Used to indicate that the translation unit will be serialized with
65+
/// `clang_saveTranslationUnit.`
66+
public static let forSerialization = TranslationUnitOptions(rawValue:
67+
CXTranslationUnit_ForSerialization.rawValue)
68+
69+
/// DEPRECATED: Enabled chained precompiled preambles in C++.
70+
/// Note: this is a *temporary* option that is available only while we are
71+
/// testing C++ precompiled preamble support. It is deprecated.
72+
public static let cxxChainedPCH = TranslationUnitOptions(rawValue:
73+
CXTranslationUnit_CXXChainedPCH.rawValue)
74+
75+
/// Used to indicate that function/method bodies should be skipped while
76+
/// parsing.
77+
/// This option can be used to search for declarations/definitions while
78+
/// ignoring the usages.
79+
public static let skipFunctionBodies = TranslationUnitOptions(rawValue:
80+
CXTranslationUnit_SkipFunctionBodies.rawValue)
81+
82+
/// Used to indicate that brief documentation comments should be included into
83+
/// the set of code completions returned from this translation unit.
84+
public static let includeBriefCommentsInCodeCompletion = TranslationUnitOptions(rawValue:
85+
CXTranslationUnit_IncludeBriefCommentsInCodeCompletion.rawValue)
86+
87+
/// Used to indicate that the precompiled preamble should be created on the
88+
/// first parse. Otherwise it will be created on the first reparse. This
89+
/// trades runtime on the first parse (serializing the preamble takes time)
90+
/// for reduced runtime on the second parse (can now reuse the preamble).
91+
public static let createPreambleOnFirstParse = TranslationUnitOptions(rawValue:
92+
CXTranslationUnit_CreatePreambleOnFirstParse.rawValue)
93+
94+
/// Do not stop processing when fatal errors are encountered.
95+
/// When fatal errors are encountered while parsing a translation unit,
96+
/// semantic analysis is typically stopped early when compiling code. A common
97+
/// source for fatal errors are unresolvable include files. For the purposes
98+
/// of an IDE, this is undesirable behavior and as much information as
99+
/// possible should be reported. Use this flag to enable this behavior.
100+
public static let keepGoing = TranslationUnitOptions(rawValue:
101+
CXTranslationUnit_KeepGoing.rawValue)
29102
}
30103

31104
public class TranslationUnit {

0 commit comments

Comments
 (0)