[index] Take advantage of 'external_source_symbol' attribute for indexing purposes

- Ignore decls marked as 'generated_declaration'
- Include the 'defined_in' in the USR for additional namespacing

llvm-svn: 300949
diff --git a/clang/lib/Index/IndexDecl.cpp b/clang/lib/Index/IndexDecl.cpp
index 9bf1927..8028f88 100644
--- a/clang/lib/Index/IndexDecl.cpp
+++ b/clang/lib/Index/IndexDecl.cpp
@@ -14,6 +14,13 @@
 using namespace clang;
 using namespace index;
 
+#define TRY_DECL(D,CALL_EXPR)                                                  \
+  do {                                                                         \
+    if (!IndexCtx.shouldIndex(D)) return true;                                 \
+    if (!CALL_EXPR)                                                            \
+      return false;                                                            \
+  } while (0)
+
 #define TRY_TO(CALL_EXPR)                                                      \
   do {                                                                         \
     if (!CALL_EXPR)                                                            \
@@ -120,8 +127,7 @@
                                D->getDeclContext(), 0);
     }
 
-    if (!IndexCtx.handleDecl(D, MethodLoc, Roles, Relations))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D, MethodLoc, Roles, Relations));
     IndexCtx.indexTypeSourceInfo(D->getReturnTypeSourceInfo(), D);
     bool hasIBActionAndFirst = D->hasAttr<IBActionAttr>();
     for (const auto *I : D->parameters()) {
@@ -153,8 +159,7 @@
       }
     }
 
-    if (!IndexCtx.handleDecl(D, Roles, Relations))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D, Roles, Relations));
     handleDeclarator(D);
 
     if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
@@ -189,16 +194,14 @@
   }
 
   bool VisitVarDecl(const VarDecl *D) {
-    if (!IndexCtx.handleDecl(D))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D));
     handleDeclarator(D);
     IndexCtx.indexBody(D->getInit(), D);
     return true;
   }
 
   bool VisitFieldDecl(const FieldDecl *D) {
-    if (!IndexCtx.handleDecl(D))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D));
     handleDeclarator(D);
     if (D->isBitField())
       IndexCtx.indexBody(D->getBitWidth(), D);
@@ -212,8 +215,7 @@
       // handled in VisitObjCPropertyImplDecl
       return true;
     }
-    if (!IndexCtx.handleDecl(D))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D));
     handleDeclarator(D);
     return true;
   }
@@ -224,16 +226,14 @@
   }
 
   bool VisitEnumConstantDecl(const EnumConstantDecl *D) {
-    if (!IndexCtx.handleDecl(D))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D));
     IndexCtx.indexBody(D->getInitExpr(), D);
     return true;
   }
 
   bool VisitTypedefNameDecl(const TypedefNameDecl *D) {
     if (!D->isTransparentTag()) {
-      if (!IndexCtx.handleDecl(D))
-        return false;
+      TRY_DECL(D, IndexCtx.handleDecl(D));
       IndexCtx.indexTypeSourceInfo(D->getTypeSourceInfo(), D);
     }
     return true;
@@ -273,7 +273,7 @@
 
   bool VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
     if (D->isThisDeclarationADefinition()) {
-      TRY_TO(IndexCtx.handleDecl(D));
+      TRY_DECL(D, IndexCtx.handleDecl(D));
       SourceLocation SuperLoc = D->getSuperClassLoc();
       if (auto *SuperD = D->getSuperClass()) {
         bool hasSuperTypedef = false;
@@ -304,7 +304,7 @@
 
   bool VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
     if (D->isThisDeclarationADefinition()) {
-      TRY_TO(IndexCtx.handleDecl(D));
+      TRY_DECL(D, IndexCtx.handleDecl(D));
       TRY_TO(handleReferencedProtocols(D->getReferencedProtocols(), D,
                                        /*superLoc=*/SourceLocation()));
       TRY_TO(IndexCtx.indexDeclContext(D));
@@ -323,8 +323,7 @@
     if (Class->isImplicitInterfaceDecl())
       IndexCtx.handleDecl(Class);
 
-    if (!IndexCtx.handleDecl(D))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D));
 
     // Visit implicit @synthesize property implementations first as their
     // location is reported at the name of the @implementation block. This
@@ -343,6 +342,8 @@
   }
 
   bool VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
+    if (!IndexCtx.shouldIndex(D))
+      return true;
     const ObjCInterfaceDecl *C = D->getClassInterface();
     if (!C)
       return true;
@@ -371,8 +372,7 @@
     SourceLocation CategoryLoc = D->getCategoryNameLoc();
     if (!CategoryLoc.isValid())
       CategoryLoc = D->getLocation();
-    if (!IndexCtx.handleDecl(D, CategoryLoc))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D, CategoryLoc));
     IndexCtx.indexDeclContext(D);
     return true;
   }
