Skip to content

Commit 944b5f6

Browse files
committed
Add a new driver tool/mode to print the synthesized Swift interface for a module.
This mode is similar to `swift-symbolgraph-extract`; it takes a subset of compiler flags to configure the invocation for module loading, as well as a module name whose contents should be extracted. It does not take any other input files. The output is a single text file specified by `-o` (or `stdout` if not specified). While the most common use case for this would be viewing the synthesized Swift interface for a Clang module, since the implementation simply calls `swift::ide::printModuleInterface` under the hood, it's usable for any module that Swift can import. Thus, it could also be used to view a synthesized textual representation of, say, a compiled `.swiftmodule`. One could imagine that in the future, we might add more flags to `swift-synthesize-interface` to modify various `PrintOptions` used when generating the output, if we think those would be useful.
1 parent 62993ff commit 944b5f6

File tree

17 files changed

+375
-24
lines changed

17 files changed

+375
-24
lines changed

include/swift/Driver/Driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class Driver {
184184
APIDigester, // swift-api-digester
185185
CacheTool, // swift-cache-tool
186186
ParseTest, // swift-parse-test
187+
SynthesizeInterface, // swift-synthesize-interface
187188
};
188189

189190
class InputInfoMap;

include/swift/Option/Options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace options {
3333
DoesNotAffectIncrementalBuild = (1 << 8),
3434
AutolinkExtractOption = (1 << 9),
3535
ModuleWrapOption = (1 << 10),
36-
// 1 << 11 was previously `SwiftIndentOption`
36+
SwiftSynthesizeInterfaceOption = (1 << 11),
3737
ArgumentIsPath = (1 << 12),
3838
ModuleInterfaceOption = (1 << 13),
3939
SupplementaryOutput = (1 << 14),

include/swift/Option/Options.td

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def SupplementaryOutput : OptionFlag;
6060
// The option should be accepted by swift-symbolgraph-extract.
6161
def SwiftSymbolGraphExtractOption : OptionFlag;
6262

63+
// The option should be accepted by swift-synthesize-interface.
64+
def SwiftSynthesizeInterfaceOption : OptionFlag;
65+
6366
// The option should be accepted by swift-api-digester.
6467
def SwiftAPIDigesterOption : OptionFlag;
6568

@@ -215,15 +218,17 @@ def driver_mode : Joined<["--"], "driver-mode=">, Flags<[HelpHidden]>,
215218

216219
def help : Flag<["-", "--"], "help">,
217220
Flags<[FrontendOption, AutolinkExtractOption, ModuleWrapOption,
218-
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
221+
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption,
222+
SwiftSynthesizeInterfaceOption]>,
219223
HelpText<"Display available options">;
220224
def h : Flag<["-"], "h">, Alias<help>;
221225
def help_hidden : Flag<["-", "--"], "help-hidden">,
222226
Flags<[HelpHidden, FrontendOption]>,
223227
HelpText<"Display available options, including hidden options">;
224228

225229
def v : Flag<["-"], "v">,
226-
Flags<[DoesNotAffectIncrementalBuild, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
230+
Flags<[DoesNotAffectIncrementalBuild, SwiftSymbolGraphExtractOption,
231+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
227232
HelpText<"Show commands to run and use verbose output">;
228233
def version : Flag<["-", "--"], "version">, Flags<[FrontendOption]>,
229234
HelpText<"Print version information and exit">;
@@ -236,26 +241,30 @@ def parseable_output : Flag<["-"], "parseable-output">,
236241

237242
def windows_sdk_root : Separate<["-"], "windows-sdk-root">,
238243
Flags<[ArgumentIsPath, FrontendOption,
239-
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
244+
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption,
245+
SwiftSynthesizeInterfaceOption]>,
240246
HelpText<"Windows SDK Root">, MetaVarName<"<root>">;
241247
def windows_sdk_version : Separate<["-"], "windows-sdk-version">,
242248
Flags<[FrontendOption,
243-
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
249+
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption,
250+
SwiftSynthesizeInterfaceOption]>,
244251
HelpText<"Windows SDK Version">, MetaVarName<"<version>">;
245252
def visualc_tools_root : Separate<["-"], "visualc-tools-root">,
246253
Flags<[ArgumentIsPath, FrontendOption,
247-
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
254+
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption,
255+
SwiftSynthesizeInterfaceOption]>,
248256
HelpText<"VisualC++ Tools Root">, MetaVarName<"<root>">;
249257
def visualc_tools_version : Separate<["-"], "visualc-tools-version">,
250258
Flags<[FrontendOption,
251-
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
259+
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption,
260+
SwiftSynthesizeInterfaceOption]>,
252261
HelpText<"VisualC++ ToolSet Version">, MetaVarName<"<version>">;
253262

254263
// Android Options
255264

256265
def sysroot : Separate<["-"], "sysroot">,
257266
Flags<[ArgumentIsPath, FrontendOption, SwiftSymbolGraphExtractOption,
258-
SwiftAPIDigesterOption]>,
267+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
259268
HelpText<"Native Platform sysroot">, MetaVarName<"<sysroot>">;
260269

261270
// Standard Options
@@ -265,26 +274,26 @@ def _DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>,
265274
def o : JoinedOrSeparate<["-"], "o">,
266275
Flags<[FrontendOption, AutolinkExtractOption, ModuleWrapOption,
267276
NoInteractiveOption, ArgumentIsPath, SwiftAPIDigesterOption,
268-
CacheInvariant]>,
277+
SwiftSynthesizeInterfaceOption, CacheInvariant]>,
269278
HelpText<"Write output to <file>">, MetaVarName<"<file>">;
270279

271280
def j : JoinedOrSeparate<["-"], "j">, Flags<[DoesNotAffectIncrementalBuild]>,
272281
HelpText<"Number of commands to execute in parallel">, MetaVarName<"<n>">;
273282

274283
def sdk : Separate<["-"], "sdk">,
275284
Flags<[FrontendOption, ArgumentIsPath, SwiftSymbolGraphExtractOption,
276-
SwiftAPIDigesterOption]>,
285+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
277286
HelpText<"Compile against <sdk>">, MetaVarName<"<sdk>">;
278287

279288
def swift_version : Separate<["-"], "swift-version">,
280289
Flags<[FrontendOption, ModuleInterfaceOption, SwiftSymbolGraphExtractOption,
281-
SwiftAPIDigesterOption]>,
290+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
282291
HelpText<"Interpret input according to a specific Swift language version number">,
283292
MetaVarName<"<vers>">;
284293

285294
def language_mode : Separate<["-"], "language-mode">,
286295
Flags<[FrontendOption, ModuleInterfaceOption, SwiftSymbolGraphExtractOption,
287-
SwiftAPIDigesterOption]>,
296+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
288297
HelpText<"Interpret input according to a specific Swift language mode">,
289298
MetaVarName<"<mode>">,
290299
Alias<swift_version>;
@@ -306,17 +315,20 @@ def e : Separate<["-"], "e">, Flags<[NewDriverOnlyOption]>,
306315
HelpText<"Executes a line of code provided on the command line">;
307316

308317
def F : JoinedOrSeparate<["-"], "F">,
309-
Flags<[FrontendOption, ArgumentIsPath, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
318+
Flags<[FrontendOption, ArgumentIsPath, SwiftSymbolGraphExtractOption,
319+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
310320
HelpText<"Add directory to framework search path">;
311321
def F_EQ : Joined<["-"], "F=">, Flags<[FrontendOption, ArgumentIsPath]>,
312322
Alias<F>;
313323

314324
def Fsystem : Separate<["-"], "Fsystem">,
315-
Flags<[FrontendOption, ArgumentIsPath, SwiftSymbolGraphExtractOption]>,
325+
Flags<[FrontendOption, ArgumentIsPath, SwiftSymbolGraphExtractOption,
326+
SwiftSynthesizeInterfaceOption]>,
316327
HelpText<"Add directory to system framework search path">;
317328

318329
def I : JoinedOrSeparate<["-"], "I">,
319-
Flags<[FrontendOption, ArgumentIsPath, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
330+
Flags<[FrontendOption, ArgumentIsPath, SwiftSymbolGraphExtractOption,
331+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
320332
HelpText<"Add directory to the import search path">;
321333
def I_EQ : Joined<["-"], "I=">, Flags<[FrontendOption, ArgumentIsPath]>,
322334
Alias<I>;
@@ -492,7 +504,8 @@ def localization_path : Separate<["-"], "localization-path">,
492504

493505
def module_cache_path : Separate<["-"], "module-cache-path">,
494506
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath,
495-
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
507+
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption,
508+
SwiftSynthesizeInterfaceOption]>,
496509
HelpText<"Specifies the module cache path">;
497510

498511
def enable_library_evolution : Flag<["-"], "enable-library-evolution">,
@@ -556,11 +569,11 @@ def library_level_EQ : Joined<["-"], "library-level=">,
556569

557570
def module_name : Separate<["-"], "module-name">,
558571
Flags<[FrontendOption, ModuleInterfaceOption,
559-
SwiftSymbolGraphExtractOption]>,
572+
SwiftSymbolGraphExtractOption, SwiftSynthesizeInterfaceOption]>,
560573
HelpText<"Name of the module to build">;
561574
def project_name : Separate<["-"], "project-name">,
562575
Flags<[FrontendOption, ModuleInterfaceOptionIgnorable,
563-
SwiftSymbolGraphExtractOption]>,
576+
SwiftSymbolGraphExtractOption, SwiftSynthesizeInterfaceOption]>,
564577
HelpText<"Name of the project this module to build belongs to">;
565578

566579
def module_name_EQ : Joined<["-"], "module-name=">, Flags<[FrontendOption]>,
@@ -787,7 +800,8 @@ def enable_experimental_cxx_interop :
787800

788801
def cxx_interoperability_mode :
789802
Joined<["-"], "cxx-interoperability-mode=">,
790-
Flags<[FrontendOption, ModuleInterfaceOption, SwiftSymbolGraphExtractOption]>,
803+
Flags<[FrontendOption, ModuleInterfaceOption, SwiftSymbolGraphExtractOption,
804+
SwiftSynthesizeInterfaceOption]>,
791805
HelpText<"Enables C++ interoperability; pass 'default' to enable or 'off' to disable">;
792806

793807
def experimental_c_foreign_reference_types :
@@ -1381,7 +1395,9 @@ def num_threads : Separate<["-"], "num-threads">,
13811395
def Xfrontend : Separate<["-"], "Xfrontend">, Flags<[HelpHidden]>,
13821396
MetaVarName<"<arg>">, HelpText<"Pass <arg> to the Swift frontend">;
13831397

1384-
def Xcc : Separate<["-"], "Xcc">, Flags<[FrontendOption, SwiftSymbolGraphExtractOption]>,
1398+
def Xcc : Separate<["-"], "Xcc">,
1399+
Flags<[FrontendOption, SwiftSymbolGraphExtractOption,
1400+
SwiftSynthesizeInterfaceOption]>,
13851401
MetaVarName<"<arg>">,
13861402
HelpText<"Pass <arg> to the C/C++/Objective-C compiler">;
13871403

@@ -1393,13 +1409,16 @@ def Xllvm : Separate<["-"], "Xllvm">,
13931409
MetaVarName<"<arg>">, HelpText<"Pass <arg> to LLVM.">;
13941410

13951411
def resource_dir : Separate<["-"], "resource-dir">,
1396-
Flags<[FrontendOption, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption, HelpHidden, ArgumentIsPath]>,
1412+
Flags<[FrontendOption, SwiftSymbolGraphExtractOption,
1413+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption,
1414+
HelpHidden, ArgumentIsPath]>,
13971415
MetaVarName<"</usr/lib/swift>">,
13981416
HelpText<"The directory that holds the compiler resource files">;
13991417

14001418
def target : Separate<["-"], "target">,
1401-
Flags<[FrontendOption, ModuleWrapOption, ModuleInterfaceOption, SwiftSymbolGraphExtractOption,
1402-
SwiftAPIDigesterOption]>,
1419+
Flags<[FrontendOption, ModuleWrapOption, ModuleInterfaceOption,
1420+
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption,
1421+
SwiftSynthesizeInterfaceOption]>,
14031422
HelpText<"Generate code for the given target <triple>, such as x86_64-apple-macos10.9">, MetaVarName<"<triple>">;
14041423
def target_legacy_spelling : Joined<["--"], "target=">,
14051424
Flags<[FrontendOption]>, Alias<target>;
@@ -1416,7 +1435,8 @@ def target_variant : Separate<["-"], "target-variant">,
14161435
" variant target triple in addition to the main -target triple">;
14171436

14181437
def clang_target : Separate<["-"], "clang-target">,
1419-
Flags<[FrontendOption, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
1438+
Flags<[FrontendOption, SwiftSymbolGraphExtractOption,
1439+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
14201440
HelpText<"Separately set the target we should use for internal Clang instance">;
14211441

14221442
def disable_clang_target : Flag<["-"], "disable-clang-target">,

lib/Driver/Driver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ void Driver::parseDriverKind(ArrayRef<const char *> Args) {
112112
.Case("swift-api-digester", DriverKind::APIDigester)
113113
.Case("swift-cache-tool", DriverKind::CacheTool)
114114
.Case("swift-parse-test", DriverKind::ParseTest)
115+
.Case("swift-synthesize-interface", DriverKind::SynthesizeInterface)
115116
.Default(std::nullopt);
116117

117118
if (Kind.has_value())
@@ -3117,6 +3118,7 @@ void Driver::printHelp(bool ShowHidden) const {
31173118
case DriverKind::APIDigester:
31183119
case DriverKind::CacheTool:
31193120
case DriverKind::ParseTest:
3121+
case DriverKind::SynthesizeInterface:
31203122
ExcludedFlagsBitmask |= options::NoBatchOption;
31213123
break;
31223124
}

lib/DriverTool/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(driver_sources_and_options
1313
swift_cache_tool_main.cpp
1414
swift_indent_main.cpp
1515
swift_symbolgraph_extract_main.cpp
16+
swift_synthesize_interface_main.cpp
1617
swift_parse_test_main.cpp)
1718

1819
set(driver_common_libs

lib/DriverTool/driver.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ extern int modulewrap_main(ArrayRef<const char *> Args, const char *Argv0,
9898
extern int swift_symbolgraph_extract_main(ArrayRef<const char *> Args, const char *Argv0,
9999
void *MainAddr);
100100

101+
/// Run 'swift-synthesize-interface'
102+
extern int swift_synthesize_interface_main(ArrayRef<const char *> Args,
103+
const char *Argv0, void *MainAddr);
104+
101105
/// Run 'swift-api-digester'
102106
extern int swift_api_digester_main(ArrayRef<const char *> Args,
103107
const char *Argv0, void *MainAddr);
@@ -387,6 +391,10 @@ static int run_driver(StringRef ExecName,
387391
argv[0], (void *)(intptr_t)getExecutablePath);
388392
case Driver::DriverKind::SymbolGraph:
389393
return swift_symbolgraph_extract_main(TheDriver.getArgsWithoutProgramNameAndDriverMode(argv), argv[0], (void *)(intptr_t)getExecutablePath);
394+
case Driver::DriverKind::SynthesizeInterface:
395+
return swift_synthesize_interface_main(
396+
TheDriver.getArgsWithoutProgramNameAndDriverMode(argv), argv[0],
397+
(void *)(intptr_t)getExecutablePath);
390398
case Driver::DriverKind::APIDigester:
391399
return swift_api_digester_main(
392400
TheDriver.getArgsWithoutProgramNameAndDriverMode(argv), argv[0],

0 commit comments

Comments
 (0)