From a8ffc16dc1c5b21c3fdb2b53a9c0eb5af5d3babb Mon Sep 17 00:00:00 2001
From: Hans Wennborg <hans@hanshq.net>
Date: Tue, 24 Sep 2013 18:17:21 +0000
Subject: [PATCH] clang-cl: fix passing optimization level to cl.exe in
 /fallback mode

We were previously mostly passing it through, but -O0 and -O3 are not valid
options to cl.exe.

We should translate -O0 to /Od and -O3 to /Ox. -O{1,2,s} get passed through.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@191323 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Driver/Tools.cpp      | 14 +++++++++++++-
 test/Driver/cl-fallback.c | 18 +++++++++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 5b96120df9..e5f2681f7b 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -6712,7 +6712,19 @@ Command *visualstudio::Compile::GetCommand(Compilation &C, const JobAction &JA,
   // These are spelled the same way in clang and cl.exe,.
   Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
   Args.AddAllArgs(CmdArgs, options::OPT_I);
-  Args.AddLastArg(CmdArgs, options::OPT_O, options::OPT_O0);
+
+  // Optimization level.
+  if (Arg *A = Args.getLastArg(options::OPT_O, options::OPT_O0)) {
+    if (A->getOption().getID() == options::OPT_O0) {
+      CmdArgs.push_back("/Od");
+    } else {
+      StringRef OptLevel = A->getValue();
+      if (OptLevel == "1" || OptLevel == "2" || OptLevel == "s")
+        A->render(Args, CmdArgs);
+      else if (OptLevel == "3")
+        CmdArgs.push_back("/Ox");
+    }
+  }
 
   // Flags for which clang-cl have an alias.
   // FIXME: How can we ensure this stays in sync with relevant clang-cl options?
diff --git a/test/Driver/cl-fallback.c b/test/Driver/cl-fallback.c
index 715686c624..a80068bce4 100644
--- a/test/Driver/cl-fallback.c
+++ b/test/Driver/cl-fallback.c
@@ -15,10 +15,26 @@
 // CHECK: "-D" "foo=bar"
 // CHECK: "-U" "baz"
 // CHECK: "-I" "foo"
-// CHECK: "-O3"
+// CHECK: "/Ox"
 // CHECK: "/GR-"
 // CHECK: "/LD"
 // CHECK: "/LDd"
 // CHECK: "/MT"
 // CHECK: "/Tc" "{{.*cl-fallback.c}}"
 // CHECK: "/Fo{{.*cl-fallback.*.obj}}"
+
+// RUN: %clang_cl /fallback /Od -### -- %s 2>&1 | FileCheck -check-prefix=O0 %s
+// O0: cl.exe
+// O0: "/Od"
+// RUN: %clang_cl /fallback /O1 -### -- %s 2>&1 | FileCheck -check-prefix=O1 %s
+// O1: cl.exe
+// O1: "-O1"
+// RUN: %clang_cl /fallback /O2 -### -- %s 2>&1 | FileCheck -check-prefix=O2 %s
+// O2: cl.exe
+// O2: "-O2"
+// RUN: %clang_cl /fallback /Os -### -- %s 2>&1 | FileCheck -check-prefix=Os %s
+// Os: cl.exe
+// Os: "-Os"
+// RUN: %clang_cl /fallback /Ox -### -- %s 2>&1 | FileCheck -check-prefix=Ox %s
+// Ox: cl.exe
+// Ox: "/Ox"
-- 
2.40.0