Skip to content

Commit 3b3adef

Browse files
hoodmaneFznamznon
andauthored
[Clang] Fix Sema::checkArgCount for 0-arg functions (#139638)
When calling a function that expects zero arguments with one argument, `Call->getArg(1)` will trap when trying to format the diagnostic. This also seems to improve the rendering of the diagnostic some of the time. Before: ``` $ ./bin/clang -c a.c a.c:2:30: error: too many arguments to function call, expected 2, have 4 2 | __builtin_annotation(1, 2, 3, 4); | ~ ^ ``` After: ``` $ ./bin/clang -c a.c a.c:2:30: error: too many arguments to function call, expected 2, have 4 2 | __builtin_annotation(1, 2, 3, 4); | ^~~~ ``` Split from #139580. --------- Co-authored-by: Mariya Podchishchaeva <[email protected]>
1 parent 8767d55 commit 3b3adef

File tree

3 files changed

+4
-7
lines changed

3 files changed

+4
-7
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ bool Sema::checkArgCount(CallExpr *Call, unsigned DesiredArgCount) {
168168

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

174174
static bool checkBuiltinVerboseTrap(CallExpr *Call, Sema &S) {

clang/lib/Sema/SemaWasm.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static bool CheckWasmBuiltinArgIsInteger(Sema &S, CallExpr *E,
5252
}
5353

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

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

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

7268
// This custom type checking code ensures that the nodes are as expected
7369
// in order to later on generate the necessary builtin.

clang/test/Sema/builtins-wasm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ static __externref_t table[0];
77
typedef void (*__funcref funcref_t)();
88
void test_ref_null() {
99
funcref_t func = __builtin_wasm_ref_null_func(0); // expected-error {{too many arguments to function call, expected 0, have 1}}
10+
__externref_t ref = __builtin_wasm_ref_null_extern(0); // expected-error {{too many arguments to function call, expected 0, have 1}}
1011
}
1112

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

0 commit comments

Comments
 (0)