Skip to content

Commit

Permalink
Parser: add warning for empty if body
Browse files Browse the repository at this point in the history
But only if the semicolon is on the same line as the if
  • Loading branch information
ehaas authored and Vexu committed Jan 11, 2025
1 parent b2d9b1f commit 21ef6e5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/aro/Diagnostics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ pub const Options = struct {
nonnull: Kind = .default,
@"atomic-access": Kind = .default,
@"gnu-designator": Kind = .default,
@"empty-body": Kind = .default,
};

const Diagnostics = @This();
Expand Down
10 changes: 10 additions & 0 deletions src/aro/Diagnostics/messages.def
Original file line number Diff line number Diff line change
Expand Up @@ -2633,3 +2633,13 @@ gnu_missing_eq_designator
.msg = "use of GNU 'missing =' extension in designator"
.kind = .warning
.opt = W("gnu-designator")

empty_if_body
.msg = "if statement has empty body"
.kind = .warning
.opt = W("empty-body")

empty_if_body_note
.msg = "put the semicolon on a separate line to silence this warning"
.kind = .note
.opt = W("empty-body")
11 changes: 11 additions & 0 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4720,6 +4720,17 @@ fn stmt(p: *Parser) Error!Node.Index {
const then_body = try p.stmt();
const else_body = if (p.eatToken(.keyword_else)) |_| try p.stmt() else null;

if (p.nodeIs(then_body, .null_stmt) and else_body == null) {
const semicolon_tok = then_body.get(&p.tree).null_stmt.semicolon_or_r_brace_tok;
const locs = p.pp.tokens.items(.loc);
const if_loc = locs[kw_if];
const semicolon_loc = locs[semicolon_tok];
if (if_loc.line == semicolon_loc.line) {
try p.errTok(.empty_if_body, semicolon_tok);
try p.errTok(.empty_if_body_note, semicolon_tok);
}
}

return p.addNode(.{ .if_stmt = .{
.if_tok = kw_if,
.cond = cond.node,
Expand Down
9 changes: 8 additions & 1 deletion test/cases/statements.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ void baz(int arg) {
default:
return;
}
if (1)
;
}

#define EXPECTED_ERRORS "statements.c:3:9: error: statement requires expression with scalar type ('void' invalid)" \
#define EXPECTED_ERRORS "statements.c:2:17: warning: if statement has empty body [-Wempty-body]" \
"statements.c:2:17: note: put the semicolon on a separate line to silence this warning" \
"statements.c:3:9: error: statement requires expression with scalar type ('void' invalid)" \
"statements.c:3:17: warning: if statement has empty body [-Wempty-body]" \
"statements.c:3:17: note: put the semicolon on a separate line to silence this warning" \
"statements.c:4:13: error: statement requires expression with integer type ('float' invalid)" \
"statements.c:5:10: warning: expression result unused [-Wunused-value]" \
"statements.c:5:10: error: statement requires expression with scalar type ('void' invalid)" \
"statements.c:7:21: error: case value must be an integer constant expression" \
"statements.c:32:16: warning: use of GNU case range extension [-Wgnu-case-range]" \
"statements.c:33:14: error: duplicate case value '2'" \
"statements.c:32:14: note: previous case defined here" \

0 comments on commit 21ef6e5

Please sign in to comment.