summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel E. Denny <jdenny@clemson.edu>2009-07-24 10:29:07 -0400
committerJoel E. Denny <jdenny@clemson.edu>2009-07-24 11:30:27 -0400
commit3208e3f4d9aff080a546685560536336f0852bcb (patch)
treed1c9132a18d60020ec3e6d93699ff8d1514d0bb2
parent13cdf208ed0f643dd00262ef87fcc7b83dab27a6 (diff)
downloadbison-3208e3f4d9aff080a546685560536336f0852bcb.tar.gz
Warn about character literals not of length one.
* NEWS (2.5): Document. * src/scan-gram.l (INITIAL): Remove comment that we don't check the length. (SC_ESCAPED_CHARACTER): Warn if length is wrong. * tests/input.at (Bad character literals): New test group. (cherry picked from commit ac9b0e954b1d3aed514a3bbd363da1514202af0f)
-rw-r--r--ChangeLog9
-rw-r--r--NEWS13
-rw-r--r--src/scan-gram.l32
-rw-r--r--tests/input.at62
4 files changed, 108 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index 7e2844a3..a67b8ca2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2009-07-24 Joel E. Denny <jdenny@ces.clemson.edu>
+
+ Warn about character literals not of length one.
+ * NEWS (2.5): Document.
+ * src/scan-gram.l (INITIAL): Remove comment that we don't check
+ the length.
+ (SC_ESCAPED_CHARACTER): Warn if length is wrong.
+ * tests/input.at (Bad character literals): New test group.
+
2009-07-24 Alex Rozenman <rozenmam@gmail.com>
Fix some memory leaks.
diff --git a/NEWS b/NEWS
index aa8c7ea2..b4887427 100644
--- a/NEWS
+++ b/NEWS
@@ -133,6 +133,19 @@ Bison News
about a missing semicolon where it did not before. Future releases of
Bison will cease to append semicolons entirely.
+** Character literals not of length one.
+
+ Previously, Bison quietly converted all character literals to length
+ one. For example, without warning, Bison interpreted the operators in
+ the following grammar to be the same token:
+
+ exp: exp '++'
+ | exp '+' exp
+ ;
+
+ Bison now warns when a character literal is not of length one. In
+ some future release, Bison will report an error instead.
+
* Changes in version 2.4.2 (????-??-??):
** %code is now a permanent feature.
diff --git a/src/scan-gram.l b/src/scan-gram.l
index 6813e605..5c0418c6 100644
--- a/src/scan-gram.l
+++ b/src/scan-gram.l
@@ -260,7 +260,7 @@ splice (\\[ \f\t\v]*\n)*
complain_at (*loc, _("invalid identifier: %s"), quote (yytext));
}
- /* Characters. We don't check there is only one. */
+ /* Characters. */
"'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
/* Strings. */
@@ -493,24 +493,40 @@ splice (\\[ \f\t\v]*\n)*
<SC_ESCAPED_CHARACTER>
{
"'"|"\n" {
- if (yytext[0] == '\n')
- unexpected_newline (token_start, "'");
STRING_GROW;
STRING_FINISH;
loc->start = token_start;
val->character = last_string[1];
+ {
+ /* FIXME: Eventually, make these errors. */
+ size_t length = strlen (last_string);
+ if (strlen (last_string) < 3)
+ warn_at (*loc, _("empty character literal"));
+ else if (strlen (last_string) > 3)
+ warn_at (*loc, _("extra characters in character literal"));
+ }
+ if (yytext[0] == '\n')
+ unexpected_newline (token_start, "'");
STRING_FREE;
BEGIN INITIAL;
return CHAR;
}
<<EOF>> {
- unexpected_eof (token_start, "'");
STRING_FINISH;
loc->start = token_start;
- if (strlen (last_string) > 1)
- val->character = last_string[1];
- else
- val->character = last_string[0];
+ {
+ size_t length = strlen (last_string);
+ /* FIXME: Eventually, make these errors. */
+ if (length < 2)
+ warn_at (*loc, _("empty character literal"));
+ else if (length > 2)
+ warn_at (*loc, _("extra characters in character literal"));
+ if (length > 1)
+ val->character = last_string[1];
+ else
+ val->character = last_string[0];
+ }
+ unexpected_eof (token_start, "'");
STRING_FREE;
BEGIN INITIAL;
return CHAR;
diff --git a/tests/input.at b/tests/input.at
index 51b8819f..9c62f9ef 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -1157,3 +1157,65 @@ AT_CHECK_NAMESPACE_ERROR([[::]],
[[namespace reference has a trailing "::"]])
AT_CLEANUP
+
+## ------------------------ ##
+## Bad character literals. ##
+## ------------------------ ##
+
+# Bison used to accept character literals that were empty or contained
+# too many characters.
+
+# FIXME: $ECHO_N and $ECHO_C are not very portable according to the
+# Autoconf manual. Switch to AS_ECHO_N when Autoconf 2.64 is released?
+# Even better, AT_DATA or some variant of AT_DATA may eventually permit
+# a trailing newline. See the threads starting at
+# <http://lists.gnu.org/archive/html/bison-patches/2009-07/msg00019.html>.
+
+AT_SETUP([[Bad character literals]])
+
+AT_DATA([empty.y],
+[[%%
+start: '';
+start: '
+]])
+echo $ECHO_N "start: '$ECHO_C" >> empty.y
+
+AT_BISON_CHECK([empty.y], [1], [],
+[[empty.y:2.8-9: warning: empty character literal
+empty.y:3.8-4.0: warning: empty character literal
+empty.y:3.8-4.0: missing `'' at end of line
+empty.y:4.8: warning: empty character literal
+empty.y:4.8: missing `'' at end of file
+]])
+
+AT_DATA([two.y],
+[[%%
+start: 'ab';
+start: 'ab
+]])
+echo $ECHO_N "start: 'ab$ECHO_C" >> two.y
+
+AT_BISON_CHECK([two.y], [1], [],
+[[two.y:2.8-11: warning: extra characters in character literal
+two.y:3.8-4.0: warning: extra characters in character literal
+two.y:3.8-4.0: missing `'' at end of line
+two.y:4.8-10: warning: extra characters in character literal
+two.y:4.8-10: missing `'' at end of file
+]])
+
+AT_DATA([three.y],
+[[%%
+start: 'abc';
+start: 'abc
+]])
+echo $ECHO_N "start: 'abc$ECHO_C" >> three.y
+
+AT_BISON_CHECK([three.y], [1], [],
+[[three.y:2.8-12: warning: extra characters in character literal
+three.y:3.8-4.0: warning: extra characters in character literal
+three.y:3.8-4.0: missing `'' at end of line
+three.y:4.8-11: warning: extra characters in character literal
+three.y:4.8-11: missing `'' at end of file
+]])
+
+AT_CLEANUP