Fix <rdar://problem/5987482> clang on xcode: null dereference in Sema::ActOnMemberReferenceExpr.

In addition to fixing the crasher, this commit fixes further improves property lookup (by searching protocols of qualified interfaces..."NSObject <prot>").


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52001 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclObjC.cpp b/lib/AST/DeclObjC.cpp
index e493d27..6536df6 100644
--- a/lib/AST/DeclObjC.cpp
+++ b/lib/AST/DeclObjC.cpp
@@ -157,6 +157,14 @@
     if (property)
       return property;
   }
+  // Look through protocols.
+  for (ObjCInterfaceDecl::protocol_iterator I = protocol_begin(),
+       E = protocol_end(); I != E; ++I) {
+    ObjCProtocolDecl *Protocol = *I;
+    ObjCPropertyDecl *property = Protocol->FindPropertyDeclaration(PropertyId);
+    if (property)
+      return property;
+  }
   if (getSuperClass())
     return getSuperClass()->FindPropertyDeclaration(PropertyId);
   return 0;
@@ -397,6 +405,20 @@
   return 0;
 }
 
+/// FindPropertyDeclaration - Finds declaration of the property given its name
+/// in 'PropertyId' and returns it. It returns 0, if not found.
+///
+ObjCPropertyDecl *
+ObjCProtocolDecl::FindPropertyDeclaration(IdentifierInfo *PropertyId) const {
+  for (ObjCProtocolDecl::classprop_iterator I = classprop_begin(),
+       E = classprop_end(); I != E; ++I) {
+    ObjCPropertyDecl *property = *I;
+    if (property->getIdentifier() == PropertyId)
+      return property;
+  }
+  return 0;
+}
+
 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(
   IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
   ObjCInterfaceDecl* ClassDecl = this;
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 8d38248..8960950 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -602,10 +602,11 @@
     return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
   } else if (BaseType->isObjCInterfaceType()) {
     ObjCInterfaceDecl *IFace;
-    if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
-      IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
+    QualType CanonType = BaseType.getCanonicalType();
+    if (isa<ObjCInterfaceType>(CanonType))
+      IFace = dyn_cast<ObjCInterfaceType>(CanonType)->getDecl();
     else
-      IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
+      IFace = dyn_cast<ObjCQualifiedInterfaceType>(CanonType)->getDecl();
     ObjCInterfaceDecl *clsDeclared;
     if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
       return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr, 
@@ -614,10 +615,11 @@
     PointerType *pointerType = static_cast<PointerType*>(BaseType.getTypePtr());
     BaseType = pointerType->getPointeeType();
     ObjCInterfaceDecl *IFace;
-    if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
-      IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
+    QualType CanonType = BaseType.getCanonicalType();
+    if (isa<ObjCInterfaceType>(CanonType))
+      IFace = dyn_cast<ObjCInterfaceType>(CanonType)->getDecl();
     else
-      IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
+      IFace = dyn_cast<ObjCQualifiedInterfaceType>(CanonType)->getDecl();
     ObjCInterfaceDecl *clsDeclared;
     if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
       return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr, 
@@ -639,6 +641,15 @@
       // void someMethod() { frameworkBundle.bundlePath = 0; }
       //
       ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member);
+      
+      if (!PD) { // Lastly, check protocols on qualified interfaces.
+        if (ObjCQualifiedInterfaceType *QIT = 
+            dyn_cast<ObjCQualifiedInterfaceType>(CanonType)) {
+          for (unsigned i = 0; i < QIT->getNumProtocols(); i++)
+            if ((PD = QIT->getProtocols(i)->FindPropertyDeclaration(&Member)))
+              break;
+        }
+      }
       if (PD)
         return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
     }