cpplib: Command line assertions made easier

Neil Booth NeilB@earthling.net
Tue Oct 31 12:47:00 GMT 2000


Zack suggested this back in April I think.  This allows specifying
assertions on the command line, like

  -A predicate=answer

rather than

  -A predicate(answer)

(though the latter is still allowed).  The former does not fall afoul
of shell special characters.

Includes doc update and test.

How to get GCC to use this now?  Having to quote the output of -v is
a royal pain it would be nice to relieve ourselves of permanently.

Neil.

	* cpp.texi: Update for new command line -A syntax.
	* cpplib.c (cpp_define): Simplify a bit and no '\n' needed.
	(cpp_assert): Accept new command line syntax with '='.
	* testsuite/gcc.dg/cpp/assert3.c: New test.

Index: cpp.texi
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpp.texi,v
retrieving revision 1.34
diff -u -p -r1.34 cpp.texi
--- cpp.texi	2000/10/27 23:50:15	1.34
+++ cpp.texi	2000/10/31 20:46:45
@@ -3364,7 +3364,7 @@ Define the macros @var{__GNUC__}, @var{_
 @var{__GNUC_PATCHLEVEL__}. These are defined automatically when you use
 @samp{gcc -E}; you can turn them off in that case with @samp{-no-gcc}.
 
-@item -A @var{predicate}(@var{answer})
+@item -A @var{predicate}(@var{answer}) or -A @var{predicate}=@var{answer}
 @findex -A
 Make an assertion with the predicate @var{predicate} and answer
 @var{answer}.  @xref{Assertions}.
Index: cpplib.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpplib.c,v
retrieving revision 1.211
diff -u -p -r1.211 cpplib.c
--- cpplib.c	2000/10/30 22:28:59	1.211
+++ cpplib.c	2000/10/31 20:46:45
@@ -1608,29 +1608,25 @@ cpp_define (pfile, str)
   char *buf, *p;
   size_t count;
 
-  p = strchr (str, '=');
   /* Copy the entire option so we can modify it. 
      Change the first "=" in the string to a space.  If there is none,
-     tack " 1" on the end.  Then add a newline and a NUL.  */
-  
+     tack " 1\0" on the end.  */
+
+  /* Length including the null.  */  
+  count = strlen (str) + 1;
+  buf = (char *) alloca (count + 2);
+  memcpy (buf, str, count);
+
+  p = strchr (str, '=');
   if (p)
-    {
-      count = strlen (str) + 2;
-      buf = (char *) alloca (count);
-      memcpy (buf, str, count - 2);
-      buf[p - str] = ' ';
-      buf[count - 2] = '\n';
-      buf[count - 1] = '\0';
-    }
+    buf[p - str] = ' ';
   else
     {
-      count = strlen (str) + 4;
-      buf = (char *) alloca (count);
-      memcpy (buf, str, count - 4);
-      strcpy (&buf[count-4], " 1\n");
+      memcpy (&buf[count - 1], " 1", 3);
+      count += 2;
     }
 
-  run_directive (pfile, T_DEFINE, buf, count - 1, 0);
+  run_directive (pfile, T_DEFINE, buf, count, 0);
 }
 
 /* Slight variant of the above for use by initialize_builtins, which (a)
@@ -1659,7 +1655,22 @@ cpp_assert (pfile, str)
      cpp_reader *pfile;
      const char *str;
 {
-  run_directive (pfile, T_ASSERT, str, strlen (str), 0);
+  size_t count = strlen (str);
+  const char *p = strchr (str, '=');
+
+  if (p)
+    {
+      /* Copy the entire option so we can modify it.  Change the first
+	 "=" in the string to a '(', and tack a ')' on the end.  */
+      char *buf = (char *) alloca (count + 1);
+
+      memcpy (buf, str, count);
+      buf[p - str] = '(';
+      buf[count++] = ')';
+      str = buf;
+    }
+
+  run_directive (pfile, T_ASSERT, str, count, 0);
 }
 
 /* Process STR as if it appeared as the body of an #unassert. */
Index: testsuite/gcc.dg/cpp/assert3.c
===================================================================
RCS file: assert3.c
diff -N assert3.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ assert3.c	Tue Oct 31 12:46:45 2000
@@ -0,0 +1,10 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.  */
+
+/* { dg-do preprocess } */
+/* { dg-options "-A abc=def" "-A abc(ghi)" "-Aabc = jkl " } */
+
+/* Test -A command line syntax.  Source Neil Booth.  31 Oct 2000.  */
+
+#if !#abc (def) || !#abc (ghi) || !#abc (jkl)
+#error Command line -A assertions
+#endif


More information about the Gcc-patches mailing list