Skip to content

Commit 1f73a47

Browse files
authored
Merge pull request #81971 from xedin/fix-optional-features-cherry-pick-6.2
[6.2] Add optional language features to the supported features output
2 parents 3e79e5e + 09dbbea commit 1f73a47

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/Basic/SupportedFeatures.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,31 @@ static std::vector<DiagGroupID> migratableCategories(Feature feature) {
5252
}
5353
}
5454

55+
/// For optional language features, return the flag name used by the compiler to enable the feature. For all others,
56+
/// returns an empty optional.
57+
static std::optional<std::string_view> optionalFlagName(Feature feature) {
58+
switch (feature) {
59+
case Feature::StrictMemorySafety:
60+
return "-strict-memory-safety";
61+
62+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) case Feature::FeatureName:
63+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description)
64+
#include "swift/Basic/Features.def"
65+
return std::nullopt;
66+
}
67+
}
68+
5569
/// Print information about what features upcoming/experimental are
5670
/// supported by the compiler.
5771
/// The information includes whether a feature is adoptable and for
5872
/// upcoming features - what is the first mode it's introduced.
5973
void printSupportedFeatures(llvm::raw_ostream &out) {
74+
std::array optional{
75+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
76+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) Feature::FeatureName,
77+
#include "swift/Basic/Features.def"
78+
};
79+
6080
std::array upcoming{
6181
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
6282
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) Feature::FeatureName,
@@ -93,10 +113,19 @@ void printSupportedFeatures(llvm::raw_ostream &out) {
93113
if (auto version = feature.getLanguageVersion()) {
94114
out << ", \"enabled_in\": \"" << *version << "\"";
95115
}
116+
117+
if (auto flagName = optionalFlagName(feature)) {
118+
out << ", \"flag_name\": \"" << *flagName << "\"";
119+
}
120+
96121
out << " }";
97122
};
98123

99124
out << " \"features\": {\n";
125+
out << " \"optional\": [\n";
126+
llvm::interleave(optional, printFeature, [&out] { out << ",\n"; });
127+
out << "\n ],\n";
128+
100129
out << " \"upcoming\": [\n";
101130
llvm::interleave(upcoming, printFeature, [&out] { out << ",\n"; });
102131
out << "\n ],\n";

test/Frontend/print-supported-features.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %target-swift-frontend -print-supported-features | %FileCheck %s
22

33
// CHECK: "features": {
4+
// CHECK-NEXT: "optional": [
5+
// CHECK: { "name": "StrictMemorySafety", "migratable": true, "categories": ["StrictMemorySafety"], "flag_name": "-strict-memory-safety" }
6+
// CHECK-NEXT: ],
47
// CHECK-NEXT: "upcoming": [
58
// CHECK: { "name": "InferIsolatedConformances", "migratable": true, "categories": ["IsolatedConformances"], "enabled_in": "7" },
69
// CHECK: ],

0 commit comments

Comments
 (0)