PATCH: __COUNTER__ + -fdirectives-only compatibility constraint

Ollie Wild aaw@google.com
Fri Jun 15 08:26:00 GMT 2007


This patch provides the integration between the __COUNTER__ macro and
-fdirectives-only option alluded to at
http://gcc.gnu.org/ml/gcc-patches/2007-05/msg01579.html.

Because -fdirectives-only alters the normal flow of macro expansion,
it causes problems in code like this:

    1: #define SUM (__COUNTER__ +  __COUNTER)
    2: static int sum = SUM;  /* 0 + 1 == 1 */
    3: #if SUM != 5  /* 2 + 3 == 5 */
    4:     #error SUM != 5.  Darn you directives-only!
    5: #endif

Normally, the error condition is not reached.  However, when
preprocessing with -fdirectives-only, the macro expansion in line 2 is
suppressed so SUM in line 3 expands to 1.

The patch avoids the problem by triggering an error if __COUNTER__ is
expanded inside a preprocessing directive with -fdirectives-only
enabled.  Note, though, that this does not prevent __COUNTER__ from
being used inside macro replacement lists or tests for macro
definition.

This patch depends on the -fdirectives-only patch at
http://gcc.gnu.org/ml/gcc-patches/2007-06/msg01017.html.

Ollie

:ADDPATCH libcpp:

2007-06-14  Ollie Wild  <aaw@google.com>

      * macro.c (_cpp_builtin_macro_text): Print an error if __COUNTER__
      is expanded inside a directive while -fdirectives-only is enabled.

2007-06-14  Ollie Wild  <aaw@google.com>

      * gcc.dg/cpp/counter-2.c: New test.
      * gcc.dg/cpp/counter-3.c: New test.
-------------- next part --------------
diff --git a/gcc/testsuite/gcc.dg/cpp/counter-2.c b/gcc/testsuite/gcc.dg/cpp/counter-2.c
new file mode 100644
index 0000000..7d6578d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/counter-2.c
@@ -0,0 +1,14 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+/* { dg-do preprocess } */
+/* { dg-options -fdirectives-only } */
+
+/* Tests __COUNTER__ macro expansion is disabled inside directives with
+   -fdirectives-only. */
+
+#ifdef __COUNTER__  /* Macro not expanded. */
+#endif
+
+#if __COUNTER__ == 0  /* { dg-error "__COUNTER__ expanded inside directive with -fdirectives-only" } */
+#endif
diff --git a/gcc/testsuite/gcc.dg/cpp/counter-3.c b/gcc/testsuite/gcc.dg/cpp/counter-3.c
new file mode 100644
index 0000000..3b1824f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/counter-3.c
@@ -0,0 +1,10 @@
+/* Copyright 2007 Free Software Foundation, Inc.
+   Contributed by Ollie Wild <aaw@google.com>.  */
+
+/* { dg-do preprocess } */
+/* { dg-options "-fdirectives-only -fpreprocessed" } */
+
+/* Tests __COUNTER__ macro expansion is enabled outside directives with
+   -fdirectives-only. */
+
+int zero = __COUNTER__;
diff --git a/libcpp/macro.c b/libcpp/macro.c
index c8d099e..f242717 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -265,6 +265,9 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
       break;
 
     case BT_COUNTER:
+      if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
+	cpp_error (pfile, CPP_DL_ERROR,
+	    "__COUNTER__ expanded inside directive with -fdirectives-only");
       number = pfile->counter++;
       break;
     }


More information about the Gcc-patches mailing list