https://github.com/quic-k updated https://github.com/llvm/llvm-project/pull/201599
>From c2a820dfacbb67e6ac23e2b92297c4a182fe4777 Mon Sep 17 00:00:00 2001 From: Kushal Pal <[email protected]> Date: Thu, 4 Jun 2026 20:00:00 +0530 Subject: [PATCH] [Clang][Hexagon] Predefine _GNU_SOURCE for C++ compilations C++ programs using both picolibc and libcxx headers on Hexagon required manually passing -D_GNU_SOURCE on every compilation. Picolibc guards GNU-extension locale functions (e.g. strtold_l) behind __GNU_VISIBLE, which is only enabled when _GNU_SOURCE is defined. Predefine _GNU_SOURCE in C++ mode for H2 and QURT Hexagon targets, following the convention of other targets. Signed-off-by: Kushal Pal <[email protected]> --- clang/lib/Basic/Targets/Hexagon.cpp | 3 +++ clang/lib/Basic/Targets/OSTargets.h | 4 ++++ clang/test/Preprocessor/hexagon-predefines.c | 16 ++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/clang/lib/Basic/Targets/Hexagon.cpp b/clang/lib/Basic/Targets/Hexagon.cpp index 9bf34e67a03fd..615114f0fd1ea 100644 --- a/clang/lib/Basic/Targets/Hexagon.cpp +++ b/clang/lib/Basic/Targets/Hexagon.cpp @@ -116,6 +116,9 @@ void HexagonTargetInfo::getTargetDefines(const LangOptions &Opts, Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2"); Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4"); Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8"); + + if (Opts.CPlusPlus && getTriple().getOS() == llvm::Triple::UnknownOS) + Builder.defineMacro("_GNU_SOURCE"); } bool HexagonTargetInfo::initFeatureMap( diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h index 943373c20af32..9461680df8bdb 100644 --- a/clang/lib/Basic/Targets/OSTargets.h +++ b/clang/lib/Basic/Targets/OSTargets.h @@ -1092,6 +1092,8 @@ class LLVM_LIBRARY_VISIBILITY QURTTargetInfo : public OSTargetInfo<Target> { void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple, MacroBuilder &Builder) const override { Builder.defineMacro("__qurt__"); + if (Opts.CPlusPlus) + Builder.defineMacro("_GNU_SOURCE"); } public: @@ -1105,6 +1107,8 @@ class LLVM_LIBRARY_VISIBILITY H2TargetInfo : public OSTargetInfo<Target> { void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple, MacroBuilder &Builder) const override { Builder.defineMacro("__h2__"); + if (Opts.CPlusPlus) + Builder.defineMacro("_GNU_SOURCE"); } public: diff --git a/clang/test/Preprocessor/hexagon-predefines.c b/clang/test/Preprocessor/hexagon-predefines.c index cb3e9492ea07e..f115e6e0a9926 100644 --- a/clang/test/Preprocessor/hexagon-predefines.c +++ b/clang/test/Preprocessor/hexagon-predefines.c @@ -261,3 +261,19 @@ // CHECK-H2: #define __h2__ 1 // CHECK-H2: #define __hexagon__ 1 // CHECK-H2-NOT: #define __linux__ + +// RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf -x c++ %s | FileCheck \ +// RUN: %s -check-prefix CHECK-CXX-GNU +// CHECK-CXX-GNU: #define _GNU_SOURCE 1 + +// RUN: %clang_cc1 -E -dM -triple hexagon-unknown-h2 -x c++ %s | FileCheck \ +// RUN: %s -check-prefix CHECK-H2-CXX-GNU +// CHECK-H2-CXX-GNU: #define _GNU_SOURCE 1 + +// RUN: %clang_cc1 -E -dM -triple hexagon-unknown-qurt -x c++ %s | FileCheck \ +// RUN: %s -check-prefix CHECK-QURT-CXX-GNU +// CHECK-QURT-CXX-GNU: #define _GNU_SOURCE 1 + +// RUN: %clang_cc1 -E -dM -triple hexagon-unknown-elf %s | FileCheck \ +// RUN: %s -check-prefix CHECK-C-GNU +// CHECK-C-GNU-NOT: #define _GNU_SOURCE _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
