scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit b043794874408ea4331d53ba978ad8cb5c0efe4e
parent 67dd2d72eacab11d263530d495d0019168ea9d6e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  4 Oct 2013 14:10:23 +0200

Allow mixed declarations and code

c99 allow mix declarations and code, but c89 no. This patch adds
this feature with a customizable warning.

Diffstat:
cc.h | 1+
flow.c | 16+++++++++++++---
2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/cc.h b/cc.h @@ -11,6 +11,7 @@ extern const char *filename; struct user_opt { unsigned char implicit; unsigned char c99; + unsigned char mixdcls; unsigned char useless; unsigned char repeat; unsigned char charsign; diff --git a/flow.c b/flow.c @@ -2,6 +2,7 @@ #include <assert.h> #include <stdio.h> +#include "cc.h" #include "symbol.h" #include "tokens.h" #include "syntax.h" @@ -234,13 +235,22 @@ static struct node * compound(void) { register struct node *lp = nodecomp(), *np; + unsigned char nodecl = 0; expect('{'); new_ctx(); - while (np = decl()) + while (!accept('}')) { + if (np = decl()) { + if (nodecl) { + warn(options.mixdcls, + "mixed declarations and code"); + } + } else { + np = stmt(); + nodecl = 1; + } addstmt(lp, np); - while (!accept('}')) - addstmt(lp, stmt()); + } del_ctx(); return lp;