From: Rafael Espindola Date: Tue, 16 Dec 2014 21:57:03 +0000 (+0000) Subject: Fix handling of invalid -O options. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=046ea41e0a46efede23805cd80a4d132ed75d799;p=clang Fix handling of invalid -O options. We were checking the value after truncating it to a bitfield. Thanks to Yunzhong Gao for noticing it. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@224378 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/CodeGenOptions.def b/include/clang/Frontend/CodeGenOptions.def index 6cfe191931..15b4a5a793 100644 --- a/include/clang/Frontend/CodeGenOptions.def +++ b/include/clang/Frontend/CodeGenOptions.def @@ -87,7 +87,7 @@ CODEGENOPT(NoZeroInitializedInBSS , 1, 0) ///< -fno-zero-initialized-in-bss. ENUM_CODEGENOPT(ObjCDispatchMethod, ObjCDispatchMethodKind, 2, Legacy) CODEGENOPT(OmitLeafFramePointer , 1, 0) ///< Set when -momit-leaf-frame-pointer is ///< enabled. -VALUE_CODEGENOPT(OptimizationLevel, 3, 0) ///< The -O[0-4] option specified. +VALUE_CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified. VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is specified. CODEGENOPT(ProfileInstrGenerate , 1, 0) ///< Instrument code to generate diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 97be3d3fbf..776848ba7f 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -330,15 +330,17 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, using namespace options; bool Success = true; - Opts.OptimizationLevel = getOptimizationLevel(Args, IK, Diags); + unsigned OptimizationLevel = getOptimizationLevel(Args, IK, Diags); // TODO: This could be done in Driver unsigned MaxOptLevel = 3; - if (Opts.OptimizationLevel > MaxOptLevel) { - // If the optimization level is not supported, fall back on the default optimization + if (OptimizationLevel > MaxOptLevel) { + // If the optimization level is not supported, fall back on the default + // optimization Diags.Report(diag::warn_drv_optimization_value) << Args.getLastArg(OPT_O)->getAsString(Args) << "-O" << MaxOptLevel; - Opts.OptimizationLevel = MaxOptLevel; + OptimizationLevel = MaxOptLevel; } + Opts.OptimizationLevel = OptimizationLevel; // We must always run at least the always inlining pass. Opts.setInlining( diff --git a/test/Frontend/invalid-o-level.c b/test/Frontend/invalid-o-level.c index 73be9b1c21..0314448b0f 100644 --- a/test/Frontend/invalid-o-level.c +++ b/test/Frontend/invalid-o-level.c @@ -1,4 +1,5 @@ -// RUN: %clang_cc1 %s -O900 -o /dev/null 2> %t.log -// RUN: FileCheck %s -input-file=%t.log +// RUN: %clang_cc1 %s -O900 -o /dev/null 2>&1 | FileCheck %s -// CHECK: warning: optimization level '-O900' is not supported; using '-O3' instead +// RUN: %clang_cc1 %s -O8 -o /dev/null 2>&1 | FileCheck %s + +// CHECK: warning: optimization level '-O{{.*}}' is not supported; using '-O3' instead