[libclang] For a class extension, give it a unique USR but for any property or ivar
it contains give it a USR based on its semantic context, which is the interface.

This follows what we already did for objc methods. rdar://10371669

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@143464 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/libclang/CIndexUSRs.cpp b/tools/libclang/CIndexUSRs.cpp
index 11b124c..32c4db6 100644
--- a/tools/libclang/CIndexUSRs.cpp
+++ b/tools/libclang/CIndexUSRs.cpp
@@ -169,7 +169,12 @@
 }
 
 void USRGenerator::VisitFieldDecl(FieldDecl *D) {
-  VisitDeclContext(D->getDeclContext());
+  // The USR for an ivar declared in a class extension is based on the
+  // ObjCInterfaceDecl, not the ObjCCategoryDecl.
+  if (ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
+    Visit(ID);
+  else
+    VisitDeclContext(D->getDeclContext());
   Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
   if (EmitDeclName(D)) {
     // Bit fields can be anonymous.
@@ -336,9 +341,11 @@
         IgnoreResults = true;
         return;
       }
+      // Specially handle class extensions, which are anonymous categories.
+      // We want to mangle in the location to uniquely distinguish them.
       if (CD->IsClassExtension()) {
-        // An extension semantically continues the interface of the class.
-        GenObjCClass(ID->getName());
+        Out << "objc(ext)" << ID->getName() << '@';
+        GenLoc(CD);
       }
       else
         GenObjCCategory(ID->getName(), CD->getName());
@@ -366,7 +373,12 @@
 }
 
 void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
-  Visit(cast<Decl>(D->getDeclContext()));
+  // The USR for a property declared in a class extension or category is based
+  // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
+  if (ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
+    Visit(ID);
+  else
+    Visit(cast<Decl>(D->getDeclContext()));
   GenObjCProperty(D->getName());
 }