Skip to content

Commit

Permalink
[NFC] Sema: use checkArgCount instead of custom checking
Browse files Browse the repository at this point in the history
 As requested in D79279.

Differential Revision: https://reviews.llvm.org/D84666
  • Loading branch information
jfbastien committed Jul 28, 2020
1 parent e9b236f commit 389f009
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 54 deletions.
2 changes: 0 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -9724,8 +9724,6 @@ def err_opencl_block_ref_block : Error<
"cannot refer to a block inside block">;

// OpenCL v2.0 s6.13.9 - Address space qualifier functions.
def err_opencl_builtin_to_addr_arg_num : Error<
"invalid number of arguments to function: %0">;
def err_opencl_builtin_to_addr_invalid_arg : Error<
"invalid argument %0 to function: %1, expecting a generic pointer argument">;

Expand Down
55 changes: 8 additions & 47 deletions clang/lib/Sema/SemaChecking.cpp
Expand Up @@ -1274,11 +1274,8 @@ static bool SemaBuiltinPipePackets(Sema &S, CallExpr *Call) {
// \return True if a semantic error has been found, false otherwise.
static bool SemaOpenCLBuiltinToAddr(Sema &S, unsigned BuiltinID,
CallExpr *Call) {
if (Call->getNumArgs() != 1) {
S.Diag(Call->getBeginLoc(), diag::err_opencl_builtin_to_addr_arg_num)
<< Call->getDirectCallee() << Call->getSourceRange();
if (checkArgCount(S, Call, 1))
return true;
}

auto RT = Call->getArg(0)->getType();
if (!RT->isPointerType() || RT->getPointeeType()
Expand Down Expand Up @@ -5572,21 +5569,8 @@ bool Sema::SemaBuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) {
if (checkVAStartABI(*this, BuiltinID, Fn))
return true;

if (TheCall->getNumArgs() > 2) {
Diag(TheCall->getArg(2)->getBeginLoc(),
diag::err_typecheck_call_too_many_args)
<< 0 /*function call*/ << 2 << TheCall->getNumArgs()
<< Fn->getSourceRange()
<< SourceRange(TheCall->getArg(2)->getBeginLoc(),
(*(TheCall->arg_end() - 1))->getEndLoc());
if (checkArgCount(*this, TheCall, 2))
return true;
}

if (TheCall->getNumArgs() < 2) {
return Diag(TheCall->getEndLoc(),
diag::err_typecheck_call_too_few_args_at_least)
<< 0 /*function call*/ << 2 << TheCall->getNumArgs();
}

// Type-check the first argument normally.
if (checkBuiltinArgument(*this, TheCall, 0))
Expand Down Expand Up @@ -5696,15 +5680,8 @@ bool Sema::SemaBuiltinVAStartARMMicrosoft(CallExpr *Call) {
/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
/// friends. This is declared to take (...), so we have to check everything.
bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
if (TheCall->getNumArgs() < 2)
return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args)
<< 0 << 2 << TheCall->getNumArgs() /*function call*/;
if (TheCall->getNumArgs() > 2)
return Diag(TheCall->getArg(2)->getBeginLoc(),
diag::err_typecheck_call_too_many_args)
<< 0 /*function call*/ << 2 << TheCall->getNumArgs()
<< SourceRange(TheCall->getArg(2)->getBeginLoc(),
(*(TheCall->arg_end() - 1))->getEndLoc());
if (checkArgCount(*this, TheCall, 2))
return true;

ExprResult OrigArg0 = TheCall->getArg(0);
ExprResult OrigArg1 = TheCall->getArg(1);
Expand Down Expand Up @@ -5742,15 +5719,8 @@ bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
/// to check everything. We expect the last argument to be a floating point
/// value.
bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
if (TheCall->getNumArgs() < NumArgs)
return Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args)
<< 0 << NumArgs << TheCall->getNumArgs() /*function call*/;
if (TheCall->getNumArgs() > NumArgs)
return Diag(TheCall->getArg(NumArgs)->getBeginLoc(),
diag::err_typecheck_call_too_many_args)
<< 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
<< SourceRange(TheCall->getArg(NumArgs)->getBeginLoc(),
(*(TheCall->arg_end() - 1))->getEndLoc());
if (checkArgCount(*this, TheCall, NumArgs))
return true;

// __builtin_fpclassify is the only case where NumArgs != 1, so we can count
// on all preceding parameters just being int. Try all of those.
Expand Down Expand Up @@ -5854,17 +5824,8 @@ bool Sema::SemaBuiltinComplex(CallExpr *TheCall) {
// vector short vec_xxsldwi(vector short, vector short, int);
bool Sema::SemaBuiltinVSX(CallExpr *TheCall) {
unsigned ExpectedNumArgs = 3;
if (TheCall->getNumArgs() < ExpectedNumArgs)
return Diag(TheCall->getEndLoc(),
diag::err_typecheck_call_too_few_args_at_least)
<< 0 /*function call*/ << ExpectedNumArgs << TheCall->getNumArgs()
<< TheCall->getSourceRange();

if (TheCall->getNumArgs() > ExpectedNumArgs)
return Diag(TheCall->getEndLoc(),
diag::err_typecheck_call_too_many_args_at_most)
<< 0 /*function call*/ << ExpectedNumArgs << TheCall->getNumArgs()
<< TheCall->getSourceRange();
if (checkArgCount(*this, TheCall, ExpectedNumArgs))
return true;

// Check the third argument is a compile time constant
if (!TheCall->getArg(2)->isIntegerConstantExpr(Context))
Expand Down
8 changes: 4 additions & 4 deletions clang/test/CodeGen/builtins-ppc-error.c
Expand Up @@ -32,16 +32,16 @@ void testInsertWord(void) {
}

void testXXPERMDI(int index) {
vec_xxpermdi(vsi); //expected-error {{too few arguments to function call, expected at least 3, have 1}}
vec_xxpermdi(vsi, vsi, 2, 4); //expected-error {{too many arguments to function call, expected at most 3, have 4}}
vec_xxpermdi(vsi); //expected-error {{too few arguments to function call, expected 3, have 1}}
vec_xxpermdi(vsi, vsi, 2, 4); //expected-error {{too many arguments to function call, expected 3, have 4}}
vec_xxpermdi(vsi, vsi, index); //expected-error {{argument 3 to '__builtin_vsx_xxpermdi' must be a 2-bit unsigned literal (i.e. 0, 1, 2 or 3)}}
vec_xxpermdi(1, 2, 3); //expected-error {{first two arguments to '__builtin_vsx_xxpermdi' must be vectors}}
vec_xxpermdi(vsi, vuc, 2); //expected-error {{first two arguments to '__builtin_vsx_xxpermdi' must have the same type}}
}

void testXXSLDWI(int index) {
vec_xxsldwi(vsi); //expected-error {{too few arguments to function call, expected at least 3, have 1}}
vec_xxsldwi(vsi, vsi, 2, 4); //expected-error {{too many arguments to function call, expected at most 3, have 4}}
vec_xxsldwi(vsi); //expected-error {{too few arguments to function call, expected 3, have 1}}
vec_xxsldwi(vsi, vsi, 2, 4); //expected-error {{too many arguments to function call, expected 3, have 4}}
vec_xxsldwi(vsi, vsi, index); //expected-error {{argument 3 to '__builtin_vsx_xxsldwi' must be a 2-bit unsigned literal (i.e. 0, 1, 2 or 3)}}
vec_xxsldwi(1, 2, 3); //expected-error {{first two arguments to '__builtin_vsx_xxsldwi' must be vectors}}
vec_xxsldwi(vsi, vuc, 2); //expected-error {{first two arguments to '__builtin_vsx_xxsldwi' must have the same type}}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaOpenCL/to_addr_builtin.cl
Expand Up @@ -15,7 +15,7 @@ void test(void) {
// expected-error@-2{{implicit declaration of function 'to_global' is invalid in OpenCL}}
// expected-warning@-3{{incompatible integer to pointer conversion assigning to '__global int *__private' from 'int'}}
#else
// expected-error@-5{{invalid number of arguments to function: 'to_global'}}
// expected-error@-5{{too many arguments to function call, expected 1, have 2}}
#endif

int x;
Expand Down

0 comments on commit 389f009

Please sign in to comment.