[libclang] Indexing API: if the CXIndexOpt_OneRefPerFile option is set, only report one reference
per file.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@144763 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/libclang/IndexingContext.h b/tools/libclang/IndexingContext.h
index fa43a55..35545d2 100644
--- a/tools/libclang/IndexingContext.h
+++ b/tools/libclang/IndexingContext.h
@@ -12,7 +12,7 @@
 
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/DeclGroup.h"
-#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DenseSet.h"
 
 namespace clang {
   class FileEntry;
@@ -133,6 +133,14 @@
   static bool classof(const ObjCCategoryDeclInfo *D) { return true; }
 };
 
+struct RefFileOccurence {
+  const FileEntry *File;
+  const Decl *Dcl;
+
+  RefFileOccurence(const FileEntry *File, const Decl *Dcl)
+    : File(File), Dcl(Dcl) { }
+};
+
 class IndexingContext {
   ASTContext *Ctx;
   CXClientData ClientData;
@@ -145,6 +153,8 @@
   FileMapTy FileMap;
   ContainerMapTy ContainerMap;
 
+  llvm::DenseSet<RefFileOccurence> RefFileOccurences;
+
   SmallVector<DeclGroupRef, 8> TUDeclsInObjCContainer;
   
   llvm::SmallString<256> StrScratch;
@@ -204,6 +214,10 @@
 
   void setASTContext(ASTContext &ctx);
 
+  bool onlyOneRefPerFile() const {
+    return IndexOptions & CXIndexOpt_OneRefPerFile;
+  }
+
   void enteredMainFile(const FileEntry *File);
 
   void ppIncludedFile(SourceLocation hashLoc,
@@ -313,3 +327,31 @@
 };
 
 }} // end clang::cxindex
+
+namespace llvm {
+  /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
+  /// DenseSets.
+  template <>
+  struct DenseMapInfo<clang::cxindex::RefFileOccurence> {
+    static inline clang::cxindex::RefFileOccurence getEmptyKey() {
+      return clang::cxindex::RefFileOccurence(0, 0);
+    }
+
+    static inline clang::cxindex::RefFileOccurence getTombstoneKey() {
+      return clang::cxindex::RefFileOccurence((const clang::FileEntry *)~0,
+                                              (const clang::Decl *)~0);
+    }
+
+    static unsigned getHashValue(clang::cxindex::RefFileOccurence S) {
+      llvm::FoldingSetNodeID ID;
+      ID.AddPointer(S.File);
+      ID.AddPointer(S.Dcl);
+      return ID.ComputeHash();
+    }
+
+    static bool isEqual(clang::cxindex::RefFileOccurence LHS,
+                        clang::cxindex::RefFileOccurence RHS) {
+      return LHS.File == RHS.File && LHS.Dcl == RHS.Dcl;
+    }
+  };
+}