Skip to content

Commit

Permalink
implement retrieving function names to warn about 'unused result'
Browse files Browse the repository at this point in the history
  • Loading branch information
wrongnull authored Jan 21, 2024
1 parent 74909b8 commit 557fcc0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5039,15 +5039,19 @@ pub const Result = struct {
.call_expr_one => {
const fn_ptr = p.nodes.items(.data)[@intFromEnum(cur_node)].bin.lhs;
const fn_ty = p.nodes.items(.ty)[@intFromEnum(fn_ptr)].elemType();
if (fn_ty.hasAttribute(.nodiscard)) try p.errStr(.nodiscard_unused, expr_start, "TODO get name");
if (fn_ty.hasAttribute(.warn_unused_result)) try p.errStr(.warn_unused_result, expr_start, "TODO get name");
const cast_info = p.nodes.items(.data)[@intFromEnum(fn_ptr)].cast.operand;
const decl_ref = p.nodes.items(.data)[@intFromEnum(cast_info)].decl_ref;
if (fn_ty.hasAttribute(.nodiscard)) try p.errStr(.nodiscard_unused, expr_start, p.tokSlice(decl_ref));
if (fn_ty.hasAttribute(.warn_unused_result)) try p.errStr(.warn_unused_result, expr_start, p.tokSlice(decl_ref));
return;
},
.call_expr => {
const fn_ptr = p.data.items[p.nodes.items(.data)[@intFromEnum(cur_node)].range.start];
const fn_ty = p.nodes.items(.ty)[@intFromEnum(fn_ptr)].elemType();
if (fn_ty.hasAttribute(.nodiscard)) try p.errStr(.nodiscard_unused, expr_start, "TODO get name");
if (fn_ty.hasAttribute(.warn_unused_result)) try p.errStr(.warn_unused_result, expr_start, "TODO get name");
const cast_info = p.nodes.items(.data)[@intFromEnum(fn_ptr)].cast.operand;
const decl_ref = p.nodes.items(.data)[@intFromEnum(cast_info)].decl_ref;
if (fn_ty.hasAttribute(.nodiscard)) try p.errStr(.nodiscard_unused, expr_start, p.tokSlice(decl_ref));
if (fn_ty.hasAttribute(.warn_unused_result)) try p.errStr(.warn_unused_result, expr_start, p.tokSlice(decl_ref));
return;
},
.stmt_expr => {
Expand Down
10 changes: 10 additions & 0 deletions test/cases/warn unused result.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
int foo() __attribute__((warn_unused_result)) {
return 0;
}

void bar() {
foo();
}


#define EXPECTED_ERRORS "warn unused result.c:6:4: warning: ignoring return value of 'foo', declared with 'warn_unused_result' attribute [-Wunused-result]"

0 comments on commit 557fcc0

Please sign in to comment.