From 2e9902b95bea53da9862545cbb2c7d5042db0cd1 Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <andrew@dunslane.net>
Date: Tue, 8 Sep 2026 09:24:59 -0400
Subject: [PATCH v2 2/2] pgindent: Fix indentation of alignas() in struct
 members

pg_bsd_indent doesn't know about alignas(), so a struct member
declared with it is misindented unless it's the first member.

Disguise each alignas(...) call as a plain identifier while
indenting, restoring it afterward.  Stash the call's original text
by index rather than embedding it in the placeholder: embedding only
works for a single-token argument, and fails to match at all against
nested parens (e.g. alignas(sizeof(x))) or a line break, silently
skipping the disguise.

Also reindent the one affected place in xlogreader.h.

Author: Peter Eisentraut <peter@eisentraut.org>
Reviewed-by: Andrew Dunstan <andrew@dunslane.net>
Discussion: https://postgr.es/m/94a128da-bf3e-46bd-9e2d-609573c484da@eisentraut.org
---
 src/include/access/xlogreader.h |  2 +-
 src/tools/pgindent/pgindent     | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h
index a2563458018..6e27b30fb35 100644
--- a/src/include/access/xlogreader.h
+++ b/src/include/access/xlogreader.h
@@ -262,7 +262,7 @@ struct XLogReaderState
 	/*
 	 * Buffer for currently read page (valid up to at least readLen bytes)
 	 */
-				alignas(MAXIMUM_ALIGNOF) char readBuf[XLOG_BLCKSZ];
+	alignas(MAXIMUM_ALIGNOF) char readBuf[XLOG_BLCKSZ];
 	uint32		readLen;
 
 	/* last read XLOG position for data currently in readBuf */
diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index 004b8fcab00..eea6c0ad734 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -235,6 +235,10 @@ sub write_source
 	return;
 }
 
+# Text of each alignas(...) call, stashed by pre_indent() and restored
+# by post_indent().
+my @alignas_stash;
+
 sub pre_indent
 {
 	my $source = shift;
@@ -263,6 +267,19 @@ sub pre_indent
 	# Protect wrapping in CATALOG()
 	$source =~ s!^(CATALOG\(.*)$!/*$1*/!gm;
 
+	# pg_bsd_indent doesn't know about alignas(), so a non-first struct
+	# member declared with it gets misindented.  Disguise each call as
+	# a plain identifier; stash the original text rather than embed it,
+	# so nested parens or line breaks in the argument aren't a problem.
+	@alignas_stash = ();
+	$source =~ s!
+		\b alignas \s*
+		( \( (?: [^()]++ | (?1) )*+ \) )
+	!
+		push(@alignas_stash, "alignas" . $1);
+		"alignas_" . $#alignas_stash . "_";
+	!gex;
+
 	return $source;
 }
 
@@ -270,6 +287,9 @@ sub post_indent
 {
 	my $source = shift;
 
+	# Restore alignas(...)
+	$source =~ s!\balignas_(\d+)_!$alignas_stash[$1]!g;
+
 	# Restore CATALOG lines
 	$source =~ s!^/\*(CATALOG\(.*)\*/$!$1!gm;
 
-- 
2.43.0

