macro.c (_cpp_builtin_macro_text): Handle BT_COUNTER.
authorOllie Wild <aaw@google.com>
Thu, 24 May 2007 20:55:36 +0000 (20:55 +0000)
committerOllie Wild <aaw@gcc.gnu.org>
Thu, 24 May 2007 20:55:36 +0000 (20:55 +0000)
* macro.c (_cpp_builtin_macro_text): Handle BT_COUNTER.
* pch.c (cpp_write_pch_deps): Save __COUNTER__ state.
(cpp_write_pch_state): Save __COUNTER__ state.
(cpp_valid_state): Check valid __COUNTER__ state.
(cpp_read_state): Read new __COUNTER__ state.
* include/cpplib.h (enum builtin_type): Add BT_COUNTER enumerator.
* init.c (builtin_array): Add __COUNTER__/BT_COUNTER.
* internal.h (struct cpp_reader): Add counter member.

* gcc.dg/cpp/counter-1.c: New test.
* gcc.dg/pch/counter-1.c: New test.
* gcc.dg/pch/counter-1.hs: New file.
* gcc.dg/pch/counter-2.c: New test.
* gcc.dg/pch/counter-2.hs: New file.
* gcc.dg/pch/counter-3.c: New test.
* gcc.dg/pch/counter-3.hs: New file.

* doc/cpp.texi (Common Predefined Macros): Add __COUNTER__
description.

From-SVN: r125041

16 files changed:
gcc/ChangeLog
gcc/doc/cpp.texi
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/cpp/counter-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pch/counter-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pch/counter-1.hs [new file with mode: 0644]
gcc/testsuite/gcc.dg/pch/counter-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pch/counter-2.hs [new file with mode: 0644]
gcc/testsuite/gcc.dg/pch/counter-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pch/counter-3.hs [new file with mode: 0644]
libcpp/ChangeLog
libcpp/include/cpplib.h
libcpp/init.c
libcpp/internal.h
libcpp/macro.c
libcpp/pch.c

index d2fa44daf6a9c6f2e09394940db3bc17d3db8d8f..28dcaec58ae06eda5e63a815e2e8351ae4e3e2ee 100644 (file)
@@ -1,3 +1,8 @@
+2007-05-24  Ollie Wild  <aaw@google.com>
+
+       * doc/cpp.texi (Common Predefined Macros): Add __COUNTER__
+       description.
+
 2007-05-24  Richard Sandiford  <rsandifo@nildram.co.uk>
 
        * postreload-gcse.c (reg_changed_after_insn_p): New function.
index e6786cf81023f1796eeecd4ff4d081ade2ee5a96..54941a9f3c6deb61728550df98868d8cd4accbb6 100644 (file)
@@ -1917,6 +1917,13 @@ underscores.
 
 @table @code
 
