Skip to content

[Clang] Fix Sema::checkArgCount for 0-arg functions #139638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ bool Sema::checkArgCount(CallExpr *Call, unsigned DesiredArgCount) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also assert that ArgCount > 0? In case checkArgCountAtLeast becomes broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's covered by:

  assert(ArgCount > DesiredArgCount && "should have diagnosed this");

since DesiredArgCount is nonnegative.

return Diag(Range.getBegin(), diag::err_typecheck_call_too_many_args)
<< 0 /*function call*/ << DesiredArgCount << ArgCount
<< /*is non object*/ 0 << Call->getArg(1)->getSourceRange();
<< /*is non object*/ 0 << Range;
}

static bool checkBuiltinVerboseTrap(CallExpr *Call, Sema &S) {
Expand Down
8 changes: 2 additions & 6 deletions clang/lib/Sema/SemaWasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static bool CheckWasmBuiltinArgIsInteger(Sema &S, CallExpr *E,
}

bool SemaWasm::BuiltinWasmRefNullExtern(CallExpr *TheCall) {
if (TheCall->getNumArgs() != 0)
if (SemaRef.checkArgCount(TheCall, /*DesiredArgCount=*/0))
return true;

TheCall->setType(getASTContext().getWebAssemblyExternrefType());
Expand All @@ -62,12 +62,8 @@ bool SemaWasm::BuiltinWasmRefNullExtern(CallExpr *TheCall) {

bool SemaWasm::BuiltinWasmRefNullFunc(CallExpr *TheCall) {
ASTContext &Context = getASTContext();
if (TheCall->getNumArgs() != 0) {
Diag(TheCall->getBeginLoc(), diag::err_typecheck_call_too_many_args)
<< 0 /*function call*/ << /*expected*/ 0 << TheCall->getNumArgs()
<< /*is non object*/ 0;
if (SemaRef.checkArgCount(TheCall, /*DesiredArgCount=*/0))
return true;
}

// This custom type checking code ensures that the nodes are as expected
// in order to later on generate the necessary builtin.
Expand Down
1 change: 1 addition & 0 deletions clang/test/Sema/builtins-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static __externref_t table[0];
typedef void (*__funcref funcref_t)();
void test_ref_null() {
funcref_t func = __builtin_wasm_ref_null_func(0); // expected-error {{too many arguments to function call, expected 0, have 1}}
__externref_t ref = __builtin_wasm_ref_null_extern(0); // expected-error {{too many arguments to function call, expected 0, have 1}}
}

void test_table_size(__externref_t ref, void *ptr, int arr[]) {
Expand Down