Skip to content

Commit 414adb5

Browse files
committed
Add optional language features to the supported features output
Optional language features don't have a specific "-enable-*" flag, because they're rare and don't fit the same upcoming/experimental distinction. Add a flag_name field to provide the flag name as well.
1 parent 7f2649f commit 414adb5

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
@@ -49,11 +49,31 @@ static std::vector<DiagGroupID> migratableCategories(Feature feature) {
4949
}
5050
}
5151

52+
/// For optional language features, return the flag name used by the compiler to enable the feature. For all others,
53+
/// returns an empty optional.
54+
static std::optional<std::string_view> optionalFlagName(Feature feature) {
55+
switch (feature) {
56+
case Feature::StrictMemorySafety:
57+
return "-strict-memory-safety";
58+
59+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) case Feature::FeatureName:
60+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description)
61+
#include "swift/Basic/Features.def"
62+
return std::nullopt;
63+
}
64+
}
65+
5266
/// Print information about what features upcoming/experimental are
5367
/// supported by the compiler.
5468
/// The information includes whether a feature is adoptable and for
5569
/// upcoming features - what is the first mode it's introduced.
5670
void printSupportedFeatures(llvm::raw_ostream &out) {
71+
std::array optional{
72+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
73+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) Feature::FeatureName,
74+
#include "swift/Basic/Features.def"
75+
};
76+
5777
std::array upcoming{
5878
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
5979
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) Feature::FeatureName,
@@ -90,10 +110,19 @@ void printSupportedFeatures(llvm::raw_ostream &out) {
90110
if (auto version = feature.getLanguageVersion()) {
91111
out << ", \"enabled_in\": \"" << *version << "\"";
92112
}
113+
114+
if (auto flagName = optionalFlagName(feature)) {
115+
out << ", \"flag_name\": \"" << *flagName << "\"";
116+
}
117+
93118
out << " }";
94119
};
95120

96121
out << " \"features\": {\n";
122+
out << " \"optional\": [\n";
123+
llvm::interleave(optional, printFeature, [&out] { out << ",\n"; });
124+
out << "\n ],\n";
125+
97126
out << " \"upcoming\": [\n";
98127
llvm::interleave(upcoming, printFeature, [&out] { out << ",\n"; });
99128
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)