@@ -394,8 +394,7 @@
     if (ObjCMethodDecl *MD = D->getSetterMethodDecl())
       if (MD->getLexicalDeclContext() == D->getLexicalDeclContext())
         handleObjCMethod(MD, D);
-    if (!IndexCtx.handleDecl(D))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D));
     if (IBOutletCollectionAttr *attr = D->getAttr<IBOutletCollectionAttr>())
       IndexCtx.indexTypeSourceInfo(attr->getInterfaceLoc(), D,
                                    D->getLexicalDeclContext(), false, true);
@@ -416,8 +415,7 @@
       Loc = Container->getLocation();
       Roles |= (SymbolRoleSet)SymbolRole::Implicit;
     }
-    if (!IndexCtx.handleDecl(D, Loc, Roles, Relations))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D, Loc, Roles, Relations));
 
     if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
       return true;
@@ -451,8 +449,7 @@
         } else if (D->getLocation() == IvarLoc) {
           IvarRoles = (SymbolRoleSet)SymbolRole::Implicit;
         }
-        if(!IndexCtx.handleDecl(IvarD, IvarLoc, IvarRoles))
-          return false;
+        TRY_DECL(IvarD, IndexCtx.handleDecl(IvarD, IvarLoc, IvarRoles));
       } else {
         IndexCtx.handleReference(IvarD, D->getPropertyIvarDeclLoc(), nullptr,
                                  D->getDeclContext(), SymbolRoleSet());
@@ -462,8 +459,7 @@
   }
 
   bool VisitNamespaceDecl(const NamespaceDecl *D) {
-    if (!IndexCtx.handleDecl(D))
-      return false;
+    TRY_DECL(D, IndexCtx.handleDecl(D));
     IndexCtx.indexDeclContext(D);
     return true;
   }
diff --git a/clang/lib/Index/IndexTypeSourceInfo.cpp b/clang/lib/Index/IndexTypeSourceInfo.cpp
index a3566a9..44d1241 100644
--- a/clang/lib/Index/IndexTypeSourceInfo.cpp
+++ b/clang/lib/Index/IndexTypeSourceInfo.cpp
@@ -210,6 +210,8 @@
 
 void IndexingContext::indexTagDecl(const TagDecl *D,
                                    ArrayRef<SymbolRelation> Relations) {
+  if (!shouldIndex(D))
+    return;
   if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalSymbol(D))
     return;
 
diff --git a/clang/lib/Index/IndexingContext.cpp b/clang/lib/Index/IndexingContext.cpp
index 85574d0..254abec 100644
--- a/clang/lib/Index/IndexingContext.cpp
+++ b/clang/lib/Index/IndexingContext.cpp
@@ -17,6 +17,17 @@
 using namespace clang;
 using namespace index;
 
+static bool isGeneratedDecl(const Decl *D) {
+  if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) {
+    return attr->getGeneratedDeclaration();
+  }
+  return false;
+}
+
+bool IndexingContext::shouldIndex(const Decl *D) {
+  return !isGeneratedDecl(D);
+}
+
 bool IndexingContext::shouldIndexFunctionLocalSymbols() const {
   return IndexOpts.IndexFunctionLocals;
 }
diff --git a/clang/lib/Index/IndexingContext.h b/clang/lib/Index/IndexingContext.h
index 1ebf6f9..c70cb0a 100644
--- a/clang/lib/Index/IndexingContext.h
+++ b/clang/lib/Index/IndexingContext.h
@@ -48,6 +48,8 @@
 
   void setASTContext(ASTContext &ctx) { Ctx = &ctx; }
 
+  bool shouldIndex(const Decl *D);
+
   bool shouldSuppressRefs() const {
     return false;
   }
diff --git a/clang/lib/Index/USRGeneration.cpp b/clang/lib/Index/USRGeneration.cpp
index 73dddd9..c373eda 100644
--- a/clang/lib/Index/USRGeneration.cpp
+++ b/clang/lib/Index/USRGeneration.cpp
@@ -46,6 +46,15 @@
   return false;
 }
 
