From ce261a0edd6e4a3755f1929e7f9c164cc1474618 Mon Sep 17 00:00:00 2001
From: Per Hedbor <ph@opera.com>
Date: Tue, 26 Aug 2014 12:22:53 +0200
Subject: [PATCH] Added the __COUNTER__ macro. Who knows, someone might find a
 use for it.

---
 CHANGES   |  4 ++++
 src/cpp.c | 16 ++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/CHANGES b/CHANGES
index 62675cb9d2..7a7c578319 100644
--- a/CHANGES
+++ b/CHANGES
@@ -195,6 +195,10 @@ o Support for the ", ##__VA_ARGS__" cpp feature.
   happen if you pass an empty argument, nor does it happen if the
   token preceding ‘##’ is anything other than a comma.
 
+o The define __COUNTER__ has been added. It is a unique integer
+  value, the first time the macro is expaded it will be 1, the next time
+  2 etc.
+
 o The preprocessor can now be run with a cpp prefix feature.
 
   This is currently used by the precompiler to avoid two levels of
diff --git a/src/cpp.c b/src/cpp.c
index 78f919230b..872798c16f 100644
--- a/src/cpp.c
+++ b/src/cpp.c
@@ -2924,6 +2924,20 @@ static void insert_current_minor(struct cpp *this,
   string_builder_sprintf(tmp, " %d ", this->compat_minor);
 }
 
+/*! @decl int(1..) __COUNTER__
+ *! This define contains a unique counter (unless it has been expanded
+ *! Inte.NATIVE_MAX times) represented as an integer.
+ *!
+ */
+static void insert_current_counter(struct cpp *UNUSED(this),
+				 struct define *UNUSED(def),
+				 struct define_argument *UNUSED(args),
+				 struct string_builder *tmp)
+{
+  static int counter = 0;
+  string_builder_sprintf(tmp, " %d ", ++counter);
+}
+
 /*! @decl constant __MAJOR__
  *!
  *! This define contains the major part of the current Pike version,
@@ -3429,6 +3443,8 @@ void f_cpp(INT32 args)
   do_magic_define(&this,"_Pragma",insert_pragma)->args = 1;
   simple_add_define(&this, "static_assert", "_Static_assert");
 
+  do_magic_define(&this,"__COUNTER__",insert_current_counter);
+
   /* These are Pike extensions. */
   do_magic_define(&this,"__DIR__",insert_current_dir_as_string);
   do_magic_define(&this,"__VERSION__",insert_current_version);
-- 
GitLab