[hackers] [scc] [cc1] Add warning about statements without side effects || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Fri, 13 May 2016 18:13:20 +0200 (CEST)

commit 493d5098799d588ed3878e2bc79f2f437cba4fb3
Author: Roberto E. Vargas Caballero <Roberto E. Vargas Caballero>
AuthorDate: Thu May 12 16:21:52 2016 +0200
Commit: Roberto E. Vargas Caballero <Roberto E. Vargas Caballero>
CommitDate: Thu May 12 16:21:52 2016 +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.

diff --git a/cc1/code.c b/cc1/code.c
index e175fdd..30799be 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -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
index 60a78ee..98531e6 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -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");
_AT_@ -739,6 +740,7 @@ postfix(Node *lp)
                         break;
                 case '(':
                         lp = arguments(lp);
+ lp->flags |= NEFFECT;
                         break;
                 default:
                         return lp;
_AT_@ -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
index 7a1b710..1a8003b 100644
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
_AT_@ -38,6 +38,8 @@ label(void)
 static void
 stmtexp(Symbol *lbreak, Symbol *lcont, Switch *lswitch)
 {
+ Node *np;
+
         if (accept(';'))
                 return;
         if (yytoken == IDEN && ahead() == ':') {
_AT_@ -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(';');
 }
 
Received on Fri May 13 2016 - 18:13:20 CEST

This archive was generated by hypermail 2.3.0 : Fri May 13 2016 - 18:24:13 CEST