[Driver] Don't crash on invalid values of -mrelocation-model=.
authorDavide Italiano <davide@freebsd.org>
Sat, 1 Apr 2017 21:07:07 +0000 (21:07 +0000)
committerDavide Italiano <davide@freebsd.org>
Sat, 1 Apr 2017 21:07:07 +0000 (21:07 +0000)
This is handled in a similar way we handle invalid -mcode-model.

PR: 31840
llvm-svn: 299315

clang/lib/CodeGen/BackendUtil.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/test/Driver/reloc-model.c [new file with mode: 0644]

index 874fd14..855d679 100644 (file)
@@ -323,7 +323,8 @@ static llvm::CodeModel::Model getCodeModel(const CodeGenOptions &CodeGenOpts) {
 }
 
 static llvm::Reloc::Model getRelocModel(const CodeGenOptions &CodeGenOpts) {
-  // Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp.
+  // Keep this synced with the equivalent code in
+  // lib/Frontend/CompilerInvocation.cpp
   llvm::Optional<llvm::Reloc::Model> RM;
   RM = llvm::StringSwitch<llvm::Reloc::Model>(CodeGenOpts.RelocationModel)
       .Case("static", llvm::Reloc::Static)
index a9d805b..9138450 100644 (file)
@@ -330,6 +330,17 @@ static StringRef getCodeModel(ArgList &Args, DiagnosticsEngine &Diags) {
   return "default";
 }
 
+static StringRef getRelocModel(ArgList &Args, DiagnosticsEngine &Diags) {
+  if (Arg *A = Args.getLastArg(OPT_mrelocation_model)) {
+    StringRef Value = A->getValue();
+    if (Value == "static" || Value == "pic" || Value == "ropi" ||
+        Value == "rwpi" || Value == "ropi-rwpi" || Value == "dynamic-no-pic")
+      return Value;
+    Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Value;
+  }
+  return "pic";
+}
+
 /// \brief Create a new Regex instance out of the string value in \p RpassArg.
 /// It returns a pointer to the newly generated Regex instance.
 static std::shared_ptr<llvm::Regex>
@@ -615,7 +626,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
                       Args.hasArg(OPT_cl_unsafe_math_optimizations) ||
                       Args.hasArg(OPT_cl_fast_relaxed_math);
   Opts.UnwindTables = Args.hasArg(OPT_munwind_tables);
-  Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
+  Opts.RelocationModel = getRelocModel(Args, Diags);
   Opts.ThreadModel = Args.getLastArgValue(OPT_mthread_model, "posix");
   if (Opts.ThreadModel != "posix" && Opts.ThreadModel != "single")
     Diags.Report(diag::err_drv_invalid_value)
diff --git a/clang/test/Driver/reloc-model.c b/clang/test/Driver/reloc-model.c
new file mode 100644 (file)
index 0000000..5d08eee
--- /dev/null
@@ -0,0 +1,4 @@
+// RUN: not %clang -cc1 -mrelocation-model tinkywinky \
+// RUN: -emit-llvm %s 2>&1 | FileCheck -check-prefix CHECK-INVALID %s
+
+// CHECK-INVALID: error: invalid value 'tinkywinky' in '-mrelocation-model tinkywinky'