@@ -399,6 +399,60 @@ LangStd parseHLSLVersion(llvm::StringRef Ver) {
399399 .Case (" 202x" , hlsl::LangStd::v202x)
400400 .Default (hlsl::LangStd::vError);
401401}
402+
403+ // Returns the preprocess output filename based on /P or /Po flags,
404+ // or empty string if neither is set.
405+ static std::string getPreprocessOutput (InputArgList &Args,
406+ llvm::raw_ostream &Errors) {
407+ if (Args.hasFlag (OPT_P, OPT_INVALID, false )) {
408+ // cl.exe-compatible /P: preprocess to <inputname>.i, or use /Fi to
409+ // override.
410+ llvm::SmallString<128 > Path (Args.getLastArgValue (OPT_INPUT));
411+ llvm::sys::path::replace_extension (Path, " i" );
412+ return Args.getLastArgValue (OPT_Fi, Path).str ();
413+ }
414+
415+ if (!Args.hasFlag (OPT_Po, OPT_INVALID, false ))
416+ return " " ;
417+
418+ // /Po: backward-compatible preprocessing (deprecated, use /P instead).
419+ // Default preprocess filename is InputName.i.
420+ llvm::SmallString<128 > Path (Args.getLastArgValue (OPT_INPUT));
421+ llvm::sys::path::replace_extension (Path, " i" );
422+ // Try to get preprocess filename from Fi.
423+ std::string Result = Args.getLastArgValue (OPT_Fi, Path).str ();
424+
425+ // Hack to support fxc style /Po preprocess_filename.
426+ // When there're more than 1 Input file, use the input which is after /Po
427+ // as preprocess.
428+ if (!Args.hasArg (OPT_Fi)) {
429+ std::vector<std::string> Inputs = Args.getAllArgValues (OPT_INPUT);
430+ if (Inputs.size () > 1 ) {
431+ llvm::opt::Arg *PoArg = Args.getLastArg (OPT_Po);
432+ std::string LastInput = Inputs.back ();
433+ llvm::opt::Arg *PrevInputArg = nullptr ;
434+ for (llvm::opt::Arg *InputArg : Args.filtered (OPT_INPUT)) {
435+ // Find Input after /Po.
436+ if ((PoArg->getIndex () + 1 ) == InputArg->getIndex ()) {
437+ Result = InputArg->getValue ();
438+ if (LastInput == Result && PrevInputArg) {
439+ // When InputArg is last Input, update it to other Input so
440+ // Args.getLastArgValue(OPT_INPUT) get expect Input.
441+ InputArg->getValues ()[0 ] = PrevInputArg->getValues ()[0 ];
442+ }
443+ break ;
444+ }
445+ PrevInputArg = InputArg;
446+ }
447+ }
448+ }
449+ Errors << " warning: /Po is deprecated, please use /P" ;
450+ if (!Result.empty () && Result != Path.str ())
451+ Errors << " /Fi " << Result;
452+ Errors << " instead.\n " ;
453+ return Result;
454+ }
455+
402456namespace options {
403457
404458// / Reads all options from the given argument strings, populates opts, and
@@ -583,40 +637,7 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
583637 opts.UseInstructionNumbers = Args.hasFlag (OPT_Ni, OPT_INVALID, false );
584638 opts.UseInstructionByteOffsets = Args.hasFlag (OPT_No, OPT_INVALID, false );
585639 opts.UseHexLiterals = Args.hasFlag (OPT_Lx, OPT_INVALID, false );
586- if (Args.hasFlag (OPT_P, OPT_INVALID, false )) {
587- // Default preprocess filename is InputName.i.
588- llvm::SmallString<128 > Path (Args.getLastArgValue (OPT_INPUT));
589- llvm::sys::path::replace_extension (Path, " i" );
590- // Try to get preprocess filename from Fi.
591- opts.Preprocess = Args.getLastArgValue (OPT_Fi, Path).str ();
592- // Hack to support fxc style /P preprocess_filename.
593- // When there're more than 1 Input file, use the input which is after /P as
594- // preprocess.
595- if (!Args.hasArg (OPT_Fi)) {
596- std::vector<std::string> Inputs = Args.getAllArgValues (OPT_INPUT);
597- if (Inputs.size () > 1 ) {
598- llvm::opt::Arg *PArg = Args.getLastArg (OPT_P);
599- std::string LastInput = Inputs.back ();
600- llvm::opt::Arg *PrevInputArg = nullptr ;
601- for (llvm::opt::Arg *InputArg : Args.filtered (OPT_INPUT)) {
602- // Find Input after /P.
603- if ((PArg->getIndex () + 1 ) == InputArg->getIndex ()) {
604- opts.Preprocess = InputArg->getValue ();
605- if (LastInput == opts.Preprocess && PrevInputArg) {
606- // When InputArg is last Input, update it to other Input so
607- // Args.getLastArgValue(OPT_INPUT) get expect Input.
608- InputArg->getValues ()[0 ] = PrevInputArg->getValues ()[0 ];
609- }
610- errors << " warning: -P " << opts.Preprocess
611- << " is deprecated, please use -P -Fi " << opts.Preprocess
612- << " instead.\n " ;
613- break ;
614- }
615- PrevInputArg = InputArg;
616- }
617- }
618- }
619- }
640+ opts.Preprocess = getPreprocessOutput (Args, errors);
620641 opts.AstDumpImplicit =
621642 Args.hasFlag (OPT_ast_dump_implicit, OPT_INVALID, false );
622643 // -ast-dump-implicit should imply -ast-dump.
0 commit comments