Use a single function for doing vararg argument promotion. Also, make sure to do the promotion before checking the type - fixes PR3340.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62323 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 9bfa1ff..668cf5b 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -1523,6 +1523,17 @@
   // do not have a prototype. Integer promotions are performed on each 
   // argument, and arguments that have type float are promoted to double.
   void DefaultArgumentPromotion(Expr *&Expr);
+
+  // Used for emitting the right warning by DefaultVariadicArgumentPromotion
+  enum VariadicCallType {
+    VariadicFunction,
+    VariadicBlock,
+    VariadicMethod
+  };
+  
+  // DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but
+  // will warn if the resulting type is not a POD type.
+  void 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 bf2d7b1..820326f 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -87,6 +87,21 @@
   UsualUnaryConversions(Expr);
 }
 
+// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but
+// will warn if the resulting type is not a POD type.
+void 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;
+  }
+}
+
+
 /// UsualArithmeticConversions - Performs various conversions that are common to
 /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
 /// routine returns the first non-arithmetic type found. The client is 
@@ -1703,21 +1718,16 @@
   
   // If this is a variadic call, handle args passed through "...".
   if (Proto->isVariadic()) {
+    VariadicCallType CallType = VariadicFunction;
+    if (Fn->getType()->isBlockPointerType())
+      CallType = VariadicBlock; // Block
+    else if (isa<MemberExpr>(Fn))
+      CallType = VariadicMethod;
+
     // Promote the arguments (C99 6.5.2.2p7).
     for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
       Expr *Arg = Args[i];
-      if (!Arg->getType()->isPODType()) {
-        int CallType = 0;
-        if (Fn->getType()->isBlockPointerType())
-          CallType = 1; // Block
-        else if (isa<MemberExpr>(Fn))
-          CallType = 2;
-        
-        Diag(Arg->getLocStart(), 
-             diag::warn_cannot_pass_non_pod_arg_to_vararg) << 
-        Arg->getType() << CallType;
-      }
-      DefaultArgumentPromotion(Arg);
+      DefaultVariadicArgumentPromotion(Arg, CallType);
       Call->setArg(i, Arg);
     }
   }
diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp
index a73f386..548389d 100644
--- a/lib/Sema/SemaExprObjC.cpp
+++ b/lib/Sema/SemaExprObjC.cpp
@@ -156,15 +156,8 @@
 
   // Promote additional arguments to variadic methods.
   if (Method->isVariadic()) {
-    for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
-      if (!Args[i]->getType()->isPODType()) {
-        Diag(Args[i]->getLocStart(), 
-             diag::warn_cannot_pass_non_pod_arg_to_vararg) << 
-        Args[i]->getType() << 2; // Method
-      }
-      
-      DefaultArgumentPromotion(Args[i]);
-    }
+    for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
+      DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
   } else {
     // Check for extra arguments to non-variadic methods.
     if (NumArgs != NumNamedArgs) {
diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp
index fca1556..170ef1b 100644
--- a/lib/Sema/SemaOverload.cpp
+++ b/lib/Sema/SemaOverload.cpp
@@ -3624,14 +3624,8 @@
     // Promote the arguments (C99 6.5.2.2p7).
     for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
       Expr *Arg = Args[i];
-        
-      if (!Arg->getType()->isPODType()) {
-        Diag(Arg->getLocStart(), 
-             diag::warn_cannot_pass_non_pod_arg_to_vararg) << 
-        Arg->getType() << 2; // Method
-      }
 
-      DefaultArgumentPromotion(Arg);
+      DefaultVariadicArgumentPromotion(Arg, VariadicMethod);
       TheCall->setArg(i + 1, Arg);
     }
   }