[llvm-branch-commits] [clang] release/22.x: [Hexagon] Fix B0 macro conflict between hexagon_types.h and termios.h (#184539) (PR #185779)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Mar 10 17:09:19 PDT 2026


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/185779

Backport b84ea71e2a81b3e51fa84d611bfc4a3416adc42c

Requested by: @androm3da

>From 22b8f7b26fda916ca4aeca903acb1779615c10e9 Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Tue, 10 Mar 2026 19:00:12 -0500
Subject: [PATCH] [Hexagon] Fix B0 macro conflict between hexagon_types.h and
 termios.h (#184539)

POSIX termios.h defines `#define B0 0000000` for baud rate 0. This
conflicts with the B0() member functions in hexagon_types.h vector
classes, causing compilation failures when both headers are included.

Use #pragma push_macro/pop_macro to save, undefine, and restore B0
around the class definitions so the header is safe to use alongside
termios.h without losing the macro afterward.

Fixes #183815

(cherry picked from commit b84ea71e2a81b3e51fa84d611bfc4a3416adc42c)
---
 clang/lib/Headers/hexagon_types.h             | 43 ++++++++++---------
 .../test/Headers/hexagon-types-b0-conflict.c  | 38 ++++++++++++++++
 2 files changed, 61 insertions(+), 20 deletions(-)
 create mode 100644 clang/test/Headers/hexagon-types-b0-conflict.c

diff --git a/clang/lib/Headers/hexagon_types.h b/clang/lib/Headers/hexagon_types.h
index 8e73fad4bcd42..54e8c1dd6937b 100644
--- a/clang/lib/Headers/hexagon_types.h
+++ b/clang/lib/Headers/hexagon_types.h
@@ -11,6 +11,11 @@
 
 #include <hexagon_protos.h>
 
+// Save and undefine B0 to avoid conflicts with POSIX termios.h which
+// defines B0 as a macro for baud rate 0.
+#pragma push_macro("B0")
+#undef B0
+
 /* Hexagon names */
 #define HEXAGON_Vect HEXAGON_Vect64
 #define HEXAGON_V_GET_D HEXAGON_V64_GET_D
@@ -697,9 +702,8 @@ class HEXAGON_Vect64C {
   };
 
   // Extract byte methods
-  signed char B0(void) {
-    return HEXAGON_V64_GET_B0(data);
-  };
+  signed char b0(void) { return HEXAGON_V64_GET_B0(data); };
+  signed char B0(void) { return b0(); };
   signed char B1(void) {
     return HEXAGON_V64_GET_B1(data);
   };
@@ -776,9 +780,10 @@ class HEXAGON_Vect64C {
   };
 
   // Set byte methods
-  HEXAGON_Vect64C B0(signed char b) {
+  HEXAGON_Vect64C b0(signed char b) {
     return HEXAGON_Vect64C(HEXAGON_V64_PUT_B0(data, b));
   };
+  HEXAGON_Vect64C B0(signed char b) { return b0(b); };
   HEXAGON_Vect64C B1(signed char b) {
     return HEXAGON_Vect64C(HEXAGON_V64_PUT_B1(data, b));
   };
@@ -1121,9 +1126,8 @@ class HEXAGON_Vect32C {
   };
 
   // Extract byte methods
-  signed char B0(void) {
-    return HEXAGON_V32_GET_B0(data);
-  };
+  signed char b0(void) { return HEXAGON_V32_GET_B0(data); };
+  signed char B0(void) { return b0(); };
   signed char B1(void) {
     return HEXAGON_V32_GET_B1(data);
   };
@@ -1162,9 +1166,10 @@ class HEXAGON_Vect32C {
   };
 
   // Set byte methods
-  HEXAGON_Vect32C B0(signed char b) {
+  HEXAGON_Vect32C b0(signed char b) {
     return HEXAGON_Vect32C(HEXAGON_V32_PUT_B0(data, b));
   };
+  HEXAGON_Vect32C B0(signed char b) { return b0(b); };
   HEXAGON_Vect32C B1(signed char b) {
     return HEXAGON_Vect32C(HEXAGON_V32_PUT_B1(data, b));
   };
@@ -1924,9 +1929,8 @@ class Q6Vect64C {
   };
 
   // Extract byte methods
-  signed char B0(void) {
-    return Q6V64_GET_B0(data);
-  };
+  signed char b0(void) { return Q6V64_GET_B0(data); };
+  signed char B0(void) { return b0(); };
   signed char B1(void) {
     return Q6V64_GET_B1(data);
   };
@@ -2003,9 +2007,8 @@ class Q6Vect64C {
   };
 
   // Set byte methods
-  Q6Vect64C B0(signed char b) {
-    return Q6Vect64C(Q6V64_PUT_B0(data, b));
-  };
+  Q6Vect64C b0(signed char b) { return Q6Vect64C(Q6V64_PUT_B0(data, b)); };
+  Q6Vect64C B0(signed char b) { return b0(b); };
   Q6Vect64C B1(signed char b) {
     return Q6Vect64C(Q6V64_PUT_B1(data, b));
   };
@@ -2348,9 +2351,8 @@ class Q6Vect32C {
   };
 
   // Extract byte methods
-  signed char B0(void) {
-    return Q6V32_GET_B0(data);
-  };
+  signed char b0(void) { return Q6V32_GET_B0(data); };
+  signed char B0(void) { return b0(); };
   signed char B1(void) {
     return Q6V32_GET_B1(data);
   };
@@ -2389,9 +2391,8 @@ class Q6Vect32C {
   };
 
   // Set byte methods
-  Q6Vect32C B0(signed char b) {
-    return Q6Vect32C(Q6V32_PUT_B0(data, b));
-  };
+  Q6Vect32C b0(signed char b) { return Q6Vect32C(Q6V32_PUT_B0(data, b)); };
+  Q6Vect32C B0(signed char b) { return b0(b); };
   Q6Vect32C B1(signed char b) {
     return Q6Vect32C(Q6V32_PUT_B1(data, b));
   };
@@ -2622,4 +2623,6 @@ typedef struct hexagon_udma_descriptor_type1_s
     unsigned int dstwidthoffset:16;
 } hexagon_udma_descriptor_type1_t;
 
+#pragma pop_macro("B0")
+
 #endif /* !HEXAGON_TYPES_H */
diff --git a/clang/test/Headers/hexagon-types-b0-conflict.c b/clang/test/Headers/hexagon-types-b0-conflict.c
new file mode 100644
index 0000000000000..a27712ab9d3a2
--- /dev/null
+++ b/clang/test/Headers/hexagon-types-b0-conflict.c
@@ -0,0 +1,38 @@
+// REQUIRES: hexagon-registered-target
+
+// Verify that hexagon_types.h can be included after a B0 macro definition
+// without conflicts, that B0 is restored afterward, and that the
+// lowercase b0() alias is usable even while B0 is still a macro.
+
+// RUN: %clang_cc1 -fsyntax-only -internal-isystem %S/../../lib/Headers/ \
+// RUN:   -target-cpu hexagonv68 -triple hexagon-unknown-linux-musl \
+// RUN:   -verify %s
+
+// RUN: %clang_cc1 -fsyntax-only -internal-isystem %S/../../lib/Headers/ \
+// RUN:   -target-cpu hexagonv68 -triple hexagon-unknown-linux-musl \
+// RUN:   -x c++ -verify %s
+
+// expected-no-diagnostics
+
+// Simulate the POSIX termios.h B0 macro definition.
+#define B0 0000000
+
+#include <hexagon_types.h>
+
+// Verify B0 is restored after including hexagon_types.h.
+#ifndef B0
+#error "B0 should be defined after including hexagon_types.h"
+#endif
+
+_Static_assert(B0 == 0, "B0 should still be 0 after including hexagon_types.h");
+
+// In C++ mode, verify the lowercase b0() alias works even with B0 defined.
+#ifdef __cplusplus
+void test_b0_alias(void) {
+  HEXAGON_Vect64C v(0x0807060504030201LL);
+  signed char got = v.b0();
+  (void)got;
+  HEXAGON_Vect64C v2 = v.b0(0x42);
+  (void)v2;
+}
+#endif



More information about the llvm-branch-commits mailing list