small cleanup of built-in macros

Zack Weinberg zack@wolery.cumb.org
Sun Apr 9 20:26:00 GMT 2000


This patch cleans up the built-in macro code a bit.

zw

	* cpphash.c (timestamp): Delete.
	(del_HASHNODE): If type is T_MCONST, free value.cpval.
	(special_symbol): Remove unnecessary braces.  Remove
	T_VERSION.  Treat T_STDC like T_CONST unless
	STDC_0_IN_SYSTEM_HEADERS.  Render both __DATE__ and __TIME__
	when one is encountered, then convert them into T_MCONST
	nodes.
	* cppinit.c (builtin_array): version_string is T_MCONST.
	__STDC__ has a "1" in its cpval.  Don't have a terminator
	entry.  Clean up which entries are dumped.
	(initialize_builtins): Only __STDC__ gets the special
	-traditional treatment.  Count the length of builtin_array.
	Render version_string here.
	* cpphash.h: Remove T_VERSION.  Add T_MCONST.
	* cpplib.h (struct cpp_reader): Remove timebuf.	

===================================================================
Index: cpphash.c
--- cpphash.c	2000/04/06 07:56:14	1.63
+++ cpphash.c	2000/04/10 03:20:59
@@ -42,7 +42,6 @@ static void push_macro_expansion PARAMS 
 static int unsafe_chars		 PARAMS ((cpp_reader *, int, int));
 static int macro_cleanup	 PARAMS ((cpp_buffer *, cpp_reader *));
 static enum cpp_token macarg	 PARAMS ((cpp_reader *, int));
-static struct tm *timestamp	 PARAMS ((cpp_reader *));
 static void special_symbol	 PARAMS ((HASHNODE *, cpp_reader *));
 
 /* Initial hash table size.  (It can grow if necessary - see hashtab.c.)  */
@@ -141,6 +140,8 @@ del_HASHNODE (x)
   
   if (h->type == T_MACRO)
     _cpp_free_definition (h->value.defn);
+  else if (h->type == T_MCONST)
+    free ((void *) h->value.cpval);
   free ((void *) h->name);
   free (h);
 }
@@ -794,18 +795,6 @@ macarg (pfile, rest_args)
 }
 
 
