From 5abf2ec12dbd5b554fa1c7a24c6556b9a1d3f859 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 18 Nov 2013 13:23:07 +0000 Subject: [PATCH] Using an invalid -O falls back on -O3 instead of an error Summary: Currently with clang: $ clang -O20 foo.c error: invalid value '20' in '-O20' With the patch: $ clang -O20 foo.c warning: optimization level '-O20' is unsupported; using '-O3' instead. 1 warning generated. This matches the gcc behavior (with a warning added) Pass all tests: Testing: 0 .. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. Testing Time: 94.14s Expected Passes : 6721 Expected Failures : 20 Unsupported Tests : 17 (which was not the case of http://llvm-reviews.chandlerc.com/D2125) Differential Revision: http://llvm-reviews.chandlerc.com/D2212 llvm-svn: 195009 --- clang/docs/ReleaseNotes.rst | 3 ++- .../include/clang/Basic/DiagnosticDriverKinds.td | 2 ++ clang/include/clang/Basic/DiagnosticGroups.td | 1 + clang/lib/Frontend/CompilerInvocation.cpp | 15 ++++++++------- clang/test/Driver/clang_f_opts.c | 3 +++ clang/test/Frontend/invalid-o-level.c | 4 ++-- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 7d63744bdf9b..597d48142f0e 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -77,7 +77,8 @@ New Compiler Flags - 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 diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index ed49006b215d..ee87353bdaaa 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -113,6 +113,8 @@ def err_drv_unknown_toolchain : Error< "cannot recognize the type of the toolchain">; def warn_O4_is_O3 : Warning<"-O4 is equivalent to -O3">, InGroup; +def warn_drv_optimization_value : Warning<"optimization level '%0' is unsupported; using '%1%2' instead.">, + InGroup; def warn_c_kext : Warning< "ignoring -fapple-kext which is valid for C++ and Objective-C++ only">; def warn_drv_input_file_unused : Warning< diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index 93d5f00be5e2..2643ee9dbf9d 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -344,6 +344,7 @@ def UnusedArgument : DiagGroup<"unused-argument">; 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">; diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 94a3a33235a3..8db5cf1bc21a 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -299,14 +299,15 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, 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); + // 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 + 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( diff --git a/clang/test/Driver/clang_f_opts.c b/clang/test/Driver/clang_f_opts.c index 9d9cd86e8291..e6867ab18a2c 100644 --- a/clang/test/Driver/clang_f_opts.c +++ b/clang/test/Driver/clang_f_opts.c @@ -110,6 +110,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 \ diff --git a/clang/test/Frontend/invalid-o-level.c b/clang/test/Frontend/invalid-o-level.c index d5242c7ac066..1b800188e0e4 100644 --- a/clang/test/Frontend/invalid-o-level.c +++ b/clang/test/Frontend/invalid-o-level.c @@ -1,4 +1,4 @@ -// RUN: not %clang_cc1 %s -O900 2> %t.log +// RUN: %clang_cc1 %s -O900 -o /dev/nul 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.