Module Name: src
Committed By: rillig
Date: Sun Feb 21 09:17:56 UTC 2021
Modified Files:
src/tests/usr.bin/xlint/lint1: d_cvt_constant.c d_cvt_constant.exp
msg_217.c
src/usr.bin/xlint/lint1: func.c
Log Message:
lint: fix wrong warning about main falling off the bottom in C99 mode
This gets lint a small step closer to implementing C99.
To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/xlint/lint1/d_cvt_constant.c
cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/xlint/lint1/d_cvt_constant.exp \
src/tests/usr.bin/xlint/lint1/msg_217.c
cvs rdiff -u -r1.71 -r1.72 src/usr.bin/xlint/lint1/func.c
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/d_cvt_constant.c
diff -u src/tests/usr.bin/xlint/lint1/d_cvt_constant.c:1.4 src/tests/usr.bin/xlint/lint1/d_cvt_constant.c:1.5
--- src/tests/usr.bin/xlint/lint1/d_cvt_constant.c:1.4 Sun Feb 21 09:07:58 2021
+++ src/tests/usr.bin/xlint/lint1/d_cvt_constant.c Sun Feb 21 09:17:55 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: d_cvt_constant.c,v 1.4 2021/02/21 09:07:58 rillig Exp $ */
+/* $NetBSD: d_cvt_constant.c,v 1.5 2021/02/21 09:17:55 rillig Exp $ */
# 3 "d_cvt_constant.c"
/* the second assignment assumes failed before */
@@ -9,6 +9,4 @@ main(void)
int foo = 0;
if (foo)
x = 1;
-} /* expect: 217 */
-// FIXME: Since C99, main may fall off the bottom without returning a value.
-// Therefore there should be no warning 217.
+}
Index: src/tests/usr.bin/xlint/lint1/d_cvt_constant.exp
diff -u src/tests/usr.bin/xlint/lint1/d_cvt_constant.exp:1.3 src/tests/usr.bin/xlint/lint1/d_cvt_constant.exp:1.4
--- src/tests/usr.bin/xlint/lint1/d_cvt_constant.exp:1.3 Sun Jan 31 14:57:28 2021
+++ src/tests/usr.bin/xlint/lint1/d_cvt_constant.exp Sun Feb 21 09:17:55 2021
@@ -1,2 +1 @@
d_cvt_constant.c(8): warning: x set but not used in function main [191]
-d_cvt_constant.c(12): warning: function main falls off bottom without returning value [217]
Index: src/tests/usr.bin/xlint/lint1/msg_217.c
diff -u src/tests/usr.bin/xlint/lint1/msg_217.c:1.3 src/tests/usr.bin/xlint/lint1/msg_217.c:1.4
--- src/tests/usr.bin/xlint/lint1/msg_217.c:1.3 Sun Jan 31 13:33:10 2021
+++ src/tests/usr.bin/xlint/lint1/msg_217.c Sun Feb 21 09:17:55 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: msg_217.c,v 1.3 2021/01/31 13:33:10 rillig Exp $ */
+/* $NetBSD: msg_217.c,v 1.4 2021/02/21 09:17:55 rillig Exp $ */
# 3 "msg_217.c"
// Test for message: function %s falls off bottom without returning value [217]
@@ -30,3 +30,15 @@ do_while_return(int i)
return i;
} while (/*CONSTCOND*/0); /*FIXME*//* expect: 193 */
} /*FIXME*//* expect: 217 */
+
+/*
+ * C99 5.1.2.2.3 "Program termination" p1 defines that as a special exception,
+ * the function 'main' does not have to return a value, reaching the bottom
+ * is equivalent to returning 0.
+ *
+ * Before func.c 1.72 from 2021-02-21, lint had wrongly warned about this.
+ */
+int
+main(void)
+{
+}
Index: src/usr.bin/xlint/lint1/func.c
diff -u src/usr.bin/xlint/lint1/func.c:1.71 src/usr.bin/xlint/lint1/func.c:1.72
--- src/usr.bin/xlint/lint1/func.c:1.71 Fri Feb 19 22:27:49 2021
+++ src/usr.bin/xlint/lint1/func.c Sun Feb 21 09:17:55 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: func.c,v 1.71 2021/02/19 22:27:49 rillig Exp $ */
+/* $NetBSD: func.c,v 1.72 2021/02/21 09:17:55 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: func.c,v 1.71 2021/02/19 22:27:49 rillig Exp $");
+__RCSID("$NetBSD: func.c,v 1.72 2021/02/21 09:17:55 rillig Exp $");
#endif
#include <stdlib.h>
@@ -337,6 +337,22 @@ funcdef(sym_t *fsym)
reached = true;
}
+static void
+check_missing_return_value(void)
+{
+ if (funcsym->s_type->t_subt->t_tspec == VOID)
+ return;
+ if (funcsym->s_return_type_implicit_int)
+ return;
+
+ /* C99 5.1.2.2.3 "Program termination" p1 */
+ if (Sflag && strcmp(funcsym->s_name, "main") == 0)
+ return;
+
+ /* function %s falls off bottom without returning value */
+ warning(217, funcsym->s_name);
+}
+
/*
* Called at the end of a function definition.
*/
@@ -348,11 +364,7 @@ funcend(void)
if (reached) {
cstmt->c_had_return_noval = true;
- if (funcsym->s_type->t_subt->t_tspec != VOID &&
- !funcsym->s_return_type_implicit_int) {
- /* func. %s falls off bottom without returning value */
- warning(217, funcsym->s_name);
- }
+ check_missing_return_value();
}
/*