Skip to content

Commit ee3aca2

Browse files
authored
Merge pull request #81518 from xedin/rdar-148124973-6.2
[6.2][Frontend] Add a way to print features supported by the compiler
2 parents f8f749b + de6ef08 commit ee3aca2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+444
-275
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ GROUPED_WARNING(cannot_disable_feature_with_mode, StrictLanguageFeatures, none,
5959
"'%0' argument '%1' cannot specify a mode",
6060
(StringRef, StringRef))
6161

62-
GROUPED_WARNING(feature_does_not_support_adoption_mode, StrictLanguageFeatures,
62+
GROUPED_WARNING(feature_does_not_support_migration_mode, StrictLanguageFeatures,
6363
none,
64-
"feature '%0' does not support adoption mode",
64+
"feature '%0' does not support migration mode",
6565
(StringRef))
6666

6767
ERROR(error_unknown_library_level, none,

include/swift/Basic/Feature.h

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef SWIFT_BASIC_FEATURES_H
14-
#define SWIFT_BASIC_FEATURES_H
13+
#ifndef SWIFT_BASIC_FEATURE_H
14+
#define SWIFT_BASIC_FEATURE_H
15+
16+
#include "swift/Basic/LLVM.h"
1517

1618
#include "llvm/ADT/StringRef.h"
1719
#include <optional>
@@ -21,53 +23,72 @@ namespace swift {
2123
class LangOptions;
2224

2325
/// Enumeration describing all of the named features.
24-
enum class Feature : uint16_t {
26+
struct Feature {
27+
enum class InnerKind : uint16_t {
2528
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) FeatureName,
2629
#include "swift/Basic/Features.def"
27-
};
30+
};
31+
32+
InnerKind kind;
2833

29-
constexpr unsigned numFeatures() {
30-
enum Features {
34+
constexpr Feature(InnerKind kind) : kind(kind) {}
35+
constexpr Feature(unsigned inputKind) : kind(InnerKind(inputKind)) {}
36+
37+
constexpr operator InnerKind() const { return kind; }
38+
constexpr explicit operator unsigned() const { return unsigned(kind); }
39+
constexpr explicit operator size_t() const { return size_t(kind); }
40+
41+
static constexpr unsigned getNumFeatures() {
42+
enum Features {
3143
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) FeatureName,
3244
#include "swift/Basic/Features.def"
33-
NumFeatures
34-
};
35-
return NumFeatures;
36-
}
45+
NumFeatures
46+
};
47+
return NumFeatures;
48+
}
3749

38-
/// Check whether the given feature is available in production compilers.
39-
bool isFeatureAvailableInProduction(Feature feature);
50+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
51+
static const Feature FeatureName;
52+
#include "swift/Basic/Features.def"
4053

41-
/// Determine the in-source name of the given feature.
42-
llvm::StringRef getFeatureName(Feature feature);
54+
/// Check whether the given feature is available in production compilers.
55+
bool isAvailableInProduction() const;
4356

44-
/// Determine whether the first feature is more recent (and thus implies
45-
/// the existence of) the second feature. Only meaningful for suppressible
46-
/// features.
47-
inline bool featureImpliesFeature(Feature feature, Feature implied) {
48-
// Suppressible features are expected to be listed in order of
49-
// addition in Features.def.
50-
return (unsigned) feature < (unsigned) implied;
51-
}
57+
/// Determine the in-source name of the given feature.
58+
llvm::StringRef getName() const;
5259

53-
/// Get the feature corresponding to this "future" feature, if there is one.
54-
std::optional<Feature> getUpcomingFeature(llvm::StringRef name);
60+
/// Determine whether the given feature supports migration mode.
61+
bool isMigratable() const;
5562

56-
/// Get the feature corresponding to this "experimental" feature, if there is
57-
/// one.
58-
std::optional<Feature> getExperimentalFeature(llvm::StringRef name);
63+
/// Determine whether this feature should be included in the
64+
/// module interface
65+
bool includeInModuleInterface() const;
5966

60-
/// Get the major language version in which this feature was introduced, or
61-
/// \c None if it does not have such a version.
62-
std::optional<unsigned> getFeatureLanguageVersion(Feature feature);
67+
/// Determine whether the first feature is more recent (and thus implies
68+
/// the existence of) the second feature. Only meaningful for suppressible
69+
/// features.
70+
constexpr bool featureImpliesFeature(Feature implied) const {
71+
// Suppressible features are expected to be listed in order of
72+
// addition in Features.def.
73+
return (unsigned)kind < (unsigned)implied.kind;
74+
}
6375

64-
/// Determine whether the given feature supports adoption mode.
65-
bool isFeatureAdoptable(Feature feature);
76+
/// Get the feature corresponding to this "future" feature, if there is one.
77+
static std::optional<Feature> getUpcomingFeature(StringRef name);
6678

67-
/// Determine whether this feature should be included in the
68-
/// module interface
69-
bool includeInModuleInterface(Feature feature);
79+
/// Get the feature corresponding to this "experimental" feature, if there is
80+
/// one.
81+
static std::optional<Feature> getExperimentalFeature(StringRef name);
7082

83+
/// Get the major language version in which this feature was introduced, or
84+
/// \c None if it does not have such a version.
85+
std::optional<unsigned> getLanguageVersion() const;
86+
};
87+
88+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
89+
constexpr inline Feature Feature::FeatureName = \
90+
Feature::InnerKind::FeatureName;
91+
#include "swift/Basic/Features.def"
7192
}
7293

