From: Douglas Gregor Date: Sat, 21 May 2011 16:27:21 +0000 (+0000) Subject: Implement C++0x semantics for passing non-POD classes through varargs. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0fd228d48bbf05d08d9b408023d7c8ddb681bc91;p=clang Implement C++0x semantics for passing non-POD classes through varargs. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@131792 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index d96b40ac7e..92a8228f14 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -3324,8 +3324,8 @@ def err_cannot_pass_objc_interface_to_vararg : Error< "%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|constructor}1; call will abort at runtime">, + "cannot pass object of %select{non-POD|non-trivial}0 type %1 through variadic" + " %select{function|block|method|constructor}2; call will abort at runtime">, InGroup>, DefaultError; def err_typecheck_call_invalid_ordered_compare : Error< diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 5afd263ea1..6bccdaeb24 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -455,12 +455,32 @@ ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, << E->getType() << CT)) return ExprError(); - if (!E->getType()->isPODType() && - DiagRuntimeBehavior(E->getLocStart(), 0, + // C++ [expr.call]p7 prohibits non-POD types. + if (!E->getType()->isPODType()) { + // C++0x [expr.call]p7: + // Passing a potentially-evaluated argument of class type (Clause 9) + // having a non-trivial copy constructor, a non-trivial move constructor, + // or a non-trivial destructor, with no corresponding parameter, + // is conditionally-supported with implementation-defined semantics. + bool TrivialEnough = false; + if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { + if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { + if (Record->hasTrivialCopyConstructor() && + Record->hasTrivialMoveConstructor() && + Record->hasTrivialDestructor()) + TrivialEnough = true; + } + } + + if (TrivialEnough) { + // Nothing to diagnose. This is okay. + } else if (DiagRuntimeBehavior(E->getLocStart(), 0, PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) - << E->getType() << CT)) - return ExprError(); - + << getLangOptions().CPlusPlus0x << E->getType() + << CT)) + return ExprError(); + } + return Owned(E); } diff --git a/test/CXX/expr/expr.post/expr.call/p7-0x.cpp b/test/CXX/expr/expr.post/expr.call/p7-0x.cpp new file mode 100644 index 0000000000..bb4726dd33 --- /dev/null +++ b/test/CXX/expr/expr.post/expr.call/p7-0x.cpp @@ -0,0 +1,17 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x %s + +struct X1 { + X1(); +}; + +struct X2 { + X2(); + ~X2(); +}; + +void vararg(...); + +void f(X1 x1, X2 x2) { + vararg(x1); // okay + vararg(x2); // expected-error{{cannot pass object of non-trivial type 'X2' through variadic function; call will abort at runtime}} +}