[binutils-gdb] gdb: make $(YACC) and $(FLEX) generate headers

Simon Marchi simark@sourceware.org
Thu Oct 1 18:09:18 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=9eedd1a99240e658e5d9ec328ff264eb8b23aae5

commit 9eedd1a99240e658e5d9ec328ff264eb8b23aae5
Author: Simon Marchi <simon.marchi@efficios.com>
Date:   Sat Sep 5 00:23:11 2026 -0400

    gdb: make $(YACC) and $(FLEX) generate headers
    
    I'd like to move the hand-written support code of parsers out of the .y
    files, because I find it very impractical to read and edit.  C++ tooling
    also typically doesn't work for the C++ code inside .y files.
    
    To help with this, pass -d to $(YACC), so that it also produces header
    files exposing the generated parsers, allowing to call them from other
    files.  I checked that the following yacc implementations support it:
    
     - bison
     - AIX (https://www.ibm.com/docs/en/aix/7.2.0?topic=y-yacc-command)
     - Solaris (https://docs.oracle.com/cd/E88353_01/html/E37839/yacc-1.html)
     - NetBSD (https://man.netbsd.org/yacc.1)
     - FreeBSD (https://man.freebsd.org/cgi/man.cgi?query=yacc&apropos=0&sektion=1&manpath=FreeBSD+15.1-RELEASE&arch=default&format=html)
     - OpenBSD (https://man.openbsd.org/yacc)
    
    Also pass `--header-file` to $(FLEX) to have it generate a header
    file.  This is a flex-specific option, but a previous patch in this
    series (to use -P) already effectively required the use of flex.
    
    The post-processing must be applied to the header files exactly as it is
    applied to the source files (it contains declarations of things that get
    renamed by that post-processing), so call the post processing script for
    the headers too.
    
    Add the generated headers to generated_files, so they are created before
    anything that could include them is compiled.
    
    Change-Id: I7153f85c69e989b5a1e4318288b85ba4b456a319
    Approved-By: Kevin Buettner <kevinb@redhat.com>

Diff:
---
 gdb/.gitignore  |  9 +++++++++
 gdb/Makefile.in | 38 +++++++++++++++++++++++++-------------
 2 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/gdb/.gitignore b/gdb/.gitignore
index c4d0f059544..6b5600dd695 100644
--- a/gdb/.gitignore
+++ b/gdb/.gitignore
@@ -6,15 +6,24 @@
 /xml-builtin.c
 
 /ada-exp-gen.c
+/ada-exp-gen.h
 /ada-lex-gen.c
+/ada-lex-gen.h
 /c-exp-gen.c
+/c-exp-gen.h
 /cp-name-parser-gen.c
+/cp-name-parser-gen.h
 /d-exp-gen.c
+/d-exp-gen.h
 /f-exp-gen.c
+/f-exp-gen.h
 /gdb
 /gcore
 /go-exp-gen.c
+/go-exp-gen.h
 /init.c
 /jit-reader.h
 /m2-exp-gen.c
+/m2-exp-gen.h
 /p-exp-gen.c
+/p-exp-gen.h
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index e12b72cc71c..cb7148f78cf 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -2070,9 +2070,12 @@ YYFILES = \
 # a .o itself.
 YYOBJ = $(filter-out ada-lex-gen.o,$(patsubst %.c,%.o,$(YYFILES)))
 
+# The headers bison/byacc and flex generate alongside the files above.
+YYHFILES = $(patsubst %-gen.c,%-gen.h,$(YYFILES))
+
 # Things which need to be built when making a distribution.
 
-DISTSTUFF = $(YYFILES)
+DISTSTUFF = $(YYFILES) $(YYHFILES)
 
 
 # All generated files which can be included by another file.
@@ -2080,6 +2083,7 @@ generated_files = \
 	ada-lex-gen.c \
 	config.h \
 	jit-reader.h \
+	$(YYHFILES) \
 	$(NAT_GENERATED_FILES) \
 	$(NM_H)
 
@@ -2436,7 +2440,7 @@ local-maintainer-clean:
 	@echo "This command is intended for maintainers to use;"
 	@echo "it deletes files that may require special tools to rebuild."
 	rm -f TAGS
-	rm -f $(YYFILES)
+	rm -f $(YYFILES) $(YYHFILES)
 	rm -f nm.h config.status
 
 do-maintainer-clean:
@@ -2709,17 +2713,25 @@ YY_PREFIX_go-exp = go_yy
 YY_PREFIX_m2-exp = m2_yy
 YY_PREFIX_p-exp = pascal_yy
 
-%-gen.c: %.y $(POST_PROCESS_PARSER_OUTPUT_SH)
-	$(ECHO_YACC) $(SHELL) $(YLWRAP) $< y.tab.c $@.tmp -- \
-		$(YACC) $(YFLAGS) -p $(YY_PREFIX_$*) -t || (rm -f $@.tmp; false)
-	@$(POST_PROCESS_PARSER_OUTPUT) bison $* < $@.tmp > $@.new && \
-	  rm -f $@.tmp && \
-	  mv $@.new $@
-%-gen.c: %.l $(POST_PROCESS_PARSER_OUTPUT_SH)
-	$(ECHO_LEX) $(FLEX) -t -P $(YY_PREFIX_$*) $< > $@.tmp || (rm -f $@.tmp; false)
-	@$(POST_PROCESS_PARSER_OUTPUT) flex $* < $@.tmp > $@.new && \
-	  rm -f $@.tmp && \
-	  mv $@.new $@
+%-gen.c %-gen.h: %.y $(POST_PROCESS_PARSER_OUTPUT_SH)
+	$(ECHO_YACC) $(SHELL) $(YLWRAP) $< \
+		y.tab.c $*-gen.c.tmp \
+		y.tab.h $*-gen.h.tmp -- \
+		$(YACC) $(YFLAGS) -p $(YY_PREFIX_$*) -t -d \
+			|| (rm -f $*-gen.c.tmp $*-gen.h.tmp; false)
+	@$(POST_PROCESS_PARSER_OUTPUT) bison $* < $*-gen.c.tmp > $*-gen.c.new \
+		&& rm -f $*-gen.c.tmp && mv $*-gen.c.new $*-gen.c
+	@$(POST_PROCESS_PARSER_OUTPUT) bison $* < $*-gen.h.tmp > $*-gen.h.new \
+		&& rm -f $*-gen.h.tmp && mv $*-gen.h.new $*-gen.h
+
+%-gen.c %-gen.h: %.l $(POST_PROCESS_PARSER_OUTPUT_SH)
+	$(ECHO_LEX) $(FLEX) -t -P $(YY_PREFIX_$*) \
+		--header-file=$*-gen.h.tmp $< > $*-gen.c.tmp \
+			|| (rm -f $*-gen.c.tmp $*-gen.h.tmp; false)
+	@$(POST_PROCESS_PARSER_OUTPUT) flex $* < $*-gen.c.tmp > $*-gen.c.new \
+		&& rm -f $*-gen.c.tmp && mv $*-gen.c.new $*-gen.c
+	@$(POST_PROCESS_PARSER_OUTPUT) flex $* < $*-gen.h.tmp > $*-gen.h.new \
+		&& rm -f $*-gen.h.tmp && mv $*-gen.h.new $*-gen.h
 
 # XML rules


More information about the Gdb-cvs mailing list