<br><br>On Wednesday, May 7, 2014, Saleem Abdulrasool <<a href="mailto:compnerd@compnerd.org">compnerd@compnerd.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: compnerd<br>
Date: Wed May  7 21:28:32 2014<br>
New Revision: 208275<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=208275&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=208275&view=rev</a><br>
Log:<br>
Driver: parse -mcmodel earlier<br>
<br>
This addresses an existing FIXME item in the driver.  The code model flag was<br>
parsed in the actual tool rather than in the driver.  This was problematic since<br>
the value may be invalid.  In that case, we would silently treat it as a default<br>
value in non-assert builds, and abort in assert builds.  Add a check in the<br>
driver to validate that the value being passed is valid, and if not provide a<br>
proper error message.<br>
<br>
Added:<br>
    cfe/trunk/test/Driver/code-model.c<br>
Modified:<br>
    cfe/trunk/lib/CodeGen/BackendUtil.cpp<br>
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp<br>
<br>
Modified: cfe/trunk/lib/CodeGen/BackendUtil.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/BackendUtil.cpp?rev=208275&r1=208274&r2=208275&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/BackendUtil.cpp?rev=208275&r1=208274&r2=208275&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/CodeGen/BackendUtil.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/BackendUtil.cpp Wed May  7 21:28:32 2014<br>
@@ -14,6 +14,7 @@<br>
 #include "clang/Frontend/CodeGenOptions.h"<br>
 #include "clang/Frontend/FrontendDiagnostic.h"<br>
 #include "clang/Frontend/Utils.h"<br>
+#include "llvm/ADT/StringSwitch.h"<br>
 #include "llvm/Bitcode/BitcodeWriterPass.h"<br>
 #include "llvm/CodeGen/RegAllocRegistry.h"<br>
 #include "llvm/CodeGen/SchedulerRegistry.h"<br>
@@ -378,20 +379,16 @@ TargetMachine *EmitAssemblyHelper::Creat<br>
   TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections);<br>
   TargetMachine::setDataSections    (CodeGenOpts.DataSections);<br>
<br>
-  // FIXME: Parse this earlier.<br>
-  llvm::CodeModel::Model CM;<br>
-  if (CodeGenOpts.CodeModel == "small") {<br>
-    CM = llvm::CodeModel::Small;<br>
-  } else if (CodeGenOpts.CodeModel == "kernel") {<br>
-    CM = llvm::CodeModel::Kernel;<br>
-  } else if (CodeGenOpts.CodeModel == "medium") {<br>
-    CM = llvm::CodeModel::Medium;<br>
-  } else if (CodeGenOpts.CodeModel == "large") {<br>
-    CM = llvm::CodeModel::Large;<br>
-  } else {<br>
-    assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!");<br>
-    CM = llvm::CodeModel::Default;<br>
-  }<br>
+  unsigned CodeModel =<br>
+    llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)<br>
+      .Case("small", llvm::CodeModel::Small)<br>
+      .Case("kernel", llvm::CodeModel::Kernel)<br>
+      .Case("medium", llvm::CodeModel::Medium)<br>
+      .Case("large", llvm::CodeModel::Medium)</blockquote><div><br></div><div>Doesn't this map large to medium?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+      .Case("default", llvm::CodeModel::Default)<br>
+      .Default(~0u);<br>
+  assert(CodeModel != ~0u && "invalid code model!");<br>
+  llvm::CodeModel::Model CM = static_cast<llvm::CodeModel::Model>(CodeModel);<br>
<br>
   SmallVector<const char *, 16> BackendArgs;<br>
   BackendArgs.push_back("clang"); // Fake program name.<br>
<br>
Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=208275&r1=208274&r2=208275&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=208275&r1=208274&r2=208275&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)<br>
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Wed May  7 21:28:32 2014<br>
@@ -27,6 +27,7 @@<br>
 #include "llvm/Option/ArgList.h"<br>
 #include "llvm/Option/OptTable.h"<br>
 #include "llvm/Option/Option.h"<br>
+#include "llvm/Support/CodeGen.h"<br>
 #include "llvm/Support/ErrorHandling.h"<br>
 #include "llvm/Support/FileSystem.h"<br>
 #include "llvm/Support/Host.h"<br>
@@ -295,6 +296,17 @@ static void ParseCommentArgs(CommentOpti<br>
   Opts.ParseAllComments = Args.hasArg(OPT_fparse_all_comments);<br>
 }<br>
<br>
+static StringRef getCodeModel(ArgList &Args, DiagnosticsEngine &Diags) {<br>
+  if (Arg *A = Args.getLastArg(OPT_mcode_model)) {<br>
+    StringRef Value = A->getValue();<br>
+    if (Value == "small" || Value == "kernel" || Value == "medium" ||<br>
+        Value == "large")<br>
+      return Value;<br>
+    Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Value;<br>
+  }<br>
+  return "default";<br>
+}<br>
+<br>
 static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,<br>
                              DiagnosticsEngine &Diags,<br>
                              const TargetOptions &TargetOpts) {<br>
@@ -376,7 +388,7 @@ static bool ParseCodeGenArgs(CodeGenOpti<br>
   Opts.CUDAIsDevice = Args.hasArg(OPT_fcuda_is_device);<br>
   Opts.CXAAtExit = !Args.hasArg(OPT_fno_use_cxa_atexit);<br>
   Opts.CXXCtorDtorAliases = Args.hasArg(OPT_mconstructor_aliases);<br>
-  Opts.CodeModel = Args.getLastArgValue(OPT_mcode_model);<br>
+  Opts.CodeModel = getCodeModel(Args, Diags);<br>
   Opts.DebugPass = Args.getLastArgValue(OPT_mdebug_pass);<br>
   Opts.DisableFPElim = Args.hasArg(OPT_mdisable_fp_elim);<br>
   Opts.DisableFree = Args.hasArg(OPT_disable_free);<br>
<br>
Added: cfe/trunk/test/Driver/code-model.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/code-model.c?rev=208275&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/code-model.c?rev=208275&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/test/Driver/code-model.c (added)<br>
+++ cfe/trunk/test/Driver/code-model.c Wed May  7 21:28:32 2014<br>
@@ -0,0 +1,13 @@<br>
+// RUN: %clang -### -c -mcmodel=small %s 2>&1 | FileCheck -check-prefix CHECK-SMALL %s<br>
+// RUN: %clang -### -S -mcmodel=kernel %s 2>&1 | FileCheck -check-prefix CHECK-KERNEL %s<br>
+// RUN: %clang -### -c -mcmodel=medium %s 2>&1 | FileCheck -check-prefix CHECK-MEDIUM %s<br>
+// RUN: %clang -### -S -mcmodel=large %s 2>&1 | FileCheck -check-prefix CHECK-LARGE %s<br>
+// RUN: not %clang -c -mcmodel=lager %s 2>&1 | FileCheck -check-prefix CHECK-INVALID %s<br>
+<br>
+// CHECK-SMALL: "-mcode-model" "small"<br>
+// CHECK-KERNEL: "-mcode-model" "kernel"<br>
+// CHECK-MEDIUM: "-mcode-model" "medium"<br>
+// CHECK-LARGE: "-mcode-model" "large"<br>
+<br>
+// CHECK-INVALID: error: invalid value 'lager' in '-mcode-model lager'<br>
+<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="javascript:;" onclick="_e(event, 'cvml', 'cfe-commits@cs.uiuc.edu')">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote>