Skip to content

Commit

Permalink
Preprocessor: free arg expansion locations if macro arg count is inco…
Browse files Browse the repository at this point in the history
…rrect

Fixes #695
  • Loading branch information
ehaas committed May 2, 2024
1 parent 6fc04f0 commit 9c1bddf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/aro/Preprocessor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2230,8 +2230,10 @@ fn expandMacroExhaustive(
else => |e| return e,
};
assert(r_paren.id == .r_paren);
var free_arg_expansion_locs = false;
defer {
for (args.items) |item| {
if (free_arg_expansion_locs) for (item) |tok| TokenWithExpansionLocs.free(tok.expansion_locs, pp.gpa);
pp.gpa.free(item);
}
args.deinit();
Expand All @@ -2257,6 +2259,7 @@ fn expandMacroExhaustive(
.arguments = .{ .expected = @intCast(macro.params.len), .actual = args_count },
};
if (macro.var_args and args_count < macro.params.len) {
free_arg_expansion_locs = true;
try pp.comp.addDiagnostic(
.{ .tag = .expected_at_least_arguments, .loc = buf.items[idx].loc, .extra = extra },
buf.items[idx].expansionSlice(),
Expand All @@ -2266,6 +2269,7 @@ fn expandMacroExhaustive(
continue;
}
if (!macro.var_args and args_count != macro.params.len) {
free_arg_expansion_locs = true;
try pp.comp.addDiagnostic(
.{ .tag = .expected_arguments, .loc = buf.items[idx].loc, .extra = extra },
buf.items[idx].expansionSlice(),
Expand Down
4 changes: 4 additions & 0 deletions test/cases/indirect macro invocation wrong arg count.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#define NO_ERROR_VALIDATION
#define h(x) x(0)
#define s()
h(s)

0 comments on commit 9c1bddf

Please sign in to comment.