summaryrefslogtreecommitdiff
path: root/examples/c
diff options
context:
space:
mode:
authorAkim Demaille <akim.demaille@gmail.com>2020-06-03 08:12:10 +0200
committerAkim Demaille <akim.demaille@gmail.com>2020-06-03 08:12:10 +0200
commit7e16bd2cae4f6762449927b2ca3159f44724a3ee (patch)
treee8c9c289569632ec9009a052d965236829aa5a92 /examples/c
parent94f7606db6f8e3d6c1cc71be6be304e16ba06a66 (diff)
parent508ac09939960eac8d30afab6e252469afe18714 (diff)
downloadbison-7e16bd2cae4f6762449927b2ca3159f44724a3ee.tar.gz
Merge maint into HEAD
* upstream/maint: maint: post-release administrivia version 3.6.3 build: check -Wmissing-prototypes tests: show logs c++: fix printing of state number on streams
Diffstat (limited to 'examples/c')
-rw-r--r--examples/c/bistromathic/local.mk1
-rw-r--r--examples/c/bistromathic/parse.y37
-rw-r--r--examples/c/calc/calc.y7
-rw-r--r--examples/c/calc/local.mk1
-rw-r--r--examples/c/lexcalc/local.mk2
-rw-r--r--examples/c/lexcalc/scan.l4
-rw-r--r--examples/c/mfcalc/local.mk1
-rw-r--r--examples/c/pushcalc/calc.y8
-rw-r--r--examples/c/pushcalc/local.mk1
-rw-r--r--examples/c/reccalc/local.mk2
-rw-r--r--examples/c/reccalc/scan.l4
-rw-r--r--examples/c/rpcalc/local.mk1
12 files changed, 44 insertions, 25 deletions
diff --git a/examples/c/bistromathic/local.mk b/examples/c/bistromathic/local.mk
index cad0425b..e9801ab2 100644
--- a/examples/c/bistromathic/local.mk
+++ b/examples/c/bistromathic/local.mk
@@ -31,6 +31,7 @@ if ENABLE_BISTROMATHIC
-DBISON_LOCALEDIR='"$(localdir)"' \
-DLOCALEDIR='"$(localdir)"' \
-I$(top_srcdir)/%D% -I$(top_builddir)/%D%
+ %C%_bistromathic_CFLAGS = $(TEST_CFLAGS)
%C%_bistromathic_LDADD = -lm $(LIBREADLINE) $(LIBINTL)
endif
diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y
index 78ec6eb7..dc048cfe 100644
--- a/examples/c/bistromathic/parse.y
+++ b/examples/c/bistromathic/parse.y
@@ -1,7 +1,6 @@
%require "3.6"
%code top {
- #include <assert.h>
#include <ctype.h> // isdigit
#include <locale.h> // LC_ALL
#include <math.h> // cos, sin, etc.
@@ -218,7 +217,7 @@ getsym (char const *name)
}
// How many symbols are registered.
-int
+static int
symbol_count (void)
{
int res = 0;
@@ -314,7 +313,7 @@ yylex (const char **line, YYSTYPE *yylval, YYLTYPE *yylloc)
`---------*/
-const char *
+static const char *
error_format_string (int argc)
{
switch (argc)
@@ -409,7 +408,8 @@ xstrndup (const char *string, size_t n)
const char *end = memchr (string, '\0', n);
size_t len = end ? (size_t) (end - string) : n;
char *new = malloc (len + 1);
- assert (new);
+ if (!new)
+ abort ();
new[len] = '\0';
return memcpy (new, string, len);
}
@@ -420,7 +420,8 @@ xstrndup (const char *string, size_t n)
`-----------*/
// Parse (and execute) this line.
-int process_line (YYLTYPE *lloc, const char *line)
+static int
+process_line (YYLTYPE *lloc, const char *line)
{
yypstate *ps = yypstate_new ();
int status = 0;
@@ -435,7 +436,8 @@ int process_line (YYLTYPE *lloc, const char *line)
}
// Get the list of possible tokens after INPUT was read.
-int
+// Returns a nonnegative.
+static int
expected_tokens (const char *input,
int *tokens, int ntokens)
{
@@ -456,6 +458,8 @@ expected_tokens (const char *input,
// Then query for the accepted tokens at this point.
int res = yypstate_expected_tokens (ps, tokens, ntokens);
+ if (res < 0)
+ abort ();
yypstate_delete (ps);
return res;
}
@@ -465,7 +469,7 @@ expected_tokens (const char *input,
// TEXT is the word to complete. We can use the entire contents of
// rl_line_buffer in case we want to do some simple parsing. Return
// the array of matches, or NULL if there aren't any.
-char **
+static char **
completion (const char *text, int start, int end)
{
YYDPRINTF ((stderr, "completion (\"%.*s[%.*s]%s\")\n",
@@ -475,14 +479,17 @@ completion (const char *text, int start, int end)
// Get list of token numbers.
int tokens[YYNTOKENS];
- char *line = xstrndup (rl_line_buffer, start);
+ char *line = xstrndup (rl_line_buffer, (size_t) start);
int ntokens = expected_tokens (line, tokens, YYNTOKENS);
free (line);
// Build MATCHES, the list of possible completions.
- const int len = strlen (text);
+ const size_t len = strlen (text);
// Need initial prefix and final NULL.
- char **matches = calloc (ntokens + symbol_count () + 2, sizeof *matches);
+ char **matches
+ = calloc ((size_t) ntokens + (size_t) symbol_count () + 2, sizeof *matches);
+ if (!matches)
+ abort ();
int match = 1;
for (int i = 0; i < ntokens; ++i)
switch (tokens[i])
@@ -512,9 +519,9 @@ completion (const char *text, int start, int end)
matches[0] = strdup (text);
else
{
- int lcplen = strlen (matches[1]);
+ size_t lcplen = strlen (matches[1]);
for (int i = 2; i < match && lcplen; ++i)
- for (int j = 0; j < lcplen; ++j)
+ for (size_t j = 0; j < lcplen; ++j)
if (matches[1][j] != matches[i][j])
lcplen = j;
matches[0] = xstrndup (matches[1], lcplen);
@@ -538,7 +545,8 @@ completion (const char *text, int start, int end)
return matches;
}
-void init_readline (void)
+static void
+init_readline (void)
{
// Allow conditional parsing of the ~/.inputrc file.
rl_readline_name = "bistromathic";
@@ -557,7 +565,8 @@ void init_readline (void)
| Main. |
`-------*/
-int main (int argc, char const* argv[])
+int
+main (int argc, char const* argv[])
{
#if defined ENABLE_NLS && ENABLE_NLS
// Set up internationalization.
diff --git a/examples/c/calc/calc.y b/examples/c/calc/calc.y
index ff571114..6daf22b3 100644
--- a/examples/c/calc/calc.y
+++ b/examples/c/calc/calc.y
@@ -1,7 +1,8 @@
%code top {
#include <assert.h>
#include <ctype.h> /* isdigit. */
- #include <stdio.h> /* For printf, etc. */
+ #include <stdio.h> /* printf. */
+ #include <stdlib.h> /* abort. */
#include <string.h> /* strcmp. */
int yylex (void);
@@ -74,8 +75,8 @@ yylex (void)
if (c == '.' || isdigit (c))
{
ungetc (c, stdin);
- int n = scanf ("%lf", &yylval.NUM);
- assert (n == 1);
+ if (scanf ("%lf", &yylval.NUM) != 1)
+ abort ();
return NUM;
}
diff --git a/examples/c/calc/local.mk b/examples/c/calc/local.mk
index 14b78f3b..503b034a 100644
--- a/examples/c/calc/local.mk
+++ b/examples/c/calc/local.mk
@@ -27,6 +27,7 @@ nodist_%C%_calc_SOURCES = %D%/calc.y
# Don't use gnulib's system headers.
%C%_calc_CPPFLAGS = -I$(top_srcdir)/%D% -I$(top_builddir)/%D%
+%C%_calc_CFLAGS = $(TEST_CFLAGS)
dist_calc_DATA = %D%/calc.y %D%/Makefile %D%/README.md
CLEANFILES += %D%/calc.[ch] %D%/calc.output %D%/scan.c
diff --git a/examples/c/lexcalc/local.mk b/examples/c/lexcalc/local.mk
index 0acd2607..b73887e2 100644
--- a/examples/c/lexcalc/local.mk
+++ b/examples/c/lexcalc/local.mk
@@ -25,6 +25,8 @@ if FLEX_WORKS
nodist_%C%_lexcalc_SOURCES = %D%/parse.y %D%/parse.h %D%/scan.l
# Don't use gnulib's system headers.
%C%_lexcalc_CPPFLAGS = -I$(top_srcdir)/%D% -I$(top_builddir)/%D%
+ # Fighting warnings triggered by Flex is just too painful.
+ # %C%_lexcalc_CFLAGS = $(TEST_CFLAGS)
endif FLEX_WORKS
%D%/parse.c: $(dependencies)
diff --git a/examples/c/lexcalc/scan.l b/examples/c/lexcalc/scan.l
index d66a23cf..708fd28b 100644
--- a/examples/c/lexcalc/scan.l
+++ b/examples/c/lexcalc/scan.l
@@ -4,7 +4,7 @@
%option nodefault noinput nounput noyywrap
%{
-#include <errno.h> /* errno, ERANGE */
+#include <errno.h> /* errno, ERANGE */
#include <limits.h> /* INT_MIN */
#include <stdlib.h> /* strtol */
@@ -12,7 +12,7 @@
// Each time a rule is matched, advance the end cursor/position.
#define YY_USER_ACTION \
- yylloc->last_column += yyleng;
+ yylloc->last_column += (int) yyleng;
// Move the first position onto the last.
#define LOCATION_STEP() \
diff --git a/examples/c/mfcalc/local.mk b/examples/c/mfcalc/local.mk
index 2c0fcb5e..69c91f71 100644
--- a/examples/c/mfcalc/local.mk
+++ b/examples/c/mfcalc/local.mk
@@ -31,6 +31,7 @@ nodist_%C%_mfcalc_SOURCES = $(mfcalc_sources)
%D%/mfcalc.c: $(dependencies)
# Don't use gnulib's system headers.
%C%_mfcalc_CPPFLAGS = -I$(top_srcdir)/%D% -I$(top_builddir)/%D%
+%C%_mfcalc_CFLAGS = $(TEST_CFLAGS)
%C%_mfcalc_LDADD = -lm
dist_TESTS += %D%/mfcalc.test
diff --git a/examples/c/pushcalc/calc.y b/examples/c/pushcalc/calc.y
index bc27ae95..6d49470c 100644
--- a/examples/c/pushcalc/calc.y
+++ b/examples/c/pushcalc/calc.y
@@ -1,7 +1,7 @@
%code top {
- #include <assert.h>
#include <ctype.h> /* isdigit. */
- #include <stdio.h> /* For printf, etc. */
+ #include <stdio.h> /* printf. */
+ #include <stdlib.h> /* abort. */
#include <string.h> /* strcmp. */
}
@@ -81,8 +81,8 @@ yylex (YYSTYPE *yylval)
if (c == '.' || isdigit (c))
{
ungetc (c, stdin);
- int n = scanf ("%lf", &yylval->NUM);
- assert (n == 1);
+ if (scanf ("%lf", &yylval->NUM) != 1)
+ abort ();
return NUM;
}
diff --git a/examples/c/pushcalc/local.mk b/examples/c/pushcalc/local.mk
index 9b6b19d6..7f53a0c1 100644
--- a/examples/c/pushcalc/local.mk
+++ b/examples/c/pushcalc/local.mk
@@ -27,6 +27,7 @@ nodist_%C%_calc_SOURCES = %D%/calc.y
# Don't use gnulib's system headers.
%C%_calc_CPPFLAGS = -I$(top_srcdir)/%D% -I$(top_builddir)/%D%
+%C%_calc_CFLAGS = $(TEST_CFLAGS)
dist_pushcalc_DATA = %D%/calc.y %D%/Makefile %D%/README.md
CLEANFILES += %D%/calc.[ch] %D%/calc.output
diff --git a/examples/c/reccalc/local.mk b/examples/c/reccalc/local.mk
index 0538f120..41a12828 100644
--- a/examples/c/reccalc/local.mk
+++ b/examples/c/reccalc/local.mk
@@ -26,6 +26,8 @@ if FLEX_WORKS
BUILT_SOURCES += $(nodist_%C%_reccalc_SOURCES)
# Don't use gnulib's system headers.
%C%_reccalc_CPPFLAGS = -I$(top_srcdir)/%D% -I$(top_builddir)/%D%
+ # Fighting warnings triggered by Flex is just too painful.
+ # %C%_reccalc_CFLAGS = $(TEST_CFLAGS)
endif FLEX_WORKS
%D%/parse.c: $(dependencies)
diff --git a/examples/c/reccalc/scan.l b/examples/c/reccalc/scan.l
index 0bf5210c..45d70d3b 100644
--- a/examples/c/reccalc/scan.l
+++ b/examples/c/reccalc/scan.l
@@ -32,9 +32,9 @@
do \
capacity = capacity ? 2 * capacity : 128; \
while (capacity < size + yyleng + 1); \
- str = realloc (str, capacity); \
+ str = realloc (str, (size_t) capacity); \
} \
- memcpy (str + size, yytext, yyleng); \
+ memcpy (str + size, yytext, (size_t) yyleng); \
size += yyleng; \
assert (size < capacity); \
} while (0)
diff --git a/examples/c/rpcalc/local.mk b/examples/c/rpcalc/local.mk
index f10f7c19..0c3bfd8a 100644
--- a/examples/c/rpcalc/local.mk
+++ b/examples/c/rpcalc/local.mk
@@ -31,6 +31,7 @@ nodist_%C%_rpcalc_SOURCES = $(rpcalc_sources)
%D%/rpcalc.c: $(dependencies)
# Don't use gnulib's system headers.
%C%_rpcalc_CPPFLAGS = -I$(top_builddir)/%D%
+%C%_rpcalc_CFLAGS = $(TEST_CFLAGS)
%C%_rpcalc_LDADD = -lm
dist_TESTS += %D%/rpcalc.test