diff --git a/clang/include/clang/Frontend/CompilerInvocation.h b/clang/include/clang/Frontend/CompilerInvocation.h --- a/clang/include/clang/Frontend/CompilerInvocation.h +++ b/clang/include/clang/Frontend/CompilerInvocation.h @@ -262,7 +262,8 @@ /// Generate command line options from LangOptions. static void GenerateLangArgs(const LangOptions &Opts, SmallVectorImpl &Args, - StringAllocator SA, const llvm::Triple &T); + StringAllocator SA, const llvm::Triple &T, + InputKind IK); /// Parse command line options that map to CodeGenOptions. static bool ParseCodeGenArgs(CodeGenOptions &Opts, llvm::opt::ArgList &Args, diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -3278,7 +3278,21 @@ void CompilerInvocation::GenerateLangArgs(const LangOptions &Opts, SmallVectorImpl &Args, StringAllocator SA, - const llvm::Triple &T) { + const llvm::Triple &T, InputKind IK) { + if (IK.getFormat() == InputKind::Precompiled || + IK.getLanguage() == Language::LLVM_IR) { + if (Opts.ObjCAutoRefCount) + GenerateArg(Args, OPT_fobjc_arc, SA); + if (Opts.PICLevel != 0) + GenerateArg(Args, OPT_pic_level, Twine(Opts.PICLevel), SA); + if (Opts.PIE) + GenerateArg(Args, OPT_pic_is_pie, SA); + for (StringRef Sanitizer : serializeSanitizerKinds(Opts.Sanitize)) + GenerateArg(Args, OPT_fsanitize_EQ, Sanitizer, SA); + + return; + } + OptSpecifier StdOpt; switch (Opts.LangStd) { case LangStandard::lang_opencl10: @@ -3500,6 +3514,26 @@ DiagnosticsEngine &Diags) { unsigned NumErrorsBefore = Diags.getNumErrors(); + if (IK.getFormat() == InputKind::Precompiled || + IK.getLanguage() == Language::LLVM_IR) { + // ObjCAAutoRefCount and Sanitize LangOpts are used to setup the + // PassManager in BackendUtil.cpp. They need to be initialized no matter + // what the input type is. + if (Args.hasArg(OPT_fobjc_arc)) + Opts.ObjCAutoRefCount = 1; + // PICLevel and PIELevel are needed during code generation and this should + // be set regardless of the input type. + Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags); + Opts.PIE = Args.hasArg(OPT_pic_is_pie); + parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ), + Diags, Opts.Sanitize); + + return Diags.getNumErrors() == NumErrorsBefore; + } + + // Other LangOpts are only initialized when the input is not AST or LLVM IR. + // FIXME: Should we really be parsing this for an Language::Asm input? + // FIXME: Cleanup per-file based stuff. LangStandard::Kind LangStd = LangStandard::lang_unspecified; if (const Arg *A = Args.getLastArg(OPT_std_EQ)) { @@ -4303,27 +4337,11 @@ llvm::Triple T(Res.getTargetOpts().Triple); ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags, Res.getFileSystemOpts().WorkingDir); - if (DashX.getFormat() == InputKind::Precompiled || - DashX.getLanguage() == Language::LLVM_IR) { - // ObjCAAutoRefCount and Sanitize LangOpts are used to setup the - // PassManager in BackendUtil.cpp. They need to be initializd no matter - // what the input type is. - if (Args.hasArg(OPT_fobjc_arc)) - LangOpts.ObjCAutoRefCount = 1; - // PIClevel and PIELevel are needed during code generation and this should be - // set regardless of the input type. - LangOpts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags); - LangOpts.PIE = Args.hasArg(OPT_pic_is_pie); - parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ), - Diags, LangOpts.Sanitize); - } else { - // Other LangOpts are only initialized when the input is not AST or LLVM IR. - // FIXME: Should we really be calling this for an Language::Asm input? - ParseLangArgs(LangOpts, Args, DashX, T, Res.getPreprocessorOpts().Includes, - Diags); - if (Res.getFrontendOpts().ProgramAction == frontend::RewriteObjC) - LangOpts.ObjCExceptions = 1; - } + + ParseLangArgs(LangOpts, Args, DashX, T, Res.getPreprocessorOpts().Includes, + Diags); + if (Res.getFrontendOpts().ProgramAction == frontend::RewriteObjC) + LangOpts.ObjCExceptions = 1; if (LangOpts.CUDA) { // During CUDA device-side compilation, the aux triple is the @@ -4517,24 +4535,7 @@ GenerateFrontendArgs(FrontendOpts, Args, SA, LangOpts->IsHeaderFile); GenerateTargetArgs(*TargetOpts, Args, SA); GenerateHeaderSearchArgs(*HeaderSearchOpts, Args, SA); - - InputKind DashX = FrontendOpts.DashX; - if (DashX.getFormat() == InputKind::Precompiled || - DashX.getLanguage() == Language::LLVM_IR) { - if (LangOpts->ObjCAutoRefCount) - GenerateArg(Args, OPT_fobjc_arc, SA); - if (LangOpts->PICLevel != 0) - GenerateArg(Args, OPT_pic_level, Twine(LangOpts->PICLevel), SA); - if (LangOpts->PIE) - GenerateArg(Args, OPT_pic_is_pie, SA); - for (StringRef Sanitizer : serializeSanitizerKinds(LangOpts->Sanitize)) - GenerateArg(Args, OPT_fsanitize_EQ, Sanitizer, SA); - } else { - // FIXME: Move this whole condition into GenerateLangArgs. (And do the same - // for ParseLangArgs). - GenerateLangArgs(*LangOpts, Args, SA, T); - } - + GenerateLangArgs(*LangOpts, Args, SA, T, FrontendOpts.DashX); GenerateCodeGenArgs(CodeGenOpts, Args, SA, T, FrontendOpts.OutputFile, &*LangOpts); GeneratePreprocessorArgs(*PreprocessorOpts, Args, SA, *LangOpts, FrontendOpts,