From a3417bd5af87032added773a7d121d7f1218991d Mon Sep 17 00:00:00 2001 From: danielsan901998 Date: Thu, 5 Oct 2023 18:57:32 +0200 Subject: [PATCH 1/2] add C23 error diagnostic: missing type specifier --- src/Diagnostics.zig | 4 ++++ src/Type.zig | 6 +++++- test/cases/missing type specifier.c | 4 ++++ test/cases/missing type specifier_c2x.c | 4 ++++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/cases/missing type specifier.c create mode 100644 test/cases/missing type specifier_c2x.c diff --git a/src/Diagnostics.zig b/src/Diagnostics.zig index 038f2544..bf525c8c 100644 --- a/src/Diagnostics.zig +++ b/src/Diagnostics.zig @@ -374,6 +374,10 @@ const messages = struct { const kind = .warning; const all = true; }; + pub const missing_type_specifier_c2x = struct { + const msg = "a type specifier is required for all declarations"; + const kind = .@"error"; + }; pub const multiple_storage_class = struct { const msg = "cannot combine with previous '{s}' declaration specifier"; const extra = .str; diff --git a/src/Type.zig b/src/Type.zig index 805f6aaa..c7d5e5ef 100644 --- a/src/Type.zig +++ b/src/Type.zig @@ -1727,7 +1727,11 @@ pub const Builder = struct { ty = typeof; } else { ty.specifier = .int; - try p.err(.missing_type_specifier); + if (p.comp.langopts.standard.atLeast(.c2x)) { + try p.err(.missing_type_specifier_c2x); + } else { + try p.err(.missing_type_specifier); + } } }, .void => ty.specifier = .void, diff --git a/test/cases/missing type specifier.c b/test/cases/missing type specifier.c new file mode 100644 index 00000000..6958bf85 --- /dev/null +++ b/test/cases/missing type specifier.c @@ -0,0 +1,4 @@ +//aro-args +#define EXPECTED_ERRORS \ + "missing type specifier.c:4:8: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]" +static x = 5; diff --git a/test/cases/missing type specifier_c2x.c b/test/cases/missing type specifier_c2x.c new file mode 100644 index 00000000..ee137dbe --- /dev/null +++ b/test/cases/missing type specifier_c2x.c @@ -0,0 +1,4 @@ +//aro-args -std=c2x +#define EXPECTED_ERRORS \ + "missing type specifier_c2x.c:4:8: error: a type specifier is required for all declarations" +static x = 5; From 2a58f02a64b6dd148c630600c5ce938f75b122d3 Mon Sep 17 00:00:00 2001 From: danielsan901998 Date: Fri, 27 Oct 2023 15:29:33 +0200 Subject: [PATCH 2/2] fix: only check fixed underlying type in declarations, not in references --- src/Parser.zig | 4 +++- test/cases/enum fixed.c | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Parser.zig b/src/Parser.zig index 44f12a2a..b44470c3 100644 --- a/src/Parser.zig +++ b/src/Parser.zig @@ -2365,7 +2365,9 @@ fn enumSpec(p: *Parser) Error!Type { // check if this is a reference to a previous type const interned_name = try p.comp.intern(p.tokSlice(ident)); if (try p.syms.findTag(p, interned_name, .keyword_enum, ident, p.tok_ids[p.tok_i])) |prev| { - try p.checkEnumFixedTy(fixed_ty, ident, prev); + // only check fixed underlying type in forward declarations and not in references. + if (p.tok_ids[p.tok_i] == .semicolon) + try p.checkEnumFixedTy(fixed_ty, ident, prev); return prev.ty; } else { // this is a forward declaration, create a new enum Type. diff --git a/test/cases/enum fixed.c b/test/cases/enum fixed.c index 10339404..d02b7484 100644 --- a/test/cases/enum fixed.c +++ b/test/cases/enum fixed.c @@ -30,6 +30,10 @@ enum E6: char { a = 0u, }; +enum E e; +enum E: int; +void fn(enum E); + #define EXPECTED_ERRORS "enum fixed.c:2:7: warning: enumeration types with a fixed underlying type are a Clang extension [-Wfixed-enum-extension]" \ "enum fixed.c:4:6: error: enumeration previously declared with fixed underlying type" \ "enum fixed.c:2:6: note: previous definition is here" \