33
33
#include " swift/AST/DiagnosticsModuleDiffer.h"
34
34
#include " swift/IDE/APIDigesterData.h"
35
35
#include < functional>
36
- #include " ModuleAnalyzerNodes.h"
37
- #include " ModuleDiagsConsumer.h"
36
+ #include " swift/APIDigester/ ModuleAnalyzerNodes.h"
37
+ #include " swift/APIDigester/ ModuleDiagsConsumer.h"
38
38
39
39
using namespace swift ;
40
40
using namespace ide ;
@@ -2551,13 +2551,6 @@ static int generateMigrationScript(StringRef LeftPath, StringRef RightPath,
2551
2551
return 0 ;
2552
2552
}
2553
2553
2554
- // This function isn't referenced outside its translation unit, but it
2555
- // can't use the "static" keyword because its address is used for
2556
- // getMainExecutable (since some platforms don't support taking the
2557
- // address of main, and some platforms can't implement getMainExecutable
2558
- // without being given the address of a function in the main executable).
2559
- void anchorForGetMainExecutable () {}
2560
-
2561
2554
static void setSDKPath (CompilerInvocation &InitInvok, bool IsBaseline) {
2562
2555
if (IsBaseline) {
2563
2556
// Set baseline SDK
@@ -2578,12 +2571,10 @@ static void setSDKPath(CompilerInvocation &InitInvok, bool IsBaseline) {
2578
2571
}
2579
2572
}
2580
2573
2581
- static int prepareForDump (const char *Main ,
2574
+ static int prepareForDump (std::string MainExecutablePath ,
2582
2575
CompilerInvocation &InitInvok,
2583
- llvm::StringSet<> &Modules,
2584
- bool IsBaseline = false ) {
2585
- InitInvok.setMainExecutablePath (fs::getMainExecutable (Main,
2586
- reinterpret_cast <void *>(&anchorForGetMainExecutable)));
2576
+ llvm::StringSet<> &Modules, bool IsBaseline = false ) {
2577
+ InitInvok.setMainExecutablePath (MainExecutablePath);
2587
2578
InitInvok.setModuleName (" swift_ide_test" );
2588
2579
setSDKPath (InitInvok, IsBaseline);
2589
2580
@@ -2692,7 +2683,7 @@ static int deserializeNameCorrection(APIDiffItemStore &Store,
2692
2683
return EC.value ();
2693
2684
}
2694
2685
2695
- static CheckerOptions getCheckOpts (int argc, char *argv[] ) {
2686
+ static CheckerOptions getCheckOpts (ArrayRef< const char *> Args ) {
2696
2687
CheckerOptions Opts;
2697
2688
Opts.AvoidLocation = options::AvoidLocation;
2698
2689
Opts.AvoidToolArgs = options::AvoidToolArgs;
@@ -2708,8 +2699,8 @@ static CheckerOptions getCheckOpts(int argc, char *argv[]) {
2708
2699
Opts.SkipOSCheck = options::DisableOSChecks;
2709
2700
Opts.CompilerStyle = options::CompilerStyleDiags ||
2710
2701
!options::SerializedDiagPath.empty ();
2711
- for (int i = 1 ; i < argc; ++i )
2712
- Opts.ToolArgs .push_back (argv[i] );
2702
+ for (auto Arg : Args )
2703
+ Opts.ToolArgs .push_back (Arg );
2713
2704
2714
2705
if (!options::SDK.empty ()) {
2715
2706
auto Ver = getSDKBuildVersion (options::SDK);
@@ -2721,10 +2712,11 @@ static CheckerOptions getCheckOpts(int argc, char *argv[]) {
2721
2712
return Opts;
2722
2713
}
2723
2714
2724
- static SDKNodeRoot *getSDKRoot (const char *Main, SDKContext &Ctx, bool IsBaseline) {
2715
+ static SDKNodeRoot *getSDKRoot (std::string MainExecutablePath, SDKContext &Ctx,
2716
+ bool IsBaseline) {
2725
2717
CompilerInvocation Invok;
2726
2718
llvm::StringSet<> Modules;
2727
- if (prepareForDump (Main , Invok, Modules, IsBaseline))
2719
+ if (prepareForDump (MainExecutablePath , Invok, Modules, IsBaseline))
2728
2720
return nullptr ;
2729
2721
return getSDKNodeRoot (Ctx, Invok, Modules);
2730
2722
}
@@ -2749,20 +2741,18 @@ static ComparisonInputMode checkComparisonInputMode() {
2749
2741
return ComparisonInputMode::BaselineJson;
2750
2742
}
2751
2743
2752
- static std::string getDefaultBaselineDir (const char *Main ) {
2744
+ static std::string getDefaultBaselineDir (std::string MainExecutablePath ) {
2753
2745
llvm::SmallString<128 > BaselineDir;
2754
- // The path of the swift-api-digester executable.
2755
- std::string ExePath = llvm::sys::fs::getMainExecutable (Main,
2756
- reinterpret_cast <void *>(&anchorForGetMainExecutable));
2757
- BaselineDir.append (ExePath);
2746
+ BaselineDir.append (MainExecutablePath);
2758
2747
llvm::sys::path::remove_filename (BaselineDir); // Remove /swift-api-digester
2759
2748
llvm::sys::path::remove_filename (BaselineDir); // Remove /bin
2760
2749
llvm::sys::path::append (BaselineDir, " lib" , " swift" , " FrameworkABIBaseline" );
2761
2750
return BaselineDir.str ().str ();
2762
2751
}
2763
2752
2764
- static std::string getEmptyBaselinePath (const char *Main) {
2765
- llvm::SmallString<128 > BaselinePath (getDefaultBaselineDir (Main));
2753
+ static std::string getEmptyBaselinePath (std::string MainExecutablePath) {
2754
+ llvm::SmallString<128 > BaselinePath (
2755
+ getDefaultBaselineDir (MainExecutablePath));
2766
2756
llvm::sys::path::append (BaselinePath, " nil.json" );
2767
2757
return BaselinePath.str ().str ();
2768
2758
}
@@ -2788,10 +2778,11 @@ static StringRef getBaselineFilename(llvm::Triple Triple) {
2788
2778
}
2789
2779
}
2790
2780
2791
- static std::string getDefaultBaselinePath (const char *Main, StringRef Module ,
2792
- llvm::Triple Triple,
2781
+ static std::string getDefaultBaselinePath (std::string MainExecutablePath ,
2782
+ StringRef Module, llvm::Triple Triple,
2793
2783
bool ABI) {
2794
- llvm::SmallString<128 > BaselinePath (getDefaultBaselineDir (Main));
2784
+ llvm::SmallString<128 > BaselinePath (
2785
+ getDefaultBaselineDir (MainExecutablePath));
2795
2786
llvm::sys::path::append (BaselinePath, Module);
2796
2787
// Look for ABI or API baseline
2797
2788
llvm::sys::path::append (BaselinePath, ABI? " ABI" : " API" );
@@ -2807,12 +2798,13 @@ static std::string getCustomBaselinePath(llvm::Triple Triple, bool ABI) {
2807
2798
return BaselinePath.str ().str ();
2808
2799
}
2809
2800
2810
- static SDKNodeRoot *getBaselineFromJson (const char *Main, SDKContext &Ctx) {
2801
+ static SDKNodeRoot *getBaselineFromJson (std::string MainExecutablePath,
2802
+ SDKContext &Ctx) {
2811
2803
SwiftDeclCollector Collector (Ctx);
2812
2804
CompilerInvocation Invok;
2813
2805
llvm::StringSet<> Modules;
2814
2806
// We need to call prepareForDump to parse target triple.
2815
- if (prepareForDump (Main , Invok, Modules, true ))
2807
+ if (prepareForDump (MainExecutablePath , Invok, Modules, true ))
2816
2808
return nullptr ;
2817
2809
2818
2810
assert (Modules.size () == 1 &&
@@ -2825,9 +2817,9 @@ static SDKNodeRoot *getBaselineFromJson(const char *Main, SDKContext &Ctx) {
2825
2817
Path = getCustomBaselinePath (Invok.getLangOptions ().Target ,
2826
2818
Ctx.checkingABI ());
2827
2819
} else if (options::UseEmptyBaseline) {
2828
- Path = getEmptyBaselinePath (Main );
2820
+ Path = getEmptyBaselinePath (MainExecutablePath );
2829
2821
} else {
2830
- Path = getDefaultBaselinePath (Main , Modules.begin ()->getKey (),
2822
+ Path = getDefaultBaselinePath (MainExecutablePath , Modules.begin ()->getKey (),
2831
2823
Invok.getLangOptions ().Target ,
2832
2824
Ctx.checkingABI ());
2833
2825
}
@@ -2860,26 +2852,37 @@ static std::string getJsonOutputFilePath(llvm::Triple Triple, bool ABI) {
2860
2852
exit (1 );
2861
2853
}
2862
2854
2863
- int main ( int argc, char *argv[]) {
2864
- PROGRAM_START (argc, argv);
2855
+ int swift_api_digester_main (ArrayRef< const char *> Args, const char *Argv0,
2856
+ void *MainAddr) {
2865
2857
INITIALIZE_LLVM ();
2866
2858
2859
+ // LLVM Command Line parsing expects to trim off argv[0].
2860
+ SmallVector<const char *, 8 > ArgsWithArgv0{Argv0};
2861
+ ArgsWithArgv0.append (Args.begin (), Args.end ());
2862
+
2863
+ std::string MainExecutablePath = fs::getMainExecutable (Argv0, MainAddr);
2864
+
2867
2865
llvm::cl::HideUnrelatedOptions (options::Category);
2868
- llvm::cl::ParseCommandLineOptions (argc, argv, " Swift SDK Digester\n " );
2866
+ llvm::cl::ParseCommandLineOptions (ArgsWithArgv0.size (),
2867
+ llvm::makeArrayRef (ArgsWithArgv0).data (),
2868
+ " Swift SDK Digester\n " );
2869
2869
CompilerInvocation InitInvok;
2870
2870
2871
2871
llvm::StringSet<> Modules;
2872
2872
std::vector<std::string> PrintApis;
2873
2873
llvm::StringSet<> IgnoredUsrs;
2874
2874
readIgnoredUsrs (IgnoredUsrs);
2875
- CheckerOptions Opts = getCheckOpts (argc, argv );
2875
+ CheckerOptions Opts = getCheckOpts (Args );
2876
2876
for (auto Name : options::ApisPrintUsrs)
2877
2877
PrintApis.push_back (Name);
2878
2878
switch (options::Action) {
2879
2879
case ActionType::DumpSDK:
2880
- return (prepareForDump (argv[0 ], InitInvok, Modules)) ? 1 :
2881
- dumpSDKContent (InitInvok, Modules,
2882
- getJsonOutputFilePath (InitInvok.getLangOptions ().Target , Opts.ABI ),
2880
+ return (prepareForDump (MainExecutablePath, InitInvok, Modules))
2881
+ ? 1
2882
+ : dumpSDKContent (
2883
+ InitInvok, Modules,
2884
+ getJsonOutputFilePath (InitInvok.getLangOptions ().Target ,
2885
+ Opts.ABI ),
2883
2886
Opts);
2884
2887
case ActionType::MigratorGen:
2885
2888
case ActionType::DiagnoseSDKs: {
@@ -2904,17 +2907,17 @@ int main(int argc, char *argv[]) {
2904
2907
}
2905
2908
case ComparisonInputMode::BaselineJson: {
2906
2909
SDKContext Ctx (Opts);
2907
- return diagnoseModuleChange (Ctx, getBaselineFromJson (argv[ 0 ], Ctx),
2908
- getSDKRoot (argv[ 0 ] , Ctx, false ),
2909
- options::OutputFile,
2910
- std::move (protocolAllowlist));
2910
+ return diagnoseModuleChange (
2911
+ Ctx, getBaselineFromJson (MainExecutablePath , Ctx),
2912
+ getSDKRoot (MainExecutablePath, Ctx, false ), options::OutputFile,
2913
+ std::move (protocolAllowlist));
2911
2914
}
2912
2915
case ComparisonInputMode::BothLoad: {
2913
2916
SDKContext Ctx (Opts);
2914
- return diagnoseModuleChange (Ctx, getSDKRoot (argv[ 0 ], Ctx, true ),
2915
- getSDKRoot (argv[ 0 ] , Ctx, false ),
2916
- options::OutputFile,
2917
- std::move (protocolAllowlist));
2917
+ return diagnoseModuleChange (
2918
+ Ctx, getSDKRoot (MainExecutablePath , Ctx, true ),
2919
+ getSDKRoot (MainExecutablePath, Ctx, false ), options::OutputFile,
2920
+ std::move (protocolAllowlist));
2918
2921
}
2919
2922
}
2920
2923
}
0 commit comments