Skip to content

Commit 0f9aade

Browse files
committed
[Clang] Fix Sema::checkArgCount when calling a function that wants zero arguments with one argument
In this case, `Call->getArg(1)` will trap when trying to format the diagnostic. It also improves 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 llvm#139580
1 parent 0eff410 commit 0f9aade

File tree

4 files changed

+5
-8
lines changed

4 files changed

+5
-8
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, 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, 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[]) {

clang/test/Sema/builtins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void test10(void) __attribute__((noreturn));
7575

7676
void test10(void) {
7777
__asm__("int3");
78-
__builtin_unreachable();
78+
__builtin_unreachable(1, 2, 3, 6, 7);
7979

8080
// No warning about falling off the end of a noreturn function.
8181
}

0 commit comments

Comments
 (0)