Update URLs to prefer https: to http:
[bison.git] / README-hacking.md
blob20d7285f5e7957eda50b5222adcb4638006f4955
1 This file attempts to describe the rules to use when hacking Bison.
2 Don't put this file into the distribution.
4 Everything related to the development of Bison is on Savannah:
5 https://savannah.gnu.org/projects/bison/.
8 Working from the Repository
9 ===========================
11 These notes intend to help people working on the checked-out sources.  These
12 requirements do not apply when building from a distribution tarball.
14 ## Requirements
16 We've opted to keep only the highest-level sources in the repository.  This
17 eases our maintenance burden, (fewer merges etc.), but imposes more
18 requirements on anyone wishing to build from the just-checked-out sources.
19 For example, you have to use the latest stable versions of the maintainer
20 tools we depend upon, including:
22 - Autoconf <https://www.gnu.org/software/autoconf/>
23 - Automake <https://www.gnu.org/software/automake/>
24 - Flex <https://www.gnu.org/software/flex/>
25 - Gettext <https://www.gnu.org/software/gettext/>
26 - Gperf <https://www.gnu.org/software/gperf/>
27 - Graphviz <https://www.graphviz.org>
28 - Gzip <https://www.gnu.org/software/gzip/>
29 - Help2man <https://www.gnu.org/software/help2man/>
30 - Perl <https://www.cpan.org/>
31 - Rsync <https://rsync.samba.org/>
32 - Tar <https://www.gnu.org/software/tar/>
33 - Texinfo <https://www.gnu.org/software/texinfo/>
35 Valgrind <https://www.valgrind.org/> is also highly recommended, if it
36 supports your architecture.
38 If you're using a GNU/Linux distribution, the easiest way to install the
39 above packages depends on your system.  The following shell command should
40 work for Debian-based systems such as Ubuntu:
42     sudo apt-get install \
43       autoconf automake autopoint flex gperf graphviz help2man texinfo valgrind
45 Bison is written using Bison grammars, so there are bootstrapping issues.
46 The bootstrap script attempts to discover when the C code generated from the
47 grammars is out of date, and to bootstrap with an out-of-date version of the
48 C code, but the process is not foolproof.  Also, you may run into similar
49 problems yourself if you modify Bison.
51 Only building the initial full source tree will be a bit painful.  Later,
52 after synchronizing from the repository a plain 'make' should be sufficient.
53 Note, however, that when gnulib is updated, running './bootstrap' again
54 might be needed.
56 ## First checkout
58 Obviously, if you are reading these notes, you did manage to check out this
59 package from the repository.  For the record, you will find all the relevant
60 information on https://savannah.gnu.org/git/?group=bison.
62 Bison uses Git submodules: subscriptions to other Git repositories.  In
63 particular it uses gnulib, the GNU portability library.  To ask Git to
64 perform the first checkout of the submodules, run
66     $ git submodule update --init
68 The next step is to get other files needed to build, which are extracted
69 from other source packages:
71     $ ./bootstrap
73 Bootstrapping updates the submodules to the versions registered in the
74 top-level directory.  To change gnulib, first check out the version you want
75 in `gnulib`, then commit this change in Bison's repository, and finally run
76 bootstrap.
78 If it fails with missing symbols (e.g., `error: possibly undefined macro:
79 AC_PROG_GNU_M4`), you are likely to have forgotten the submodule
80 initialization part.  To recover from it, run `git reset --hard HEAD`, and
81 restart with the submodule initialization.  Otherwise, there you are!  Just
83     $ ./configure
84     $ make
85     $ make check
87 At this point, there should be no difference between your local copy, and
88 the master copy:
90     $ git diff
92 should output no difference.
94 Enjoy!
96 ## Updating
98 If you have git at version 1.8.2 or later, the command
100     $ git submodule update --recursive --remote
102 will be useful for updating to the latest version of all submodules.
104 Under earlier versions, use of submodules make things somewhat different
105 because git does not yet support recursive operations: submodules must be
106 taken care of explicitly.
108 ### Updating Bison
110 If you pull a newer version of a branch, say via `git pull`, you might
111 import requests for updated submodules.  A simple `git diff` will reveal if
112 the current version of the submodule (i.e., the actual contents of the
113 gnulib directory) and the current request from the subscriber (i.e., the
114 reference of the version of gnulib that the Bison repository requests)
115 differ.  To upgrade the submodules (i.e., to check out the version that is
116 actually requested by the subscriber, run `git submodule update`.
118     $ git pull
119     $ git submodule update
121 ### Updating a submodule
122 To update a submodule, say gnulib, do as follows:
124 Get the most recent version of the master branch from git.
126     $ cd gnulib
127     $ git fetch
128     $ git checkout -b master --track origin/master
130 Make sure Bison can live with that version of gnulib.
132     $ cd ..
133     $ ./bootstrap
134     $ make distcheck
136 Register your changes.
138     $ git commit ...
140 For a suggestion of what gnulib commit might be stable enough for a formal
141 release, see the ChangeLog in the latest gnulib snapshot at
142 https://erislabs.net/ianb/projects/gnulib/.
144 The Autoconf files we use are currently:
145 - m4/m4.m4
146 - lib/m4sugar/m4sugar.m4
147 - lib/m4sugar/foreach.m4
149 These files don't change very often in Autoconf, so it should be relatively
150 straight-forward to examine the differences in order to decide whether to
151 update.
153 ## Troubleshooting
155 Bison is self-hosted: its parser is generated by Bison.  We don't force
156 ourselves to use the previous release of Bison, we use the current git
157 master for several reasons:
158 - dogfooding: let Bison be its first user
159 - monitoring: seeing the diff on the generated parsers with git is very
160   helpful, as it allows to see easily the impact of the changes on a real
161   case parser.
163 If you are unlucky the generated files, src/parse-gram.[ch], may be older
164 than their source, src/parse-gram.y.  And your current version of Bison
165 might not be able to grok it.  In that case, first refresh the generated
166 files:
168     $ touch src/parse-gram.[ch]
170 Then proceed.
172 In case you wrecked your current copy of the parser, get back the previous
173 version, compile bison, then force it to recreate the files:
175     $ git checkout HEAD^ src/parse-gram.[ch]
176     $ make -C _build
177     $ touch src/parse-gram.y
178     $ make -C _build
181 Administrivia
182 =============
184 ## If you incorporate a change from somebody on the net:
185 First, if it is a large change, you must make sure they have signed the
186 appropriate paperwork.  Second, be sure to add their name and email address
187 to THANKS.
189 ## If a change fixes a test, mention the test in the commit message.
191 ## Bug reports
192 If somebody reports a new bug, mention his name in the commit message and in
193 the test case you write.  Put him into THANKS.
195 The correct response to most actual bugs is to write a new test case which
196 demonstrates the bug.  Then fix the bug, re-run the test suite, and check
197 everything in.
201 Hacking
202 =======
204 ## Visible Changes
205 Which include serious bug fixes, must be mentioned in NEWS.
207 ## Translations
208 Only user visible strings are to be translated: error messages, bits of the
209 .output file etc.  This excludes impossible error messages (comparable to
210 assert/abort), and all the --trace output which is meant for the maintainers
211 only.
213 ## Vocabulary
214 - "lookahead", not "look-ahead".
215 - "midrule", not "mid-rule".
216 - "nonterminal", not "variable" or "non-terminal" or "non terminal".
217   Abbreviated as "nterm".
218 - "shift/reduce" and "reduce/reduce", not "shift-reduce" or "shift reduce",
219   etc.
221 ## Syntax Highlighting
222 It's quite nice to be in C++ mode when editing lalr1.cc for instance.
223 However tools such as Emacs will be fooled by the fact that braces and
224 parens do not nest, as in `[[}]]`.  As a consequence you might be misguided
225 by its visual pairing to parens.  The m4-mode is safer.  Unfortunately the
226 m4-mode is also fooled by `#` which is sees as a comment, stops pairing with
227 parens/brackets that are inside...
229 ## Implementation Notes
230 There are several places with interesting details about the implementation:
231 - [Understanding C parsers generated by GNU
232 Bison](https://www.cs.uic.edu/~spopuri/cparser.html) by Satya Kiran Popuri,
233 is a wonderful piece of work that explains the implementation of Bison,
234 - [src/gram.h](src/gram.h) documents the way the grammar is represented
235 - [src/tables.h](src/tables.h) documents the generated tables
236 - [data/README.md](data/README.md) contains details about the m4 implementation
238 ## Coding Style
239 Do not add horizontal tab characters to any file in Bison's repository
240 except where required.  For example, do not use tabs to format C code.
241 However, make files, ChangeLog, and some regular expressions require tabs.
242 Also, test cases might need to contain tabs to check that Bison properly
243 processes tabs in its input.
245 Prefer `res` as the name of the local variable that will be "return"ed by
246 the function.
248 In writing arithmetic comparisons, use "<" and "<=" rather than ">" and ">="
249 <https://public-inbox.org/git/7vfyw7yebj.fsf_-_@assigned-by-dhcp.cox.net/>.
251 ### Bison
252 Follow the GNU Coding Standards.
254 Don't reinvent the wheel: we use gnulib, which features many components.
255 Actually, Bison has legacy code that we should replace with gnulib modules
256 (e.g., many ad hoc implementations of lists).
258 #### Includes
259 The `#include` directives follow an order:
260 - first section for *.c files is `<config.h>`.  Don't include it in header
261   files
262 - then, for *.c files, the corresponding *.h file
263 - then possibly the `"system.h"` header
264 - then the system headers.
265   Consider headers from `lib/` like system headers (i.e., `#include
266   <verify.h>`, not `#include "verify.h"`).
267 - then headers from src/ with double quotes (`#include "getargs.h"`).
269 Keep headers sorted alphabetically in each section.
271 See also the [Header
272 files](https://www.gnu.org/software/gnulib/manual/html_node/Header-files.html)
273 and the [Implementation
274 files](https://www.gnu.org/software/gnulib/manual/html_node/Implementation-files.html#Implementation-files)
275 nodes of the gnulib documentation.
277 Some source files are in the build tree (e.g., `src/scan-gram.c` made from
278 `src/scan-gram.l`).  For them to find the headers from `src/`, we actually
279 use `#include "src/getargs.h"` instead of `#include "getargs.h"`---that
280 saves us from additional `-I` flags.
282 ### Skeletons
283 We try to use the "typical" coding style for each language.
285 #### CPP
286 We indent the CPP directives this way:
289 #if FOO
290 # if BAR
291 #  define BAZ
292 # endif
293 #endif
296 Don't indent with leading spaces in the skeletons (it's OK in the grammar
297 files though, e.g., in `%code {...}` blocks).
299 On occasions, use `cppi -c` to see where we stand.  We don't aim at full
300 correctness: depending `-d`, some bits can be in the *.c file, or the *.h
301 file within the double-inclusion cpp-guards.  In that case, favor the case
302 of the *.h file, but don't waste time on this.
304 Don't hesitate to leave a comment on the `#endif` (e.g., `#endif /* FOO
305 */`), especially for long blocks.
307 There is no consistency on `! defined` vs. `!defined`.  The day gnulib
308 decides, we'll follow them.
310 #### C/C++
311 Follow the GNU Coding Standards.
313 The `glr.c` skeleton was implemented with `camlCase`.  We are migrating it
314 to `snake_case`.  Because we are gradually standardizing the code, it is
315 currently inconsistent.
317 Use `YYFOO` and `yyfoo` for entities that are exposed to the user.  They are
318 part of our contract with the users wrt backward compatibility.
320 Use `YY_FOO` and `yy_foo` for private matters.  Users should not use them,
321 we are free to change them without fear of backward compatibility issues.
323 Use `*_t` for types, especially for `yy*_t` in which case we shouldn't worry
324 about the C standard introducing such a name.
326 #### C++
327 Follow the [C++ Core
328 Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines).
329 The [Google ones](https://google.github.io/styleguide/cppguide.html) may be
330 interesting too.
332 Our enumerators, such as the kinds (symbol and token kinds), should be lower
333 case, but it was too late to follow that track for token kinds, and symbol
334 kind enumerators are made to be consistent with them.
336 Use `*_type` for type aliases.  Use `foo_get()` and `foo_set(v)` for
337 accessors, or simply `foo()` and `foo(v)`.
339 Use the `yy` prefix for private stuff, but there's no need for it in the
340 public API.  The `yy` prefix is already taken care of via the namespace.
342 #### Java
343 We follow the [Java Code
344 Conventions](https://www.oracle.com/technetwork/java/codeconventions-150003.pdf)
345 and [Google Java Style
346 Guide](https://google.github.io/styleguide/javaguide.html).  Unfortunately
347 at some point some GNU Coding Style was installed in Java, but it's an
348 error.  So we should for instance stop putting spaces in function calls.
349 Because we are standardizing the code, it is currently inconsistent.
351 Use a 2-space indentation (Google) rather than 4 (Oracle).
353 Don't use the "yy" prefix for public members: "getExpectedTokens", not
354 "yyexpectedTokens" or "yygetExpectedTokens".
356 ## Commit Messages
357 Imitate the style we use.  Use `git log` to get sources of inspiration.
359 If the changes have a small impact on Bison's generated parser, embed these
360 changes in the commit itself.  If the impact is large, first push all the
361 changes except those about src/parse-gram.[ch], and then another commit
362 named "regen" which is only about them.
364 ## Debugging
365 Bison supports tracing of its various steps, via the `--trace` option.
366 Since it is not meant for the end user, it is not displayed by `bison
367 --help`, nor is it documented in the manual.  Instead, run `bison
368 --trace=help`.
370 ## Documentation
371 Use `@option` for options and options with their argument if they have no
372 space (e.g., `@option{-Dfoo=bar}`).  However, use `@samp` elsewhere (e.g.,
373 `@samp{-I foo}`).
376 Test Suite
377 ==========
379 ## make check
380 Consume without moderation.  It is composed of two kinds of tests: the
381 examples, and the main test suite.
383 ### The Examples
384 In examples/, there is a number of ready-to-use examples (see
385 [examples/README.md](examples/README.md)).  These examples have small test
386 suites run by `make check`.  The test results are in local `*.log` files
387 (e.g., `$build/examples/c/calc/calc.log`).
389 ### The Main Test Suite
390 The main test suite, in tests/, is written on top of GNU Autotest, which is
391 part of Autoconf.  Run `info autoconf 'Using Autotest'` to read the
392 documentation, not only about how to write tests, but also where are the
393 logs, how to read them etc.
395 The main test suite generates a log for each test (e.g.,
396 `$build/tests/testsuite.dir/004/testsuite.log` for test #4), and a main log
397 file in `$build/tests/testsuite.log`.  The latter is meant for end users: it
398 contains lots of details that should help diagnosing issues, including build
399 issues.  The per-test logs are more convenient when working locally.
401 #### TESTSUITEFLAGS
402 To run just the main test suite, run `make check-local`.
404 The default is for make check-local to run all tests sequentially.  This can
405 be very time consuming when checking repeatedly or on slower setups.  This
406 can be sped up in two ways:
408 Using -j, in a make-like fashion, for example:
410     $ make check-local TESTSUITEFLAGS='-j8'
412 Actually, when using GNU Make, TESTSUITEFLAGS defaults to the -jN passed to
413 it, so you may simply run
415     $ make check-local -j8
417 Running only the tests of a certain category, as specified in the AT files
418 with AT_KEYWORDS([[category]]). Categories include:
419 - c++, for c++ parsers
420 - deprec, for tests concerning deprecated constructs.
421 - glr, for glr parsers
422 - java, for java parsers
423 - report, for automaton dumps
425 To get a list of all the tests (and their keywords for -k), run
427     $ ./tests/testsuite -l
429 To run a specific set of tests, use -k (for "keyword"). For example:
431     $ make check-local TESTSUITEFLAGS='-k c++'
433 Both can be combined.
435     $ make check-local TESTSUITEFLAGS='-j8 -k c++'
437 To rerun the tests that failed:
439     $ make recheck -j5
441 #### Updating the Expectations
442 Sometimes some changes have a large impact on the test suite (e.g., when we
443 added the `[-Wother]` part to all the warnings).  Part of the update can be
444 done with a crude tool: `build-aux/update-test`.
446 Once you ran the test suite, and therefore have many `testsuite.log` files,
447 run `make update-tests`.  Or, by hand, from the *source* tree:
449     $ ./build-aux/update-test $build/tests/testsuite.dir/*/testsuite.log
451 where `$build` would be your build tree.  This will hopefully update most
452 tests.  Re-run the test suite.  It might be interesting to run `update-test`
453 again, since some early failures may stop latter tests from being run.  Yet
454 at some point, you'll have to fix remaining issues by hand...
457 ## Running Java parsers
458 Use the `javaexec.sh` script.  For instance to run the parser of test case
459 504:
461     $ sh ./_build/javaexec.sh -cp ./_build/tests/testsuite.dir/504 Calc
463 ## Using Sanitizers
464 Address sanitizer (ASAN) and undefined-behavior sanitizer (UBSAN) are very
465 useful.  Here's one way to set ASAN up with GCC 10 on Mac Ports
467 1. Configure with
469     $ ./configure -C --enable-gcc-warnings \
470         CPPFLAGS='-isystem /opt/local/include' \
471         CC='gcc-mp-10 -fsanitize=address' \
472         CFLAGS='-ggdb' \
473         CXX='g++-mp-10.0 -fsanitize=address' \
474         CXXFLAGS='-ggdb' \
475         LDFLAGS='-L/opt/local/lib'
477 2. Compile
479 3. Generate debug symbols:
481     $ dsymutil src/bison
483 4. Run the tests with leak detection enabled
484    (`ASAN_OPTIONS=detect_leaks=1`).  E.g. for counterexamples:
486     $ make check-local TESTSUITEFLAGS='-j5 -k cex' ASAN_OPTIONS=detect_leaks=1
488 ## make maintainer-check-valgrind
489 This target uses valgrind both to check bison, and the generated parsers.
491 This is not mature on Mac OS X.  First, Valgrind does support the way bison
492 calls m4, so Valgrind cannot be used to check bison on Mac OS X.
494 Second, there are many errors that come from the platform itself, not from
495 bison.  build-aux/darwin11.4.0.valgrind addresses some of them.
497 Third, valgrind issues warnings such as:
499     --99312:0:syswrap- WARNING: Ignoring sigreturn( ..., UC_RESET_ALT_STACK );
501 which cause the test to fail uselessly.  It is hard to ignore these errors
502 with a major overhaul of the way instrumentation is performed in the test
503 suite.  So currently, do not try to run valgrind on Mac OS X.
505 ## Release checks
506 Try to run the test suite with more severe conditions before a
507 release:
509 - Configure the package with --enable-gcc-warnings, so that one checks that
510   1. Bison compiles cleanly, 2. the parsers it produces compile cleanly too.
512 - Maybe build with -DGNULIB_POSIXCHECK, which suggests gnulib modules that
513   can fix portability issues.  See if you really want to pay attention to
514   its warnings; there's no need to obey blindly to it
515   (<https://lists.gnu.org/r/bison-patches/2012-05/msg00057.html>).
517 - Check with `make syntax-check` if there are issues diagnosed by gnulib.
519 - run `make maintainer-check` which:
520   - runs `valgrind -q bison` to run Bison under Valgrind.
521   - runs the parsers under Valgrind.
522   - runs the test suite with G++ as C compiler...
524 - run `make maintainer-check-push`, which runs `make maintainer-check` while
525   activating the push implementation and its pull interface wrappers in many
526   test cases that were originally written to exercise only the pull
527   implementation.  This makes certain the push implementation can perform
528   every task the pull implementation can.
530 - run `make maintainer-check-xml`, which runs `make maintainer-check` while
531   checking Bison's XML automaton report for every working grammar passed to
532   Bison in the test suite.  The check just diffs the output of Bison's
533   included XSLT style sheets with the output of --report=all and --graph.
535 - running `make maintainer-check-release` takes care of running
536   maintainer-check, maintainer-check-push and maintainer-check-xml.
538 - Change tests/atlocal/CFLAGS to add your preferred options.
540 - Test with a very recent version of GCC for both C and C++.  Testing with
541   older versions that are still in use is nice too.
543 ## gnulib
544 To run tests on gnulib components (e.g., on bitset):
546     cd gnulib
547     ./gnulib-tool --test bitset-tests
549 possibly within a specified environment:
551     CC='gcc-mp-8 -fsanitize=undefined' ./gnulib-tool --test bitset-tests
553 To be able to run the tests several times, and to use symlinks instead of
554 copies so that one can update the origin gnulib directory and immediately
555 re-run the tests, run:
557     ./gnulib-tool --symlink --create-test --dir=/tmp/gnutest bitset-tests
558     cd /tmp/gnutest
559     ./configure -C CC='gcc-mp-8 -fsanitize=undefined' CFLAGS='-ggdb'
560     make check
564 Release Procedure
565 =================
567 See the [README-release file](README-release), created when the package is
568 bootstrapped.
570 <!--
572 Copyright (C) 2002-2005, 2007-2015, 2018-2021 Free Software Foundation,
573 Inc.
575 This file is part of GNU Bison.
577 This program is free software: you can redistribute it and/or modify
578 it under the terms of the GNU General Public License as published by
579 the Free Software Foundation, either version 3 of the License, or
580 (at your option) any later version.
582 This program is distributed in the hope that it will be useful,
583 but WITHOUT ANY WARRANTY; without even the implied warranty of
584 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
585 GNU General Public License for more details.
587 You should have received a copy of the GNU General Public License
588 along with this program.  If not, see <https://www.gnu.org/licenses/>.
590 Local Variables:
591 mode: markdown
592 fill-column: 76
593 ispell-dictionary: "american"
594 End:
596 LocalWords:  Automake Autoconf Gettext Gzip Rsync Valgrind gnulib submodules
597 LocalWords:  submodule init cd distcheck ChangeLog valgrind sigreturn sudo
598 LocalWords:  UC gcc DGNULIB POSIXCHECK xml XSLT glr lalr README po runtime rc
599 LocalWords:  gnupload gnupg gpg keyserver BDF ncftp filename clearsign cvs dir
600 LocalWords:  symlinks vti html lt POSIX Cc'ed Graphviz Texinfo autoconf jN
601 LocalWords:  automake autopoint graphviz texinfo PROG Wother parsers YYFOO
602 LocalWords:  TESTSUITEFLAGS deprec struct gnulib's getopt config ggdb yyfoo
603 LocalWords:  bitset fsanitize symlink CFLAGS MERCHANTABILITY ispell wrt YY
604 LocalWords:  american Administrivia camlCase yy accessors namespace src hoc
605 LocalWords:  getExpectedTokens yyexpectedTokens yygetExpectedTokens parens
606 LocalWords:  regen dogfooding Autotest testsuite getargs CPP BAZ endif cppi
607 LocalWords:  cpp javaexec cp Calc ASAN UBSAN CPPFLAGS isystem CXX cex Gperf
608 LocalWords:  CXXFLAGS LDFLAGS dsymutil gperf