From 3eda6fa901b462f0b5f72d0651f46d804aac1844 Mon Sep 17 00:00:00 2001 From: Jordy Rose Date: Sat, 12 May 2012 17:32:56 +0000 Subject: [PATCH] Only check NSArray/NSDictionary boxing method params once. Once we've found a "good" method, we don't need to check its argument types again. (Even if we might have later found a "bad" method, we were already caching the method we first looked up.) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156719 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExprObjC.cpp | 178 ++++++++++++++++--------------- test/SemaObjC/objc-literal-sig.m | 14 ++- 2 files changed, 102 insertions(+), 90 deletions(-) diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index 06a981859b..7ade400c53 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -655,35 +655,38 @@ ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) { if (!validateBoxingMethod(*this, SR.getBegin(), NSArrayDecl, Sel, Method)) return ExprError(); + // Dig out the type that all elements should be converted to. + QualType T = Method->param_begin()[0]->getType(); + const PointerType *PtrT = T->getAs(); + if (!PtrT || + !Context.hasSameUnqualifiedType(PtrT->getPointeeType(), IdT)) { + Diag(SR.getBegin(), diag::err_objc_literal_method_sig) + << Sel; + Diag(Method->param_begin()[0]->getLocation(), + diag::note_objc_literal_method_param) + << 0 << T + << Context.getPointerType(IdT.withConst()); + return ExprError(); + } + + // Check that the 'count' parameter is integral. + if (!Method->param_begin()[1]->getType()->isIntegerType()) { + Diag(SR.getBegin(), diag::err_objc_literal_method_sig) + << Sel; + Diag(Method->param_begin()[1]->getLocation(), + diag::note_objc_literal_method_param) + << 1 + << Method->param_begin()[1]->getType() + << "integral"; + return ExprError(); + } + + // We've found a good +arrayWithObjects:count: method. Save it! ArrayWithObjectsMethod = Method; } - // Dig out the type that all elements should be converted to. - QualType T = ArrayWithObjectsMethod->param_begin()[0]->getType(); - const PointerType *PtrT = T->getAs(); - if (!PtrT || - !Context.hasSameUnqualifiedType(PtrT->getPointeeType(), IdT)) { - Diag(SR.getBegin(), diag::err_objc_literal_method_sig) - << ArrayWithObjectsMethod->getSelector(); - Diag(ArrayWithObjectsMethod->param_begin()[0]->getLocation(), - diag::note_objc_literal_method_param) - << 0 << T - << Context.getPointerType(IdT.withConst()); - return ExprError(); - } - T = PtrT->getPointeeType(); - - // Check that the 'count' parameter is integral. - if (!ArrayWithObjectsMethod->param_begin()[1]->getType()->isIntegerType()) { - Diag(SR.getBegin(), diag::err_objc_literal_method_sig) - << ArrayWithObjectsMethod->getSelector(); - Diag(ArrayWithObjectsMethod->param_begin()[1]->getLocation(), - diag::note_objc_literal_method_param) - << 1 - << ArrayWithObjectsMethod->param_begin()[1]->getType() - << "integral"; - return ExprError(); - } + QualType ObjectsType = ArrayWithObjectsMethod->param_begin()[0]->getType(); + QualType RequiredType = ObjectsType->castAs()->getPointeeType(); // Check that each of the elements provided is valid in a collection literal, // performing conversions as necessary. @@ -691,7 +694,7 @@ ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) { for (unsigned I = 0, N = Elements.size(); I != N; ++I) { ExprResult Converted = CheckObjCCollectionLiteralElement(*this, ElementsBuffer[I], - T); + RequiredType); if (Converted.isInvalid()) return ExprError(); @@ -781,73 +784,76 @@ ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR, Method)) return ExprError(); - DictionaryWithObjectsMethod = Method; - } - - // Dig out the type that all values should be converted to. - QualType ValueT = DictionaryWithObjectsMethod->param_begin()[0]->getType(); - const PointerType *PtrValue = ValueT->getAs(); - if (!PtrValue || - !Context.hasSameUnqualifiedType(PtrValue->getPointeeType(), IdT)) { - Diag(SR.getBegin(), diag::err_objc_literal_method_sig) - << DictionaryWithObjectsMethod->getSelector(); - Diag(DictionaryWithObjectsMethod->param_begin()[0]->getLocation(), - diag::note_objc_literal_method_param) - << 0 << ValueT - << Context.getPointerType(IdT.withConst()); - return ExprError(); - } - ValueT = PtrValue->getPointeeType(); - - // Dig out the type that all keys should be converted to. - QualType KeyT = DictionaryWithObjectsMethod->param_begin()[1]->getType(); - const PointerType *PtrKey = KeyT->getAs(); - if (!PtrKey || - !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), - IdT)) { - bool err = true; - if (PtrKey) { - if (QIDNSCopying.isNull()) { - // key argument of selector is id? - if (ObjCProtocolDecl *NSCopyingPDecl = - LookupProtocol(&Context.Idents.get("NSCopying"), SR.getBegin())) { - ObjCProtocolDecl *PQ[] = {NSCopyingPDecl}; - QIDNSCopying = - Context.getObjCObjectType(Context.ObjCBuiltinIdTy, - (ObjCProtocolDecl**) PQ,1); - QIDNSCopying = Context.getObjCObjectPointerType(QIDNSCopying); - } - } - if (!QIDNSCopying.isNull()) - err = !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), - QIDNSCopying); - } - - if (err) { + // Dig out the type that all values should be converted to. + QualType ValueT = Method->param_begin()[0]->getType(); + const PointerType *PtrValue = ValueT->getAs(); + if (!PtrValue || + !Context.hasSameUnqualifiedType(PtrValue->getPointeeType(), IdT)) { Diag(SR.getBegin(), diag::err_objc_literal_method_sig) - << DictionaryWithObjectsMethod->getSelector(); - Diag(DictionaryWithObjectsMethod->param_begin()[1]->getLocation(), + << Sel; + Diag(Method->param_begin()[0]->getLocation(), diag::note_objc_literal_method_param) - << 1 << KeyT + << 0 << ValueT << Context.getPointerType(IdT.withConst()); return ExprError(); } - } - KeyT = PtrKey->getPointeeType(); - // Check that the 'count' parameter is integral. - if (!DictionaryWithObjectsMethod->param_begin()[2]->getType() - ->isIntegerType()) { - Diag(SR.getBegin(), diag::err_objc_literal_method_sig) - << DictionaryWithObjectsMethod->getSelector(); - Diag(DictionaryWithObjectsMethod->param_begin()[2]->getLocation(), - diag::note_objc_literal_method_param) - << 2 - << DictionaryWithObjectsMethod->param_begin()[2]->getType() - << "integral"; - return ExprError(); + // Dig out the type that all keys should be converted to. + QualType KeyT = Method->param_begin()[1]->getType(); + const PointerType *PtrKey = KeyT->getAs(); + if (!PtrKey || + !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), + IdT)) { + bool err = true; + if (PtrKey) { + if (QIDNSCopying.isNull()) { + // key argument of selector is id? + if (ObjCProtocolDecl *NSCopyingPDecl = + LookupProtocol(&Context.Idents.get("NSCopying"), SR.getBegin())) { + ObjCProtocolDecl *PQ[] = {NSCopyingPDecl}; + QIDNSCopying = + Context.getObjCObjectType(Context.ObjCBuiltinIdTy, + (ObjCProtocolDecl**) PQ,1); + QIDNSCopying = Context.getObjCObjectPointerType(QIDNSCopying); + } + } + if (!QIDNSCopying.isNull()) + err = !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), + QIDNSCopying); + } + + if (err) { + Diag(SR.getBegin(), diag::err_objc_literal_method_sig) + << Sel; + Diag(Method->param_begin()[1]->getLocation(), + diag::note_objc_literal_method_param) + << 1 << KeyT + << Context.getPointerType(IdT.withConst()); + return ExprError(); + } + } + + // Check that the 'count' parameter is integral. + QualType CountType = Method->param_begin()[2]->getType(); + if (!CountType->isIntegerType()) { + Diag(SR.getBegin(), diag::err_objc_literal_method_sig) + << Sel; + Diag(Method->param_begin()[2]->getLocation(), + diag::note_objc_literal_method_param) + << 2 << CountType + << "integral"; + return ExprError(); + } + + // We've found a good +dictionaryWithObjects:keys:count: method; save it! + DictionaryWithObjectsMethod = Method; } + QualType ValuesT = DictionaryWithObjectsMethod->param_begin()[0]->getType(); + QualType ValueT = ValuesT->castAs()->getPointeeType(); + QualType KeysT = DictionaryWithObjectsMethod->param_begin()[1]->getType(); + QualType KeyT = KeysT->castAs()->getPointeeType(); + // Check that each of the keys and values provided is valid in a collection // literal, performing conversions as necessary. bool HasPackExpansions = false; diff --git a/test/SemaObjC/objc-literal-sig.m b/test/SemaObjC/objc-literal-sig.m index 350fdd0747..86f42d3abc 100644 --- a/test/SemaObjC/objc-literal-sig.m +++ b/test/SemaObjC/objc-literal-sig.m @@ -16,30 +16,36 @@ typedef _Bool BOOL; + (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value; + (NSNumber *)numberWithFloat:(float)value; + (NSNumber *)numberWithDouble:(double)value; -+ (int)numberWithBool:(BOOL)value; // expected-note{{method returns unexpected type 'int' (should be an object type)}} ++ (int)numberWithBool:(BOOL)value; // expected-note 2 {{method returns unexpected type 'int' (should be an object type)}} @end @interface NSString -+ (char)stringWithUTF8String:(const char *)value; // expected-note{{method returns unexpected type 'char' (should be an object type)}} ++ (char)stringWithUTF8String:(const char *)value; // expected-note 2 {{method returns unexpected type 'char' (should be an object type)}} @end @interface NSArray @end @interface NSArray (NSArrayCreation) -+ (id)arrayWithObjects:(const int [])objects // expected-note{{first parameter has unexpected type 'const int *' (should be 'const id *')}} ++ (id)arrayWithObjects:(const int [])objects // expected-note 2 {{first parameter has unexpected type 'const int *' (should be 'const id *')}} count:(unsigned long)cnt; @end @interface NSDictionary + (id)dictionaryWithObjects:(const id [])objects - forKeys:(const int [])keys // expected-note{{second parameter has unexpected type 'const int *' (should be 'const id *')}} + forKeys:(const int [])keys // expected-note 2 {{second parameter has unexpected type 'const int *' (should be 'const id *')}} count:(unsigned long)cnt; @end +// All tests are doubled to make sure that a bad method is not saved +// and then used un-checked. void test_sig() { + (void)@__objc_yes; // expected-error{{literal construction method 'numberWithBool:' has incompatible signature}} (void)@__objc_yes; // expected-error{{literal construction method 'numberWithBool:' has incompatible signature}} id array = @[ @17 ]; // expected-error{{literal construction method 'arrayWithObjects:count:' has incompatible signature}} + id array2 = @[ @17 ]; // expected-error{{literal construction method 'arrayWithObjects:count:' has incompatible signature}} id dict = @{ @"hello" : @17 }; // expected-error{{literal construction method 'dictionaryWithObjects:forKeys:count:' has incompatible signature}} + id dict2 = @{ @"hello" : @17 }; // expected-error{{literal construction method 'dictionaryWithObjects:forKeys:count:' has incompatible signature}} id str = @("hello"); // expected-error{{literal construction method 'stringWithUTF8String:' has incompatible signature}} + id str2 = @("hello"); // expected-error{{literal construction method 'stringWithUTF8String:' has incompatible signature}} }