Skip to content

Commit 214c197

Browse files
committed
Warning Control Settings for SwiftPM
1 parent 089e543 commit 214c197

File tree

1 file changed

+284
-0
lines changed

1 file changed

+284
-0
lines changed

Diff for: proposals/NNNN-swiftpm-warning-control.md

+284
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
# Warning Control Settings for SwiftPM
2+
3+
* Proposal: [SE-NNNN](NNNN-swiftpm-warning-control.md)
4+
* Authors: [Dmitrii Galimzianov](https://github.com/DmT021)
5+
* Review Manager: TBD
6+
* Status: **Awaiting review**
7+
* Implementation: [swiftlang/swift-package-manager#8315](https://github.com/swiftlang/swift-package-manager/pull/8315)
8+
* Previous Proposal: [SE-0443](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0443-warning-control-flags.md)
9+
10+
## Introduction
11+
12+
This proposal adds new settings to SwiftPM to control how the Swift, C, and C++ compilers treat warnings during the build process. It builds on [SE-0443](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0443-warning-control-flags.md), which introduced warning control flags for the Swift compiler but left SwiftPM support as a future direction.
13+
14+
## Motivation
15+
16+
The Swift Package Manager currently lacks a unified way to control warnings across Swift, C, and C++ compilation. This limitation forces developers to either use `unsafeFlags` or accept the default warning settings.
17+
18+
## Proposed solution
19+
20+
This proposal introduces new methods to SwiftPM's build settings API, allowing fine-grained control over warnings.
21+
22+
### API
23+
24+
#### Cross-language API (Swift, C, and C++)
25+
26+
```swift
27+
/// The level at which a compiler warning should be treated.
28+
public enum WarningLevel: String {
29+
/// Treat as a warning.
30+
///
31+
/// Warnings will be displayed during compilation but will not cause the build to fail.
32+
case warning
33+
34+
/// Treat as an error.
35+
///
36+
/// Warnings will be elevated to errors, causing the build to fail if any such warnings occur.
37+
case error
38+
}
39+
40+
public static func treatAllWarnings(
41+
as level: WarningLevel,
42+
_ condition: BuildSettingCondition? = nil
43+
) -> SwiftSetting // or CSetting or CXXSetting
44+
45+
public static func treatWarning(
46+
_ name: String,
47+
as level: WarningLevel,
48+
_ condition: BuildSettingCondition? = nil
49+
) -> SwiftSetting // or CSetting or CXXSetting
50+
```
51+
52+
#### C/C++-specific API
53+
54+
In C/C++ targets, we can also enable or disable specific warning groups, in addition to controlling their severity.
55+
56+
```swift
57+
public static func enableWarning(
58+
_ name: String,
59+
_ condition: BuildSettingCondition? = nil
60+
) -> CSetting // or CXXSetting
61+
62+
public static func disableWarning(
63+
_ name: String,
64+
_ condition: BuildSettingCondition? = nil
65+
) -> CSetting // or CXXSetting
66+
```
67+
_The necessity of these functions is also explained below in the Alternatives considered section._
68+
69+
### Example usage
70+
71+
```swift
72+
.target(
73+
name: "MyLib",
74+
swiftSettings: [
75+
.treatAllWarnings(as: .error),
76+
.treatWarning("DeprecatedDeclaration", as: .warning),
77+
],
78+
cSettings: [
79+
.enableWarning("all"),
80+
.disableWarning("unused-function"),
81+
82+
.treatAllWarnings(as: .error),
83+
.treatWarning("unused-variable", as: .warning),
84+
],
85+
cxxSettings: [
86+
.enableWarning("all"),
87+
.disableWarning("unused-function"),
88+
89+
.treatAllWarnings(as: .error),
90+
.treatWarning("unused-variable", as: .warning),
91+
]
92+
)
93+
```
94+
95+
## Detailed design
96+
97+
### Settings and their corresponding compiler flags
98+
99+
| Method | Swift | C/C++ |
100+
|--------|-------|-------|
101+
| `treatAllWarnings(as: .error)` | `-warnings-as-errors` | `-Werror` |
102+
| `treatAllWarnings(as: .warning)` | `-no-warnings-as-errors` | `-Wno-error` |
103+
| `treatWarning("XXXX", as: .error)` | `-Werror XXXX` | `-Werror=XXXX` |
104+
| `treatWarning("XXXX", as: .warning)` | `-Wwarning XXXX` | `-Wno-error=XXXX` |
105+
| `enableWarning("XXXX")` | N/A | `-WXXXX` |
106+
| `disableWarning("XXXX")` | N/A | `-Wno-XXXX` |
107+
108+
### Order of settings evaluation
109+
110+
The order in which warning control settings are specified in a target's settings array directly affects the order of the resulting compiler flags. This is critical because when multiple flags affect the same warning group, compilers apply them sequentially with the last flag taking precedence.
111+
112+
For example, consider these two different orderings for C++ settings:
113+
114+
```swift
115+
// Example 1: "unused-variable" in front of "unused"
116+
cxxSettings: [
117+
.treatWarning("unused-variable", as: .error),
118+
.treatWarning("unused", as: .warning),
119+
]
120+
121+
// Example 2: "unused" in front of "unused-variable"
122+
cxxSettings: [
123+
.treatWarning("unused", as: .warning),
124+
.treatWarning("unused-variable", as: .error),
125+
]
126+
```
127+
128+
In Example 1, the compiler will receive flags in this order:
129+
```
130+
-Werror=unused-variable -Wno-error=unused
131+
```
132+
Since "unused-variable" is a specific subgroup of the broader "unused" group, and the "unused" flag is applied last, all unused warnings (including unused-variable) will be treated as warnings.
133+
134+
In Example 2, the compiler will receive flags in this order:
135+
```
136+
-Wno-error=unused -Werror=unused-variable
137+
```
138+
Due to the "last one wins" rule, unused-variable warnings will be treated as errors, while other unused warnings remain as warnings.
139+
140+
The same principle applies when combining any of the new build settings:
141+
142+
```swift
143+
cxxSettings: [
144+
.enableWarning("all"), // Enable the "all" warning group
145+
.enableWarning("extra"), // Enable the "extra" warning group
146+
.disableWarning("unused-parameter"), // Disable the "unused-parameter" warning group
147+
.treatAllWarnings(as: .error), // Treat all warnings as errors
148+
.treatWarning("unused", as: .warning), // Keep warnings of the "unused" group as warnings
149+
]
150+
```
151+
152+
This will result in compiler flags:
153+
```
154+
-Wall -Wextra -Wno-unused-parameter -Werror -Wno-error=unused
155+
```
156+
157+
When configuring warnings, be mindful of the order to achieve the desired behavior.
158+
159+
### Remote targets behavior
160+
161+
When a target is remote (pulled from a package dependency rather than defined in the local package), the warning control settings specified in the manifest do not apply to it. SwiftPM will strip all of the warning control flags for remote targets and substitute them with options for suppressing warnings (`-w` for Clang and `-suppress-warnings` for Swift).
162+
163+
This behavior is already in place but takes into account only `-warnings-as-errors` (for Swift) and `-Werror` (for Clang) flags. We expand this list to include the following warning-related flags:
164+
165+
**For C/C++:**
166+
* `-Wxxxx`
167+
* `-Wno-xxxx`
168+
* `-Werror`
169+
* `-Werror=xxxx`
170+
* `-Wno-error`
171+
* `-Wno-error=xxxx`
172+
173+
**For Swift:**
174+
* `-warnings-as-errors`
175+
* `-no-warnings-as-errors`
176+
* `-Wwarning xxxx`
177+
* `-Werror xxxx`
178+
179+
This approach ensures that warning control settings are applied only to the targets you directly maintain in your package, while dependencies remain buildable without warnings regardless of their warning settings.
180+
181+
### Interaction with command-line flags
182+
183+
SwiftPM allows users to pass additional flags to the compilers using the `-Xcc`, `-Xswiftc`, and `-Xcxx` options with the `swift build` command. These flags are appended **after** the flags generated from the package manifest.
184+
185+
This ordering enables users to modify or override package-defined warning settings without modifying the package manifest.
186+
187+
#### Example
188+
189+
```swift
190+
let package = Package(
191+
name: "MyExecutable",
192+
targets: [
193+
// C target with warning settings
194+
.target(
195+
name: "cfoo",
196+
cSettings: [
197+
.enableWarning("all"),
198+
.treatAllWarnings(as: .error),
199+
.treatWarning("unused-variable", as: .warning),
200+
]
201+
),
202+
// Swift target with warning settings
203+
.executableTarget(
204+
name: "swiftfoo",
205+
swiftSettings: [
206+
.treatAllWarnings(as: .error),
207+
.treatWarning("DeprecatedDeclaration", as: .warning),
208+
]
209+
),
210+
]
211+
)
212+
```
213+
214+
When built with additional command-line flags:
215+
216+
```sh
217+
swift build -Xcc -Wno-error -Xswiftc -no-warnings-as-errors
218+
```
219+
220+
The resulting compiler invocations will include both sets of flags:
221+
222+
```
223+
# C compiler invocation
224+
clang ... -Wall -Werror -Wno-error=unused-variable ... -Wno-error ...
225+
226+
# Swift compiler invocation
227+
swiftc ... -warnings-as-errors -Wwarning DeprecatedDeclaration ... -no-warnings-as-errors -Xcc -Wno-error ...
228+
```
229+
230+
Flags are processed from left to right, and since `-no-warnings-as-errors` and `-Wno-error` apply globally to all warnings, they override the warning treating flags defined in the package manifest.
231+
232+
#### Limitations
233+
234+
This approach has a limitation when used with `-suppress-warnings`, which is mutually exclusive with other warning control flags:
235+
236+
```sh
237+
swift build -Xswiftc -suppress-warnings
238+
```
239+
240+
Results in compiler errors:
241+
242+
```
243+
error: conflicting options '-warnings-as-errors' and '-suppress-warnings'
244+
error: conflicting options '-Wwarning' and '-suppress-warnings'
245+
```
246+
247+
248+
## Security
249+
250+
This change has no impact on security, safety, or privacy.
251+
252+
## Impact on existing packages
253+
254+
The proposed API will only be available to packages that specify a tools version equal to or later than the SwiftPM version in which this functionality is implemented.
255+
256+
## Alternatives considered
257+
258+
### Disabling a warning via a treat level
259+
260+
Clang allows users to completely disable a specific warning, so for C/C++ settings we could implement that as a new case in the `WarningLevel` enum:
261+
262+
```swift
263+
public enum WarningLevel {
264+
case warning
265+
case error
266+
case ignored
267+
}
268+
```
269+
270+
_(Since Swift doesn't allow selective warning suppression, we would actually have to split the enum into two: `SwiftWarningLevel` and `CFamilyWarningLevel`)_
271+
272+
But some warnings in Clang are disabled by default. If we simply pass `-Wno-error=unused-variable`, the compiler won't actually produce a warning for an unused variable. It only makes sense to use it if we have enabled the warning: `-Wunused-variable -Werror -Wno-error=unused-variable`.
273+
274+
This necessitates separate functions to enable and disable warnings. Therefore, instead of `case ignored`, we propose the functions `enableWarning` and `disableWarning`.
275+
276+
## Future directions
277+
278+
### Package-level settings
279+
280+
It has been noted that warning control settings are often similar across all targets. It makes sense to declare them at the package level while allowing target-level customizations. However, many other settings would also likely benefit from such inheritance, and SwiftPM doesn't currently provide such an option. Therefore, it was decided to factor this improvement out and look at all the settings holistically in the future.
281+
282+
## Acknowledgments
283+
284+
Thank you to [Doug Gregor](https://github.com/douggregor) for the motivation, and to both [Doug Gregor](https://github.com/douggregor) and [Holly Borla](https://github.com/hborla) for their guidance during the implementation of this API.

0 commit comments

Comments
 (0)