scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 493d5098799d588ed3878e2bc79f2f437cba4fb3
parent c0e2b0c3d1aac699ef0484f9f56884eb48ea9cde
Author: Roberto E. Vargas Caballero <Roberto E. Vargas Caballero>
Date:   Thu, 12 May 2016 16:21:52 +0200

[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.

Diffstat:
Mcc1/code.c | 4++++
Mcc1/expr.c | 3+++
Mcc1/stmt.c | 7++++++-
3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/cc1/code.c b/cc1/code.c @@ -466,6 +466,10 @@ node(unsigned op, Type *tp, Node *lp, Node *rp) np->left = lp; np->right = rp; + if (lp) + np->flags |= lp->flags & NEFFECT; + if (rp) + np->flags |= rp->flags & NEFFECT; return np; } diff --git a/cc1/expr.c b/cc1/expr.c @@ -530,6 +530,7 @@ incdec(Node *np, char op) Node *inc; chklvalue(np); + np->flags |= NEFFECT; if (!tp->defined) { errorp("invalid use of undefined type"); @@ -739,6 +740,7 @@ postfix(Node *lp) break; case '(': lp = arguments(lp); + lp->flags |= NEFFECT; break; default: return lp; @@ -1047,6 +1049,7 @@ assign(void) default: return np; } chklvalue(np); + np->flags |= NEFFECT; next(); np = (fun)(op, np, assign()); } diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -38,6 +38,8 @@ label(void) static void stmtexp(Symbol *lbreak, Symbol *lcont, Switch *lswitch) { + Node *np; + if (accept(';')) return; if (yytoken == IDEN && ahead() == ':') { @@ -45,7 +47,10 @@ stmtexp(Symbol *lbreak, Symbol *lcont, Switch *lswitch) stmt(lbreak, lcont, lswitch); return; } - emit(OEXPR, expr()); + np = expr(); + if ((np->flags & NEFFECT) == 0) + warn("expression without side effects"); + emit(OEXPR, np); expect(';'); }