7394
#endif // SWIFT_BASIC_FEATURES_H

include/swift/Basic/Features.def

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,36 @@
121121
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
122122
#endif
123123

124-
// An upcoming feature that supports adoption mode.
124+
// An upcoming feature that supports migration mode.
125125
//
126126
// If the feature is source-breaking and provides for a
127-
// mechanical code migration, it should implement adoption mode.
127+
// mechanical code migration, it should implement migration mode.
128128
//
129-
// Adoption mode is a feature-oriented code migration mechanism: a mode
129+
// Migration mode is a feature-oriented code migration mechanism: a mode
130130
// of operation that should produce compiler warnings with attached
131131
// fix-its that can be applied to preserve the behavior of the code once
132132
// the upcoming feature is enacted.
133133
// These warnings must belong to a diagnostic group named after the
134-
// feature. Adoption mode itself *and* the fix-its it produces must be
134+
// feature. Migration mode itself *and* the fix-its it produces must be
135135
// source and binary compatible with how the code is compiled when the
136136
// feature is disabled.
137-
#ifndef ADOPTABLE_UPCOMING_FEATURE
137+
#ifndef MIGRATABLE_UPCOMING_FEATURE
138138
#if defined(UPCOMING_FEATURE)
139-
#define ADOPTABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version) \
139+
#define MIGRATABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version) \
140140
UPCOMING_FEATURE(FeatureName, SENumber, Version)
141141
#else
142-
#define ADOPTABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version) \
142+
#define MIGRATABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version) \
143143
LANGUAGE_FEATURE(FeatureName, SENumber, #FeatureName)
144144
#endif
145145
#endif
146146

147-
// See `ADOPTABLE_UPCOMING_FEATURE`.
148-
#ifndef ADOPTABLE_EXPERIMENTAL_FEATURE
147+
// See `MIGRATABLE_UPCOMING_FEATURE`.
148+
#ifndef MIGRATABLE_EXPERIMENTAL_FEATURE
149149
#if defined(EXPERIMENTAL_FEATURE)
150-
#define ADOPTABLE_EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
150+
#define MIGRATABLE_EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
151151
EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd)
152152
#else
153-
#define ADOPTABLE_EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
153+
#define MIGRATABLE_EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
154154
LANGUAGE_FEATURE(FeatureName, 0, #FeatureName)
155155
#endif
156156
#endif
@@ -278,11 +278,11 @@ UPCOMING_FEATURE(NonfrozenEnumExhaustivity, 192, 6)
278278
UPCOMING_FEATURE(GlobalActorIsolatedTypesUsability, 0434, 6)
279279

280280
// Swift 7
281-
ADOPTABLE_UPCOMING_FEATURE(ExistentialAny, 335, 7)
281+
MIGRATABLE_UPCOMING_FEATURE(ExistentialAny, 335, 7)
282282
UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
283283
UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
284284
UPCOMING_FEATURE(InferIsolatedConformances, 470, 7)
285-
ADOPTABLE_UPCOMING_FEATURE(NonisolatedNonsendingByDefault, 461, 7)
285+
MIGRATABLE_UPCOMING_FEATURE(NonisolatedNonsendingByDefault, 461, 7)
286286

287287
// Optional language features / modes
288288

@@ -518,8 +518,8 @@ EXPERIMENTAL_FEATURE(CopyBlockOptimization, true)
518518
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
519519
#undef EXPERIMENTAL_FEATURE
520520
#undef UPCOMING_FEATURE
521-
#undef ADOPTABLE_UPCOMING_FEATURE
522-
#undef ADOPTABLE_EXPERIMENTAL_FEATURE
521+
#undef MIGRATABLE_UPCOMING_FEATURE
522+
#undef MIGRATABLE_EXPERIMENTAL_FEATURE
523523
#undef BASELINE_LANGUAGE_FEATURE
524524
#undef OPTIONAL_LANGUAGE_FEATURE
525525
#undef CONDITIONALLY_SUPPRESSIBLE_EXPERIMENTAL_FEATURE

include/swift/Basic/FileTypes.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ TYPE("fine-module-trace", FineModuleTrace, "", "")
7878
// Complete dependency information for the given Swift files as JSON.
7979
TYPE("json-dependencies", JSONDependencies, "dependencies.json", "")
8080

81-
// Complete feature information for the given Swift compiler.
82-
TYPE("json-features", JSONFeatures, "features.json", "")
81+
// Complete supported argument information for the given Swift compiler.
82+
TYPE("json-arguments", JSONArguments, "arguments.json", "")
8383

8484
// Gathered compile-time-known value information for the given Swift input file as JSON.
8585
TYPE("const-values", ConstValues, "swiftconstvalues", "")

include/swift/Basic/LangOptions.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ namespace swift {
830830

831831
/// A wrapper around the feature state enumeration.
832832
struct FeatureState {
833-
enum class Kind : uint8_t { Off, EnabledForAdoption, Enabled };
833+
enum class Kind : uint8_t { Off, EnabledForMigration, Enabled };
834834

835835
private:
836836
Feature feature;
@@ -843,9 +843,9 @@ namespace swift {
843843
/// Returns whether the feature is enabled.
844844
bool isEnabled() const;
845845

846-
/// Returns whether the feature is enabled in adoption mode. Should only
846+
/// Returns whether the feature is enabled in migration mode. Should only
847847
/// be called if the feature is known to support this mode.
848-
bool isEnabledForAdoption() const;
848+
bool isEnabledForMigration() const;
849849

850850
operator Kind() const { return state; }
851851
};
@@ -878,9 +878,9 @@ namespace swift {
878878
/// `false` if a feature by this name is not known.
879879
bool hasFeature(llvm::StringRef featureName) const;
880880

881-
/// Enables the given feature (enables in adoption mode if `forAdoption` is
882-
/// `true`).
883-
void enableFeature(Feature feature, bool forAdoption = false);
881+
/// Enables the given feature (enables in migration mode if `forMigration`
882+
/// is `true`).
883+
void enableFeature(Feature feature, bool forMigration = false);
884884

885885
/// Disables the given feature.
886886
void disableFeature(Feature feature);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===--- SupportedFeatures.h - Supported Features Output --------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file provides a high-level API for supported features info
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_SUPPORTEDFEATURES_H
18+
#define SWIFT_SUPPORTEDFEATURES_H
19+
20+
#include "swift/Basic/LLVM.h"
21+
22+
namespace swift {
23+
namespace features {
24+
void printSupportedFeatures(llvm::raw_ostream &out);
25+
} // namespace features
26+
} // namespace swift
27+
28+
#endif

include/swift/Frontend/FrontendOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class FrontendOptions {
201201

202202
ScanDependencies, ///< Scan dependencies of Swift source files
203203
PrintVersion, ///< Print version information.
204-
PrintFeature, ///< Print supported feature of this compiler
204+
PrintArguments, ///< Print supported arguments of this compiler
205205
};
206206

207207
/// Indicates the action the user requested that the frontend perform.
@@ -314,6 +314,10 @@ class FrontendOptions {
314314
/// exit.
315315
bool PrintTargetInfo = false;
316316

317+
/// Indicates that the frontend should print the supported features and then
318+
/// exit.
319+
bool PrintSupportedFeatures = false;
320+
317321
/// See the \ref SILOptions.EmitVerboseSIL flag.
318322
bool EmitVerboseSIL = false;
319323

include/swift/Option/Options.td

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,10 @@ def print_target_info : Flag<["-"], "print-target-info">,
15301530
Flags<[FrontendOption]>,
15311531
HelpText<"Print target information for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
15321532

1533+
def print_supported_features : Flag<["-"], "print-supported-features">,
1534+
Flags<[FrontendOption]>,
1535+
HelpText<"Print information about features supported by the compiler">;
1536+
15331537
def target_cpu : Separate<["-"], "target-cpu">, Flags<[FrontendOption, ModuleInterfaceOption]>,
15341538
HelpText<"Generate code for a particular CPU variant">;
15351539

@@ -1646,9 +1650,13 @@ def scan_dependencies : Flag<["-"], "scan-dependencies">,
16461650
HelpText<"Scan dependencies of the given Swift sources">, ModeOpt,
16471651
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
16481652

1649-
def emit_supported_features : Flag<["-"], "emit-supported-features">,
1650-
HelpText<"Emit a JSON file including all supported compiler features">, ModeOpt,
1653+
def emit_supported_arguments : Flag<["-"], "emit-supported-arguments">,
1654+
HelpText<"Emit a JSON file including all supported compiler arguments">, ModeOpt,
16511655
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
1656+
def emit_supported_features : Flag<["-"], "emit-supported-features">,
1657+
HelpText<"This is a compatibility alias for '-emit-supported-arguments'">, ModeOpt,
1658+
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild, HelpHidden]>,
1659+
Alias<emit_supported_arguments>;
16521660

16531661
def enable_incremental_imports :
16541662
Flag<["-"], "enable-incremental-imports">,

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ bool BridgedPassContext::enableMergeableTraps() const {
560560

561561
bool BridgedPassContext::hasFeature(BridgedFeature feature) const {
562562
swift::SILModule *mod = invocation->getPassManager()->getModule();
563-
return mod->getASTContext().LangOpts.hasFeature((swift::Feature)feature);
563+
return mod->getASTContext().LangOpts.hasFeature(swift::Feature(feature));
564564
}
565565

566566
bool BridgedPassContext::enableMoveInoutStackProtection() const {

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3338,7 +3338,7 @@ static void printCompatibilityCheckIf(ASTPrinter &printer, bool isElseIf,
33383338
} else {
33393339
first = false;
33403340
}
3341-
printer << "$" << getFeatureName(feature);
3341+
printer << "$" << Feature(feature).getName();
33423342
}
33433343

33443344
#ifndef NDEBUG

0 commit comments

Comments
 (0)