[cfe-commits] r153584 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp lib/Sema/SemaPseudoObject.cpp test/SemaObjC/objc-container-subscripting-2.m test/SemaObjC/objc-dictionary-literal.m test/SemaObjCXX/objc-container-subscripting.mm

Fariborz Jahanian fjahanian at apple.com
Wed Mar 28 10:56:50 PDT 2012


Author: fjahanian
Date: Wed Mar 28 12:56:49 2012
New Revision: 153584

URL: http://llvm.org/viewvc/llvm-project?rev=153584&view=rev
Log:
objective-c: Improve diagnostics and
provide 'fixit' hint when dictionary index 
is not of proper type. // rdar://11062080

Added:
    cfe/trunk/test/SemaObjC/objc-dictionary-literal.m
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaPseudoObject.cpp
    cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m
    cfe/trunk/test/SemaObjCXX/objc-container-subscripting.mm

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=153584&r1=153583&r2=153584&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Mar 28 12:56:49 2012
@@ -3780,7 +3780,10 @@
   "multiple type conversion functions">;
 def err_objc_subscript_type_conversion : Error<
   "indexing expression is invalid because subscript type %0 is not an intergal"
-  "or objective-C pointer type">;
+  " or objective-C pointer type">;
+def err_objc_subscript_pointer : Error<
+  "indexing expression is invalid because subscript type %0 is not an"
+  " objective-C pointer">;
 def err_objc_indexing_method_result_type : Error<
   "method for accessing %select{dictionary|array}1 element must have Objective-C"
   " object return type instead of %0">;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=153584&r1=153583&r2=153584&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Mar 28 12:56:49 2012
@@ -3115,19 +3115,19 @@
     BaseExpr = LHSExp;
     IndexExpr = RHSExp;
     ResultType = PTy->getPointeeType();
-  } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) {
-     // Handle the uncommon case of "123[Ptr]".
-    BaseExpr = RHSExp;
-    IndexExpr = LHSExp;
-    ResultType = PTy->getPointeeType();
   } else if (const ObjCObjectPointerType *PTy =
-               LHSTy->getAs<ObjCObjectPointerType>()) {
+             LHSTy->getAs<ObjCObjectPointerType>()) {
     BaseExpr = LHSExp;
     IndexExpr = RHSExp;
     Result = BuildObjCSubscriptExpression(RLoc, BaseExpr, IndexExpr, 0, 0);
     if (!Result.isInvalid())
       return Owned(Result.take());
     ResultType = PTy->getPointeeType();
+  } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) {
+     // Handle the uncommon case of "123[Ptr]".
+    BaseExpr = RHSExp;
+    IndexExpr = LHSExp;
+    ResultType = PTy->getPointeeType();
   } else if (const ObjCObjectPointerType *PTy =
                RHSTy->getAs<ObjCObjectPointerType>()) {
      // Handle the uncommon case of "123[Ptr]".

Modified: cfe/trunk/lib/Sema/SemaPseudoObject.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaPseudoObject.cpp?rev=153584&r1=153583&r2=153584&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaPseudoObject.cpp (original)
+++ cfe/trunk/lib/Sema/SemaPseudoObject.cpp Wed Mar 28 12:56:49 2012
@@ -842,14 +842,20 @@
   // If we don't have a class type in C++, there's no way we can get an
   // expression of integral or enumeration type.
   const RecordType *RecordTy = T->getAs<RecordType>();
-  if (!RecordTy)
+  if (!RecordTy && T->isObjCObjectPointerType())
     // All other scalar cases are assumed to be dictionary indexing which
     // caller handles, with diagnostics if needed.
     return OS_Dictionary;
-  if (!getLangOpts().CPlusPlus || RecordTy->isIncompleteType()) {
+  if (!getLangOpts().CPlusPlus || 
+      !RecordTy || RecordTy->isIncompleteType()) {
     // No indexing can be done. Issue diagnostics and quit.
-    Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
-    << FromE->getType();
+    const Expr *IndexExpr = FromE->IgnoreParenImpCasts();
+    if (isa<StringLiteral>(IndexExpr))
+      Diag(FromE->getExprLoc(), diag::err_objc_subscript_pointer)
+        << T << FixItHint::CreateInsertion(FromE->getExprLoc(), "@");
+    else
+      Diag(FromE->getExprLoc(), diag::err_objc_subscript_type_conversion)
+        << T;
     return OS_Error;
   }
   

Modified: cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m?rev=153584&r1=153583&r2=153584&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m (original)
+++ cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m Wed Mar 28 12:56:49 2012
@@ -16,8 +16,8 @@
 id func() {
   NSMutableArray *array;
   float f; 
-  array[f] = array; // expected-error {{expected method to write dictionary element not found on object of type 'NSMutableArray *'}}
-  return array[3.14]; // expected-error {{expected method to read dictionary element not found on object of type 'NSMutableArray *'}}
+  array[f] = array; // expected-error {{indexing expression is invalid because subscript type 'float' is not an intergal or objective-C pointer type}}
+  return array[3.14]; // expected-error {{indexing expression is invalid because subscript type 'double' is not an intergal or objective-C pointer type}}
 }
 
 void test_unused() {

Added: cfe/trunk/test/SemaObjC/objc-dictionary-literal.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/objc-dictionary-literal.m?rev=153584&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/objc-dictionary-literal.m (added)
+++ cfe/trunk/test/SemaObjC/objc-dictionary-literal.m Wed Mar 28 12:56:49 2012
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1  -fsyntax-only -verify %s
+// rdar://11062080
+
+ at interface NSNumber
++ (NSNumber *)numberWithChar:(char)value;
++ (NSNumber *)numberWithInt:(int)value;
+ at end
+
+ at protocol NSCopying @end
+typedef unsigned long NSUInteger;
+typedef long NSInteger;
+
+ at interface NSDictionary
++ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
+- (void)setObject:(id)object forKeyedSubscript:(id)key;
+ at end
+
+ at interface NSString<NSCopying>
+ at end
+
+ at interface NSArray
+- (id)objectAtIndexedSubscript:(NSInteger)index;
+- (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
+ at end
+
+int main() {
+	NSDictionary *dict = @{ @"name":@666 };
+        dict[@"name"] = @666;
+
+        dict["name"] = @666; // expected-error {{indexing expression is invalid because subscript type 'char *' is not an objective-C pointer}}
+
+	return 0;
+}
+

Modified: cfe/trunk/test/SemaObjCXX/objc-container-subscripting.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/objc-container-subscripting.mm?rev=153584&r1=153583&r2=153584&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjCXX/objc-container-subscripting.mm (original)
+++ cfe/trunk/test/SemaObjCXX/objc-container-subscripting.mm Wed Mar 28 12:56:49 2012
@@ -32,8 +32,8 @@
 
 template<typename T, typename U, typename O>
 void test_array_subscripts(T base, U index, O obj) {
-  base[index] = obj; // expected-error {{expected method to write dictionary element not found on object of type 'NSMutableArray *'}}
-  obj = base[index]; // expected-error {{expected method to read dictionary element not found on object of type 'NSMutableArray *'}}
+  base[index] = obj; // expected-error {{indexing expression is invalid because subscript type 'double' is not an intergal or objective-C pointer type}}
+  obj = base[index]; // expected-error {{indexing expression is invalid because subscript type 'double' is not an intergal or objective-C pointer type}}
 }
 
 template void  test_array_subscripts(NSMutableArray *, int, id);





More information about the cfe-commits mailing list