Skip to content

Commit fc41db4

Browse files
committed
Add overview documentation for the SwiftIfConfig library
1 parent 2d2021f commit fc41db4

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleName</key>
6+
<string>SwiftIfConfig</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>SwiftIfConfig</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>com.apple.swift-if-config</string>
11+
<key>CFBundleDevelopmentRegion</key>
12+
<string>en</string>
13+
<key>CFBundleIconFile</key>
14+
<string>DocumentationIcon</string>
15+
<key>CFBundleIconName</key>
16+
<string>DocumentationIcon</string>
17+
<key>CFBundlePackageType</key>
18+
<string>DOCS</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>0.1.0</string>
21+
<key>CDDefaultCodeListingLanguage</key>
22+
<string>swift</string>
23+
<key>CFBundleVersion</key>
24+
<string>0.1.0</string>
25+
<key>CDAppleDefaultAvailability</key>
26+
<dict>
27+
<key>SwiftIfConfig</key>
28+
<array>
29+
<dict>
30+
<key>name</key>
31+
<string>macOS</string>
32+
<key>version</key>
33+
<string>10.15</string>
34+
</dict>
35+
</array>
36+
</dict>
37+
</dict>
38+
</plist>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# `SwiftIfConfig`
2+
3+
4+
5+
A library to evaluate `#if` conditionals within a Swift syntax tree.
6+
7+
## Overview
8+
9+
Swift provides the ability to conditionally compile parts of a source file based on various built-time conditions, including information about the target (operating system, processor architecture, environment), information about the compiler (version, supported attributes and features), and user-supplied conditions specified as part of the build (e.g., `DEBUG`), which we collectively refer to as the *build configuration*. These conditions can occur within a `#if` in the source code, e.g.,
10+
11+
```swift
12+
func f() {
13+
#if DEBUG
14+
log("called f")
15+
#endif
16+
17+
#if os(Linux)
18+
// use Linux API
19+
#elseif os(iOS) || os(macOS)
20+
// use iOS/macOS API
21+
#else
22+
#error("unsupported platform")
23+
#endif
24+
}
25+
```
26+
27+
The syntax tree and its parser do not reason about the build configuration. Rather, the syntax tree produced by parsing this code will include `IfConfigDeclSyntax` nodes wherever there is a `#if`, and each such node contains the a list of clauses, each with a condition to check (e.g., `os(Linux)`) and a list of syntax nodes that are conditionally part of the program. Therefore, the syntax tree captures all the information needed to process the source file for any build configuration.
28+
29+
The `SwiftIfConfig` library provides utilities to determine which syntax nodes are part of a particular build configuration. Each utility requires that one provide a specific build configuration (i.e., an instance of a type that conforms to the <doc:BuildConfiguration> protocol), and provides a different view on essentially the same information:
30+
31+
* <doc:ActiveSyntaxVisitor> and <doc:ActiveSyntaxAnyVisitor> are visitor types that only visit the syntax nodes that are included ("active") for a given build configuration, implicitly skipping any nodes within inactive `#if` clauses.
32+
* `SyntaxProtocol.removingInactive(in:)` produces a syntax node that removes all inactive regions (and their corresponding `IfConfigDeclSyntax` nodes) from the given syntax tree, returning a new tree that is free of `#if` conditions.
33+
* `IfConfigDeclSyntax.activeClause(in:)` determines which of the clauses of an `#if` is active for the given build configuration, returning the active clause.
34+
* `SyntaxProtocol.isActive(in:)` determines whether the given syntax node is active for the given build configuration.

0 commit comments

Comments
 (0)