Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: demonstrate bug in handling enum d...



details:   https://anonhg.NetBSD.org/src/rev/3e57b5bf4c77
branches:  trunk
changeset: 1022196:3e57b5bf4c77
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Jul 08 18:50:57 2021 +0000

description:
lint: demonstrate bug in handling enum declarations (since today)

Since cgram.y 1.270 from today (a "cleanup" commit), the enum constants
were only registered in the symbol table, but they were not added to the
enum type (en_first_enumerator).  That information is used for
validating switch statements on enum types.

The actual bug is an off-by-one error in the grammar, in the grammar
rule 'enum_declaration'.  Yacc does not notice this obvious error.
Bison does, but it is not involved in building lint.

In the grammar rule 'enum_declaration', the intended $3 contains the
first enumeration constant of the type, while $2, which yacc interprets
as a symbol, contains a null pointer, at least on x86_64.

The existing tests did not cover this scenario, so the bug went
unnoticed.

diffstat:

 tests/usr.bin/xlint/lint1/msg_206.c   |  28 +++++++++++++++++++++++++---
 tests/usr.bin/xlint/lint1/msg_206.exp |   3 ++-
 usr.bin/xlint/lint1/cgram.y           |   5 +++--
 3 files changed, 30 insertions(+), 6 deletions(-)

diffs (70 lines):

diff -r f37ae43f0436 -r 3e57b5bf4c77 tests/usr.bin/xlint/lint1/msg_206.c
--- a/tests/usr.bin/xlint/lint1/msg_206.c       Thu Jul 08 18:10:52 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_206.c       Thu Jul 08 18:50:57 2021 +0000
@@ -1,7 +1,29 @@
-/*     $NetBSD: msg_206.c,v 1.2 2021/02/21 09:07:58 rillig Exp $       */
+/*     $NetBSD: msg_206.c,v 1.3 2021/07/08 18:50:57 rillig Exp $       */
 # 3 "msg_206.c"
 
 // Test for message: enumeration value(s) not handled in switch [206]
 
-TODO: "Add example code that triggers the above message." /* expect: 249 */
-TODO: "Add example code that almost triggers the above message."
+/* lint1-extra-flags: -eh */
+
+enum number {
+       ONE, TWO, THREE
+};
+
+void
+test(enum number num)
+{
+       switch (num) {
+       case ONE:
+       case TWO:
+               break;
+       }
+       /* expect-1: warning: enumeration value(s) not handled in switch [206] */
+
+       switch (num) {
+       case ONE:
+       case TWO:
+       case THREE:
+               break;
+       }
+       /* FIXME *//* expect-1: warning: enumeration value(s) not handled in switch [206] */
+}
diff -r f37ae43f0436 -r 3e57b5bf4c77 tests/usr.bin/xlint/lint1/msg_206.exp
--- a/tests/usr.bin/xlint/lint1/msg_206.exp     Thu Jul 08 18:10:52 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_206.exp     Thu Jul 08 18:50:57 2021 +0000
@@ -1,1 +1,2 @@
-msg_206.c(6): error: syntax error ':' [249]
+msg_206.c(19): warning: enumeration value(s) not handled in switch [206]
+msg_206.c(27): warning: enumeration value(s) not handled in switch [206]
diff -r f37ae43f0436 -r 3e57b5bf4c77 usr.bin/xlint/lint1/cgram.y
--- a/usr.bin/xlint/lint1/cgram.y       Thu Jul 08 18:10:52 2021 +0000
+++ b/usr.bin/xlint/lint1/cgram.y       Thu Jul 08 18:50:57 2021 +0000
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.271 2021/07/08 04:09:10 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.272 2021/07/08 18:50:57 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: cgram.y,v 1.271 2021/07/08 04:09:10 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.272 2021/07/08 18:50:57 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -853,6 +853,7 @@
                symtyp = FVFT;
                enumval = 0;
          } enumerator_list enumerator_list_comma_opt T_RBRACE {
+               /* FIXME: $2 must be $3 */
                $$ = $2;
          }
        ;



Home | Main Index | Thread Index | Old Index