diff mbox series

[1/8] arch: fix the signedness of plain chars

Message ID 20191115011355.53495-2-luc.vanoostenryck@gmail.com (mailing list archive)
State Superseded, archived
Headers show
Series Miscellaneous arch specific fixes | expand

Commit Message

Luc Van Oostenryck Nov. 15, 2019, 1:13 a.m. UTC
Some architectures, like ARM or PPC, use 'unsigned' for
plain chars while others, like the Intel's, use signed ones.

Sparse understands -funsigned-char but by default uses the
native signedness.

Fix this by setting the proper signedness of plain chars
for the archs that Sparse know about.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 lib.c                             |  2 +-
 machine.h                         |  6 ------
 target.c                          | 18 ++++++++++++++++++
 validation/char-signed-native.c   |  9 +++++++++
 validation/char-unsigned-native.c | 11 +++++++++++
 validation/char-unsigned.c        |  2 +-
 6 files changed, 40 insertions(+), 8 deletions(-)
 create mode 100644 validation/char-signed-native.c
 create mode 100644 validation/char-unsigned-native.c
diff mbox series

Patch

diff --git a/lib.c b/lib.c
index 332eebebb..350d881a9 100644
--- a/lib.c
+++ b/lib.c
@@ -313,7 +313,7 @@  unsigned long long fmemcpy_max_count = 100000;
 unsigned long fpasses = ~0UL;
 int fpic = 0;
 int fpie = 0;
-int funsigned_char = UNSIGNED_CHAR;
+int funsigned_char = -1;
 
 int preprocess_only;
 
diff --git a/machine.h b/machine.h
index e24822a7f..22b05d91a 100644
--- a/machine.h
+++ b/machine.h
@@ -70,10 +70,4 @@  enum machine {
 #define MACH_NATIVE	MACH_UNKNOWN
 #endif
 
-#if defined(__CHAR_UNSIGNED__)
-#define	UNSIGNED_CHAR	1
-#else
-#define UNSIGNED_CHAR	0
-#endif
-
 #endif
diff --git a/target.c b/target.c
index 90097818f..497ecdc5e 100644
--- a/target.c
+++ b/target.c
@@ -137,6 +137,24 @@  void init_target(void)
 		break;
 	}
 
+	switch (arch_mach) {
+	case MACH_ARM:
+	case MACH_ARM64:
+	case MACH_PPC32:
+	case MACH_PPC64:
+	case MACH_RISCV32:
+	case MACH_RISCV64:
+	case MACH_S390:
+	case MACH_S390X:
+		if (funsigned_char == -1)
+			funsigned_char = 1;
+		break;
+	default:
+		if (funsigned_char == -1)
+			funsigned_char = 0;
+		break;
+	}
+
 	switch (arch_m64) {
 	case ARCH_X32:
 		max_int_alignment = 8;
diff --git a/validation/char-signed-native.c b/validation/char-signed-native.c
new file mode 100644
index 000000000..5185fce9e
--- /dev/null
+++ b/validation/char-signed-native.c
@@ -0,0 +1,9 @@ 
+void foo(void)
+{
+	_Static_assert((char) -1 == -1, "plain char is not signed");
+}
+
+/*
+ * check-name: char-signed-native
+ * check-command: sparse --arch=i386 -Wno-decl $file
+ */
diff --git a/validation/char-unsigned-native.c b/validation/char-unsigned-native.c
new file mode 100644
index 000000000..b86458427
--- /dev/null
+++ b/validation/char-unsigned-native.c
@@ -0,0 +1,11 @@ 
+#define	MASK ((1 << __CHAR_BIT__) - 1)
+
+void foo(void)
+{
+	_Static_assert((char) -1 == (-1 & MASK), "plain char is not unsigned");
+}
+
+/*
+ * check-name: char-unsigned-native
+ * check-command: sparse --arch=arm -Wno-decl $file
+ */
diff --git a/validation/char-unsigned.c b/validation/char-unsigned.c
index 19cadbda3..354aa40d6 100644
--- a/validation/char-unsigned.c
+++ b/validation/char-unsigned.c
@@ -6,6 +6,6 @@  void foo(void)
 }
 
 /*
- * check-name: fsigned-char
+ * check-name: funsigned-char
  * check-command: sparse -funsigned-char -Wno-decl $file
  */