Skip to content

Commit f014504

Browse files
authored
Merge pull request #2868 from DmT021/wp/swiftpm-warning-controls-2
[SE-0480] Add Future Directions for compiler support and dev-only settings
2 parents 8cbeeb7 + ee052bb commit f014504

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

proposals/0480-swiftpm-warning-control.md

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,36 @@ public enum WarningLevel: String {
3838
case error
3939
}
4040

41-
public static func treatAllWarnings(
42-
as level: WarningLevel,
43-
_ condition: BuildSettingCondition? = nil
44-
) -> SwiftSetting // or CSetting or CXXSetting
45-
46-
public static func treatWarning(
47-
_ name: String,
48-
as level: WarningLevel,
49-
_ condition: BuildSettingCondition? = nil
50-
) -> SwiftSetting // or CSetting or CXXSetting
41+
extension SwiftSetting { // Same for CSetting and CXXSetting
42+
public static func treatAllWarnings(
43+
as level: WarningLevel,
44+
_ condition: BuildSettingCondition? = nil
45+
) -> SwiftSetting // or CSetting or CXXSetting
46+
47+
public static func treatWarning(
48+
_ name: String,
49+
as level: WarningLevel,
50+
_ condition: BuildSettingCondition? = nil
51+
) -> SwiftSetting // or CSetting or CXXSetting
52+
}
5153
```
5254

5355
#### C/C++-specific API
5456

5557
In C/C++ targets, we can also enable or disable specific warning groups, in addition to controlling their severity.
5658

5759
```swift
58-
public static func enableWarning(
59-
_ name: String,
60-
_ condition: BuildSettingCondition? = nil
61-
) -> CSetting // or CXXSetting
62-
63-
public static func disableWarning(
64-
_ name: String,
65-
_ condition: BuildSettingCondition? = nil
66-
) -> CSetting // or CXXSetting
60+
extension CSetting { // Same for CXXSetting
61+
public static func enableWarning(
62+
_ name: String,
63+
_ condition: BuildSettingCondition? = nil
64+
) -> CSetting // or CXXSetting
65+
66+
public static func disableWarning(
67+
_ name: String,
68+
_ condition: BuildSettingCondition? = nil
69+
) -> CSetting // or CXXSetting
70+
}
6771
```
6872
_The necessity of these functions is also explained below in the Alternatives considered section._
6973

@@ -280,6 +284,46 @@ This necessitates separate functions to enable and disable warnings. Therefore,
280284

281285
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.
282286

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:
292+
293+
| SwiftPM Setting | Clang | MSVC (Potential Mapping) |
294+
| :-------------------------------- | :---------------- | :----------------------- |
295+
| `.treatAllWarnings(as: .error)` | `-Werror` | `/WX` |
296+
| `.treatAllWarnings(as: .warning)` | `-Wno-error` | `/WX-` |
297+
| `.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:
307+
308+
```swift
309+
cxxSettings: [
310+
// Clang-specific warning
311+
.enableWarning("unused-variable", .when(cxxCompiler: .clang)),
312+
// MSVC-specific warning (using its numeric code)
313+
.enableWarning("4101", .when(cxxCompiler: .msvc)),
314+
// Common setting that maps well
315+
.treatAllWarnings(as: .error)
316+
]
317+
```
318+
319+
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+
283327
## Acknowledgments
284328

285329
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)