From b8e5dd44edd52942c6c291823841369342e8365f Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Sat, 4 Dec 2010 01:50:36 +0000 Subject: [PATCH] Refactor optimisation level code llvm-svn: 120875 --- clang/lib/Frontend/CompilerInvocation.cpp | 30 ++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index d1d1a381b094..b4e676b43ecd 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -772,6 +772,12 @@ using namespace clang::driver::cc1options; // +static unsigned getOptimizationLevel(ArgList &Args, InputKind IK, + Diagnostic &Diags) { + // -Os implies -O2 + return Args.hasArg(OPT_Os) ? 2 : Args.getLastArgIntValue(OPT_O, 0, Diags); +} + static void ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args, Diagnostic &Diags) { using namespace cc1options; @@ -849,19 +855,15 @@ static void ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args, Opts.IdempotentOps = Args.hasArg(OPT_analysis_WarnIdempotentOps); } -static void ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, +static void ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Diagnostic &Diags) { using namespace cc1options; - // -Os implies -O2 - if (Args.hasArg(OPT_Os)) - Opts.OptimizationLevel = 2; - else { - Opts.OptimizationLevel = Args.getLastArgIntValue(OPT_O, 0, Diags); - if (Opts.OptimizationLevel > 3) { - Diags.Report(diag::err_drv_invalid_value) - << Args.getLastArg(OPT_O)->getAsString(Args) << Opts.OptimizationLevel; - Opts.OptimizationLevel = 3; - } + + Opts.OptimizationLevel = getOptimizationLevel(Args, IK, Diags); + if (Opts.OptimizationLevel > 3) { + Diags.Report(diag::err_drv_invalid_value) + << Args.getLastArg(OPT_O)->getAsString(Args) << Opts.OptimizationLevel; + Opts.OptimizationLevel = 3; } // We must always run at least the always inlining pass. @@ -1415,8 +1417,7 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK, Opts.OptimizeSize = 0; // FIXME: Eliminate this dependency. - unsigned Opt = - Args.hasArg(OPT_Os) ? 2 : Args.getLastArgIntValue(OPT_O, 0, Diags); + unsigned Opt = getOptimizationLevel(Args, IK, Diags); Opts.Optimize = Opt != 0; // This is the __NO_INLINE__ define, which just depends on things like the @@ -1570,11 +1571,12 @@ void CompilerInvocation::CreateFromArgs(CompilerInvocation &Res, Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args); ParseAnalyzerArgs(Res.getAnalyzerOpts(), *Args, Diags); - ParseCodeGenArgs(Res.getCodeGenOpts(), *Args, Diags); ParseDependencyOutputArgs(Res.getDependencyOutputOpts(), *Args); ParseDiagnosticArgs(Res.getDiagnosticOpts(), *Args, Diags); ParseFileSystemArgs(Res.getFileSystemOpts(), *Args); + // FIXME: We shouldn't have to pass the DashX option around here InputKind DashX = ParseFrontendArgs(Res.getFrontendOpts(), *Args, Diags); + ParseCodeGenArgs(Res.getCodeGenOpts(), *Args, DashX, Diags); ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), *Args); if (DashX != IK_AST && DashX != IK_LLVM_IR) ParseLangArgs(Res.getLangOpts(), *Args, DashX, Diags);