You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/0480-swiftpm-warning-control.md
+63-19Lines changed: 63 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -38,32 +38,36 @@ public enum WarningLevel: String {
38
38
caseerror
39
39
}
40
40
41
-
publicstaticfunctreatAllWarnings(
42
-
aslevel: WarningLevel,
43
-
_condition: BuildSettingCondition?=nil
44
-
) -> SwiftSetting // or CSetting or CXXSetting
45
-
46
-
publicstaticfunctreatWarning(
47
-
_name: String,
48
-
aslevel: WarningLevel,
49
-
_condition: BuildSettingCondition?=nil
50
-
) -> SwiftSetting // or CSetting or CXXSetting
41
+
extensionSwiftSetting { // Same for CSetting and CXXSetting
42
+
publicstaticfunctreatAllWarnings(
43
+
aslevel: WarningLevel,
44
+
_condition: BuildSettingCondition?=nil
45
+
) -> SwiftSetting // or CSetting or CXXSetting
46
+
47
+
publicstaticfunctreatWarning(
48
+
_name: String,
49
+
aslevel: WarningLevel,
50
+
_condition: BuildSettingCondition?=nil
51
+
) -> SwiftSetting // or CSetting or CXXSetting
52
+
}
51
53
```
52
54
53
55
#### C/C++-specific API
54
56
55
57
In C/C++ targets, we can also enable or disable specific warning groups, in addition to controlling their severity.
56
58
57
59
```swift
58
-
publicstaticfuncenableWarning(
59
-
_name: String,
60
-
_condition: BuildSettingCondition?=nil
61
-
) -> CSetting // or CXXSetting
62
-
63
-
publicstaticfuncdisableWarning(
64
-
_name: String,
65
-
_condition: BuildSettingCondition?=nil
66
-
) -> CSetting // or CXXSetting
60
+
extensionCSetting { // Same for CXXSetting
61
+
publicstaticfuncenableWarning(
62
+
_name: String,
63
+
_condition: BuildSettingCondition?=nil
64
+
) -> CSetting // or CXXSetting
65
+
66
+
publicstaticfuncdisableWarning(
67
+
_name: String,
68
+
_condition: BuildSettingCondition?=nil
69
+
) -> CSetting // or CXXSetting
70
+
}
67
71
```
68
72
_The necessity of these functions is also explained below in the Alternatives considered section._
69
73
@@ -280,6 +284,46 @@ This necessitates separate functions to enable and disable warnings. Therefore,
280
284
281
285
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.
282
286
287
+
### Support for other C/C++ Compilers
288
+
289
+
The C/C++ warning control settings introduced in this proposal are initially implemented with Clang's warning flag syntax as the primary target. However, the API itself is largely compiler-agnostic, and there's potential to extend support to other C/C++ compilers in the future.
290
+
291
+
For instance, many of the proposed functions could be mapped to flags for other compilers like MSVC:
|`.treatWarning("name", as: .error)`|`-Werror=name`|`/we####` (where `####` is MSVC warning code) |
298
+
|`.treatWarning("name", as: .warning)`|`-Wno-error=name`| No direct equivalent |
299
+
|`.enableWarning("name")`|`-Wname`|`/wL####` (e.g., `/w4####` to enable at level 4) |
300
+
|`.disableWarning("name")`|`-Wno-name`|`/wd####`|
301
+
302
+
Where direct mappings are incomplete (like `.treatWarning(as: .warning)` for MSVC, which doesn't have a per-warning equivalent to Clang's `-Wno-error=XXXX`), SwiftPM could emit diagnostics indicating the setting is not fully supported by the current compiler. If more fine-grained control is needed for a specific compiler (e.g., MSVC's warning levels `0-4` for `enableWarning`), future enhancements could introduce compiler-specific settings or extend the existing API.
303
+
304
+
A key consideration is the handling of warning names or codes (the `"name"` parameter in the API). SwiftPM does not maintain a comprehensive list of all possible warning identifiers and their mapping across different compilers. Instead, package authors would be responsible for providing the correct warning name or code for the intended compiler.
305
+
306
+
To facilitate this, if support for other C/C++ compilers is added, the existing `BuildSettingCondition` API could be extended to allow settings to be applied conditionally based on the active C/C++ compiler. For example:
This approach, combined with the existing behavior where remote (dependency) packages have their warning control flags stripped and replaced with suppression flags, would allow projects to adopt new compilers. Even if a dependency uses Clang-specific warning flags, it would not cause build failures when the main project is built with a different compiler like MSVC, as those flags would be ignored.
320
+
321
+
### Formalizing "Development-Only" Build Settings
322
+
323
+
The warning control settings introduced by this proposal only apply when a package is built directly and are suppressed when the package is consumed as a remote dependency.
324
+
325
+
During the review of this proposal, it was suggested that this "development-only" characteristic could be made more explicit, perhaps by introducing a distinct category of settings (e.g., `devSwiftSettings`). This is an interesting avenue for future exploration. SwiftPM already has a few other settings that exhibit similar behavior. A dedicated future proposal for "development-only" settings could address all such use cases holistically, providing a clearer and more general mechanism for package authors to distinguish between "dev-only" settings and those that propagate to consumers.
326
+
283
327
## Acknowledgments
284
328
285
329
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