summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartynas Venckus <martynas@cvs.openbsd.org>2011-05-31 22:00:08 +0000
committerMartynas Venckus <martynas@cvs.openbsd.org>2011-05-31 22:00:08 +0000
commit64b9340b0385a07a259d90fbdc49fd46b7746d3a (patch)
tree8da22313d312f45d075c0da9de419b9601bafac1
parentbc8012ee675161abe6182d5f1c4850756985b64c (diff)
Warn on empty non-compound selection statements, such as "if (foo);".
- "empty body of the if statement", - "empty body of the else statement". millert@ thought it's useful.
-rw-r--r--usr.bin/xlint/lint1/cgram.y44
-rw-r--r--usr.bin/xlint/lint1/err.c6
2 files changed, 40 insertions, 10 deletions
diff --git a/usr.bin/xlint/lint1/cgram.y b/usr.bin/xlint/lint1/cgram.y
index 19d5ae34346..5229ff64376 100644
--- a/usr.bin/xlint/lint1/cgram.y
+++ b/usr.bin/xlint/lint1/cgram.y
@@ -1,5 +1,5 @@
%{
-/* $OpenBSD: cgram.y,v 1.22 2007/09/08 17:49:18 cloder Exp $ */
+/* $OpenBSD: cgram.y,v 1.23 2011/05/31 22:00:07 martynas Exp $ */
/* $NetBSD: cgram.y,v 1.8 1995/10/02 17:31:35 jpo Exp $ */
/*
@@ -34,7 +34,7 @@
*/
#ifndef lint
-static char rcsid[] = "$OpenBSD: cgram.y,v 1.22 2007/09/08 17:49:18 cloder Exp $";
+static char rcsid[] = "$OpenBSD: cgram.y,v 1.23 2011/05/31 22:00:07 martynas Exp $";
#endif
#include <stdlib.h>
@@ -56,6 +56,11 @@ int blklev;
*/
int mblklev;
+/*
+ * Is the statement empty?
+ */
+int estmnt;
+
static int toicon(tnode_t *);
static void idecl(sym_t *, int);
static void ignuptorp(void);
@@ -1181,15 +1186,26 @@ direct_abs_decl:
;
stmnt:
- labeled_stmnt
+ labeled_stmnt {
+ estmnt = 0;
+ }
| expr_stmnt
- | comp_stmnt
- | selection_stmnt
- | iteration_stmnt
+ | comp_stmnt {
+ estmnt = 0;
+ }
+ | selection_stmnt {
+ estmnt = 0;
+ }
+ | iteration_stmnt {
+ estmnt = 0;
+ }
| jump_stmnt {
+ estmnt = 0;
ftflg = 0;
}
- | asm_stmnt
+ | asm_stmnt {
+ estmnt = 0;
+ }
;
labeled_stmnt:
@@ -1262,9 +1278,11 @@ stmnt_list:
expr_stmnt:
expr T_SEMI {
expr($1, 0, 0);
+ estmnt = 0;
ftflg = 0;
}
| T_SEMI {
+ estmnt = 1;
ftflg = 0;
}
;
@@ -1277,6 +1295,11 @@ selection_stmnt:
| if_without_else T_ELSE {
if2();
} stmnt {
+ if (estmnt) {
+ /* empty body of the else statement */
+ warning(316);
+ }
+
if3(1);
}
| if_without_else T_ELSE error {
@@ -1291,7 +1314,12 @@ selection_stmnt:
;
if_without_else:
- if_expr stmnt
+ if_expr stmnt {
+ if (estmnt) {
+ /* empty body of the if statement */
+ warning(315);
+ }
+ }
| if_expr error
;
diff --git a/usr.bin/xlint/lint1/err.c b/usr.bin/xlint/lint1/err.c
index c2daa730e8c..a5ac6a31bdd 100644
--- a/usr.bin/xlint/lint1/err.c
+++ b/usr.bin/xlint/lint1/err.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: err.c,v 1.26 2006/06/02 17:38:59 cloder Exp $ */
+/* $OpenBSD: err.c,v 1.27 2011/05/31 22:00:07 martynas Exp $ */
/* $NetBSD: err.c,v 1.8 1995/10/02 17:37:00 jpo Exp $ */
/*
@@ -33,7 +33,7 @@
*/
#ifndef lint
-static char rcsid[] = "$OpenBSD: err.c,v 1.26 2006/06/02 17:38:59 cloder Exp $";
+static char rcsid[] = "$OpenBSD: err.c,v 1.27 2011/05/31 22:00:07 martynas Exp $";
#endif
/* number of errors found */
@@ -369,6 +369,8 @@ const char *msgs[] = {
"suspicious operator for sizeof: %s", /* 312 */
"conversion of %s() return value from '%s' to '%s'", /* 313 */
"hexadecimal float constants are illegal in traditional C", /* 314 */
+ "empty body of the if statement", /* 315 */
+ "empty body of the else statement", /* 316 */
};
/*