Le 21 mai 2012 à 14:42, Akim Demaille a écrit : > lalr1.cc: extract stack.hh. > To make merging easier in master. > lalr1.cc: improve Doxygen documentation. > To include the prefix part in the \file directive. > skeletons: remove support for unused directive. > Something that should never have been included. > c++: compute the header guards. > Really compute the CPP guards.
This is the patch that really computes the header guards. >From eb76fadde9c5a3b23dc6761ddd4d267a6e7c3965 Mon Sep 17 00:00:00 2001 From: Akim Demaille <a...@lrde.epita.fr> Date: Mon, 21 May 2012 14:21:51 +0200 Subject: [PATCH 4/4] c++: compute the header guards. This is a frequent request. Recently pointed out by Wei Song, <http://lists.gnu.org/archive/html/help-bison/2012-05/msg00002.html>. * src/scan-skel.l (append_guard): New. (@guard): New directive. * data/lalr1.cc, data/location.cc, data/stack.hh: Use @guard for these files. * TODO (CPP Guard): Move to... * NEWS: here. Formatting changes. --- NEWS | 32 +++++++++++++++++++++++++++----- THANKS | 1 + TODO | 4 ---- data/lalr1.cc | 7 +++---- data/location.cc | 12 ++++++------ data/stack.hh | 6 +++--- src/scan-skel.l | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 72 insertions(+), 22 deletions(-) diff --git a/NEWS b/NEWS index f554b9f..a8846a8 100644 --- a/NEWS +++ b/NEWS @@ -27,18 +27,40 @@ Bison News The Java parser no longer throws ArrayIndexOutOfBoundsException if the first token leads to a syntax error. Some minor clean ups. -** C++11 compatibility: +** Changes for C++: - C and C++ parsers use nullptr instead of 0 when __cplusplus is +*** C++11 compatibility: + + C and C++ parsers use `nullptr' instead of `0' when __cplusplus is 201103L or higher. -** C++ locations: +*** Header guards + + The header files such as parser.hh, location.hh, etc. used a + constant name for preprocessor guards, for instance: + + #ifndef BISON_LOCATION_HH + # define BISON_LOCATION_HH + ... + #endif // !BISON_LOCATION_HH + + The inclusion guard is computed from `PREFIX/FILE-NAME', where lower + case characters are converted to upper case, and remaining + non-alphanumerical characters are converted to underscore. With + `bison -o sub/parser.cc', location.hh would now include: + + #ifndef YY_SUB_LOCATION_HH + # define YY_SUB_LOCATION_HH + ... + #endif // !YY_SUB_LOCATION_HH + +*** C++ locations: The position and location constructors (and their initialize methods) accept new arguments for line and column. Several issues in the documentation were fixed. -** liby is no longer asking for "rpl_fprintf" on some platforms. +** liby is no longer asking for `rpl_fprintf' on some platforms. ** Changes in the manual: @@ -77,7 +99,7 @@ Bison News *** The install-pdf target work properly: - Running "make install-pdf" (or -dvi, -html, -info, and -ps) no + Running `make install-pdf' (or -dvi, -html, -info, and -ps) no longer halts in the middle of its course. * Changes in version 2.5 (2011-05-14): diff --git a/THANKS b/THANKS index 6d0d89e..e3bf221 100644 --- a/THANKS +++ b/THANKS @@ -112,6 +112,7 @@ Tys Lefering gccbi...@gmail.com Vin Shelton a...@alumni.princeton.edu W.C.A. Wijngaards wou...@nlnetlabs.nl Wayne Green wa...@infosavvy.com +Wei Song wson...@gmail.com Wolfgang S. Kechel wolfgang.kec...@prs.de Wolfram Wagner w...@mpi-sb.mpg.de Wwp subscr...@free.fr diff --git a/TODO b/TODO index d86d8d8..21ef4b9 100644 --- a/TODO +++ b/TODO @@ -125,10 +125,6 @@ we do the same in yacc.c. The code bw glr.c and yacc.c is really alike, we can certainly factor some parts. -* Header guards - -From François: should we keep the directory part in the CPP guard? - * Yacc.c: CPP Macros diff --git a/data/lalr1.cc b/data/lalr1.cc index 0fe3aee..ebfd4e1 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -37,7 +37,6 @@ b4_defines_if( [@output(b4_spec_defines_file@)@ b4_copyright([Skeleton interface for Bison LALR(1) parsers in C++], [2002-2012]) -dnl FIXME: This is wrong, we want computed header guards. [ /** ** \file ]b4_spec_defines_file[ @@ -46,8 +45,8 @@ dnl FIXME: This is wrong, we want computed header guards. /* C++ LALR(1) parser skeleton written by Akim Demaille. */ -#ifndef PARSER_HEADER_H -# define PARSER_HEADER_H +#ifndef @guard(]b4_spec_defines_file[@) +# define @guard(]b4_spec_defines_file[@) ]b4_percent_code_get([[requires]])[ @@ -286,7 +285,7 @@ b4_user_stype ]) b4_percent_code_get([[provides]])[]dnl -[#endif /* ! defined PARSER_HEADER_H */] +[#endif // !@guard(]b4_spec_defines_file[@)] ])dnl @output(b4_parser_file_name@)@ b4_copyright([Skeleton implementation for Bison LALR(1) parsers in C++], diff --git a/data/location.cc b/data/location.cc index 0ee02c2..06ce604 100644 --- a/data/location.cc +++ b/data/location.cc @@ -27,8 +27,8 @@ b4_copyright([Positions for Bison parsers in C++], ** Define the ]b4_namespace_ref[::position class. */ -#ifndef BISON_POSITION_HH -# define BISON_POSITION_HH +#ifndef @guard(]b4_dir_prefix[position.hh@) +# define @guard(]b4_dir_prefix[position.hh@) # include <iostream> # include <string> @@ -148,7 +148,7 @@ b4_copyright([Positions for Bison parsers in C++], } ]b4_namespace_close[ -#endif // not BISON_POSITION_HH] +#endif // !@guard(]b4_dir_prefix[position.hh@)] @output(b4_dir_prefix[]location.hh@)@ b4_copyright([Locations for Bison parsers in C++], [2002-2007, 2009-2012])[ @@ -158,8 +158,8 @@ b4_copyright([Locations for Bison parsers in C++], ** Define the ]b4_namespace_ref[::location class. */ -#ifndef BISON_LOCATION_HH -# define BISON_LOCATION_HH +#ifndef @guard(]b4_dir_prefix[location.hh@) +# define @guard(]b4_dir_prefix[location.hh@) # include <iostream> # include <string> @@ -295,6 +295,6 @@ b4_copyright([Locations for Bison parsers in C++], ]b4_namespace_close[ -#endif // not BISON_LOCATION_HH] +#endif // !@guard(]b4_dir_prefix[location.hh@)] m4_divert_pop(0) m4_changecom([#]) diff --git a/data/stack.hh b/data/stack.hh index 5293377..912e4ba 100644 --- a/data/stack.hh +++ b/data/stack.hh @@ -30,8 +30,8 @@ b4_copyright([Stack handling for Bison parsers in C++], ** Define the ]b4_namespace_ref[::stack class. */ -#ifndef BISON_STACK_HH -# define BISON_STACK_HH +#ifndef @guard(]b4_dir_prefix[stack.hh@) +# define @guard(]b4_dir_prefix[stack.hh@) # include <deque> @@ -119,7 +119,7 @@ b4_copyright([Stack handling for Bison parsers in C++], }; ]b4_namespace_close[ -#endif // not BISON_STACK_HH[]dnl +#endif // !@guard(]b4_dir_prefix[stack.hh@)[]dnl ] m4_divert_pop(0) m4_popdef([b4_copyright_years])dnl diff --git a/src/scan-skel.l b/src/scan-skel.l index 2433905..4b23d90 100644 --- a/src/scan-skel.l +++ b/src/scan-skel.l @@ -32,6 +32,7 @@ #include <dirname.h> #include <error.h> #include <quotearg.h> +#include <c-ctype.h> #include "complain.h" #include "getargs.h" @@ -173,6 +174,20 @@ skel_scanner_free (void) yylex_destroy (); } +/* Append SRC converted upper case alphanumeric or underscore to DST. + Return a pointer the next character in DST. */ +static +char * +append_guard (char *dst, char const *src) +{ + for ( ; *src; ++dst, ++src) + *dst = + c_islower (*src) ? c_toupper (*src) + : c_isalnum (*src) ? *src + : '_'; + return dst; +} + static void at_directive_perform (int at_directive_argc, char *at_directive_argv[], @@ -184,6 +199,23 @@ at_directive_perform (int at_directive_argc, fail_for_at_directive_too_many_args (at_directive_argv[0]); fputs (last_component (at_directive_argv[1]), yyout); } + else if (0 == strcmp (at_directive_argv[0], "@guard")) + { + if (at_directive_argc > 2) + fail_for_at_directive_too_many_args (at_directive_argv[0]); + else + { + char const *prefix = spec_name_prefix ? spec_name_prefix : "yy"; + char const *arg = at_directive_argv[1]; + char *guard = xmalloc (strlen (prefix) + 1 + strlen (arg) + 1); + char *cp = append_guard (guard, prefix); + *cp++ = '_'; + cp = append_guard (cp, arg); + *cp = 0; + fputs (guard, yyout); + free (guard); + } + } else if (0 == strcmp (at_directive_argv[0], "@warn") || 0 == strcmp (at_directive_argv[0], "@complain") || 0 == strcmp (at_directive_argv[0], "@fatal")) -- 1.7.10.2 _______________________________________________ help-bison@gnu.org https://lists.gnu.org/mailman/listinfo/help-bison