Skip to contentSkip to conversation

Patch series: pre-defines update

PATCH New [email protected] 1 participant 9 messages Jan 23, 2020

lore
Share & tools
lore
#0 lore raw

This set is based on merged sparse patches that add some necessary pre-defines to get close to what GCC defines by default. I've had to modify the patches as smatch is lacking lots of sparse changes, so they're mostly authored by both Luc and myself.

#1 lore raw

PATCH 1/8 allow to test the standard version outside of lib.c

From: Luc Van Oostenryck <[email protected]>

Since semantics and supported features can differ between standard version we may need the supported version.

Allow this by moving the variable 'standard' and the corresponding enum definition to lib.h

---
 lib.c |  8 +-------
 lib.h | 11 +++++++++++
 2 files changed, 12 insertions(+), 7 deletions(-)
lib.c +1 -7
index 569a1533..605e4bb6 100644
@@ -318,13 +318,7 @@ int funsigned_char = UNSIGNED_CHAR;
 
 int preprocess_only;
 
-static enum { STANDARD_C89,
-              STANDARD_C94,
-              STANDARD_C99,
-              STANDARD_C11,
-              STANDARD_GNU11,
-              STANDARD_GNU89,
-              STANDARD_GNU99, } standard = STANDARD_GNU89;
+enum standard standard = STANDARD_GNU89;
 
 int arch_m64 = ARCH_M64_DEFAULT;
 int arch_msize_long = 0;
lib.h +11 -0
index de8f1132..ed2c866a 100644
@@ -206,6 +206,17 @@ extern int arch_msize_long;
 extern int arch_big_endian;
 extern int arch_mach;
 
+enum standard {
+	STANDARD_C89,
+	STANDARD_C94,
+	STANDARD_C99,
+	STANDARD_C11,
+	STANDARD_GNU11,
+	STANDARD_GNU89,
+	STANDARD_GNU99,
+};
+extern enum standard standard;
+
 extern void dump_macro_definitions(void);
 extern struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **files);
 extern struct symbol_list *__sparse(char *filename);
Signature · 1 line
2.23.0
#2 lore raw

PATCH 2/8 alt definition for STANDARD_GNU89 & friends

In reply to [email protected]

From: Luc Van Oostenryck <[email protected]>

It may be useful to known the base standard and if we're using the gnu extensions but as these are defined it can only be done on a case-by-case basis.

Change these defines so that:
* the GNU extensions is the least significant bit
* the versions can be easily compared with <, >, <= and >=

---
 lib.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
lib.h +6 -3
index ed2c866a..44e2b5c5 100644
@@ -207,13 +207,16 @@ extern int arch_big_endian;
 extern int arch_mach;
 
 enum standard {
+	STANDARD_NONE,
+	STANDARD_GNU,
 	STANDARD_C89,
+	STANDARD_GNU89 = STANDARD_C89 | STANDARD_GNU,
 	STANDARD_C94,
+	STANDARD_GNU94 = STANDARD_C94 | STANDARD_GNU,
 	STANDARD_C99,
+	STANDARD_GNU99 = STANDARD_C99 | STANDARD_GNU,
 	STANDARD_C11,
-	STANDARD_GNU11,
-	STANDARD_GNU89,
-	STANDARD_GNU99,
+	STANDARD_GNU11 = STANDARD_C11 | STANDARD_GNU,
 };
 extern enum standard standard;
 
Signature · 1 line
2.23.0
#3 lore raw

PATCH 3/8 add predefine_nostd()

In reply to [email protected]

From: Luc Van Oostenryck <[email protected]>

GCC adds predefines for some symbols lying in the user's namespace, like "linux" or "sparc", but only if the selected dialect is not one of the standard ones.

Add an helper, predefine_nostd(), for these.

---
 lib.h         | 1 +
 pre-process.c | 8 ++++++++
 2 files changed, 9 insertions(+)
lib.h +1 -0
index 44e2b5c5..70b2ba39 100644
@@ -134,6 +134,7 @@ enum phase {
 
 extern void add_pre_buffer(const char *fmt, ...) FORMAT_ATTR(1);
 extern void predefine(const char *name, int weak, const char *fmt, ...) FORMAT_ATTR(3);
+extern void predefine_nostd(const char *name);
 
 extern int preprocess_only;
 
pre-process.c +8 -0
index c35cbcfb..5a9313a3 100644
@@ -1568,6 +1568,14 @@ void predefine(const char *name, int weak, const char *fmt, ...)
 	do_define(value->pos, NULL, ident, NULL, value, attr);
 }
 
