@@ -26,15 +26,48 @@ using namespace llvm::opt;
2626using namespace swift ;
2727using namespace modulesummary ;
2828
29+ class MergeModuleSummaryInvocation {
30+ public:
31+ std::string OutputFilename;
32+ std::vector<std::string> InputFilenames;
33+
34+ bool parseArgs (ArrayRef<const char *> Args, DiagnosticEngine &Diags) {
35+ using namespace options ;
36+
37+ std::unique_ptr<llvm::opt::OptTable> Table = createSwiftOptTable ();
38+ unsigned MissingIndex;
39+ unsigned MissingCount;
40+ llvm::opt::InputArgList ParsedArgs =
41+ Table->ParseArgs (Args, MissingIndex, MissingCount, SwiftMergeModuleSummaryOption);
42+
43+ if (MissingCount) {
44+ Diags.diagnose (SourceLoc (), diag::error_missing_arg_value,
45+ ParsedArgs.getArgString (MissingIndex), MissingCount);
46+ return true ;
47+ }
48+
49+ for (const Arg *A : ParsedArgs.filtered (OPT_INPUT)) {
50+ InputFilenames.push_back (A->getValue ());
51+ }
52+
53+ if (const Arg *A = ParsedArgs.getLastArg (OPT_o)) {
54+ OutputFilename = A->getValue ();
55+ }
56+
57+ std::vector<const char *> LLVMArgs {" " };
58+ for (const Arg *A : ParsedArgs.filtered (OPT_Xllvm)) {
59+ LLVMArgs.push_back (A->getValue ());
60+ }
61+
62+ llvm::cl::ParseCommandLineOptions (LLVMArgs.size (), LLVMArgs.data (), " " );
63+ return false ;
64+ }
65+ };
66+
2967static llvm::cl::opt<std::string>
3068 LTOPrintLiveTrace (" lto-print-live-trace" , llvm::cl::init(" " ),
3169 llvm::cl::desc(" Print liveness trace for the symbol" ));
3270
33- static llvm::cl::list<std::string>
34- InputFilenames (llvm::cl::Positional, llvm::cl::desc(" [input files...]" ));
35- static llvm::cl::opt<std::string>
36- OutputFilename (" o" , llvm::cl::desc(" output filename" ));
37-
3871static llvm::DenseSet<GUID> computePreservedGUIDs (ModuleSummaryIndex *summary) {
3972 llvm::DenseSet<GUID> Set (1 );
4073 for (auto FI = summary->functions_begin (), FE = summary->functions_end ();
@@ -239,21 +272,24 @@ int cross_module_opt_main(ArrayRef<const char *> Args, const char *Argv0,
239272 void *MainAddr) {
240273 INITIALIZE_LLVM ();
241274
242- llvm::cl::ParseCommandLineOptions (Args.size (), Args.data (), " Swift LTO\n " );
243-
244275 CompilerInstance Instance;
245276 PrintingDiagnosticConsumer PDC;
246277 Instance.addDiagnosticConsumer (&PDC);
247278
248- if (InputFilenames.empty ()) {
279+ MergeModuleSummaryInvocation Invocation;
280+ if (Invocation.parseArgs (Args, Instance.getDiags ())) {
281+ return true ;
282+ }
283+
284+ if (Invocation.InputFilenames .empty ()) {
249285 Instance.getDiags ().diagnose (SourceLoc (),
250286 diag::error_mode_requires_an_input_file);
251287 return 1 ;
252288 }
253289
254290 auto TheSummary = std::make_unique<ModuleSummaryIndex>();
255291
256- for (auto Filename : InputFilenames) {
292+ for (auto Filename : Invocation. InputFilenames ) {
257293 LLVM_DEBUG (llvm::dbgs () << " Loading module summary " << Filename << " \n " );
258294 auto ErrOrBuf = llvm::MemoryBuffer::getFile (Filename);
259295 if (!ErrOrBuf) {
@@ -276,6 +312,6 @@ int cross_module_opt_main(ArrayRef<const char *> Args, const char *Argv0,
276312 markDeadSymbols (*TheSummary.get (), PreservedGUIDs);
277313
278314 modulesummary::writeModuleSummaryIndex (*TheSummary, Instance.getDiags (),
279- OutputFilename);
315+ Invocation. OutputFilename );
280316 return 0 ;
281317}
0 commit comments