20
20
// /
21
21
// ===----------------------------------------------------------------------===//
22
22
23
+ #include " swift/FrontendTool/FrontendTool.h"
24
+
23
25
#include " swift/Subsystems.h"
24
26
#include " swift/AST/DiagnosticsFrontend.h"
25
27
#include " swift/AST/DiagnosticsSema.h"
@@ -614,7 +616,8 @@ static void debugFailWithCrash() {
614
616
static bool performCompile (CompilerInstance &Instance,
615
617
CompilerInvocation &Invocation,
616
618
ArrayRef<const char *> Args,
617
- int &ReturnValue) {
619
+ int &ReturnValue,
620
+ FrontendObserver *observer) {
618
621
FrontendOptions opts = Invocation.getFrontendOptions ();
619
622
FrontendOptions::ActionType Action = opts.RequestedAction ;
620
623
@@ -669,6 +672,10 @@ static bool performCompile(CompilerInstance &Instance,
669
672
else
670
673
Instance.performSema ();
671
674
675
+ if (observer) {
676
+ observer->performedSemanticAnalysis (Instance);
677
+ }
678
+
672
679
FrontendOptions::DebugCrashMode CrashMode = opts.CrashMode ;
673
680
if (CrashMode == FrontendOptions::DebugCrashMode::AssertAfterParse)
674
681
debugFailWithAssertion ();
@@ -759,6 +766,10 @@ static bool performCompile(CompilerInstance &Instance,
759
766
}
760
767
}
761
768
769
+ if (observer) {
770
+ observer->performedSILGeneration (*SM);
771
+ }
772
+
762
773
// We've been told to emit SIL after SILGen, so write it now.
763
774
if (Action == FrontendOptions::EmitSILGen) {
764
775
// If we are asked to link all, link all.
@@ -787,9 +798,14 @@ static bool performCompile(CompilerInstance &Instance,
787
798
}
788
799
789
800
// Perform "stable" optimizations that are invariant across compiler versions.
790
- if (!Invocation.getDiagnosticOptions ().SkipDiagnosticPasses &&
791
- runSILDiagnosticPasses (*SM))
792
- return true ;
801
+ if (!Invocation.getDiagnosticOptions ().SkipDiagnosticPasses ) {
802
+ if (runSILDiagnosticPasses (*SM))
803
+ return true ;
804
+
805
+ if (observer) {
806
+ observer->performedSILDiagnostics (*SM);
807
+ }
808
+ }
793
809
794
810
// Now if we are asked to link all, link all.
795
811
if (Invocation.getSILOptions ().LinkMode == SILOptions::LinkAll)
@@ -818,6 +834,10 @@ static bool performCompile(CompilerInstance &Instance,
818
834
}
819
835
}
820
836
837
+ if (observer) {
838
+ observer->performedSILOptimization (*SM);
839
+ }
840
+
821
841
{
822
842
SharedTimer timer (" SIL verification (post-optimization)" );
823
843
SM->verify ();
@@ -915,6 +935,11 @@ static bool performCompile(CompilerInstance &Instance,
915
935
const ProcessCmdLine &CmdLine = ProcessCmdLine (opts.ImmediateArgv .begin (),
916
936
opts.ImmediateArgv .end ());
917
937
Instance.setSILModule (std::move (SM));
938
+
939
+ if (observer) {
940
+ observer->aboutToRunImmediately (Instance);
941
+ }
942
+
918
943
ReturnValue =
919
944
RunImmediately (Instance, CmdLine, IRGenOpts, Invocation.getSILOptions ());
920
945
return false ;
@@ -997,7 +1022,8 @@ static bool dumpAPI(Module *Mod, StringRef OutDir) {
997
1022
}
998
1023
999
1024
int swift::performFrontend (ArrayRef<const char *> Args,
1000
- const char *Argv0, void *MainAddr) {
1025
+ const char *Argv0, void *MainAddr,
1026
+ FrontendObserver *observer) {
1001
1027
llvm::InitializeAllTargets ();
1002
1028
llvm::InitializeAllTargetMCs ();
1003
1029
llvm::InitializeAllAsmPrinters ();
@@ -1031,6 +1057,11 @@ int swift::performFrontend(ArrayRef<const char *> Args,
1031
1057
if (Invocation.getLangOptions ().Target .isWindowsCygwinEnvironment ())
1032
1058
IRGenOpts.DWARFVersion = swift::CygwinDWARFVersion;
1033
1059
1060
+ // The compiler invocation is now fully configured; notify our observer.
1061
+ if (observer) {
1062
+ observer->parsedArgs (Invocation);
1063
+ }
1064
+
1034
1065
if (Invocation.getFrontendOptions ().PrintHelp ||
1035
1066
Invocation.getFrontendOptions ().PrintHelpHidden ) {
1036
1067
unsigned IncludedFlagsBitmask = options::FrontendOption;
@@ -1125,9 +1156,15 @@ int swift::performFrontend(ArrayRef<const char *> Args,
1125
1156
return 1 ;
1126
1157
}
1127
1158
1159
+ // The compiler instance has been configured; notify our observer.
1160
+ if (observer) {
1161
+ observer->configuredCompiler (Instance);
1162
+ }
1163
+
1128
1164
int ReturnValue = 0 ;
1129
- bool HadError = performCompile (Instance, Invocation, Args, ReturnValue) ||
1130
- Instance.getASTContext ().hadError ();
1165
+ bool HadError =
1166
+ performCompile (Instance, Invocation, Args, ReturnValue, observer) ||
1167
+ Instance.getASTContext ().hadError ();
1131
1168
1132
1169
if (!HadError && !Invocation.getFrontendOptions ().DumpAPIPath .empty ()) {
1133
1170
HadError = dumpAPI (Instance.getMainModule (),
@@ -1148,3 +1185,11 @@ int swift::performFrontend(ArrayRef<const char *> Args,
1148
1185
1149
1186
return (HadError ? 1 : ReturnValue);
1150
1187
}
1188
+
1189
+ void FrontendObserver::parsedArgs (CompilerInvocation &invocation) {}
1190
+ void FrontendObserver::configuredCompiler (CompilerInstance &instance) {}
1191
+ void FrontendObserver::performedSemanticAnalysis (CompilerInstance &instance) {}
1192
+ void FrontendObserver::performedSILGeneration (SILModule &module ) {}
1193
+ void FrontendObserver::performedSILDiagnostics (SILModule &module ) {}
1194
+ void FrontendObserver::performedSILOptimization (SILModule &module ) {}
1195
+ void FrontendObserver::aboutToRunImmediately (CompilerInstance &instance) {}
0 commit comments