Author: David Tenty
Date: 2026-03-19T23:13:38-04:00
New Revision: caf079fbb2f72d4c3af20720e43bcf73d306ca6f

URL: 
https://github.com/llvm/llvm-project/commit/caf079fbb2f72d4c3af20720e43bcf73d306ca6f
DIFF: 
https://github.com/llvm/llvm-project/commit/caf079fbb2f72d4c3af20720e43bcf73d306ca6f.diff

LOG: [clang][headers][endian.h] add some common extensions (#187565)

This PR adds two common extensions to `endian.h` found in some
implementations which we've encountered usages of in existing code (which
are allowable under POSIX), namely:

- double underscore versions of the byte order macros
- PDP endianness macros  

(note: we don't attempt to implement the actual conversion helpers for
PDP endianness, as LLVM doesn't support these targeting platforms with
this endianness, we just provide the macro so code checking against them
on other targets will compile)

Added: 
    

Modified: 
    clang/lib/Headers/endian.h
    clang/test/Headers/endian.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Headers/endian.h b/clang/lib/Headers/endian.h
index b2ad3c16710d7..aa30d8fc5c49b 100644
--- a/clang/lib/Headers/endian.h
+++ b/clang/lib/Headers/endian.h
@@ -25,8 +25,23 @@
 
 #define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
 #define BIG_ENDIAN __ORDER_BIG_ENDIAN__
+#define PDP_ENDIAN __ORDER_PDP_ENDIAN__
 #define BYTE_ORDER __BYTE_ORDER__
 
+// Define some compatibility macros if they are not defined.
+#ifndef __BYTE_ORDER
+#define __BYTE_ORDER BYTE_ORDER
+#endif
+#ifndef __LITTLE_ENDIAN
+#define __LITTLE_ENDIAN LITTLE_ENDIAN
+#endif
+#ifndef __BIG_ENDIAN
+#define __BIG_ENDIAN BIG_ENDIAN
+#endif
+#ifndef __PDP_ENDIAN
+#define __PDP_ENDIAN PDP_ENDIAN
+#endif
+
 #if BYTE_ORDER == LITTLE_ENDIAN
 
 #define htobe16(x)                                                             
\
@@ -48,7 +63,7 @@
 #define le32toh(x) __CLANG_ENDIAN_CAST(static_cast, uint32_t, x)
 #define le64toh(x) __CLANG_ENDIAN_CAST(static_cast, uint64_t, x)
 
-#else
+#elif BYTE_ORDER == BIG_ENDIAN
 
 #define htobe16(x) __CLANG_ENDIAN_CAST(static_cast, uint16_t, x)
 #define htobe32(x) __CLANG_ENDIAN_CAST(static_cast, uint32_t, x)
@@ -69,6 +84,8 @@
 #define le64toh(x)                                                             
\
   __builtin_bswap64(__CLANG_ENDIAN_CAST(static_cast, uint64_t, x))
 
+#else
+#error "Unsupported endianness"
 #endif
 #endif // __has_include_next
 #endif // __CLANG_ENDIAN_H

diff  --git a/clang/test/Headers/endian.c b/clang/test/Headers/endian.c
index 0702d8616ca38..5ba341f2096be 100644
--- a/clang/test/Headers/endian.c
+++ b/clang/test/Headers/endian.c
@@ -6,6 +6,9 @@
 
 #include <endian.h>
 
+_Static_assert(__LITTLE_ENDIAN == __ORDER_LITTLE_ENDIAN__, "");
+_Static_assert(__BIG_ENDIAN == __ORDER_BIG_ENDIAN__, "");
+_Static_assert(__PDP_ENDIAN == __ORDER_PDP_ENDIAN__, "");
 
 #if BYTE_ORDER == BIG_ENDIAN
 


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to