[clang] [Darwin][Driver] Avoid duplicate -lc++ with -fsanitize=fuzzer (PR #161304)

Andrew Haberlandt via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 30 09:55:15 PDT 2025


https://github.com/ndrewh updated https://github.com/llvm/llvm-project/pull/161304

>From d03967ce6371f729b7f226d594f4380d4e025d80 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Mon, 29 Sep 2025 18:12:24 -0700
Subject: [PATCH] [Darwin][Driver] Avoid duplicate -lc++ with -fsanitize=fuzzer

On Darwin, duplicate -l options cause a warning to be printed.

Invoking clang as clang++ and using -fsanitize=fuzzer will
cause -lc++ to be passed twice to the linker, causing a warning.

i.e. AddCXXStdlibLibArgs is called twice in this case:
1) https://github.com/llvm/llvm-project/blob/19c4e86f3e8582c3f087a9fec5ac036838e58ec4/clang/lib/Driver/ToolChains/Darwin.cpp#L743
2) The subject of this PR

Since ShouldLinkCXXStdlib() implies that the linker will already
get the -lc++ argument, we only add the argument if
!ShouldLinkCXXStdlib().

rdar://136431775
---
 clang/lib/Driver/ToolChains/Darwin.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 234683f2f4882..d2356ebdfa86c 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1609,7 +1609,12 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
     if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) {
       AddLinkSanitizerLibArgs(Args, CmdArgs, "fuzzer", /*shared=*/false);
 
-        // Libfuzzer is written in C++ and requires libcxx.
+      // Libfuzzer is written in C++ and requires libcxx.
+      // Since darwin::Linker::ConstructJob already adds -lc++ for clang++
+      // by default if ShouldLinkCXXStdlib(Args), we only add the option if
+      // !ShouldLinkCXXStdlib(Args). This avoids duplicate library errors
+      // on Darwin.
+      if (!ShouldLinkCXXStdlib(Args))
         AddCXXStdlibLibArgs(Args, CmdArgs);
     }
     if (Sanitize.needsStatsRt()) {



More information about the cfe-commits mailing list