+///
+// like predefine() but only if one of the non-standard dialect is chosen
+void predefine_nostd(const char *name)
+{
+	if ((standard & STANDARD_GNU) || (standard == STANDARD_NONE))
+		predefine(name, 1, "1");
+}
+
 static int do_handle_define(struct stream *stream, struct token **line, struct token *token, int attr)
 {
 	struct token *arglist, *expansion;
Signature · 1 line
2.23.0
#4 lore raw

PATCH 4/8 define __PRAGMA_REDEFINE_EXTNAME

In reply to [email protected]

From: John Levon <[email protected]>

As per:

https://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Symbol_002dRenaming-Pragmas.html

we should set this define.

---
 lib.c | 2 ++
 1 file changed, 2 insertions(+)
lib.c +2 -0
index 605e4bb6..7da06530 100644
@@ -1303,6 +1303,8 @@ static void predefined_macros(void)
 	if (optimize_size)
 		predefine("__OPTIMIZE_SIZE__", 0, "1");
 
+	predefine("__PRAGMA_REDEFINE_EXTNAME", 1, "1");
+
 	// Temporary hacks
 	predefine("__extension__", 0, NULL);
 	predefine("__pragma__", 0, NULL);
Signature · 1 line
2.23.0
#5 lore raw

PATCH 5/8 add necessary defined for sunos-derived systems

In reply to [email protected]

From: John Levon <[email protected]>

---
 lib.c | 8 ++++++++
 1 file changed, 8 insertions(+)
lib.c +8 -0
index 7da06530..c85ce247 100644
@@ -1393,6 +1393,14 @@ static void predefined_macros(void)
 		predefine("__i386", 1, "1");
 		break;
 	}
+
+#if defined(__sun__) || defined(__sun)
+	predefine("__sun__", 1, "1");
+	predefine("__sun", 1, "1");
+	predefine_nostd("sun");
+	predefine("__svr4__", 1, "1");
+#endif
+
 }
 
 static void create_builtin_stream(void)
Signature · 1 line
2.23.0
#6 lore raw

PATCH 6/8 pre-define __unix__ and friends

In reply to [email protected]

From: John Levon <[email protected]>

GCC defines these, so should we.

---
 lib.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib.c +7 -0
index c85ce247..40e81ae2 100644
@@ -1394,6 +1394,13 @@ static void predefined_macros(void)
 		break;
 	}
 
+#if defined(__unix__)
+	predefine("__unix__", 1, "1");
+	predefine("__unix", 1, "1");
+	predefine_nostd("unix");
+#endif
+
+
 #if defined(__sun__) || defined(__sun)
 	predefine("__sun__", 1, "1");
 	predefine("__sun", 1, "1");
Signature · 1 line
2.23.0
#7 lore raw

PATCH 7/8 correct sparcv9 defines

In reply to [email protected]

From: Toomas Soome <[email protected]>

The SPARCV9 compile check needs to look for __sparcv9 on some systems, and should also define "sparc".

---
 lib.c     | 1 +
 machine.h | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
lib.c +1 -0
index 40e81ae2..3e9ad866 100644
@@ -1380,6 +1380,7 @@ static void predefined_macros(void)
 	case MACH_SPARC32:
 		predefine("__sparc__", 1, "1");
 		predefine("__sparc", 1, "1");
+		predefine_nostd("sparc");
 		break;
 	case MACH_X86_64:
 		if (arch_m64 != ARCH_LP32) {
machine.h +1 -1
index b46383ac..af5c96f1 100644
@@ -62,7 +62,7 @@ enum machine {
 #define	MACH_NATIVE	MACH_RISCV64
 #elif defined(__riscv) && (__riscv_xlen == 32)
 #define	MACH_NATIVE	MACH_RISCV32
-#elif defined(__sparc_v9__)
+#elif defined(__sparc_v9__) || defined(__sparcv9)
 #define	MACH_NATIVE	MACH_SPARC64
 #elif defined(__sparc__) || defined(__sparc)
 #define	MACH_NATIVE	MACH_SPARC32
Signature · 1 line
2.23.0