+@item __COUNTER__
+This macro expands to sequential integral values starting from 0.  In
+conjuction with the @code{##} operator, this provides a convenient means to
+generate unique identifiers.  Care must be taken to ensure that
+@code{__COUNTER__} is not expanded prior to inclusion of precompiled headers
+which use it.  Otherwise, the precompiled headers will not be used.
+
 @item __GNUC__
 @itemx __GNUC_MINOR__
 @itemx __GNUC_PATCHLEVEL__
index e230bc3d44df34e238cbd685b48d86ece6cfbcb3..7a33bde99a80f560a41c39973f1eee20c2656a4e 100644 (file)
@@ -1,3 +1,13 @@
+2007-05-24  Ollie Wild  <aaw@google.com>
+
+       * gcc.dg/cpp/counter-1.c: New test.
+       * gcc.dg/pch/counter-1.c: New test.
+       * gcc.dg/pch/counter-1.hs: New file.
+       * gcc.dg/pch/counter-2.c: New test.
+       * gcc.dg/pch/counter-2.hs: New file.
+       * gcc.dg/pch/counter-3.c: New test.
+       * gcc.dg/pch/counter-3.hs: New file.
+
 2007-05-24  Zdenek Dvorak  <dvorakz@suse.cz>
 
        * gcc.dg/tree-ssa/predcom-1.c: New test.
diff --git a/gcc/testsuite/gcc.dg/cpp/counter-1.c b/gcc/testsuite/gcc.dg/cpp/counter-1.c
new file mode 100644 (file)
index 0000000..80d8b78
--- /dev/null
@@ -0,0 +1,20 @@
+/* Copyright (C) 2007 Free Software Foundation
+   Contributed by Ollie Wild <aaw@google.com> */
+
+/* { dg-do preprocess } */
+
+/* Tests __COUNTER__ macro is correctly expanded.  */
+
+#define counter __COUNTER__
+
+#if __COUNTER__ != 0
+#error __COUNTER__ != 0
+#endif
+
+#if counter != 1
+#error counter != 1
+#endif
+
+#if __COUNTER__ != 2
+#error __COUNTER__ != 2
+#endif
diff --git a/gcc/testsuite/gcc.dg/pch/counter-1.c b/gcc/testsuite/gcc.dg/pch/counter-1.c
new file mode 100644 (file)
index 0000000..79cea88
--- /dev/null
@@ -0,0 +1,9 @@
+/* Copyright (C) 2007 Free Software Foundation
+   Contributed by Ollie Wild <aaw@google.com> */
+
+#include "counter-1.h"
+
+int main(void) 
+{
+  return __COUNTER__;
+}
diff --git a/gcc/testsuite/gcc.dg/pch/counter-1.hs b/gcc/testsuite/gcc.dg/pch/counter-1.hs
new file mode 100644 (file)
index 0000000..c3d2ce7
--- /dev/null
@@ -0,0 +1,5 @@
+/* Copyright (C) 2007 Free Software Foundation
+   Contributed by Ollie Wild <aaw@google.com> */
+
+static unsigned offset[] = {__COUNTER__, __COUNTER__, __COUNTER__};
+#define counter __COUNTER__
diff --git a/gcc/testsuite/gcc.dg/pch/counter-2.c b/gcc/testsuite/gcc.dg/pch/counter-2.c
new file mode 100644 (file)
index 0000000..7bbde23
--- /dev/null
@@ -0,0 +1,15 @@
+/* Copyright (C) 2007 Free Software Foundation
+   Contributed by Ollie Wild <aaw@google.com> */
+
+/* { dg-options "-I. -Winvalid-pch" } */
+
+#if __COUNTER__ != 0
+#error __COUNTER__ != 0
+#endif
+
+#include "counter-2.h"/* { dg-error "`__COUNTER__' is invalid|No such file|they were invalid" } */
+
+int main(void) 
+{
+  return __COUNTER__;
+}
diff --git a/gcc/testsuite/gcc.dg/pch/counter-2.hs b/gcc/testsuite/gcc.dg/pch/counter-2.hs
new file mode 100644 (file)
index 0000000..63b8a2b
--- /dev/null
@@ -0,0 +1,4 @@
+/* Copyright (C) 2007 Free Software Foundation
+   Contributed by Ollie Wild <aaw@google.com> */
+
+static int i = __COUNTER__;
diff --git a/gcc/testsuite/gcc.dg/pch/counter-3.c b/gcc/testsuite/gcc.dg/pch/counter-3.c
new file mode 100644 (file)
index 0000000..8279205
--- /dev/null
@@ -0,0 +1,13 @@
+/* Copyright (C) 2007 Free Software Foundation
+   Contributed by Ollie Wild <aaw@google.com> */
+
+#if __COUNTER__ != 0
+#error __COUNTER__ != 0
+#endif
+
+#include "counter-3.h"
+
+int main(void) 
+{
+  return __COUNTER__;
+}
diff --git a/gcc/testsuite/gcc.dg/pch/counter-3.hs b/gcc/testsuite/gcc.dg/pch/counter-3.hs
new file mode 100644 (file)
index 0000000..19039c8
--- /dev/null
@@ -0,0 +1,4 @@
+/* Copyright (C) 2007 Free Software Foundation
+   Contributed by Ollie Wild <aaw@google.com> */
+
+#define counter __COUNTER__
index 8f760cc1d9592e790b9e4d75960c144bceb8f34c..a95ae545b07f69620189ffc602a967d8e23d28bd 100644 (file)
@@ -1,3 +1,14 @@
+2007-05-24  Ollie Wild  <aaw@google.com>
+
+       * macro.c (_cpp_builtin_macro_text): Handle BT_COUNTER.
+       * pch.c (cpp_write_pch_deps): Save __COUNTER__ state.
+       (cpp_write_pch_state): Save __COUNTER__ state.
+       (cpp_valid_state): Check valid __COUNTER__ state.
+       (cpp_read_state): Read new __COUNTER__ state.
+       * include/cpplib.h (enum builtin_type): Add BT_COUNTER enumerator.
+       * init.c (builtin_array): Add __COUNTER__/BT_COUNTER.
+       * internal.h (struct cpp_reader): Add counter member.
+
 2007-05-23  Simon Martin  <simartin@users.sourceforge.net>
 
        PR preprocessor/20077
index b2939984f80a4fa18e4c2531ee71fe3ad684440e..ff5292ef3c8615e22bcb215ca58bb08c45295fd3 100644 (file)
@@ -555,7 +555,8 @@ enum builtin_type
   BT_TIME,                     /* `__TIME__' */
   BT_STDC,                     /* `__STDC__' */
   BT_PRAGMA,                   /* `_Pragma' operator */
-  BT_TIMESTAMP                 /* `__TIMESTAMP__' */
+  BT_TIMESTAMP,                        /* `__TIMESTAMP__' */
+  BT_COUNTER                   /* `__COUNTER__' */
 };
 
 #define CPP_HASHNODE(HNODE)    ((cpp_hashnode *) (HNODE))
