ref: bfaf7a4d88aefd1c269ae83563de4b78511cc648
parent: ae85c343cdfd8e1e5c2d58e59afb79e915f85383
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu May 12 12:21:52 EDT 2016
[cc1] Add warning about statements without side effects This is an useful warning because it indicates to the user that it is possible that maybe he doesn't want such statement since it is not going to do anything.
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -466,6 +466,10 @@
np->left = lp;
np->right = rp;
+ if (lp)
+ np->flags |= lp->flags & NEFFECT;
+ if (rp)
+ np->flags |= rp->flags & NEFFECT;
return np;
}
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -530,6 +530,7 @@
Node *inc;
chklvalue(np);
+ np->flags |= NEFFECT;
if (!tp->defined) {
errorp("invalid use of undefined type");
@@ -739,6 +740,7 @@
break;
case '(':
lp = arguments(lp);
+ lp->flags |= NEFFECT;
break;
default:
return lp;
@@ -1047,6 +1049,7 @@
default: return np;
}
chklvalue(np);
+ np->flags |= NEFFECT;
next();
np = (fun)(op, np, assign());
}
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -38,6 +38,8 @@
static void
stmtexp(Symbol *lbreak, Symbol *lcont, Switch *lswitch)
{
+ Node *np;
+
if (accept(';'))
return;
if (yytoken == IDEN && ahead() == ':') {
@@ -45,7 +47,10 @@
stmt(lbreak, lcont, lswitch);
return;
}
- emit(OEXPR, expr());
+ np = expr();
+ if ((np->flags & NEFFECT) == 0)
+ warn("expression without side effects");
+ emit(OEXPR, np);
expect(';');
}