From 6da9c0af52101930eba7b5c496d35111d29a6d04 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@meta.com>
Date: Sun, 18 Feb 2024 20:23:28 -0800
Subject: [PATCH] maint.mk: improve new syntax-check rule

* top/maint.mk (sc_prefer_angle_bracket_headers): Rename from
sc_verify_absolute_include_headers.  Simplify to run a single
invocation of grep, rather than usually many.  Derive a regular
expression matching the selected headers, and warn about any
offending matches via what is usually a single grep invocation.
---
 ChangeLog    |  9 +++++++++
 top/maint.mk | 27 +++++++++------------------
 2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3a8559bfb3..1fe354f2b0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-02-18  Jim Meyering  <meyering@meta.com>
+
+	maint.mk: improve new syntax-check rule
+	* top/maint.mk (sc_prefer_angle_bracket_headers): Rename from
+	sc_verify_absolute_include_headers.  Simplify to run a single
+	invocation of grep, rather than usually many.  Derive a regular
+	expression matching the selected headers, and warn about any
+	offending matches via what is usually a single grep invocation.
+
 2024-02-18  Bruno Haible  <bruno@clisp.org>

 	maint.mk: Add more comments.
diff --git a/top/maint.mk b/top/maint.mk
index 5746b911dd..2278619931 100644
--- a/top/maint.mk
+++ b/top/maint.mk
@@ -539,7 +539,7 @@ sc_require_config_h_first:

 # Generated headers that override system headers.
 # Keep sorted.
-gl_absolute_include_headers_ ?= \
+gl_prefer_angle_bracket_headers_ ?= \
   alloca.h		\
   arpa_inet.h		\
   assert.h		\
@@ -608,26 +608,17 @@ gl_absolute_include_headers_ ?= \
   wchar.h		\
   wctype.h

+# Remove each .h suffix and change each space to "|".
+angle_bracket_header_re = \
+  $(subst $(_sp),|,$(patsubst %.h,%,$(gl_prefer_angle_bracket_headers_)))
+
 # Suggest using '#include <header.h>' instead of '#include "header.h"' for
 # headers that override system headers.
 # Rationale: The Gnulib documentation, node 'Style of #include statements'.
-sc_verify_absolute_include_headers:
-	@if $(VC_LIST_EXCEPT) | $(GREP) '\.c$$' > /dev/null; then	\
-	  source_files=$$($(VC_LIST_EXCEPT) | $(GREP) '\.\(c\|h\)$$');	\
-	  for source_file in $$source_files; do				\
-	    local_includes=$$($(GREP) -h '^ *# *include "[^"]\+"' 	\
-                              $$source_file | $(SED) 			\
-                              -e 's/^[^"]\+"//g' -e 's/"$///g');	\
-	    for local_include in $$local_includes; do			\
-	      for header in $(gl_absolute_include_headers_); do         \
-	        if test "$$header" = "$$local_include";	then		\
-	          echo "$(ME): Use #include <$$header> instead of "     \
-	               "#include \"$$header\" in $$source_file" 2>&1;	\
-	        fi;							\
-	      done;							\
-	    done;							\
-          done;								\
-	fi;
+sc_prefer_angle_bracket_headers:
+	@prohibit='^ *# *include "($(angle_bracket_header_re))\.h"'	\
+	halt='Use #include <hdr.h>, not #include "hdr.h" for the above'	\
+	  $(_sc_search_regexp)

 sc_prohibit_HAVE_MBRTOWC:
 	@prohibit='\bHAVE_MBRTOWC\b'					\
-- 
2.41.0.376.gcba07a324d