index db697e9ed34e4db3d5ed60e9e7a2967b489762a3..71583df94d6451d108893739bdd82b1aeb446501 100644 (file)
@@ -310,6 +310,7 @@ static const struct builtin builtin_array[] =
   B("__BASE_FILE__",    BT_BASE_FILE),
   B("__LINE__",                 BT_SPECLINE),
   B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
+  B("__COUNTER__",      BT_COUNTER),
   /* Keep builtins not used for -traditional-cpp at the end, and
      update init_builtins() if any more are added.  */
   B("_Pragma",          BT_PRAGMA),
index 9395f6aa10e4efee2679fc03f9ab43a164f79b3b..d000cfda54fa0c084f4b08e2c670591bf7e015a0 100644 (file)
@@ -452,6 +452,9 @@ struct cpp_reader
   /* A saved list of the defined macros, for dependency checking
      of precompiled headers.  */
   struct cpp_savedstate *savedstate;
+
+  /* Next value of __COUNTER__ macro. */
+  unsigned int counter;
 };
 
 /* Character classes.  Based on the more primitive macros in safe-ctype.h.
index 12681c34338806579292aa899c9dc42b88654da3..c8d099ed210f6eadc630bc671de7867540d1df33 100644 (file)
@@ -263,6 +263,10 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
       else
        result = pfile->time;
       break;
+
+    case BT_COUNTER:
+      number = pfile->counter++;
+      break;
     }
 
   if (result == NULL)
index cc23b4ee908771957adf24c2d9101eab7acf32ff..09373a2fd4e14d696662c3fd8f81078a48bfa2aa 100644 (file)
@@ -337,6 +337,14 @@ cpp_write_pch_deps (cpp_reader *r, FILE *f)
   /* Free the saved state.  */
   free (ss);
   r->savedstate = NULL;
+
+  /* Save the next value of __COUNTER__. */
+  if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
+    {
+      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
+      return -1;
+    }
+
   return 0;
 }
 
@@ -361,6 +369,15 @@ cpp_write_pch_state (cpp_reader *r, FILE *f)
       return -1;
     }
 
+  /* Save the next __COUNTER__ value.  When we include a precompiled header,
+     we need to start at the offset we would have if the header had been
+     included normally. */
+  if (fwrite (&r->counter, sizeof (r->counter), 1, f) != 1)
+    {
+      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
+      return -1;
+    }
+
   return 0;
 }
 
@@ -423,6 +440,7 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
   struct ht_node_list nl = { 0, 0, 0 };
   unsigned char *first, *last;
   unsigned int i;
+  unsigned int counter;
   
   /* Read in the list of identifiers that must be defined
      Check that they are defined in the same way.  */
@@ -524,7 +542,23 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
     }
    
   free(nl.defs);
+  nl.defs = NULL;
   free (undeftab);
+  undeftab = NULL;
+
+  /* Read in the next value of __COUNTER__.
+     Check that (a) __COUNTER__ was not used in the pch or (b) __COUNTER__
+     has not been used in this translation unit. */
+  if (read (fd, &counter, sizeof (counter)) != sizeof (counter))
+    goto error;
+  if (counter && r->counter)
+    {
+      if (CPP_OPTION (r, warn_invalid_pch))
+       cpp_error (r, CPP_DL_WARNING_SYSHDR, 
+                  "%s: not used because `__COUNTER__' is invalid",
+                  name);
+       goto fail;
+    }
 
   /* We win!  */
   return 0;
@@ -631,6 +665,7 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
 {
   size_t i;
   struct lexer_state old_state;
+  unsigned int counter;
 
   /* Restore spec_nodes, which will be full of references to the old 
      hashtable entries and so will now be invalid.  */
@@ -690,6 +725,12 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
   if (! _cpp_read_file_entries (r, f))
     goto error;
 
+  if (fread (&counter, sizeof (counter), 1, f) != 1)
+    goto error;
+
+  if (!r->counter)
+    r->counter = counter;
+
   return 0;
   
  error: