[Driver][X86] Reject unsupported value for -mabi=
authorFangrui Song <i@maskray.me>
Fri, 26 May 2023 16:52:57 +0000 (09:52 -0700)
committerFangrui Song <i@maskray.me>
Fri, 26 May 2023 16:52:57 +0000 (09:52 -0700)
-mabi= was incorrectly claimed before D134671. -mabi=sysv appears to be
somewhat common in open-source packages, even if it was not intended to
be supported by Clang.
(For common options supported by multiple architectures, it's easy to
forget to report an error on unsupported targets. Unfortunately
the driver infrastructure doesn't make this less error-prone.)

On x86, support -mabi=sysv for non-Windows targets and -mabi=ms for Windows,
and remove the spurious -Wunused-command-line-argument warning.

With this change, all popular architectures claim -mabi=, so we don't
have to worry much about -Wunused-command-line-argument for other
architectures.

Differential Revision: https://reviews.llvm.org/D151509

clang/lib/Driver/ToolChains/Arch/X86.cpp
clang/test/Driver/mabi.c [deleted file]
clang/test/Driver/x86-mabi.c [new file with mode: 0644]

index f1ad370..286bac2 100644 (file)
@@ -119,6 +119,15 @@ std::string x86::getX86TargetCPU(const Driver &D, const ArgList &Args,
 void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
                                const ArgList &Args,
                                std::vector<StringRef> &Features) {
+  // Claim and report unsupported -mabi=. Note: we don't support "sysv_abi" or
+  // "ms_abi" as default function attributes.
+  if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mabi_EQ)) {
+    StringRef DefaultAbi = Triple.isOSWindows() ? "ms" : "sysv";
+    if (A->getValue() != DefaultAbi)
+      D.Diag(diag::err_drv_unsupported_opt_for_target)
+          << A->getSpelling() << Triple.getTriple();
+  }
+
   // If -march=native, autodetect the feature list.
   if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
     if (StringRef(A->getValue()) == "native") {
diff --git a/clang/test/Driver/mabi.c b/clang/test/Driver/mabi.c
deleted file mode 100644 (file)
index 01e494d..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-// RUN: %clang --target=i386-unknown-linux -mabi=ms -S %s -### 2>&1 | FileCheck --check-prefix=CHECK %s
-
-int f() {
-  // CHECK: warning: argument unused during compilation: '-mabi=ms'
-  return 0;
-}
diff --git a/clang/test/Driver/x86-mabi.c b/clang/test/Driver/x86-mabi.c
new file mode 100644 (file)
index 0000000..790d3c8
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: %clang -### --target=x86_64-windows-msvc -mabi=ms -S %s 2>&1 | FileCheck %s
+// RUN: %clang -### --target=i386-unknown-linux -mabi=ms -S %s 2>&1 | FileCheck --check-prefix=ERR %s
+// RUN: %clang -### --target=x86_64-windows-msvc -mabi=sysv -S %s 2>&1 | FileCheck --check-prefix=ERR %s
+// RUN: %clang -### --target=i386-unknown-linux -mabi=sysv -S %s 2>&1 | FileCheck %s
+
+// RUN: %clang -### --target=x86_64-windows-gnu -mabi=ms -S %s 2>&1 | FileCheck %s
+
+// CHECK-NOT: {{error|warning}}:
+// ERR: error: unsupported option '-mabi=' for target '{{.*}}'
+
+int f() {
+  return 0;
+}