[cfe-commits] r56084 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaExprObjC.cpp test/CodeGenObjC/message-arrays.m test/SemaObjC/message.m

Daniel Dunbar daniel at zuster.org
Wed Sep 10 17:50:25 PDT 2008


Author: ddunbar
Date: Wed Sep 10 19:50:25 2008
New Revision: 56084

URL: http://llvm.org/viewvc/llvm-project?rev=56084&view=rev
Log:
Fix two bugs exposed by array passing assert:
(1) Additional arguments to variadic methods should have default
promotions applied.

(2) Additional arguments to non-variadic methods were allowed.

Added:
    cfe/trunk/test/CodeGenObjC/message-arrays.m
Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExprObjC.cpp
    cfe/trunk/test/SemaObjC/message.m

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=56084&r1=56083&r2=56084&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Wed Sep 10 19:50:25 2008
@@ -965,7 +965,7 @@
   /// \param Method - May be null.
   /// \param [out] ReturnType - The return type of the send.
   /// \return true iff there were any incompatible types.
-  bool CheckMessageArgumentTypes(Expr **Args, Selector Sel,
+  bool CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, Selector Sel,
                                  ObjCMethodDecl *Method, const char *PrefixStr,
                                  SourceLocation lbrac, SourceLocation rbrac,
                                  QualType &ReturnType);  

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=56084&r1=56083&r2=56084&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Wed Sep 10 19:50:25 2008
@@ -107,12 +107,11 @@
   return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
 }
 
-bool Sema::CheckMessageArgumentTypes(Expr **Args, Selector Sel,
-                                     ObjCMethodDecl *Method, 
+bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, 
+                                     Selector Sel, ObjCMethodDecl *Method, 
                                      const char *PrefixStr,
                                      SourceLocation lbrac, SourceLocation rbrac,
                                      QualType &ReturnType) {  
-  unsigned NumArgs = Sel.getNumArgs();  
   if (!Method) {
     // Apply default argument promotion as for (C99 6.5.2.2p6).
     for (unsigned i = 0; i != NumArgs; i++)
@@ -126,8 +125,11 @@
     ReturnType = Method->getResultType();
   }
    
+  unsigned NumNamedArgs = Sel.getNumArgs();
+  assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
+
   bool anyIncompatibleArgs = false;
-  for (unsigned i = 0; i < NumArgs; i++) {
+  for (unsigned i = 0; i < NumNamedArgs; i++) {
     Expr *argExpr = Args[i];
     assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
     
@@ -149,6 +151,22 @@
       DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
                                argExpr, "sending");
   }
+
+  // Promote additional arguments to variadic methods.
+  if (Method->isVariadic()) {
+    for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
+      DefaultArgumentPromotion(Args[i]);
+  } else {
+    // Check for extra arguments to non-variadic methods.
+    if (NumArgs != NumNamedArgs) {
+      Diag(Args[NumNamedArgs]->getLocStart(), 
+           diag::err_typecheck_call_too_many_args,
+           Method->getSourceRange(),
+           SourceRange(Args[NumNamedArgs]->getLocStart(),
+                       Args[NumArgs-1]->getLocEnd()));
+    }
+  }
+
   return anyIncompatibleArgs;
 }
 
@@ -220,7 +238,7 @@
   if (!Method)
     Method = ClassDecl->lookupInstanceMethod(Sel);
 
-  if (CheckMessageArgumentTypes(ArgExprs, Sel, Method, "+", 
+  if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "+", 
                                 lbrac, rbrac, returnType))
     return true;
 
@@ -259,7 +277,7 @@
     ObjCMethodDecl *Method = InstanceMethodPool[Sel].Method;
     if (!Method)
       Method = FactoryMethodPool[Sel].Method;
-    if (CheckMessageArgumentTypes(ArgExprs, Sel, Method, "-", 
+    if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-", 
                                   lbrac, rbrac, returnType))
       return true;
     return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, 
@@ -280,7 +298,7 @@
       Method = FactoryMethodPool[Sel].Method;
     if (!Method)
       Method = InstanceMethodPool[Sel].Method;
-    if (CheckMessageArgumentTypes(ArgExprs, Sel, Method, "-", 
+    if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-", 
                                   lbrac, rbrac, returnType))
       return true;
     return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, 
@@ -344,7 +362,7 @@
         if (!Method)
           Method = InstanceMethodPool[Sel].Method;
   }
-  if (CheckMessageArgumentTypes(ArgExprs, Sel, Method, "-", 
+  if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-", 
                                 lbrac, rbrac, returnType))
     return true;
   return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, 

Added: cfe/trunk/test/CodeGenObjC/message-arrays.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/message-arrays.m?rev=56084&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenObjC/message-arrays.m (added)
+++ cfe/trunk/test/CodeGenObjC/message-arrays.m Wed Sep 10 19:50:25 2008
@@ -0,0 +1,15 @@
+// RUN: clang -emit-llvm -o %t %s
+
+void f0(id a) {
+  // This should have an implicit cast
+  [ a print: "hello" ];
+}
+
+ at interface A
+-(void) m: (int) arg0, ...;
+ at end
+
+int f1(A *a) {
+  // This should also get an implicit cast (for the vararg)
+  [a m: 1, "test"];
+}

Modified: cfe/trunk/test/SemaObjC/message.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/message.m?rev=56084&r1=56083&r2=56084&view=diff

==============================================================================
--- cfe/trunk/test/SemaObjC/message.m (original)
+++ cfe/trunk/test/SemaObjC/message.m Wed Sep 10 19:50:25 2008
@@ -61,3 +61,10 @@
 }
 @end
 
+ at interface I0
+-(void) nonVararg: (int) x;
+ at end
+
+int f0(I0 *ob) {
+  [ ob nonVararg: 0, 1, 2]; // expected-error {{too many arguments to function}}
+}





More information about the cfe-commits mailing list