-static struct tm *
-timestamp (pfile)
-     cpp_reader *pfile;
-{
-  if (!pfile->timebuf)
-    {
-      time_t t = time ((time_t *) 0);
-      pfile->timebuf = localtime (&t);
-    }
-  return pfile->timebuf;
-}
-
 static const char * const monthnames[] =
 {
   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
@@ -854,6 +843,7 @@ _cpp_quote_string (pfile, src)
  * buffer *without* rescanning.
  */
 
+#define DSC(str) (const U_CHAR *)str, sizeof str - 1
 static void
 special_symbol (hp, pfile)
      HASHNODE *hp;
@@ -867,22 +857,15 @@ special_symbol (hp, pfile)
     {
     case T_FILE:
     case T_BASE_FILE:
-      {
-	ip = cpp_file_buffer (pfile);
-	if (hp->type == T_BASE_FILE)
-	  {
-	    while (CPP_PREV_BUFFER (ip) != NULL)
-	      ip = CPP_PREV_BUFFER (ip);
-	  }
-
-	buf = ip->nominal_fname;
-
-	if (!buf)
-	  buf = "";
-	CPP_RESERVE (pfile, 3 + 4 * strlen (buf));
-	_cpp_quote_string (pfile, buf);
-	return;
-      }
+      ip = cpp_file_buffer (pfile);
+      if (hp->type == T_BASE_FILE)
+	while (CPP_PREV_BUFFER (ip) != NULL)
+	  ip = CPP_PREV_BUFFER (ip);
+
+      buf = ip->nominal_fname;
+      CPP_RESERVE (pfile, 3 + 4 * strlen (buf));
+      _cpp_quote_string (pfile, buf);
+      return;
 
     case T_INCLUDE_LEVEL:
       {
@@ -896,17 +879,22 @@ special_symbol (hp, pfile)
 	CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
 	return;
       }
-
-    case T_VERSION:
-      len = strlen (hp->value.cpval);
-      CPP_RESERVE (pfile, 3 + len);
-      CPP_PUTC_Q (pfile, '"');
-      CPP_PUTS_Q (pfile, hp->value.cpval, len);
-      CPP_PUTC_Q (pfile, '"');
-      CPP_NUL_TERMINATE_Q (pfile);
-      return;
 
+    case T_STDC:
+#ifdef STDC_0_IN_SYSTEM_HEADERS
+      ip = cpp_file_buffer (pfile);
+      if (ip->system_header_p && !cpp_defined (pfile, DSC("__STRICT_ANSI__")))
+	{
+	  CPP_RESERVE (pfile, 2);
+	  CPP_PUTC_Q (pfile, '0');
+	  CPP_NUL_TERMINATE_Q (pfile);
+	  return;
+	}
+#endif
+      /* else fall through */
     case T_CONST:
+    case T_MCONST:
+    constant:
       buf = hp->value.cpval;
       if (!buf)
 	return;
@@ -919,19 +907,6 @@ special_symbol (hp, pfile)
       CPP_NUL_TERMINATE_Q (pfile);
       return;
 
-    case T_STDC:
-      CPP_RESERVE (pfile, 2);
-#ifdef STDC_0_IN_SYSTEM_HEADERS
-      ip = cpp_file_buffer (pfile);
-      if (ip->system_header_p
-	  && !cpp_defined (pfile, (const U_CHAR *) "__STRICT_ANSI__", 15))
-	CPP_PUTC_Q (pfile, '0');
-      else
-#endif
-	CPP_PUTC_Q (pfile, '1');
-      CPP_NUL_TERMINATE_Q (pfile);
-      return;
-
     case T_SPECLINE:
       {
 	long line;
@@ -945,21 +920,31 @@ special_symbol (hp, pfile)
 
     case T_DATE:
     case T_TIME:
+      /* Generate both __DATE__ and __TIME__, stuff them into their
+	 respective hash nodes, and mark the nodes T_MCONST so we
+	 don't have to do this again.  We don't generate these strings
+	 at init time because time() and localtime() are very slow on
+	 some systems.  */
       {
-	struct tm *timebuf;
+	time_t tt = time (NULL);
+	struct tm *tb = localtime (&tt);
+	HASHNODE *d, *t;
 
-	CPP_RESERVE (pfile, 20);
-	timebuf = timestamp (pfile);
 	if (hp->type == T_DATE)
-	  sprintf (CPP_PWRITTEN (pfile), "\"%s %2d %4d\"",
-		   monthnames[timebuf->tm_mon],
-		   timebuf->tm_mday, timebuf->tm_year + 1900);
+	  d = hp, t = _cpp_lookup (pfile, DSC("__TIME__"));
 	else
-	  sprintf (CPP_PWRITTEN (pfile), "\"%02d:%02d:%02d\"",
-		   timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
+	  t = hp, d = _cpp_lookup (pfile, DSC("__DATE__"));
 
-	CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
-	return;
+	d->value.cpval = xmalloc (sizeof "'Oct 11 1347'");
+	sprintf ((char *)d->value.cpval, "\"%s %2d %4d\"",
+		 monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
+	d->type = T_MCONST;
+
+	t->value.cpval = xmalloc (sizeof "'12:34:56'");
+	sprintf ((char *)t->value.cpval, "\"%02d:%02d:%02d\"",
+		 tb->tm_hour, tb->tm_min, tb->tm_sec);
+	t->type = T_MCONST;
+	goto constant;
       }
 
     case T_POISON:
@@ -974,6 +959,7 @@ special_symbol (hp, pfile)
       return;
     }
 }
+#undef DSC
 
 /* Expand a macro call.
    HP points to the symbol that is the macro being called.
===================================================================
Index: cppinit.c
--- cppinit.c	2000/04/08 04:00:01	1.72
+++ cppinit.c	2000/04/10 03:20:59
@@ -588,9 +588,8 @@ cpp_cleanup (pfile)
    be entered in the macro hash table under the name NAME, with value
    VALUE (if any).  FLAGS tweaks the behavior a little:
    DUMP		write debug info for this macro
-   STDC		define only if not -traditional
-   ULP		value is the global user_label_prefix (which can't be
-		put directly into the table).
+   VERS		value is the global version_string, quoted
+   ULP		value is the global user_label_prefix
  */
 
 struct builtin
@@ -601,35 +600,35 @@ struct builtin
   unsigned short flags;
 };
 #define DUMP 0x01
-#define STDC 0x02
-#define VERS 0x04
-#define ULP  0x08
+#define VERS 0x02
+#define ULP  0x04
 
 static const struct builtin builtin_array[] =
 {
-  { "__TIME__",			0, T_TIME,		DUMP },
-  { "__DATE__",			0, T_DATE,		DUMP },
-  { "__FILE__",			0, T_FILE,		0    },
-  { "__BASE_FILE__",		0, T_BASE_FILE,		0    },
-  { "__LINE__",			0, T_SPECLINE,		0    },
-  { "__INCLUDE_LEVEL__",	0, T_INCLUDE_LEVEL,	0    },
-  { "__VERSION__",		0, T_VERSION,		DUMP|VERS },
-  { "__STDC__",			0, T_STDC,		DUMP|STDC },
-
-  { "__USER_LABEL_PREFIX__",	0,		 T_CONST, ULP  },
-  { "__REGISTER_PREFIX__",	REGISTER_PREFIX, T_CONST, 0    },
-  { "__HAVE_BUILTIN_SETJMP__",	"1",		 T_CONST, 0    },
+  { "__TIME__",			0, T_TIME,		0 },
+  { "__DATE__",			0, T_DATE,		0 },
+  { "__FILE__",			0, T_FILE,		0 },
+  { "__BASE_FILE__",		0, T_BASE_FILE,		0 },
+  { "__LINE__",			0, T_SPECLINE,		0 },
+  { "__INCLUDE_LEVEL__",	0, T_INCLUDE_LEVEL,	0 },
+
+  { "__VERSION__",		0,		 T_MCONST, DUMP|VERS },
+  { "__USER_LABEL_PREFIX__",	0,		 T_CONST,  DUMP|ULP  },
+  { "__STDC__",			"1",		 T_STDC,   DUMP },
+  { "__REGISTER_PREFIX__",	REGISTER_PREFIX, T_CONST,  DUMP },
+  { "__HAVE_BUILTIN_SETJMP__",	"1",		 T_CONST,  DUMP },
 #ifndef NO_BUILTIN_SIZE_TYPE
-  { "__SIZE_TYPE__",		SIZE_TYPE,	 T_CONST, DUMP },
+  { "__SIZE_TYPE__",		SIZE_TYPE,	 T_CONST,  DUMP },
 #endif
 #ifndef NO_BUILTIN_PTRDIFF_TYPE
-  { "__PTRDIFF_TYPE__",		PTRDIFF_TYPE,	 T_CONST, DUMP },
+  { "__PTRDIFF_TYPE__",		PTRDIFF_TYPE,	 T_CONST,  DUMP },
 #endif
 #ifndef NO_BUILTIN_WCHAR_TYPE
-  { "__WCHAR_TYPE__",		WCHAR_TYPE,	 T_CONST, DUMP },
+  { "__WCHAR_TYPE__",		WCHAR_TYPE,	 T_CONST,  DUMP },
 #endif
-  { 0, 0, 0, 0 }
 };
+#define builtin_array_end \
+ builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
 
 /* Subroutine of cpp_start_read; reads the builtins table above and
    enters the macros into the hash table.  */
@@ -641,15 +640,18 @@ initialize_builtins (pfile)
   const struct builtin *b;
   const char *val;
   HASHNODE *hp;
-  for(b = builtin_array; b->name; b++)
+  for(b = builtin_array; b < builtin_array_end; b++)
     {
-      if ((b->flags & STDC) && CPP_TRADITIONAL (pfile))
+      if (b->type == T_STDC && CPP_TRADITIONAL (pfile))
 	continue;
 
       if (b->flags & ULP)
 	val = user_label_prefix;
       else if (b->flags & VERS)
-	val = version_string;
+	{
+	  val = xmalloc (strlen (version_string) + 3);
+	  sprintf ((char *)val, "\"%s\"", version_string);
+	}
       else
 	val = b->value;
 
@@ -662,7 +664,6 @@ initialize_builtins (pfile)
       if ((b->flags & DUMP) && CPP_OPTION (pfile, debug_output))
 	dump_special_to_buffer (pfile, b->name);
     }
-
 }
 #undef DUMP
 #undef STDC
===================================================================
Index: cpphash.h
--- cpphash.h	2000/04/06 07:56:14	1.27
+++ cpphash.h	2000/04/10 03:20:59
@@ -91,10 +91,10 @@ enum node_type
   T_FILE,	   /* `__FILE__' */
   T_BASE_FILE,	   /* `__BASE_FILE__' */
   T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
-  T_VERSION,	   /* `__VERSION__' */
   T_TIME,	   /* `__TIME__' */
   T_STDC,	   /* `__STDC__' */
   T_CONST,	   /* Constant string, used by `__SIZE_TYPE__' etc */
+  T_MCONST,	   /* Ditto, but the string is malloced memory */
   T_MACRO,	   /* macro defined by `#define' */
   T_DISABLED,	   /* macro temporarily turned off for rescan */
   T_POISON,	   /* macro defined with `#pragma poison' */
===================================================================
Index: cpplib.h
--- cpplib.h	2000/04/06 07:56:14	1.75
+++ cpplib.h	2000/04/10 03:20:59
@@ -352,8 +352,6 @@ struct cpp_reader
 
   long lineno;
 
-  struct tm *timebuf;
-
   /* Buffer of -M output.  */
   struct deps *deps;
 


More information about the Gcc-patches mailing list