Skip to content

Commit 55bd906

Browse files
committed
[Frontend] Add -print-supported-features option
This is a replacement for `-emit-supported-features` that prints all of the upcoming/experimental features supported by the compiler with some additional meta information in JSON format to stdout.
1 parent 7ce06dd commit 55bd906

File tree

9 files changed

+153
-0
lines changed

9 files changed

+153
-0
lines changed
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,10 @@ def print_target_info : Flag<["-"], "print-target-info">,
15341534
Flags<[FrontendOption]>,
15351535
HelpText<"Print target information for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
15361536

1537+
def print_supported_features : Flag<["-"], "print-supported-features">,
1538+
Flags<[FrontendOption]>,
1539+
HelpText<"Print information about features supported by the compiler">;
1540+
15371541
def target_cpu : Separate<["-"], "target-cpu">, Flags<[FrontendOption, ModuleInterfaceOption]>,
15381542
HelpText<"Generate code for a particular CPU variant">;
15391543

lib/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ add_swift_host_library(swiftBasic STATIC
7575
StableHasher.cpp
7676
Statistic.cpp
7777
StringExtras.cpp
78+
SupportedFeatures.cpp
7879
TargetInfo.cpp
7980
TaskQueue.cpp
8081
ThreadSafeRefCounted.cpp

lib/Basic/SupportedFeatures.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===--- SupportedFeatures.cpp - Supported features printing --------------===//
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+
#include <array>
14+
#include <vector>
15+
16+
#include "swift/Basic/Feature.h"
17+
#include "swift/Frontend/Frontend.h"
18+
19+
#include "llvm/Support/raw_ostream.h"
20+
21+
using namespace swift;
22+
23+
namespace swift {
24+
namespace features {
25+
/// Print information about what features upcoming/experimental are
26+
/// supported by the compiler.
27+
/// The information includes whether a feature is adoptable and for
28+
/// upcoming features - what is the first mode it's introduced.
29+
void printSupportedFeatures(llvm::raw_ostream &out) {
30+
std::array upcoming{
31+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
32+
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) Feature::FeatureName,
33+
#include "swift/Basic/Features.def"
34+
};
35+
36+
std::vector<swift::Feature> experimental{{
37+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
38+
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) Feature::FeatureName,
39+
#include "swift/Basic/Features.def"
40+
}};
41+
42+
// Include only experimental features that are available in production.
43+
llvm::erase_if(experimental, [](auto &feature) {
44+
return feature.isAvailableInProduction();
45+
});
46+
47+
out << "{\n";
48+
auto printFeature = [&out](const Feature &feature) {
49+
out << " ";
50+
out << "{ \"name\": \"" << feature.getName() << "\"";
51+
if (feature.isAdoptable()) {
52+
out << ", \"migratable\": true";
53+
}
54+
if (auto version = feature.getLanguageVersion()) {
55+
out << ", \"enabled_in\": " << *version;
56+
}
57+
out << " }";
58+
};
59+
60+
out << " \"features\": {\n";
61+
out << " \"upcoming\": [\n";
62+
llvm::interleave(upcoming, printFeature, [&out] { out << ",\n"; });
63+
out << "\n ],\n";
64+
65+
out << " \"experimental\": [\n";
66+
llvm::interleave(experimental, printFeature, [&out] { out << ",\n"; });
67+
out << "\n ]\n";
68+
69+
out << " }\n";
70+
out << "}\n";
71+
}
72+
73+
} // end namespace features
74+
} // end namespace swift

lib/Driver/Driver.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,28 @@ bool Driver::handleImmediateArgs(const ArgList &Args, const ToolChain &TC) {
20492049
return false;
20502050
}
20512051

2052+
if (Args.hasArg(options::OPT_print_supported_features)) {
2053+
SmallVector<const char *, 5> commandLine;
2054+
commandLine.push_back("-frontend");
2055+
commandLine.push_back("-print-supported-features");
2056+
2057+
std::string executable = getSwiftProgramPath();
2058+
2059+
// FIXME(https://github.com/apple/swift/issues/54554): This bypasses
2060+
// mechanisms like -v and -###.
2061+
sys::TaskQueue queue;
2062+
queue.addTask(executable.c_str(), commandLine);
2063+
queue.execute(nullptr,
2064+
[](sys::ProcessId PID, int returnCode, StringRef output,
2065+
StringRef errors, sys::TaskProcessInformation ProcInfo,
2066+
void *unused) -> sys::TaskFinishedResponse {
2067+
llvm::outs() << output;
2068+
llvm::errs() << errors;
2069+
return sys::TaskFinishedResponse::ContinueExecution;
2070+
});
2071+
return false;
2072+
}
2073+
20522074
return true;
20532075
}
20542076

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ bool ArgsToFrontendOptionsConverter::convert(
206206
Opts.PrintTargetInfo = true;
207207
}
208208

209+
if (Args.hasArg(OPT_print_supported_features)) {
210+
Opts.PrintSupportedFeatures = true;
211+
}
212+
209213
if (const Arg *A = Args.getLastArg(OPT_verify_generic_signatures)) {
210214
Opts.VerifyGenericSignaturesInModule = A->getValue();
211215
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "swift/Basic/PrettyStackTrace.h"
4747
#include "swift/Basic/SourceManager.h"
4848
#include "swift/Basic/Statistic.h"
49+
#include "swift/Basic/SupportedFeatures.h"
4950
#include "swift/Basic/TargetInfo.h"
5051
#include "swift/Basic/UUID.h"
5152
#include "swift/Basic/Version.h"
@@ -2063,6 +2064,11 @@ int swift::performFrontend(ArrayRef<const char *> Args,
20632064
return finishDiagProcessing(0, /*verifierEnabled*/ false);
20642065
}
20652066

2067+
if (Invocation.getFrontendOptions().PrintSupportedFeatures) {
2068+
swift::features::printSupportedFeatures(llvm::outs());
2069+
return finishDiagProcessing(0, /*verifierEnabled*/ false);
2070+
}
2071+
20662072
if (Invocation.getFrontendOptions().RequestedAction ==
20672073
FrontendOptions::ActionType::NoneAction) {
20682074
Instance->getDiags().diagnose(SourceLoc(),
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-frontend -print-supported-features | %FileCheck %s
2+
3+
// CHECK: "features": {
4+
// CHECK-NEXT: "upcoming": [
5+
// CHECK: { "name": "{{.*}}"{{, "migratable": true}}, "enabled_in": {{[0-9]+}} }
6+
// CHECK: ],
7+
// CHECK-NEXT: "experimental": [
8+
// CHECK: { "name": "{{.*}}"{{, "migratable": true}} }
9+
// CHECK: ]
10+
// CHECK: }

0 commit comments

Comments
 (0)