]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Implement C2Y N3457 - The __COUNTER__ predefined macro
authorJakub Jelinek <jakub@redhat.com>
Mon, 1 Sep 2025 19:55:49 +0000 (21:55 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 1 Sep 2025 19:55:49 +0000 (21:55 +0200)
The following patch implements the
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3457.htm
paper without the first 3 lines in Recommended practice.
Seems GCC behavior already matches the expected behavior except for
diagnostics of more than 2147483648 __COUNTER__ expansions, so the
patch adds a diagnostic for that (but not testcase because
 #define A __COUNTER__ __COUNTER__ __COUNTER__ __COUNTER__ __COUNTER__ __COUNTER__ __COUNTER__ __COUNTER__
 #define B A A A A A A A A
 #define C B B B B B B B B
 #define D C C C C C C C C
 #define E D D D D D D D D
 #define F E E E E E E E E
 #define G F F F F F F F F
 #define H G G G G G G G G
 #define I H H H H H H H H
 #define J I I I I I I I I
 J J J J
 __COUNTER__
just takes too long to preprocess).
Plus I've included all the snippets from the paper into one testcase.

2025-09-01  Jakub Jelinek  <jakub@redhat.com>

* macro.cc: Implement C2Y N3457 - The __COUNTER__ predefined macro.
(_cpp_builtin_macro_text): Diagnose if __COUNTER__ reaches
2147483648 value.

* gcc.dg/cpp/c2y-counter-1.c: New test.

gcc/testsuite/gcc.dg/cpp/c2y-counter-1.c [new file with mode: 0644]
libcpp/macro.cc

diff --git a/gcc/testsuite/gcc.dg/cpp/c2y-counter-1.c b/gcc/testsuite/gcc.dg/cpp/c2y-counter-1.c
new file mode 100644 (file)
index 0000000..47d4b23
--- /dev/null
@@ -0,0 +1,44 @@
+/* N3457 - The __COUNTER__ predefined macro */
+/* { dg-do run } */
+/* { dg-options "-std=c2y -pedantic-errors" } */
+
+#ifndef __COUNTER__
+#error "__COUNTER__ not defined"
+#endif
+
+#define A(X) X + X
+static_assert (A(__COUNTER__) == 0);
+#define B(X)
+B(__COUNTER__)
+static_assert (__COUNTER__ == 1);
+#define C(...) __VA_OPT__()
+C(__COUNTER__)
+static_assert (__COUNTER__ == 3);
+#define D(...) __VA_OPT__(a)
+int D(__COUNTER__) = 1;
+static_assert (__COUNTER__ == 5);
+#define E(X) #X
+const char *b = E(__COUNTER__);
+#define F(X) a##X
+int F(__COUNTER__) = 2;
+static_assert (__COUNTER__ == 6);
+#define G(X) b##X = X
+int G(__COUNTER__);
+static_assert (__COUNTER__ == 8);
+#if !defined(__COUNTER__) || (__COUNTER__ + 1 != __COUNTER__ + 0)
+#error "Unexpected __COUNTER__ behavior")
+#endif
+static_assert (__COUNTER__ == 11);
+
+extern int strcmp (const char *, const char *);
+extern void abort ();
+
+int
+main ()
+{
+  if (a != 1
+      || strcmp (b, "__COUNTER__")
+      || a__COUNTER__ != 2
+      || b__COUNTER__ != 7)
+    abort ();
+}
index 9867098225f73d5b4eb8a71334cc2583ee916cf1..e30760272c25b9ab1f643a9671ee5df5c0c74d1f 100644 (file)
@@ -733,6 +733,9 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
                   "%<__COUNTER__%> expanded inside directive with "
                   "%<-fdirectives-only%>");
       number = pfile->counter++;
+      if (number == 0x80000000U)
+       cpp_error (pfile, CPP_DL_ERROR,
+                  "%<__COUNTER__%> expanded more than 2147483648 times");
       break;
 
     case BT_HAS_ATTRIBUTE: