diff mbox

[2/3] teach sparse about __{BIG,LITTLE}_ENDIAN__

Message ID 20170620201931.92864-3-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show

Commit Message

Luc Van Oostenryck June 20, 2017, 8:19 p.m. UTC
Some macros, structures definitions, ... depends on the
endianness. This is generaly done via some header file
but these headers need information from the compiler via
the macros __{BIG,LITTLE}_ENDIAN__.

Let sparse predefine these macros like compilers do.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 lib.c                      |  3 +++
 validation/endian-big.c    | 11 +++++++++++
 validation/endian-little.c | 11 +++++++++++
 3 files changed, 25 insertions(+)
 create mode 100644 validation/endian-big.c
 create mode 100644 validation/endian-little.c
diff mbox

Patch

diff --git a/lib.c b/lib.c
index a22635ecc..daf7d0219 100644
--- a/lib.c
+++ b/lib.c
@@ -961,6 +961,9 @@  static void predefined_macros(void)
 	predefined_sizeof("FLOAT", bits_in_float);
 	predefined_sizeof("DOUBLE", bits_in_double);
 	predefined_sizeof("LONG_DOUBLE", bits_in_longdouble);
+
+	add_pre_buffer("#weak_define __%s_ENDIAN__ 1\n",
+		arch_big_endian ? "BIG" : "LITTLE");
 }
 
 void declare_builtin_functions(void)
diff --git a/validation/endian-big.c b/validation/endian-big.c
new file mode 100644
index 000000000..d535748cd
--- /dev/null
+++ b/validation/endian-big.c
@@ -0,0 +1,11 @@ 
+#if defined(__LITTLE_ENDIAN__)
+#error "__LITTLE_ENDIAN__ defined!"
+#endif
+#if (__BIG_ENDIAN__ != 1)
+#error "__BIG_ENDIAN__ not correctly defined!"
+#endif
+
+/*
+ * check-name: endian-big.c
+ * check-command: sparse -mbig-endian $file
+ */
diff --git a/validation/endian-little.c b/validation/endian-little.c
new file mode 100644
index 000000000..cc4a14f73
--- /dev/null
+++ b/validation/endian-little.c
@@ -0,0 +1,11 @@ 
+#if defined(__BIG_ENDIAN__)
+#error "__BIG_ENDIAN__ defined!"
+#endif
+#if (__LITTLE_ENDIAN__ != 1)
+#error "__LITTLE_ENDIAN__ not correctly defined!"
+#endif
+
+/*
+ * check-name: endian-little.c
+ * check-command: sparse -mlittle-endian $file
+ */