]> granicus.if.org Git - clang/commitdiff
Implement C++0x semantics for passing non-POD classes through varargs.
authorDouglas Gregor <dgregor@apple.com>
Sat, 21 May 2011 16:27:21 +0000 (16:27 +0000)
committerDouglas Gregor <dgregor@apple.com>
Sat, 21 May 2011 16:27:21 +0000 (16:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@131792 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
test/CXX/expr/expr.post/expr.call/p7-0x.cpp [new file with mode: 0644]

index d96b40ac7eefa2012b6e2dede248b02dc29913f0..92a8228f14198bf56d8e0d15f4ac99ef4199ce80 100644 (file)
@@ -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<DiagGroup<"non-pod-varargs">>, DefaultError;
 
 def err_typecheck_call_invalid_ordered_compare : Error<
index 5afd263ea131f1832645cf4d8b9c29b1c4be29a8..6bccdaeb2466314a06c360b8b7781776b7c3fc33 100644 (file)
@@ -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 (file)
index 0000000..bb4726d
--- /dev/null
@@ -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}}
+}