From: Chris Lattner Date: Sun, 12 Apr 2009 08:11:20 +0000 (+0000) Subject: implement rdar://6780761, making sema reject some code that otherwise X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=312531a8cd69c562d5687bd69fd334be99d87320;p=clang implement rdar://6780761, making sema reject some code that otherwise crashes codegen. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68891 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 5b3ecb9299..6c76e4aa27 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1190,6 +1190,11 @@ def err_typecheck_call_too_many_args : Error< def warn_call_wrong_number_of_arguments : Warning< "too %select{few|many}0 arguments in call to %1">; def err_deleted_function_use : Error<"attempt to use a deleted function">; + +def err_cannot_pass_objc_interface_to_vararg : Error< + "cannot pass object with interface type %0 by-value through variadic " + "%select{function|block|method}1">; + def warn_cannot_pass_non_pod_arg_to_vararg : Warning< "cannot pass object of non-POD type %0 through variadic " "%select{function|block|method}1; call will abort at runtime">; diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 8138379af7..5018833267 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -2210,7 +2210,7 @@ public: // DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but // will warn if the resulting type is not a POD type. - void DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT); + bool DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT); // UsualArithmeticConversions - performs the UsualUnaryConversions on it's // operands and then handles various conversions that are common to binary diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index f2820df93e..e380f78ad0 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -154,16 +154,25 @@ void Sema::DefaultArgumentPromotion(Expr *&Expr) { UsualUnaryConversions(Expr); } -// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but -// will warn if the resulting type is not a POD type. -void Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { +/// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but +/// will warn if the resulting type is not a POD type, and rejects ObjC +/// interfaces passed by value. This returns true if the argument type is +/// completely illegal. +bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { DefaultArgumentPromotion(Expr); - if (!Expr->getType()->isPODType()) { - Diag(Expr->getLocStart(), - diag::warn_cannot_pass_non_pod_arg_to_vararg) << - Expr->getType() << CT; + if (Expr->getType()->isObjCInterfaceType()) { + Diag(Expr->getLocStart(), + diag::err_cannot_pass_objc_interface_to_vararg) + << Expr->getType() << CT; + return true; } + + if (!Expr->getType()->isPODType()) + Diag(Expr->getLocStart(), diag::warn_cannot_pass_non_pod_arg_to_vararg) + << Expr->getType() << CT; + + return false; } @@ -2223,7 +2232,7 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, // Promote the arguments (C99 6.5.2.2p7). for (unsigned i = NumArgsInProto; i != NumArgs; i++) { Expr *Arg = Args[i]; - DefaultVariadicArgumentPromotion(Arg, CallType); + Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); Call->setArg(i, Arg); } } diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index 54a18ac832..8d8fae035a 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -164,7 +164,7 @@ bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, unsigned NumNamedArgs = Sel.getNumArgs(); assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!"); - bool anyIncompatibleArgs = false; + bool IsError = false; for (unsigned i = 0; i < NumNamedArgs; i++) { Expr *argExpr = Args[i]; assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); @@ -183,7 +183,7 @@ bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, if (Args[i] != argExpr) // The expression was converted. Args[i] = argExpr; // Make sure we store the converted expression. - anyIncompatibleArgs |= + IsError |= DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType, argExpr, "sending"); } @@ -191,7 +191,7 @@ bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, // Promote additional arguments to variadic methods. if (Method->isVariadic()) { for (unsigned i = NumNamedArgs; i < NumArgs; ++i) - DefaultVariadicArgumentPromotion(Args[i], VariadicMethod); + IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod); } else { // Check for extra arguments to non-variadic methods. if (NumArgs != NumNamedArgs) { @@ -203,7 +203,7 @@ bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, } } - return anyIncompatibleArgs; + return IsError; } bool Sema::isSelfExpr(Expr *RExpr) { diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 0a12a71bb7..875a38253d 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -4178,11 +4178,13 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, else if (NumArgs > NumArgsInProto) NumArgsToCheck = NumArgsInProto; + bool IsError = false; + // Initialize the implicit object parameter. - if (PerformObjectArgumentInitialization(Object, Method)) - return true; + IsError |= PerformObjectArgumentInitialization(Object, Method); TheCall->setArg(0, Object); + // Check the argument types. for (unsigned i = 0; i != NumArgsToCheck; i++) { Expr *Arg; @@ -4191,8 +4193,7 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, // Pass the argument. QualType ProtoArgType = Proto->getArgType(i); - if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) - return true; + IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing"); } else { Arg = new (Context) CXXDefaultArgExpr(Method->getParamDecl(i)); } @@ -4205,12 +4206,13 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, // Promote the arguments (C99 6.5.2.2p7). for (unsigned i = NumArgsInProto; i != NumArgs; i++) { Expr *Arg = Args[i]; - - DefaultVariadicArgumentPromotion(Arg, VariadicMethod); + IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); TheCall->setArg(i + 1, Arg); } } + if (IsError) return true; + return CheckFunctionCall(Method, TheCall.take()).release(); } diff --git a/test/SemaObjC/method-bad-param.m b/test/SemaObjC/method-bad-param.m index 824f72d29b..f797188669 100644 --- a/test/SemaObjC/method-bad-param.m +++ b/test/SemaObjC/method-bad-param.m @@ -22,3 +22,9 @@ void somefunc(foo x) {} // expected-error {{Objective-C interface type 'foo' cannot be passed by value}} foo somefunc2() {} // expected-error {{Objective-C interface type 'foo' cannot be returned by value}} + +// rdar://6780761 +void f0(foo *a0) { + extern void g0(int x, ...); + g0(1, *(foo*)0); // expected-error {{cannot pass object with interface type 'foo' by-value through variadic function}} +}