[PATCH] Using an invalid -O falls back on -O3 instead of an error

Sylvestre Ledru sylvestre at debian.org
Thu Nov 14 05:18:40 PST 2013


  Document the change in the release

Hi rengolin, chandlerc, rafael, hfinkel,

http://llvm-reviews.chandlerc.com/D2152

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2152?vs=5472&id=5541#toc

BRANCH
  /cfe/trunk

ARCANIST PROJECT
  clang

Files:
  test/Driver/clang_f_opts.c
  test/Driver/invalid-o-level.c
  include/clang/Basic/DiagnosticDriverKinds.td
  include/clang/Basic/DiagnosticGroups.td
  docs/ReleaseNotes.rst
  lib/Frontend/CompilerInvocation.cpp
  lib/Driver/Tools.cpp

Index: test/Driver/clang_f_opts.c
===================================================================
--- test/Driver/clang_f_opts.c
+++ test/Driver/clang_f_opts.c
@@ -103,6 +103,9 @@
 // CHECK-MAX-O: warning: -O4 is equivalent to -O3
 // CHECK-MAX-O: -O3
 
+// RUN: %clang -S -O20 %s 2>&1 | FileCheck -check-prefix=CHECK-INVALID-O %s
+// CHECK-INVALID-O: warning: optimization level '-O20' is unsupported; using '-O3' instead.
+
 // Test that we don't error on these.
 // RUN: %clang -### -S -Werror                                                \
 // RUN:     -falign-functions -falign-functions=2 -fno-align-functions        \
Index: test/Driver/invalid-o-level.c
===================================================================
--- test/Driver/invalid-o-level.c
+++ test/Driver/invalid-o-level.c
@@ -1,4 +1,6 @@
-// RUN: not %clang_cc1 %s -O900 2> %t.log
+// RUN: %clang_cc1 %s -O900 2> %t.log
 // RUN: FileCheck %s -input-file=%t.log
 
-// CHECK: invalid value '900' in '-O900'
+// CHECK: warning: optimization level '-O900' is unsupported; using '-O3' instead.
+
+
Index: include/clang/Basic/DiagnosticDriverKinds.td
===================================================================
--- include/clang/Basic/DiagnosticDriverKinds.td
+++ include/clang/Basic/DiagnosticDriverKinds.td
@@ -113,6 +113,8 @@
   "cannot recognize the type of the toolchain">;
 
 def warn_O4_is_O3 : Warning<"-O4 is equivalent to -O3">, InGroup<Deprecated>;
+def warn_drv_optimization_value : Warning<"optimization level '%0' is unsupported; using '%1%2' instead.">,
+  InGroup<InvalidCommandLineArgument>;
 def warn_c_kext : Warning<
   "ignoring -fapple-kext which is valid for C++ and Objective-C++ only">;
 def warn_drv_input_file_unused : Warning<
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td
+++ include/clang/Basic/DiagnosticGroups.td
@@ -339,6 +339,7 @@
 def UnusedSanitizeArgument : DiagGroup<"unused-sanitize-argument">;
 def UnusedCommandLineArgument : DiagGroup<"unused-command-line-argument",
                                           [UnusedSanitizeArgument]>;
+def InvalidCommandLineArgument : DiagGroup<"invalid-command-line-argument">;
 def UnusedComparison : DiagGroup<"unused-comparison">;
 def UnusedExceptionParameter : DiagGroup<"unused-exception-parameter">;
 def UnneededInternalDecl : DiagGroup<"unneeded-internal-declaration">;
Index: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -77,7 +77,8 @@
 
 - Clang no longer special cases -O4 to enable lto. Explicitly pass -flto to
   enable it.
-- Command line "clang -O3 -flto a.c -c" and "clang -emit-llvm a.c -c" 
+- Clang no longer fails on >= -O5. Uses -O3 instead.
+- Command line "clang -O3 -flto a.c -c" and "clang -emit-llvm a.c -c"
   are no longer equivalent.
 - Clang now errors on unknown -m flags (``-munknown-to-clang``),
   unknown -f flags (``-funknown-to-clang``) and unknown
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -299,14 +299,14 @@
   using namespace options;
   bool Success = true;
 
-  unsigned OptLevel = getOptimizationLevel(Args, IK, Diags);
-  if (OptLevel > 3) {
-    Diags.Report(diag::err_drv_invalid_value)
-      << Args.getLastArg(OPT_O)->getAsString(Args) << OptLevel;
-    OptLevel = 3;
-    Success = false;
+  Opts.OptimizationLevel = getOptimizationLevel(Args, IK, Diags);
+  unsigned MaxOptLevel = 3;
+  if (Opts.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;
   }
-  Opts.OptimizationLevel = OptLevel;
 
   // We must always run at least the always inlining pass.
   Opts.setInlining(
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp
+++ lib/Driver/Tools.cpp
@@ -2741,7 +2741,7 @@
   // preprocessed inputs and configure concludes that -fPIC is not supported.
   Args.ClaimAllArgs(options::OPT_D);
 
-  // Manually translate -O4 to -O3; let clang reject others.
+  // Manually translate -O4 to -O3; let clang fall back on -O3 for others
   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
     if (A->getOption().matches(options::OPT_O4)) {
       CmdArgs.push_back("-O3");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2152.2.patch
Type: text/x-patch
Size: 4668 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131114/cb556d96/attachment.bin>


More information about the cfe-commits mailing list