[Driver] Reject AIX-specific link options on non-AIX targets
authorFangrui Song <i@maskray.me>
Fri, 26 May 2023 22:30:23 +0000 (15:30 -0700)
committerFangrui Song <i@maskray.me>
Fri, 26 May 2023 22:30:23 +0000 (15:30 -0700)
Follow-up to D106688 and D146431.

-b leads to a -Wunused-command-line-argument warning instead of an error
without linking phase.
-mxcoff-build-id= leads to a -Wunused-command-line-argument warning instead of
an error.

clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Driver/ToolChains/CommonArgs.cpp
clang/test/Driver/Xlinker-args.c
clang/test/Driver/aix-link-options-on-non-aix.c [new file with mode: 0644]
clang/test/Driver/unsupported-target-K.c [deleted file]

index e1adff4..2fd07b3 100644 (file)
@@ -6370,10 +6370,14 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
     }
   }
 
-  if (Arg *A = Args.getLastArgNoClaim(options::OPT_K);
-      A && !TC.getTriple().isOSAIX())
-    D.Diag(diag::err_drv_unsupported_opt_for_target)
-        << A->getAsString(Args) << TripleStr;
+  // Reject AIX-specific link options on other targets.
+  if (!TC.getTriple().isOSAIX()) {
+    for (const Arg *A : Args.filtered(options::OPT_b, options::OPT_K,
+                                      options::OPT_mxcoff_build_id_EQ)) {
+      D.Diag(diag::err_drv_unsupported_opt_for_target)
+          << A->getSpelling() << TripleStr;
+    }
+  }
 
   if (Args.getLastArg(options::OPT_fapple_kext) ||
       (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType)))
index 4a11be7..8854d13 100644 (file)
@@ -303,15 +303,6 @@ void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
       // Pass -z prefix for gcc linker compatibility.
       A.claim();
       A.render(Args, CmdArgs);
-    } else if (A.getOption().matches(options::OPT_b)) {
-      const llvm::Triple &T = TC.getTriple();
-      if (!T.isOSAIX()) {
-        TC.getDriver().Diag(diag::err_drv_unsupported_opt_for_target)
-            << A.getSpelling() << T.str();
-      }
-      // Pass -b prefix for AIX linker.
-      A.claim();
-      A.render(Args, CmdArgs);
     } else {
       A.renderAsInput(Args, CmdArgs);
     }
index ad59e0b..b2a913e 100644 (file)
 // RUN:   -b one -b two %s 2> %t
 // RUN: FileCheck -check-prefix=AIX < %t %s
 
-// RUN: %clang -target powerpc-unknown-linux -### \
-// RUN:   -b one %s 2> %t
-// RUN: FileCheck -check-prefix=NOT-AIX < %t %s
-
 // DARWIN-NOT: --no-demangle
 // DARWIN: "one" "two" "three" "four" "-z" "five" "-r"
 // LINUX: "--no-demangle" "-e" "_start" "one" "two" "three" "four" "-z" "five" "-r" {{.*}} "-T" "a.lds"
 // MINGW: "--no-demangle"
 // AIX: "-b" "one" "-b" "two"
-// NOT-AIX: error: unsupported option '-b' for target 'powerpc-unknown-linux'
 
 // Check that we forward '-Xlinker' and '-Wl,' on Windows.
 // RUN: %clang -target i686-pc-win32 -fuse-ld=link -### \
diff --git a/clang/test/Driver/aix-link-options-on-non-aix.c b/clang/test/Driver/aix-link-options-on-non-aix.c
new file mode 100644 (file)
index 0000000..d44137a
--- /dev/null
@@ -0,0 +1,10 @@
+/// AIX-specific link options are rejected for other targets.
+// RUN: %clang -### --target=powerpc64-unknown-linux-gnu \
+// RUN:   --sysroot %S/Inputs/aix_ppc_tree --unwindlib=libunwind --rtlib=compiler-rt \
+// RUN:   -b one -K -mxcoff-build-id=a %s 2>&1 | FileCheck %s --implicit-check-not=error:
+// RUN: %clang -### --target=powerpc64-unknown-linux-gnu -c \
+// RUN:   --sysroot %S/Inputs/aix_ppc_tree --unwindlib=libunwind --rtlib=compiler-rt \
+// RUN:   -b one -K -mxcoff-build-id=a %s 2>&1 | FileCheck %s --implicit-check-not=error:
+// CHECK: error: unsupported option '-b' for target '{{.*}}'
+// CHECK: error: unsupported option '-K' for target '{{.*}}'
+// CHECK: error: unsupported option '-mxcoff-build-id=' for target '{{.*}}'
diff --git a/clang/test/Driver/unsupported-target-K.c b/clang/test/Driver/unsupported-target-K.c
deleted file mode 100644 (file)
index 8b9a6f5..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-// Check powerpc64-unknown-linux-gnu. -K not supported.
-// RUN: %clang %s 2>&1 -### \
-// RUN:        --target=powerpc64-unknown-linux-gnu \
-// RUN:        --sysroot %S/Inputs/aix_ppc_tree \
-// RUN:        --unwindlib=libunwind \
-// RUN:        -K \
-// RUN:   | FileCheck --check-prefixes=CHECK-K-SUPPORT %s
-// CHECK-K-SUPPORT: clang: error: unsupported option '-K' for target 'powerpc64-unknown-linux-gnu'