+static StringRef GetExternalSourceContainer(const NamedDecl *D) {
+  if (!D)
+    return StringRef();
+  if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) {
+    return attr->getDefinedIn();
+  }
+  return StringRef();
+}
+
 namespace {
 class USRGenerator : public ConstDeclVisitor<USRGenerator> {
   SmallVectorImpl<char> &Buf;
@@ -116,6 +125,8 @@
     return D->getParentFunctionOrMethod() != nullptr;
   }
 
+  void GenExtSymbolContainer(const NamedDecl *D);
+
   /// Generate the string component containing the location of the
   ///  declaration.
   bool GenLoc(const Decl *D, bool IncludeOffset);
@@ -127,13 +138,14 @@
   /// itself.
 
   /// Generate a USR for an Objective-C class.
-  void GenObjCClass(StringRef cls) {
-    generateUSRForObjCClass(cls, Out);
+  void GenObjCClass(StringRef cls, StringRef ext) {
+    generateUSRForObjCClass(cls, Out, ext);
   }
 
   /// Generate a USR for an Objective-C class category.
-  void GenObjCCategory(StringRef cls, StringRef cat) {
-    generateUSRForObjCCategory(cls, cat, Out);
+  void GenObjCCategory(StringRef cls, StringRef cat,
+                       StringRef clsExt, StringRef catExt) {
+    generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
   }
 
   /// Generate a USR fragment for an Objective-C property.
@@ -142,8 +154,8 @@
   }
 
   /// Generate a USR for an Objective-C protocol.
-  void GenObjCProtocol(StringRef prot) {
-    generateUSRForObjCProtocol(prot, Out);
+  void GenObjCProtocol(StringRef prot, StringRef ext) {
+    generateUSRForObjCProtocol(prot, Out, ext);
   }
 
   void VisitType(QualType T);
@@ -204,7 +216,11 @@
   if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
     return;
 
+  const unsigned StartSize = Buf.size();
   VisitDeclContext(D->getDeclContext());
+  if (Buf.size() == StartSize)
+    GenExtSymbolContainer(D);
+
   bool IsTemplate = false;
   if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
     IsTemplate = true;
@@ -382,7 +398,7 @@
       llvm_unreachable("Invalid ObjC container.");
     case Decl::ObjCInterface:
     case Decl::ObjCImplementation:
-      GenObjCClass(D->getName());
+      GenObjCClass(D->getName(), GetExternalSourceContainer(D));
       break;
     case Decl::ObjCCategory: {
       const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
@@ -402,7 +418,9 @@
         GenLoc(CD, /*IncludeOffset=*/true);
       }
       else
-        GenObjCCategory(ID->getName(), CD->getName());
+        GenObjCCategory(ID->getName(), CD->getName(),
+                        GetExternalSourceContainer(ID),
+                        GetExternalSourceContainer(CD));
 
       break;
     }
@@ -417,12 +435,16 @@
         IgnoreResults = true;
         return;
       }
-      GenObjCCategory(ID->getName(), CD->getName());
+      GenObjCCategory(ID->getName(), CD->getName(),
+                      GetExternalSourceContainer(ID),
+                      GetExternalSourceContainer(CD));
       break;
     }
-    case Decl::ObjCProtocol:
-      GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
+    case Decl::ObjCProtocol: {
+      const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
+      GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
       break;
+    }
   }
 }
 
@@ -452,6 +474,8 @@
       ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
     return;
 
+  GenExtSymbolContainer(D);
+
   D = D->getCanonicalDecl();
   VisitDeclContext(D->getDeclContext());
 
@@ -544,6 +568,12 @@
   GenLoc(D, /*IncludeOffset=*/true);
 }
 
+void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
+  StringRef Container = GetExternalSourceContainer(D);
+  if (!Container.empty())
+    Out << "@M@" << Container;
+}
+
 bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
   if (generatedLoc)
     return IgnoreResults;
@@ -866,12 +896,27 @@
 // USR generation functions.
 //===----------------------------------------------------------------------===//
 
-void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS) {
+void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
+                                           StringRef ExtSymDefinedIn) {
+  if (!ExtSymDefinedIn.empty())
+    OS << "@M@" << ExtSymDefinedIn << '@';
   OS << "objc(cs)" << Cls;
 }
 
 void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
-                                              raw_ostream &OS) {
+                                              raw_ostream &OS,
+                                              StringRef ClsSymDefinedIn,
+                                              StringRef CatSymDefinedIn) {
+  if (!CatSymDefinedIn.empty() || !ClsSymDefinedIn.empty()) {
+    OS << "@M@";
+    if (!CatSymDefinedIn.empty() && !ClsSymDefinedIn.empty())
+      OS << CatSymDefinedIn << '-' << ClsSymDefinedIn;
+    else if (!CatSymDefinedIn.empty())
+      OS << CatSymDefinedIn;
+    else
+      OS << ClsSymDefinedIn;
+    OS << '@';
+  }
   OS << "objc(cy)" << Cls << '@' << Cat;
 }
 
@@ -890,10 +935,20 @@
   OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
 }
 
-void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS) {
+void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
+                                              StringRef ExtSymDefinedIn) {
+  if (!ExtSymDefinedIn.empty())
+    OS << "@M@" << ExtSymDefinedIn << '@';
   OS << "objc(pl)" << Prot;
 }
 
+void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
+                                            StringRef ExtSymDefinedIn) {
+  if (!ExtSymDefinedIn.empty())
+    OS << "@M@" << ExtSymDefinedIn;
+  OS << "@E@" << EnumName;
+}
+
 bool clang::index::generateUSRForDecl(const Decl *D,
                                       SmallVectorImpl<char> &Buf) {
   if (!D)