diagnostics: keep the fixits

Introduce proper support for fixits, instead of just printing them on
demand.

* bootstrap.conf: We need gnulib's xlists.
* src/fixits.h, src/fixits.c: New.
* src/complain.c (deprecated_directive): Use fixits_register.
* src/main.c (main): Use fixits_free.
This commit is contained in:
Akim Demaille
2019-01-12 18:22:41 +01:00
parent dad14ec3e4
commit ffe2e4aaec
7 changed files with 177 additions and 47 deletions

View File

@@ -17,7 +17,7 @@
# gnulib modules used by this package.
gnulib_modules='
argmatch assert
argmatch array-list assert
bitsetv
calloc-posix close closeout config-h c-strcase
configmake
@@ -26,7 +26,9 @@ gnulib_modules='
getopt-gnu
gettext git-version-gen gitlog-to-changelog
gpl-3.0 hash inttypes isnan javacomp-script
javaexec-script ldexpl malloc-gnu
javaexec-script
ldexpl
malloc-gnu
mbswidth
non-recursive-gnulib-prefix-hack
obstack
@@ -44,6 +46,7 @@ gnulib_modules='
xalloc
xalloc-die
xconcat-filename
xlist
xmemdup0
xstrndup

78
lib/.gitignore vendored
View File

@@ -8,6 +8,10 @@
/alignof.h
/alloca.h
/alloca.in.h
/allocator.c
/allocator.h
/areadlink.c
/areadlink.h
/arg-nonnull.h
/argmatch.c
/argmatch.h
@@ -18,6 +22,11 @@
/binary-io.h
/bitrotate.c
/bitrotate.h
/bitset.c
/bitset.h
/bitset/
/bitsetv.c
/bitsetv.h
/c++defs.h
/c-ctype.c
/c-ctype.h
@@ -26,6 +35,9 @@
/c-strcaseeq.h
/c-strncasecmp.c
/calloc.c
/canonicalize-lgpl.c
/careadlinkat.c
/careadlinkat.h
/charset.alias
/cloexec.c
/cloexec.h
@@ -78,6 +90,8 @@
/fseterr.h
/fstat.c
/getdtablesize.c
/gethrxtime.c
/gethrxtime.h
/getopt-cdefs.in.h
/getopt-core.h
/getopt-ext.h
@@ -90,7 +104,16 @@
/getopt_int.h
/getprogname.c
/getprogname.h
/getrusage.c
/gettext.h
/gettime.c
/gettimeofday.c
/gl_array_list.c
/gl_array_list.h
/gl_list.c
/gl_list.h
/gl_xlist.c
/gl_xlist.h
/gnulib.mk
/hard-locale.c
/hard-locale.h
@@ -112,6 +135,8 @@
/limits.in.h
/localcharset.c
/localcharset.h
/localtime-buffer.c
/localtime-buffer.h
/lstat.c
/malloc.c
/malloca.c
@@ -150,17 +175,23 @@
/printf.c
/progname.c
/progname.h
/progreloc.c
/quote.h
/quotearg.c
/quotearg.h
/raise.c
/rawmemchr.c
/rawmemchr.valgrind
/readlink.c
/realloc.c
/ref-add.sed
/ref-del.sed
/relocatable.c
/relocatable.h
/relocwrapper.c
/sched.h
/sched.in.h
/setenv.c
/sig-handler.c
/sig-handler.h
/sigaction.c
@@ -190,6 +221,8 @@
/spawnp.c
/sprintf.c
/stamp-h1
/stat-time.c
/stat-time.h
/stat-w32.c
/stat-w32.h
/stat.c
@@ -223,13 +256,20 @@
/strverscmp.c
/sys
/sys_ioctl.in.h
/sys_resource.in.h
/sys_socket.in.h
/sys_stat.in.h
/sys_time.in.h
/sys_times.in.h
/sys_types.in.h
/sys_wait.in.h
/sysexits.in.h
/time.h
/time.in.h
/timespec.c
/timespec.h
/timevar.c
/timevar.h
/unistd--.h
/unistd-safer.h
/unistd.c
@@ -267,45 +307,11 @@
/xmalloc.c
/xmemdup0.c
/xmemdup0.h
/xreadlink.c
/xreadlink.h
/xsize.c
/xsize.h
/xstrndup.c
/xstrndup.h
/stat-time.c
/stat-time.h
/timevar.c
/timevar.h
/sys_time.in.h
/sys_times.in.h
/gethrxtime.c
/gethrxtime.h
/getrusage.c
/gettime.c
/gettimeofday.c
/localtime-buffer.c
/localtime-buffer.h
/sys_resource.in.h
/timespec.c
/timespec.h
/xtime.c
/xtime.h
/bitset.c
/bitset.h
/bitset/
/bitsetv.c
/bitsetv.h
/allocator.c
/allocator.h
/areadlink.c
/areadlink.h
/canonicalize-lgpl.c
/careadlinkat.c
/careadlinkat.h
/progreloc.c
/readlink.c
/relocatable.c
/relocatable.h
/relocwrapper.c
/setenv.c
/xreadlink.c
/xreadlink.h

