From c5677e17b759968be9863b095c92f7587ca95dba Mon Sep 17 00:00:00 2001 From: ruslan Date: Sun, 22 Mar 2026 14:00:07 +0300 Subject: [PATCH] when some global var is declared with storage-class register or auto 8cc throws a warning --- error.c | 1 + parse.c | 8 ++++++-- test/auto.c | 7 +++++++ test/register.c | 9 +++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/auto.c create mode 100644 test/register.c diff --git a/error.c b/error.c index e6fcac42..3490b587 100644 --- a/error.c +++ b/error.c @@ -4,6 +4,7 @@ #include #include #include "8cc.h" +#include bool enable_warning = true; bool warning_is_error = false; diff --git a/parse.c b/parse.c index e11bf773..827748ff 100644 --- a/parse.c +++ b/parse.c @@ -152,6 +152,10 @@ static Map *env() { return localenv ? localenv : globalenv; } +static bool islocalenv() { +return localenv ? true : false; +} + static Node *make_ast(Node *tmpl) { Node *r = malloc(sizeof(Node)); *r = *tmpl; @@ -2057,8 +2061,8 @@ static Type *read_decl_spec(int *rsclass) { case KTYPEDEF: if (sclass) goto err; sclass = S_TYPEDEF; break; case KEXTERN: if (sclass) goto err; sclass = S_EXTERN; break; case KSTATIC: if (sclass) goto err; sclass = S_STATIC; break; - case KAUTO: if (sclass) goto err; sclass = S_AUTO; break; - case KREGISTER: if (sclass) goto err; sclass = S_REGISTER; break; + case KAUTO: if (sclass) goto err; sclass = S_AUTO; if(!islocalenv()) warnt(tok,"auto specifer should not at the global scope"); break; + case KREGISTER: if (sclass) goto err; sclass = S_REGISTER; if(!islocalenv()) warnt(tok,"register specifer should not at the global scope"); break; case KCONST: break; case KVOLATILE: break; case KINLINE: break; diff --git a/test/auto.c b/test/auto.c new file mode 100644 index 00000000..3019fe30 --- /dev/null +++ b/test/auto.c @@ -0,0 +1,7 @@ +#include "test.h" + +void testmain() { +print("test auto at the global scope(should return a warn)"); +} + +auto x = 0; diff --git a/test/register.c b/test/register.c new file mode 100644 index 00000000..f745a49e --- /dev/null +++ b/test/register.c @@ -0,0 +1,9 @@ +#include "test.h" + +void testmain() { +print("register at global scope(should invoke a warn)"); +} + +register int a = 0; + +