Skip to content

Commit 770c633

Browse files
authored
Merge pull request #83672 from tshortli/refactor-dump-availability-scopes
Frontend: Allow `-dump-availability-scopes` to be used with any action
2 parents 10eab84 + 5d3d934 commit 770c633

13 files changed

+118
-94
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,12 @@ class CompilerInstance {
710710
}
711711
}
712712

713+
SourceFile &getPrimaryOrMainSourceFile() const {
714+
if (SourceFile *SF = getPrimarySourceFile())
715+
return *SF;
716+
return getMainModule()->getMainSourceFile();
717+
}
718+
713719
/// Returns true if there was an error during setup.
714720
bool setup(const CompilerInvocation &Invocation, std::string &Error,
715721
ArrayRef<const char *> Args = {});
@@ -792,6 +798,9 @@ class CompilerInstance {
792798
/// \returns true if any errors occurred.
793799
bool performSILProcessing(SILModule *silModule);
794800

801+
/// Dumps any debugging output for the compilation, if requested.
802+
void emitEndOfPipelineDebuggingOutput();
803+
795804
private:
796805
/// Creates a new source file for the main module.
797806
SourceFile *createSourceFileForMainModule(ModuleDecl *mod,

include/swift/Frontend/FrontendOptions.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ namespace llvm {
3535
namespace swift {
3636
enum class IntermoduleDepTrackingMode;
3737

38+
/// Options for debugging the behavior of the frontend.
39+
struct CompilerDebuggingOptions {
40+
/// Indicates whether or not the Clang importer should print statistics upon
41+
/// termination.
42+
bool PrintClangStats = false;
43+
44+
/// Indicates whether or not the availability scope trees built during
45+
/// compilation should be dumped upon termination.
46+
bool DumpAvailabilityScopes = false;
47+
48+
/// Indicates whether or not the Clang importer should dump lookup tables
49+
/// upon termination.
50+
bool DumpClangLookupTables = false;
51+
};
52+
3853
/// Options for controlling the behavior of the frontend.
3954
class FrontendOptions {
4055
friend class ArgsToFrontendOptionsConverter;
@@ -124,6 +139,9 @@ class FrontendOptions {
124139
/// A set of modules allowed to import this module.
125140
std::set<std::string> AllowableClients;
126141

142+
/// Options for debugging the compiler.
143+
CompilerDebuggingOptions CompilerDebuggingOpts;
144+
127145
/// Emit index data for imported serialized swift system modules.
128146
bool IndexSystemModules = false;
129147

@@ -167,9 +185,6 @@ class FrontendOptions {
167185
/// Parse and dump scope map.
168186
DumpScopeMaps,
169187

170-
/// Parse, type-check, and dump availability scopes
171-
DumpAvailabilityScopes,
172-
173188
EmitImportedModules, ///< Emit the modules that this one imports
174189
EmitPCH, ///< Emit PCH of imported bridging header
175190

@@ -305,14 +320,6 @@ class FrontendOptions {
305320
/// termination.
306321
bool PrintStats = false;
307322

308-
/// Indicates whether or not the Clang importer should print statistics upon
309-
/// termination.
310-
bool PrintClangStats = false;
311-
312-
/// Indicates whether or not the Clang importer should dump lookup tables
313-
/// upon termination.
314-
bool DumpClangLookupTables = false;
315-
316323
/// Indicates whether standard help should be shown.
317324
bool PrintHelp = false;
318325

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ class ExplicitModuleMapParser {
455455
llvm::StringSaver Saver;
456456
};
457457

458-
struct ModuleInterfaceLoaderOptions {
458+
class ModuleInterfaceLoaderOptions {
459+
public:
459460
FrontendOptions::ActionType requestedAction =
460461
FrontendOptions::ActionType::EmitModuleOnly;
461462
bool remarkOnRebuildFromInterface = false;
@@ -464,28 +465,11 @@ struct ModuleInterfaceLoaderOptions {
464465
bool disableBuildingInterface = false;
465466
bool downgradeInterfaceVerificationError = false;
466467
bool strictImplicitModuleContext = false;
468+
CompilerDebuggingOptions compilerDebuggingOptions;
467469
std::string mainExecutablePath;
468-
ModuleInterfaceLoaderOptions(const FrontendOptions &Opts):
469-
remarkOnRebuildFromInterface(Opts.RemarkOnRebuildFromModuleInterface),
470-
disableInterfaceLock(Opts.DisableInterfaceFileLock),
471-
disableImplicitSwiftModule(Opts.DisableImplicitModules),
472-
disableBuildingInterface(Opts.DisableBuildingInterface),
473-
downgradeInterfaceVerificationError(Opts.DowngradeInterfaceVerificationError),
474-
strictImplicitModuleContext(Opts.StrictImplicitModuleContext),
475-
mainExecutablePath(Opts.MainExecutablePath)
476-
{
477-
switch (Opts.RequestedAction) {
478-
case FrontendOptions::ActionType::TypecheckModuleFromInterface:
479-
requestedAction = FrontendOptions::ActionType::Typecheck;
480-
break;
481-
case FrontendOptions::ActionType::ScanDependencies:
482-
requestedAction = Opts.RequestedAction;
483-
break;
484-
default:
485-
requestedAction = FrontendOptions::ActionType::EmitModuleOnly;
486-
break;
487-
}
488-
}
470+
471+
ModuleInterfaceLoaderOptions(const FrontendOptions &Opts,
472+
bool inheritDebuggingOpts = false);
489473
ModuleInterfaceLoaderOptions() = default;
490474
};
491475

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ def dump_clang_lookup_tables : Flag<["-"], "dump-clang-lookup-tables">,
673673
HelpText<"Dump the importer's Swift-name-to-Clang-name lookup tables to "
674674
"stderr">;
675675

676+
def dump_availability_scopes : Flag<["-"], "dump-availability-scopes">,
677+
HelpText<"Dump availability scopes to stderr">;
678+
676679
def disable_modules_validate_system_headers : Flag<["-"], "disable-modules-validate-system-headers">,
677680
HelpText<"Disable validating system headers in the Clang importer">;
678681

include/swift/Option/Options.td

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,11 +1435,7 @@ def dump_scope_maps : Separate<["-"], "dump-scope-maps">,
14351435
MetaVarName<"<expanded-or-list-of-line:column>">,
14361436
ModeOpt,
14371437
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
1438-
def dump_availability_scopes :
1439-
Flag<["-"], "dump-availability-scopes">,
1440-
HelpText<"Type-check input file(s) and dump availability scopes">,
1441-
ModeOpt,
1442-
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
1438+
14431439
def dump_type_info : Flag<["-"], "dump-type-info">,
14441440
HelpText<"Output YAML dump of fixed-size types from all imported modules">,
14451441
ModeOpt,

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@ bool ArgsToFrontendOptionsConverter::convert(
197197
computeDebugTimeOptions();
198198
computeTBDOptions();
199199

200-
Opts.DumpClangLookupTables |= Args.hasArg(OPT_dump_clang_lookup_tables);
200+
Opts.CompilerDebuggingOpts.DumpAvailabilityScopes |=
201+
Args.hasArg(OPT_dump_availability_scopes);
202+
203+
Opts.CompilerDebuggingOpts.DumpClangLookupTables |=
204+
Args.hasArg(OPT_dump_clang_lookup_tables);
201205

202206
Opts.CheckOnoneSupportCompleteness = Args.hasArg(OPT_check_onone_completeness);
203207

@@ -471,7 +475,8 @@ void ArgsToFrontendOptionsConverter::handleDebugCrashGroupArguments() {
471475
void ArgsToFrontendOptionsConverter::computePrintStatsOptions() {
472476
using namespace options;
473477
Opts.PrintStats |= Args.hasArg(OPT_print_stats);
474-
Opts.PrintClangStats |= Args.hasArg(OPT_print_clang_stats);
478+
Opts.CompilerDebuggingOpts.PrintClangStats |=
479+
Args.hasArg(OPT_print_clang_stats);
475480
Opts.PrintZeroStats |= Args.hasArg(OPT_print_zero_stats);
476481
#if defined(NDEBUG) && !LLVM_ENABLE_STATS
477482
if (Opts.PrintStats || Opts.PrintClangStats)
@@ -665,8 +670,6 @@ ArgsToFrontendOptionsConverter::determineRequestedAction(const ArgList &args) {
665670
return FrontendOptions::ActionType::MergeModules;
666671
if (Opt.matches(OPT_dump_scope_maps))
667672
return FrontendOptions::ActionType::DumpScopeMaps;
668-
if (Opt.matches(OPT_dump_availability_scopes))
669-
return FrontendOptions::ActionType::DumpAvailabilityScopes;
670673
if (Opt.matches(OPT_dump_interface_hash))
671674
return FrontendOptions::ActionType::DumpInterfaceHash;
672675
if (Opt.matches(OPT_dump_type_info))

lib/Frontend/ArgsToFrontendOutputsConverter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ static bool shouldEmitFineModuleTrace(FrontendOptions::ActionType action) {
473473
case swift::FrontendOptions::ActionType::PrintAST:
474474
case swift::FrontendOptions::ActionType::PrintASTDecl:
475475
case swift::FrontendOptions::ActionType::DumpScopeMaps:
476-
case swift::FrontendOptions::ActionType::DumpAvailabilityScopes:
477476
case swift::FrontendOptions::ActionType::EmitImportedModules:
478477
case swift::FrontendOptions::ActionType::EmitPCH:
479478
case swift::FrontendOptions::ActionType::EmitModuleOnly:

lib/Frontend/Frontend.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/Frontend/Frontend.h"
1919
#include "swift/AST/ASTContext.h"
2020
#include "swift/AST/AvailabilityDomain.h"
21+
#include "swift/AST/AvailabilityScope.h"
2122
#include "swift/AST/DiagnosticsFrontend.h"
2223
#include "swift/AST/DiagnosticsSema.h"
2324
#include "swift/AST/FileSystem.h"
@@ -1867,6 +1868,23 @@ bool CompilerInstance::performSILProcessing(SILModule *silModule) {
18671868
return false;
18681869
}
18691870

1871+
void CompilerInstance::emitEndOfPipelineDebuggingOutput() {
1872+
assert(hasASTContext());
1873+
auto &ctx = getASTContext();
1874+
const auto &Invocation = getInvocation();
1875+
const auto &opts = Invocation.getFrontendOptions().CompilerDebuggingOpts;
1876+
1877+
if (opts.PrintClangStats && ctx.getClangModuleLoader())
1878+
ctx.getClangModuleLoader()->printStatistics();
1879+
1880+
if (opts.DumpAvailabilityScopes)
1881+
getPrimaryOrMainSourceFile().getAvailabilityScope()->dump(llvm::errs(),
1882+
ctx.SourceMgr);
1883+
1884+
if (opts.DumpClangLookupTables && ctx.getClangModuleLoader())
1885+
ctx.getClangModuleLoader()->dumpSwiftLookupTables();
1886+
}
1887+
18701888
bool CompilerInstance::isCancellationRequested() const {
18711889
auto flag = getASTContext().CancellationFlag;
18721890
return flag && flag->load(std::memory_order_relaxed);

lib/Frontend/FrontendOptions.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ bool FrontendOptions::needsProperModuleName(ActionType action) {
4141
case ActionType::PrintAST:
4242
case ActionType::PrintASTDecl:
4343
case ActionType::DumpScopeMaps:
44-
case ActionType::DumpAvailabilityScopes:
4544
case ActionType::DumpPCM:
4645
case ActionType::EmitPCH:
4746
return false;
@@ -111,7 +110,6 @@ bool FrontendOptions::doesActionRequireSwiftStandardLibrary(ActionType action) {
111110
case ActionType::PrintAST:
112111
case ActionType::PrintASTDecl:
113112
case ActionType::DumpScopeMaps:
114-
case ActionType::DumpAvailabilityScopes:
115113
case ActionType::EmitSILGen:
116114
case ActionType::EmitSIL:
117115
case ActionType::EmitLoweredSIL:
@@ -157,7 +155,6 @@ bool FrontendOptions::doesActionRequireInputs(ActionType action) {
157155
case ActionType::PrintAST:
158156
case ActionType::PrintASTDecl:
159157
case ActionType::DumpScopeMaps:
160-
case ActionType::DumpAvailabilityScopes:
161158
case ActionType::EmitSILGen:
162159
case ActionType::EmitSIL:
163160
case ActionType::EmitLoweredSIL:
@@ -200,7 +197,6 @@ bool FrontendOptions::doesActionPerformEndOfPipelineActions(ActionType action) {
200197
case ActionType::PrintAST:
201198
case ActionType::PrintASTDecl:
202199
case ActionType::DumpScopeMaps:
203-
case ActionType::DumpAvailabilityScopes:
204200
case ActionType::EmitSILGen:
205201
case ActionType::EmitSIL:
206202
case ActionType::EmitLoweredSIL:
@@ -239,7 +235,6 @@ bool FrontendOptions::supportCompilationCaching(ActionType action) {
239235
case ActionType::PrintAST:
240236
case ActionType::PrintASTDecl:
241237
case ActionType::DumpScopeMaps:
242-
case ActionType::DumpAvailabilityScopes:
243238
case ActionType::MergeModules:
244239
case ActionType::Immediate:
245240
case ActionType::DumpTypeInfo:
@@ -305,7 +300,6 @@ FrontendOptions::formatForPrincipalOutputFileForAction(ActionType action) {
305300
case ActionType::PrintAST:
306301
case ActionType::PrintASTDecl:
307302
case ActionType::DumpScopeMaps:
308-
case ActionType::DumpAvailabilityScopes:
309303
case ActionType::DumpTypeInfo:
310304
case ActionType::DumpPCM:
311305
case ActionType::PrintVersion:
@@ -378,7 +372,6 @@ bool FrontendOptions::canActionEmitDependencies(ActionType action) {
378372
case ActionType::PrintAST:
379373
case ActionType::PrintASTDecl:
380374
case ActionType::DumpScopeMaps:
381-
case ActionType::DumpAvailabilityScopes:
382375
case ActionType::DumpTypeInfo:
383376
case ActionType::CompileModuleFromInterface:
384377
case ActionType::TypecheckModuleFromInterface:
@@ -422,7 +415,6 @@ bool FrontendOptions::canActionEmitReferenceDependencies(ActionType action) {
422415
case ActionType::PrintAST:
423416
case ActionType::PrintASTDecl:
424417
case ActionType::DumpScopeMaps:
425-
case ActionType::DumpAvailabilityScopes:
426418
case ActionType::DumpTypeInfo:
427419
case ActionType::CompileModuleFromInterface:
428420
case ActionType::TypecheckModuleFromInterface:
@@ -467,7 +459,6 @@ bool FrontendOptions::canActionEmitModuleSummary(ActionType action) {
467459
case ActionType::EmitImportedModules:
468460
case ActionType::EmitPCH:
469461
case ActionType::DumpScopeMaps:
470-
case ActionType::DumpAvailabilityScopes:
471462
case ActionType::DumpTypeInfo:
472463
case ActionType::EmitSILGen:
473464
case ActionType::EmitSIBGen:
@@ -509,7 +500,6 @@ bool FrontendOptions::canActionEmitClangHeader(ActionType action) {
509500
case ActionType::PrintASTDecl:
510501
case ActionType::EmitPCH:
511502
case ActionType::DumpScopeMaps:
512-
case ActionType::DumpAvailabilityScopes:
513503
case ActionType::DumpTypeInfo:
514504
case ActionType::CompileModuleFromInterface:
515505
case ActionType::TypecheckModuleFromInterface:
@@ -550,7 +540,6 @@ bool FrontendOptions::canActionEmitLoadedModuleTrace(ActionType action) {
550540
case ActionType::PrintAST:
551541
case ActionType::PrintASTDecl:
552542
case ActionType::DumpScopeMaps:
553-
case ActionType::DumpAvailabilityScopes:
554543
case ActionType::DumpTypeInfo:
555544
case ActionType::CompileModuleFromInterface:
556545
case ActionType::TypecheckModuleFromInterface:
@@ -600,7 +589,6 @@ bool FrontendOptions::canActionEmitModuleSemanticInfo(ActionType action) {
600589
case ActionType::PrintASTDecl:
601590
case ActionType::EmitPCH:
602591
case ActionType::DumpScopeMaps:
603-
case ActionType::DumpAvailabilityScopes:
604592
case ActionType::DumpTypeInfo:
605593
case ActionType::EmitSILGen:
606594
case ActionType::TypecheckModuleFromInterface:
@@ -643,7 +631,6 @@ bool FrontendOptions::canActionEmitConstValues(ActionType action) {
643631
case ActionType::PrintAST:
644632
case ActionType::PrintASTDecl:
645633
case ActionType::DumpScopeMaps:
646-
case ActionType::DumpAvailabilityScopes:
647634
case ActionType::DumpTypeInfo:
648635
case ActionType::CompileModuleFromInterface:
649636
case ActionType::TypecheckModuleFromInterface:
@@ -687,7 +674,6 @@ bool FrontendOptions::canActionEmitModule(ActionType action) {
687674
case ActionType::PrintASTDecl:
688675
case ActionType::EmitPCH:
689676
case ActionType::DumpScopeMaps:
690-
case ActionType::DumpAvailabilityScopes:
691677
case ActionType::DumpTypeInfo:
692678
case ActionType::EmitSILGen:
693679
case ActionType::CompileModuleFromInterface:
@@ -733,7 +719,6 @@ bool FrontendOptions::canActionEmitInterface(ActionType action) {
733719
case ActionType::EmitImportedModules:
734720
case ActionType::EmitPCH:
735721
case ActionType::DumpScopeMaps:
736-
case ActionType::DumpAvailabilityScopes:
737722
case ActionType::DumpTypeInfo:
738723
case ActionType::EmitSILGen:
739724
case ActionType::EmitSIBGen:
@@ -776,7 +761,6 @@ bool FrontendOptions::canActionEmitAPIDescriptor(ActionType action) {
776761
case ActionType::EmitImportedModules:
777762
case ActionType::EmitPCH:
778763
case ActionType::DumpScopeMaps:
779-
case ActionType::DumpAvailabilityScopes:
780764
case ActionType::DumpTypeInfo:
781765
case ActionType::EmitSILGen:
782766
case ActionType::EmitSIBGen:
@@ -818,7 +802,6 @@ bool FrontendOptions::doesActionProduceOutput(ActionType action) {
818802
case ActionType::PrintAST:
819803
case ActionType::PrintASTDecl:
820804
case ActionType::DumpScopeMaps:
821-
case ActionType::DumpAvailabilityScopes:
822805
case ActionType::EmitPCH:
823806
case ActionType::EmitSILGen:
824807
case ActionType::EmitSIL:
@@ -877,7 +860,6 @@ bool FrontendOptions::doesActionProduceTextualOutput(ActionType action) {
877860
case ActionType::PrintAST:
878861
case ActionType::PrintASTDecl:
879862
case ActionType::DumpScopeMaps:
880-
case ActionType::DumpAvailabilityScopes:
881863
case ActionType::EmitImportedModules:
882864
case ActionType::EmitSILGen:
883865
case ActionType::EmitSIL:
@@ -907,7 +889,6 @@ bool FrontendOptions::doesActionGenerateSIL(ActionType action) {
907889
case ActionType::PrintAST:
908890
case ActionType::PrintASTDecl:
909891
case ActionType::DumpScopeMaps:
910-
case ActionType::DumpAvailabilityScopes:
911892
case ActionType::EmitImportedModules:
912893
case ActionType::EmitPCH:
913894
case ActionType::CompileModuleFromInterface:
@@ -948,7 +929,6 @@ bool FrontendOptions::doesActionGenerateIR(ActionType action) {
948929
case ActionType::PrintAST:
949930
case ActionType::PrintASTDecl:
950931
case ActionType::DumpScopeMaps:
951-
case ActionType::DumpAvailabilityScopes:
952932
case ActionType::DumpTypeInfo:
953933
case ActionType::CompileModuleFromInterface:
954934
case ActionType::TypecheckModuleFromInterface:
@@ -994,7 +974,6 @@ bool FrontendOptions::doesActionBuildModuleFromInterface(ActionType action) {
994974
case ActionType::PrintAST:
995975
case ActionType::PrintASTDecl:
996976
case ActionType::DumpScopeMaps:
997-
case ActionType::DumpAvailabilityScopes:
998977
case ActionType::DumpTypeInfo:
999978
case ActionType::Typecheck:
1000979
case ActionType::ResolveImports:

0 commit comments

Comments
 (0)