View File

@@ -28,9 +28,9 @@
#include "complain.h"
#include "files.h"
#include "fixits.h"
#include "getargs.h"
#include "quote.h"
#include "quotearg.h"
err_status complaint_status = status_none;
@@ -394,14 +394,7 @@ deprecated_directive (location const *loc, char const *old, char const *upd)
complain (loc, Wdeprecated,
_("deprecated directive: %s, use %s"),
quote (old), quote_n (1, upd));
/* GCC and Clang follow the same pattern.
http://clang.llvm.org/docs/UsersManual.html#cmdoption-fdiagnostics-parseable-fixits */
if (feature_flag & feature_fixit_parsable)
fprintf (stderr, "fix-it:%s:{%d:%d-%d:%d}:%s\n",
quotearg_n_style (1, c_quoting_style, loc->start.file),
loc->start.line, loc->start.column,
loc->end.line, loc->end.column,
quotearg_n_style (2, c_quoting_style, upd));
fixits_register (loc, upd);
}
void

95
src/fixits.c Normal file
View File

@@ -0,0 +1,95 @@
/* Support for fixing grammar files.
Copyright (C) 2019 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "fixits.h"
#include "system.h"
#include "getargs.h"
#include "gl_xlist.h"
#include "gl_array_list.h"
#include "quotearg.h"
typedef struct
{
location location;
char *fix;
} fixit;
gl_list_t fixits = NULL;
static fixit *
fixit_new (location const *loc, char const* fix)
{
fixit *res = xmalloc (sizeof *res);
res->location = *loc;
res->fix = xstrdup (fix);
return res;
}
static void
fixit_free (fixit *f)
{
free (f->fix);
free (f);
}
/* GCC and Clang follow the same pattern.
https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Message-Formatting-Options.html
http://clang.llvm.org/docs/UsersManual.html#cmdoption-fdiagnostics-parseable-fixits */
static void
fixit_print (fixit const *f, FILE *out)
{
fprintf (out, "fix-it:%s:{%d:%d-%d:%d}:%s\n",
quotearg_n_style (1, c_quoting_style, f->location.start.file),
f->location.start.line, f->location.start.column,
f->location.end.line, f->location.end.column,
quotearg_n_style (2, c_quoting_style, f->fix));
}
void
fixits_register (location const *loc, char const* fix)
{
if (!fixits)
fixits = gl_list_create_empty (GL_ARRAY_LIST,
/* equals */ NULL,
/* hashcode */ NULL,
(gl_listelement_dispose_fn) fixit_free,
true);
fixit *f = fixit_new (loc, fix);
gl_list_add_last (fixits, f);
if (feature_flag & feature_fixit_parsable)
fixit_print (f, stderr);
}
/* Free the registered fixits. */
void fixits_free (void)
{
if (fixits)
{
gl_list_free (fixits);
fixits = NULL;
}
}

29
src/fixits.h Normal file
View File

@@ -0,0 +1,29 @@
/* Support for fixing grammar files.
Copyright (C) 2019 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef FIXITS_H_
# define FIXITS_H_ 1
# include "location.h"
/* Declare a fix to apply. */
void fixits_register (location const *loc, char const* update);
/* Free the registered fixits. */
void fixits_free (void);
#endif /* !FIXITS_H_ */

View File

@@ -46,6 +46,8 @@ src_bison_SOURCES = \
src/derives.h \
src/files.c \
src/files.h \
src/fixits.c \
src/fixits.h \
src/flex-scanner.h \
src/getargs.c \
src/getargs.h \

View File

@@ -35,6 +35,7 @@
#include "conflicts.h"
#include "derives.h"
#include "files.h"
#include "fixits.h"
#include "getargs.h"
#include "gram.h"
#include "lalr.h"
@@ -205,6 +206,7 @@ main (int argc, char *argv[])
conflicts_free ();
grammar_free ();
output_file_names_free ();
fixits_free ();
/* The scanner memory cannot be released right after parsing, as it
contains things such as user actions, prologue, epilogue etc. */