Subject: cppi: Support C23 #elifdef and #elifndef

From: Collin Funk <[email-masked]>
Date: Fri, 03 Jan 2025 18:47:57 -0800

Hi Jim,

C23 added #elifdef and #elifndef. I've attached a patch to have cppi
recognize them.

Collin

>From 02d7a219c9adaa159d5eec4ea011892859da1eb1 Mon Sep 17 00:00:00 2001
From: Collin Funk <[email-masked]>
Date: Fri, 3 Jan 2025 18:38:09 -0800
Subject: [PATCH] cppi: Support C23 #elifdef and #elifndef

* src/cpp.gp: Add EIC_ELIFDEF and EIC_ELIFNDEF.
* src/cppi.l (emit_indented_cpp, [^a-zA-Z_\n]): Check for EIC_ELIFDEF
and EIC_ELIFNDEF.
* tests/e10: New file.
* tests/e11: New file.
* tests/f10: New file.
* tests/f11: New file.
* tests/local.mk (TESTS): Add the new tests.
* NEWS: Mention the change.
---
 NEWS           |  3 +++
 src/cpp.gp     |  2 ++
 src/cppi.l     |  5 ++++-
 tests/e10      | 19 +++++++++++++++++++
 tests/e11      | 19 +++++++++++++++++++
 tests/f10      | 25 +++++++++++++++++++++++++
 tests/f11      | 21 +++++++++++++++++++++
 tests/local.mk |  4 ++++
 8 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100755 tests/e10
 create mode 100755 tests/e11
 create mode 100755 tests/f10
 create mode 100755 tests/f11

diff --git a/NEWS b/NEWS
index 68ecb03..a600801 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,9 @@ GNU cppi NEWS                                               -*- outline -*-
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** Improvements
+
+  cppi now supports the #elifdef and #elifndef directives added by C23.
 
 * Noteworthy changes in release 1.18 (2013-03-16) [stable]
 
diff --git a/src/cpp.gp b/src/cpp.gp
index 4841aa7..ca865cf 100644
--- a/src/cpp.gp
+++ b/src/cpp.gp
@@ -5,6 +5,8 @@ if, EIC_IF
 ifdef, EIC_IFDEF
 ifndef, EIC_IFNDEF
 elif, EIC_ELIF
+elifdef, EIC_ELIFDEF
+elifndef, EIC_ELIFNDEF
 else, EIC_ELSE
 endif, EIC_ENDIF
 define, EIC_DEFINE
diff --git a/src/cppi.l b/src/cppi.l
index b8dba99..f393be4 100644
--- a/src/cppi.l
+++ b/src/cppi.l
@@ -297,6 +297,8 @@ emit_indented_cpp (char const *in_file, unsigned int line_number,
 
     case EIC_ELSE:
     case EIC_ELIF:
+    case EIC_ELIFDEF:
+    case EIC_ELIFNDEF:
       if (i_depth <= initial_i_depth)
         {
           if (!list_files_only)
@@ -477,7 +479,8 @@ b [^a-zA-Z_\n]
 
   t = emit_indented_cpp (in_file, lineno, yytext, yyleng, &fail);
 
-  if (t == EIC_IF || t == EIC_IFNDEF || t == EIC_ELIF || t == EIC_DEFINE)
+  if (t == EIC_IF || t == EIC_IFNDEF || t == EIC_ELIF || t == EIC_ELIFDEF
+      || t == EIC_ELIFNDEF || t == EIC_DEFINE)
     {
       int c;
       if (inhibit_output)
diff --git a/tests/e10 b/tests/e10
new file mode 100755
index 0000000..679c6c3
--- /dev/null
+++ b/tests/e10
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+t=`basename $0`
+
+cat > $t.I <<\EOF
+#elifdef(FOO)
+EOF
+
+cat > $t.EO <<\EOF
+#elifdef (FOO)
+EOF
+
+cat > $t.Ee <<\EOF
+cppi: standard input: line 1: found #elifdef without matching #if
+EOF
+
+expected_status=2
+
+. $top_srcdir/tests/test-common
diff --git a/tests/e11 b/tests/e11
new file mode 100755
index 0000000..1399e01
--- /dev/null
+++ b/tests/e11
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+t=`basename $0`
+
+cat > $t.I <<\EOF
+#elifndef(FOO)
+EOF
+
+cat > $t.EO <<\EOF
+#elifndef (FOO)
+EOF
+
+cat > $t.Ee <<\EOF
+cppi: standard input: line 1: found #elifndef without matching #if
+EOF
+
+expected_status=2
+
+. $top_srcdir/tests/test-common
diff --git a/tests/f10 b/tests/f10
new file mode 100755
index 0000000..9c7e0d0
--- /dev/null
+++ b/tests/f10
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+t=`basename $0`
+
+cat > $t.I <<\EOF
+#if(FOO)
+#elifdef(NO_BLANK_AFTER_ELIF)
+#error
+#endif
+EOF
+
+cat > $t.EO <<\EOF
+#if (FOO)
+#elifdef (NO_BLANK_AFTER_ELIF)
+# error
+#endif
+EOF
+
+cat > $t.Ee <<\EOF
+EOF
+
+options=
+expected_status=0
+
+. $top_srcdir/tests/test-common
diff --git a/tests/f11 b/tests/f11
new file mode 100755
index 0000000..bcd8042
--- /dev/null
+++ b/tests/f11
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+t=`basename $0`
+
+cat > $t.I <<\EOF
+#if (FOO)
+#elifndef (NO_BLANK_AFTER_ELIF)
+# undef FOOBAR
+#endif
+EOF
+
+cat > $t.EO <<\EOF
+EOF
+
+cat > $t.Ee <<\EOF
+EOF
+
+options=-c
+expected_status=0
+
+. $top_srcdir/tests/test-common
diff --git a/tests/local.mk b/tests/local.mk
index e227e7b..94ecd90 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -16,6 +16,8 @@ TESTS = \
   tests/e7	\
   tests/e8	\
   tests/e9	\
+  tests/e10	\
+  tests/e11	\
   tests/f1	\
   tests/f2	\
   tests/f3	\
@@ -24,6 +26,8 @@ TESTS = \
   tests/f7	\
   tests/f8	\
   tests/f9	\
+  tests/f10	\
+  tests/f11	\
   tests/stringify-1	\
   tests/stringify-2	\
   tests/stringify-3	\
-- 
2.47.1