Module Name:    src
Committed By:   rillig
Date:           Thu Jul  8 18:50:57 UTC 2021

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_206.c msg_206.exp
        src/usr.bin/xlint/lint1: cgram.y

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/xlint/lint1/msg_206.c \
    src/tests/usr.bin/xlint/lint1/msg_206.exp
cvs rdiff -u -r1.271 -r1.272 src/usr.bin/xlint/lint1/cgram.y

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/lint1/msg_206.c
diff -u src/tests/usr.bin/xlint/lint1/msg_206.c:1.2 src/tests/usr.bin/xlint/lint1/msg_206.c:1.3
--- src/tests/usr.bin/xlint/lint1/msg_206.c:1.2	Sun Feb 21 09:07:58 2021
+++ src/tests/usr.bin/xlint/lint1/msg_206.c	Thu Jul  8 18:50:57 2021
@@ -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] */
+}
Index: src/tests/usr.bin/xlint/lint1/msg_206.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_206.exp:1.2 src/tests/usr.bin/xlint/lint1/msg_206.exp:1.3
--- src/tests/usr.bin/xlint/lint1/msg_206.exp:1.2	Sun Mar 21 20:45:00 2021
+++ src/tests/usr.bin/xlint/lint1/msg_206.exp	Thu Jul  8 18:50:57 2021
@@ -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]

Index: src/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.271 src/usr.bin/xlint/lint1/cgram.y:1.272
--- src/usr.bin/xlint/lint1/cgram.y:1.271	Thu Jul  8 04:09:10 2021
+++ src/usr.bin/xlint/lint1/cgram.y	Thu Jul  8 18:50:57 2021
@@ -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 @@ enum_declaration:
 		symtyp = FVFT;
 		enumval = 0;
 	  } enumerator_list enumerator_list_comma_opt T_RBRACE {
+	  	/* FIXME: $2 must be $3 */
 		$$ = $2;
 	  }
 	;

Reply via email to