nathawes updated this revision to Diff 127412.
nathawes marked an inline comment as done.
nathawes added a comment.

I've refactored the indexing/dependency data collection out from the writing 
with the new IndexUnitDataConsumer class, and made other smaller changes to 
address the feedback from @ioeric.


https://reviews.llvm.org/D39050

Files:
  include/clang/Basic/AllDiagnostics.h
  include/clang/Basic/CMakeLists.txt
  include/clang/Basic/Diagnostic.td
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Basic/DiagnosticIDs.h
  include/clang/Basic/DiagnosticIndexKinds.td
  include/clang/Driver/Job.h
  include/clang/Driver/Options.td
  include/clang/Frontend/CompilerInstance.h
  include/clang/Frontend/FrontendOptions.h
  include/clang/Index/DeclOccurrence.h
  include/clang/Index/IndexDataConsumer.h
  include/clang/Index/IndexDiagnostic.h
  include/clang/Index/IndexUnitDataConsumer.h
  include/clang/Index/IndexingAction.h
  include/clang/module.modulemap
  lib/Basic/DiagnosticIDs.cpp
  lib/Driver/Driver.cpp
  lib/Driver/Job.cpp
  lib/Driver/ToolChains/Clang.cpp
  lib/Driver/ToolChains/Darwin.cpp
  lib/Frontend/CompilerInstance.cpp
  lib/Frontend/CompilerInvocation.cpp
  lib/FrontendTool/CMakeLists.txt
  lib/FrontendTool/ExecuteCompilerInvocation.cpp
  lib/Index/CMakeLists.txt
  lib/Index/FileIndexData.cpp
  lib/Index/FileIndexData.h
  lib/Index/IndexingAction.cpp
  lib/Index/IndexingContext.cpp
  lib/Index/IndexingContext.h
  test/Index/Store/assembly-invocation.c
  tools/c-index-test/core_main.cpp
  tools/diagtool/DiagnosticNames.cpp
  tools/libclang/CXIndexDataConsumer.cpp
  tools/libclang/CXIndexDataConsumer.h

Index: tools/libclang/CXIndexDataConsumer.h
===================================================================
--- tools/libclang/CXIndexDataConsumer.h
+++ tools/libclang/CXIndexDataConsumer.h
@@ -463,12 +463,12 @@
 private:
   bool handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
                            ArrayRef<index::SymbolRelation> Relations,
-                           FileID FID, unsigned Offset,
+                           FileID FID, unsigned Offset, bool IsInSystemFile,
                            ASTNodeInfo ASTNode) override;
 
   bool handleModuleOccurence(const ImportDecl *ImportD,
-                             index::SymbolRoleSet Roles,
-                             FileID FID, unsigned Offset) override;
+                             index::SymbolRoleSet Roles, FileID FID,
+                             unsigned Offset, bool IsInSystemFile) override;
 
   void finish() override;
 
Index: tools/libclang/CXIndexDataConsumer.cpp
===================================================================
--- tools/libclang/CXIndexDataConsumer.cpp
+++ tools/libclang/CXIndexDataConsumer.cpp
@@ -150,11 +150,9 @@
 };
 }
 
-bool CXIndexDataConsumer::handleDeclOccurence(const Decl *D,
-                                              SymbolRoleSet Roles,
-                                             ArrayRef<SymbolRelation> Relations,
-                                              FileID FID, unsigned Offset,
-                                              ASTNodeInfo ASTNode) {
+bool CXIndexDataConsumer::handleDeclOccurence(
+    const Decl *D, SymbolRoleSet Roles, ArrayRef<SymbolRelation> Relations,
+    FileID FID, unsigned Offset, bool IsInSystemFile, ASTNodeInfo ASTNode) {
   SourceLocation Loc = getASTContext().getSourceManager()
       .getLocForStartOfFile(FID).getLocWithOffset(Offset);
 
@@ -219,9 +217,9 @@
 }
 
 bool CXIndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD,
-                                                SymbolRoleSet Roles,
-                                                FileID FID,
-                                                unsigned Offset) {
+                                                SymbolRoleSet Roles, FileID FID,
+                                                unsigned Offset,
+                                                bool IsInSystemFile) {
   IndexingDeclVisitor(*this, SourceLocation(), nullptr).Visit(ImportD);
   return !shouldAbort();
 }
Index: tools/diagtool/DiagnosticNames.cpp
===================================================================
--- tools/diagtool/DiagnosticNames.cpp
+++ tools/diagtool/DiagnosticNames.cpp
@@ -43,6 +43,7 @@
 #include "clang/Basic/DiagnosticSemaKinds.inc"
 #include "clang/Basic/DiagnosticAnalysisKinds.inc"
 #include "clang/Basic/DiagnosticRefactoringKinds.inc"
+#include "clang/Basic/DiagnosticIndexKinds.inc"
 #undef DIAG
 };
 
Index: tools/c-index-test/core_main.cpp
===================================================================
--- tools/c-index-test/core_main.cpp
+++ tools/c-index-test/core_main.cpp
@@ -87,8 +87,8 @@
   }
 
   bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
-                           ArrayRef<SymbolRelation> Relations,
-                           FileID FID, unsigned Offset,
+                           ArrayRef<SymbolRelation> Relations, FileID FID,
+                           unsigned Offset, bool IsInSystemFile,
                            ASTNodeInfo ASTNode) override {
     ASTContext &Ctx = D->getASTContext();
     SourceManager &SM = Ctx.getSourceManager();
@@ -124,7 +124,8 @@
   }
 
   bool handleModuleOccurence(const ImportDecl *ImportD, SymbolRoleSet Roles,
-                             FileID FID, unsigned Offset) override {
+                             FileID FID, unsigned Offset,
+                             bool IsInSystemFile) override {
     ASTContext &Ctx = ImportD->getASTContext();
     SourceManager &SM = Ctx.getSourceManager();
 
Index: test/Index/Store/assembly-invocation.c
===================================================================
--- /dev/null
+++ test/Index/Store/assembly-invocation.c
@@ -0,0 +1,3 @@
+// Make sure it doesn't crash.
+// RUN: %clang -target x86_64-apple-macosx10.7 -S %s -o %t.s
+// RUN: %clang -target x86_64-apple-macosx10.7 -c %t.s -o %t.o -index-store-path %t.idx
Index: lib/Index/IndexingContext.h
===================================================================
--- lib/Index/IndexingContext.h
+++ lib/Index/IndexingContext.h
@@ -11,9 +11,11 @@
 #define LLVM_CLANG_LIB_INDEX_INDEXINGCONTEXT_H
 
 #include "clang/Basic/LLVM.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Index/IndexSymbol.h"
 #include "clang/Index/IndexingAction.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
 
 namespace clang {
   class ASTContext;
@@ -29,25 +31,51 @@
   class Stmt;
   class Expr;
   class TypeLoc;
-  class SourceLocation;
+  class DirectoryEntry;
 
 namespace index {
   class IndexDataConsumer;
 
+/// Tracks the current system root path and computes and caches whether a
+/// file is considered a system file or not
+class SystemFileCache {
+  std::string SysrootPath;
+  // Records whether a directory entry is system or not.
+  llvm::DenseMap<const DirectoryEntry *, bool> DirEntries;
+  // Keeps track of the last check for whether a FileID is system or
+  // not. This is used to speed up isSystemFile() call.
+  std::pair<FileID, bool> LastFileCheck;
+
+public:
+  SystemFileCache() = default;
+  SystemFileCache(std::string SysrootPath);
+
+  void setSysrootPath(StringRef path);
+  StringRef getSysrootPath() const { return SysrootPath; }
+  bool isSystem(FileID FID, SourceManager &SM);
+};
+
+/// Generates and reports indexing data to the provided \c IndexDataConsumer
+/// for any AST nodes passed to its various \c index* methods.
 class IndexingContext {
   IndexingOptions IndexOpts;
+  SystemFileCache SystemCache;
   IndexDataConsumer &DataConsumer;
   ASTContext *Ctx = nullptr;
 
 public:
   IndexingContext(IndexingOptions IndexOpts, IndexDataConsumer &DataConsumer)
-    : IndexOpts(IndexOpts), DataConsumer(DataConsumer) {}
+      : IndexOpts(IndexOpts), DataConsumer(DataConsumer) {}
 
   const IndexingOptions &getIndexOpts() const { return IndexOpts; }
+  SystemFileCache &getSystemCache() { return SystemCache; }
   IndexDataConsumer &getDataConsumer() { return DataConsumer; }
 
   void setASTContext(ASTContext &ctx) { Ctx = &ctx; }
 
+  void setSysrootPath(StringRef path) { SystemCache.setSysrootPath(path); }
+  StringRef getSysrootPath() const { return SystemCache.getSysrootPath(); }
+
   bool shouldIndex(const Decl *D);
 
   const LangOptions &getLangOpts() const;
Index: lib/Index/IndexingContext.cpp
===================================================================
--- lib/Index/IndexingContext.cpp
+++ lib/Index/IndexingContext.cpp
@@ -8,22 +8,79 @@
 //===----------------------------------------------------------------------===//
 
 #include "IndexingContext.h"
-#include "clang/Index/IndexDataConsumer.h"
 #include "clang/AST/ASTContext.h"
-#include "clang/AST/DeclTemplate.h"
 #include "clang/AST/DeclObjC.h"
+#include "clang/AST/DeclTemplate.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Index/IndexDataConsumer.h"
+#include "llvm/Support/Path.h"
 
 using namespace clang;
 using namespace index;
+using namespace llvm;
 
 static bool isGeneratedDecl(const Decl *D) {
   if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) {
     return attr->getGeneratedDeclaration();
   }
   return false;
 }
 
+void SystemFileCache::setSysrootPath(llvm::StringRef Path) {
+  // Ignore sysroot path if it points to root, otherwise every header will be
+  // treated as system one.
+  SysrootPath = sys::path::root_path(Path) == Path ? StringRef() : Path;
+
+  // Invalidate existing results
+  LastFileCheck = {FileID(), false};
+  DirEntries.clear();
+}
+
+SystemFileCache::SystemFileCache(std::string Path) {
+  setSysrootPath(Path);
+}
+
+bool SystemFileCache::isSystem(clang::FileID FID, clang::SourceManager &SM) {
+  if (LastFileCheck.first == FID)
+    return LastFileCheck.second;
+
+  auto Result = [&](bool Res) -> bool {
+    LastFileCheck = {FID, Res};
+    return Res;
+  };
+
+  bool Invalid = false;
+  const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
+  if (Invalid || !SEntry.isFile())
+    return Result(false);
+
+  const SrcMgr::FileInfo &FI = SEntry.getFile();
+  if (FI.getFileCharacteristic() != SrcMgr::C_User)
+    return Result(true);
+
+  auto *CC = FI.getContentCache();
+  if (!CC)
+    return Result(false);
+  auto *FE = CC->OrigEntry;
+  if (!FE)
+    return Result(false);
+
+  if (SysrootPath.empty())
+    return Result(false);
+
+  // Check if directory is in sysroot so that we can consider system headers
+  // even the headers found via a user framework search path, pointing inside
+  // sysroot.
+  auto DirEntry = FE->getDir();
+  auto Pair = DirEntries.insert(std::make_pair(DirEntry, false));
+  bool &IsSystemDir = Pair.first->second;
+  bool WasInserted = Pair.second;
+  if (WasInserted) {
+    IsSystemDir = StringRef(DirEntry->getName()).startswith(SysrootPath);
+  }
+  return Result(IsSystemDir);
+}
+
 bool IndexingContext::shouldIndex(const Decl *D) {
   return !isGeneratedDecl(D);
 }
@@ -93,12 +150,8 @@
   if (FID.isInvalid())
     return true;
 
-  bool Invalid = false;
-  const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
-  if (Invalid || !SEntry.isFile())
-    return true;
-
-  if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) {
+  bool IsInSystemFile = SystemCache.isSystem(FID, SM);
+  if (IsInSystemFile) {
     switch (IndexOpts.SystemSymbolFilter) {
     case IndexingOptions::SystemSymbolFilterKind::None:
       return true;
@@ -112,7 +165,8 @@
   if (ImportD->isImplicit())
     Roles |= (unsigned)SymbolRole::Implicit;
 
-  return DataConsumer.handleModuleOccurence(ImportD, Roles, FID, Offset);
+  return DataConsumer.handleModuleOccurence(ImportD, Roles, FID, Offset,
+                                            IsInSystemFile);
 }
 
 bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
@@ -205,7 +259,8 @@
   return nullptr;
 }
 
-static bool isDeclADefinition(const Decl *D, const DeclContext *ContainerDC, ASTContext &Ctx) {
+static bool isDeclADefinition(const Decl *D, const DeclContext *ContainerDC,
+                              ASTContext &Ctx) {
   if (auto VD = dyn_cast<VarDecl>(D))
     return VD->isThisDeclarationADefinition(Ctx);
 
@@ -321,7 +376,7 @@
                                            const Expr *OrigE,
                                            const Decl *OrigD,
                                            const DeclContext *ContainerDC) {
-  if (D->isImplicit() && !isa<ObjCMethodDecl>(D))
+  if (D->isImplicit() && !(isa<ObjCMethodDecl>(D) || isa<ObjCIvarDecl>(D)))
     return true;
   if (!isa<NamedDecl>(D) || shouldSkipNamelessDecl(cast<NamedDecl>(D)))
     return true;
@@ -337,12 +392,8 @@
   if (FID.isInvalid())
     return true;
 
-  bool Invalid = false;
-  const SrcMgr::SLocEntry &SEntry = SM.getSLocEntry(FID, &Invalid);
-  if (Invalid || !SEntry.isFile())
-    return true;
-
-  if (SEntry.getFile().getFileCharacteristic() != SrcMgr::C_User) {
+  bool IsInSystemFile = SystemCache.isSystem(FID, SM);
+  if (IsInSystemFile) {
     switch (IndexOpts.SystemSymbolFilter) {
     case IndexingOptions::SystemSymbolFilterKind::None:
       return true;
@@ -416,5 +467,5 @@
 
   IndexDataConsumer::ASTNodeInfo Node{ OrigE, OrigD, Parent, ContainerDC };
   return DataConsumer.handleDeclOccurence(D, Roles, FinalRelations, FID, Offset,
-                                          Node);
+                                          IsInSystemFile, Node);
 }
Index: lib/Index/IndexingAction.cpp
===================================================================
--- lib/Index/IndexingAction.cpp
+++ lib/Index/IndexingAction.cpp
@@ -8,34 +8,47 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Index/IndexingAction.h"
-#include "clang/Index/IndexDataConsumer.h"
+#include "FileIndexData.h"
 #include "IndexingContext.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendAction.h"
+#include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/Frontend/MultiplexConsumer.h"
+#include "clang/Frontend/Utils.h"
+#include "clang/Index/IndexDataConsumer.h"
+#include "clang/Index/IndexUnitDataConsumer.h"
+#include "clang/Index/IndexDiagnostic.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Serialization/ASTReader.h"
+#include "llvm/Support/Path.h"
 
 using namespace clang;
 using namespace clang::index;
 
+void IndexUnitDataConsumer::_anchor() {}
 void IndexDataConsumer::_anchor() {}
 
 bool IndexDataConsumer::handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
                                             ArrayRef<SymbolRelation> Relations,
                                             FileID FID, unsigned Offset,
+                                            bool IsInSystemFile,
                                             ASTNodeInfo ASTNode) {
   return true;
 }
 
 bool IndexDataConsumer::handleMacroOccurence(const IdentifierInfo *Name,
-                                             const MacroInfo *MI, SymbolRoleSet Roles,
-                                             FileID FID, unsigned Offset) {
+                                             const MacroInfo *MI,
+                                             SymbolRoleSet Roles, FileID FID,
+                                             unsigned Offset,
+                                             bool IsInSystemFile) {
   return true;
 }
 
 bool IndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD,
-                                              SymbolRoleSet Roles,
-                                              FileID FID, unsigned Offset) {
+                                              SymbolRoleSet Roles, FileID FID,
+                                              unsigned Offset,
+                                              bool IsInSystemFile) {
   return true;
 }
 
@@ -70,94 +83,110 @@
   }
 };
 
-class IndexActionBase {
-protected:
-  std::shared_ptr<IndexDataConsumer> DataConsumer;
-  IndexingContext IndexCtx;
-
-  IndexActionBase(std::shared_ptr<IndexDataConsumer> dataConsumer,
-                  IndexingOptions Opts)
-    : DataConsumer(std::move(dataConsumer)),
-      IndexCtx(Opts, *DataConsumer) {}
+/// Abstracts the core logic shared between \c IndexAction and
+/// \c WrappingIndexAction frontend actions.
+struct IndexActionImpl {
+  virtual ~IndexActionImpl() = default;
 
-  std::unique_ptr<IndexASTConsumer> createIndexASTConsumer() {
-    return llvm::make_unique<IndexASTConsumer>(IndexCtx);
-  }
+  /// Called at the beginning of processing a single input, this creates the
+  /// IndexASTConsumer object to use.
+  ///
+  /// \returns the created IndexASTConsumer.
+  virtual std::unique_ptr<IndexASTConsumer>
+  createIndexASTConsumer(CompilerInstance &CI) = 0;
 
-  void finish() {
-    DataConsumer->finish();
-  }
+  /// Callback at the end of processing a single input.
+  virtual void finish(CompilerInstance &CI) = 0;
 };
 
-class IndexAction : public ASTFrontendAction, IndexActionBase {
+class IndexAction : public ASTFrontendAction {
+    std::unique_ptr<IndexActionImpl> Impl;
 public:
-  IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
-              IndexingOptions Opts)
-    : IndexActionBase(std::move(DataConsumer), Opts) {}
+  IndexAction(std::unique_ptr<IndexActionImpl> Impl)
+    : Impl(std::move(Impl)) {}
 
 protected:
   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
                                                  StringRef InFile) override {
-    return createIndexASTConsumer();
+    return Impl->createIndexASTConsumer(CI);
   }
 
   void EndSourceFileAction() override {
     FrontendAction::EndSourceFileAction();
-    finish();
+    Impl->finish(getCompilerInstance());
   }
 };
 
-class WrappingIndexAction : public WrapperFrontendAction, IndexActionBase {
-  bool IndexActionFailed = false;
+class WrappingIndexAction : public WrapperFrontendAction {
+  std::unique_ptr<IndexActionImpl> Impl;
+  bool CreatedASTConsumer = false;
 
 public:
   WrappingIndexAction(std::unique_ptr<FrontendAction> WrappedAction,
-                      std::shared_ptr<IndexDataConsumer> DataConsumer,
-                      IndexingOptions Opts)
+                      std::unique_ptr<IndexActionImpl> Impl)
     : WrapperFrontendAction(std::move(WrappedAction)),
-      IndexActionBase(std::move(DataConsumer), Opts) {}
+      Impl(std::move(Impl)) {}
 
 protected:
   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
-                                                 StringRef InFile) override;
-  void EndSourceFileAction() override;
+                                                 StringRef InFile) override {
+    auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
+    if (!OtherConsumer)
+      return nullptr;
+
+    CreatedASTConsumer = true;
+    std::vector<std::unique_ptr<ASTConsumer>> Consumers;
+    Consumers.push_back(std::move(OtherConsumer));
+    Consumers.push_back(Impl->createIndexASTConsumer(CI));
+    return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
+  };
+
+  void EndSourceFileAction() override {
+    // Invoke wrapped action's method.
+    WrapperFrontendAction::EndSourceFileAction();
+    if (CreatedASTConsumer)
+      Impl->finish(getCompilerInstance());
+  };
 };
 
-} // anonymous namespace
+/// An implementation for \c IndexAction or \c WrappingIndexAction that provides
+/// decl ocurrences information from the AST.
+class DataConsumerActionImpl : public IndexActionImpl {
+protected:
+  std::shared_ptr<IndexDataConsumer> DataConsumer;
+  IndexingContext IndexCtx;
 
-void WrappingIndexAction::EndSourceFileAction() {
-  // Invoke wrapped action's method.
-  WrapperFrontendAction::EndSourceFileAction();
-  if (!IndexActionFailed)
-    finish();
-}
+public:
+  DataConsumerActionImpl(std::shared_ptr<IndexDataConsumer> Consumer,
+                         IndexingOptions Opts)
+      : DataConsumer(std::move(Consumer)),
+        IndexCtx(Opts, *DataConsumer) {}
+
+  std::unique_ptr<IndexASTConsumer>
+  createIndexASTConsumer(CompilerInstance &CI) override {
+    IndexCtx.setSysrootPath(CI.getHeaderSearchOpts().Sysroot);
+    return llvm::make_unique<IndexASTConsumer>(IndexCtx);
+  }
 
-std::unique_ptr<ASTConsumer>
-WrappingIndexAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
-  auto OtherConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
-  if (!OtherConsumer) {
-    IndexActionFailed = true;
-    return nullptr;
+  void finish(CompilerInstance &CI) override {
+    DataConsumer->finish();
   }
+};
 
-  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
-  Consumers.push_back(std::move(OtherConsumer));
-  Consumers.push_back(createIndexASTConsumer());
-  return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
-}
+} // anonymous namespace
 
 std::unique_ptr<FrontendAction>
 index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
                             IndexingOptions Opts,
                             std::unique_ptr<FrontendAction> WrappedAction) {
+  auto ActionImpl = llvm::make_unique<DataConsumerActionImpl>(
+      std::move(DataConsumer), Opts);
   if (WrappedAction)
-    return llvm::make_unique<WrappingIndexAction>(std::move(WrappedAction),
-                                                  std::move(DataConsumer),
-                                                  Opts);
-  return llvm::make_unique<IndexAction>(std::move(DataConsumer), Opts);
+    return llvm::make_unique<WrappingIndexAction>(
+        std::move(WrappedAction), std::move(ActionImpl));
+  return llvm::make_unique<IndexAction>(std::move(ActionImpl));
 }
 
-
 static bool topLevelDeclVisitor(void *context, const Decl *D) {
   IndexingContext &IndexCtx = *static_cast<IndexingContext*>(context);
   return IndexCtx.indexTopLevelDecl(D);
@@ -203,3 +232,556 @@
   }
   DataConsumer->finish();
 }
+
+//===----------------------------------------------------------------------===//
+// Index Data Recording
+//===----------------------------------------------------------------------===//
+
+namespace {
+
+/// Collects and groups consumed index data by \c FileID.
+class IndexDataCollector : public IndexDataConsumer {
+  Preprocessor *PP = nullptr;
+  typedef llvm::DenseMap<FileID, std::unique_ptr<FileIndexData>>
+      IndexDataByFileTy;
+  IndexDataByFileTy IndexDataByFile;
+
+public:
+  void setPreprocessor(Preprocessor &PreProc) {
+    PP = &PreProc;
+  }
+
+  IndexDataByFileTy::const_iterator by_file_begin() const {
+    return IndexDataByFile.begin();
+  }
+
+  IndexDataByFileTy::const_iterator by_file_end() const {
+    return IndexDataByFile.end();
+  }
+
+  bool by_file_empty() const { return IndexDataByFile.empty(); }
+
+private:
+  bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
+                           ArrayRef<SymbolRelation> Relations, FileID FID,
+                           unsigned Offset, bool IsInSystemFile,
+                           ASTNodeInfo ASTNode) override {
+    // Ignore occurrences in the predefines buffer
+    if (FID == PP->getPredefinesFileID())
+      return true;
+
+    FileIndexData &Rec = getFileIndexData(FID, IsInSystemFile);
+    Rec.addDeclOccurence(Roles, Offset, D, Relations);
+    return true;
+  }
+
+  FileIndexData &getFileIndexData(FileID FID, bool IsInSystemFile) {
+    auto &Entry = IndexDataByFile[FID];
+    if (!Entry) {
+      Entry.reset(new FileIndexData(FID, IsInSystemFile));
+    }
+    return *Entry;
+  }
+};
+
+struct IncludeLocation {
+  const FileEntry *Source;
+  const FileEntry *Target;
+  unsigned Line;
+};
+
+/// Preprocessor callbacks to collect file to file inclusion information
+class IncludePPCallbacks : public PPCallbacks {
+  SystemFileCache &SystemCache;
+  UnitIndexingOptions::FileIncludeFilterKind FileIncludeFilter;
+  std::vector<IncludeLocation> &Includes;
+  SourceManager &SourceMgr;
+
+public:
+  IncludePPCallbacks(SystemFileCache &SystemCache,
+                     UnitIndexingOptions::FileIncludeFilterKind IncludeFilter,
+                     std::vector<IncludeLocation> &IncludesForFile,
+                     SourceManager &SourceMgr)
+      : SystemCache(SystemCache), FileIncludeFilter(IncludeFilter),
+        Includes(IncludesForFile), SourceMgr(SourceMgr) {}
+
+private:
+  void addInclude(SourceLocation From, const FileEntry *To) {
+    assert(To);
+    if (FileIncludeFilter == UnitIndexingOptions::FileIncludeFilterKind::None)
+      return;
+
+    std::pair<FileID, unsigned> LocInfo =
+        SourceMgr.getDecomposedExpansionLoc(From);
+
+    if (LocInfo.first.isInvalid())
+      return; // Ignore invalid locations
+
+    switch (FileIncludeFilter) {
+    case UnitIndexingOptions::FileIncludeFilterKind::None:
+      llvm_unreachable("should have already checked in the beginning");
+    case UnitIndexingOptions::FileIncludeFilterKind::UserOnly:
+      if (SystemCache.isSystem(LocInfo.first, SourceMgr))
+        return; // Ignore includes of system headers.
+      break;
+    case UnitIndexingOptions::FileIncludeFilterKind::All:
+      break;
+    }
+
+    if (auto *FE = SourceMgr.getFileEntryForID(LocInfo.first)) {
+      auto lineNo = SourceMgr.getLineNumber(LocInfo.first, LocInfo.second);
+      Includes.push_back({FE, To, lineNo});
+    }
+  }
+
+  virtual void InclusionDirective(SourceLocation HashLoc,
+                                  const Token &IncludeTok, StringRef FileName,
+                                  bool IsAngled, CharSourceRange FilenameRange,
+                                  const FileEntry *File, StringRef SearchPath,
+                                  StringRef RelativePath,
+                                  const Module *Imported) override {
+    if (HashLoc.isFileID() && File && File->isValid())
+      addInclude(HashLoc, File);
+  }
+};
+
+/// Abstract interface for providing the file and module dependencies of a
+/// translation unit, as well as the set of file to file inclusions
+class IndexDependencyProvider {
+public:
+  virtual ~IndexDependencyProvider() {}
+
+  virtual void visitFileDependencies(
+      const CompilerInstance &CI,
+      llvm::function_ref<void(const FileEntry *FE, bool IsSystem)> visitor) const = 0;
+  virtual void
+  visitIncludes(llvm::function_ref<void(const FileEntry *Source, unsigned Line,
+                                        const FileEntry *Target)>
+                    visitor) const = 0;
+  virtual void visitModuleImports(
+      const CompilerInstance &CI,
+      llvm::function_ref<void(serialization::ModuleFile &Mod, bool IsSystem)>
+          visitor) const = 0;
+};
+
+/// Collects and provides the file and module dependency information, including
+/// file to file inclusions, for the source files in a translation unit
+class SourceFilesIndexDependencyCollector : public DependencyCollector,
+                                            public IndexDependencyProvider {
+  SystemFileCache &SystemCache;
+  UnitIndexingOptions IndexOpts;
+  llvm::SetVector<const FileEntry *> Entries;
+  llvm::BitVector IsSystemByUID;
+  std::vector<IncludeLocation> Includes;
+  SourceManager *SourceMgr = nullptr;
+
+public:
+  SourceFilesIndexDependencyCollector(SystemFileCache &SystemCache,
+                                      UnitIndexingOptions IndexOpts)
+      : SystemCache(SystemCache), IndexOpts(IndexOpts) {}
+
+  void attachToPreprocessor(Preprocessor &PP) override {
+    DependencyCollector::attachToPreprocessor(PP);
+    PP.addPPCallbacks(llvm::make_unique<IncludePPCallbacks>(
+        SystemCache, IndexOpts.FileIncludeFilter, Includes,
+        PP.getSourceManager()));
+  }
+
+  void setSourceManager(SourceManager *SourceMgr) {
+    this->SourceMgr = SourceMgr;
+  }
+
+  void visitFileDependencies(
+      const CompilerInstance &CI,
+      llvm::function_ref<void(const FileEntry *FE, bool IsSystem)> visitor)
+      const override {
+    for (auto *FE : getEntries())
+      visitor(FE, isSystemFile(FE));
+  }
+
+  void
+  visitIncludes(llvm::function_ref<void(const FileEntry *Source, unsigned Line,
+                                        const FileEntry *Target)>
+                    visitor) const override {
+    for (auto &Include : Includes)
+      visitor(Include.Source, Include.Line, Include.Target);
+  }
+
+  void visitModuleImports(
+      const CompilerInstance &CI,
+      llvm::function_ref<void(serialization::ModuleFile &Mod, bool IsSystem)>
+          visitor) const override {
+    HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
+
+    if (auto Reader = CI.getModuleManager()) {
+      Reader->getModuleManager().visit(
+          [&](serialization::ModuleFile &Mod) -> bool {
+            bool IsSystemMod = false;
+            if (Mod.isModule()) {
+              if (auto *M =
+                      HS.lookupModule(Mod.ModuleName, /*AllowSearch=*/false))
+                IsSystemMod = M->IsSystem;
+            }
+            if (!IsSystemMod || IndexOpts.IncludeSystemDependencies)
+              visitor(Mod, IsSystemMod);
+            return true; // skip module dependencies.
+          });
+    }
+  }
+
+private:
+  bool isSystemFile(const FileEntry *FE) const {
+    auto UID = FE->getUID();
+    return IsSystemByUID.size() > UID && IsSystemByUID[UID];
+  }
+
+  ArrayRef<const FileEntry *> getEntries() const {
+    return Entries.getArrayRef();
+  }
+
+  bool needSystemDependencies() override {
+    return IndexOpts.IncludeSystemDependencies;
+  }
+
+  bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem,
+                     bool IsModuleFile, bool IsMissing) override {
+    bool SawIt = DependencyCollector::sawDependency(
+        Filename, FromModule, IsSystem, IsModuleFile, IsMissing);
+    if (auto *FE = SourceMgr->getFileManager().getFile(Filename)) {
+      if (SawIt)
+        Entries.insert(FE);
+      // Record system-ness for all files that we pass through.
+      if (IsSystemByUID.size() < FE->getUID() + 1)
+        IsSystemByUID.resize(FE->getUID() + 1);
+      IsSystemByUID[FE->getUID()] = IsSystem || isInSysroot(Filename);
+    }
+    return SawIt;
+  }
+
+  bool isInSysroot(StringRef Filename) {
+    StringRef SysrootPath = SystemCache.getSysrootPath();
+    return !SysrootPath.empty() && Filename.startswith(SysrootPath);
+  }
+};
+
+/// An implementation for IndexAction or WrappingIndexAction that gathers decl
+/// occurrence, file inclusion and dependency information for the translation
+/// and, optionally, its module dependencies.
+class UnitDataConsumerActionImpl : public IndexActionImpl {
+  UnitIndexingOptions IndexOpts;
+  IndexDataCollector Collector;
+  IndexingContext IndexCtx;
+  SourceFilesIndexDependencyCollector DepCollector;
+  IndexUnitDataConsumerFactory UnitConsumerFactory;
+
+public:
+  UnitDataConsumerActionImpl(UnitIndexingOptions UnitIndexOpts,
+                             IndexUnitDataConsumerFactory UnitConsumerFactory)
+    :  IndexOpts(UnitIndexOpts), IndexCtx(UnitIndexOpts, Collector),
+       DepCollector(IndexCtx.getSystemCache(), IndexOpts),
+       UnitConsumerFactory(std::move(UnitConsumerFactory)) {}
+
+  std::unique_ptr<IndexASTConsumer>
+  createIndexASTConsumer(CompilerInstance &CI) override {
+    IndexCtx.setSysrootPath(CI.getHeaderSearchOpts().Sysroot);
+
+    Preprocessor &PP = CI.getPreprocessor();
+    Collector.setPreprocessor(PP);
+    DepCollector.setSourceManager(&CI.getSourceManager());
+    DepCollector.attachToPreprocessor(PP);
+
+    return llvm::make_unique<IndexASTConsumer>(IndexCtx);
+  }
+
+  /// Provides the collected indexing info to the \c IndexUnitDataConsumer
+  void finish(CompilerInstance &CI) override;
+
+  /// Collects and provides indexing data for \c Mod to an \c
+  /// IndexUnitDataConsumer constructed from the \c UnitConsumerFactory if the
+  /// \c ParentUnitConsumer indicates \c Mod should be indexed.
+  ///
+  /// \returns true if \c Mod was indexed
+  static bool indexModule(
+      const CompilerInstance &CI, serialization::ModuleFile &Mod,
+      IndexUnitDataConsumer &ParentUnitConsumer,
+      IndexUnitDataConsumerFactory UnitConsumerFactory,
+      const UnitIndexingOptions &UnitIndexOpts);
+
+  /// Get unit details for the given module file
+  static UnitDetails getUnitDetails(serialization::ModuleFile &Mod,
+                                    const CompilerInstance &CI,
+                                    StringRef SysrootPath);
+
+private:
+  /// Get unit details for the current translation unit
+  static UnitDetails getUnitDetails(const CompilerInstance &CI,
+                                    StringRef SysrootPath);
+
+  static UnitDetails getUnitDetails(const CompilerInstance &CI,
+                                    std::string OutputFile,
+                                    const FileEntry *RootFile,
+                                    Module *UnitMod,
+                                    StringRef SysrootPath);
+  static void reportData(
+      const CompilerInstance &CI, const IndexDataCollector &Collector,
+      const IndexDependencyProvider &DepProvider, UnitDetails UnitInfo,
+      const IndexUnitDataConsumerFactory &UnitConsumerFactory,
+      const UnitIndexingOptions &UnitIndexOpts);
+};
+} // anonymous namespace
+
+void UnitDataConsumerActionImpl::finish(CompilerInstance &CI) {
+  // The consumer may emit more diagnostics so do the begin/end source file
+  // invocations on the diagnostic client.
+  // FIXME: FrontendAction::EndSourceFile() should probably not call
+  // CI.getDiagnosticClient().EndSourceFile()' until after it has called
+  // 'EndSourceFileAction()', so that code executing during
+  // EndSourceFileAction() can emit diagnostics. If this is fixed,
+  // DiagClientBeginEndRAII can go away.
+  struct DiagClientBeginEndRAII {
+    CompilerInstance &CI;
+    DiagClientBeginEndRAII(CompilerInstance &CI) : CI(CI) {
+      CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts());
+    }
+    ~DiagClientBeginEndRAII() { CI.getDiagnosticClient().EndSourceFile(); }
+  } diagClientBeginEndRAII(CI);
+
+  Collector.finish();
+  reportData(CI, Collector, DepCollector,
+             getUnitDetails(CI, IndexCtx.getSysrootPath()),
+             UnitConsumerFactory, IndexOpts);
+}
+
+void UnitDataConsumerActionImpl::reportData(
+    const CompilerInstance &CI, const IndexDataCollector &Collector,
+    const IndexDependencyProvider &DepProvider, UnitDetails UnitInfo,
+    const IndexUnitDataConsumerFactory &UnitConsumerFactory,
+    const UnitIndexingOptions &IndexOpts) {
+
+  std::unique_ptr<IndexUnitDataConsumer> Consumer =
+      UnitConsumerFactory(CI, UnitInfo);
+  if (!Consumer)
+    return;
+
+  DepProvider.visitFileDependencies(
+      CI, [&](const FileEntry *FE, bool IsSystemFile) {
+        Consumer->handleFileDependency(FE, IsSystemFile);
+      });
+  DepProvider.visitIncludes(
+      [&](const FileEntry *Source, unsigned Line, const FileEntry *Target) {
+        Consumer->handleInclude(Source, Line, Target);
+      });
+  DepProvider.visitModuleImports(CI, [&](serialization::ModuleFile &Mod,
+                                          bool IsSystemMod) {
+    Consumer->handleModuleImport(Mod, IsSystemMod);
+    if (Mod.isModule())
+      indexModule(CI, Mod, *Consumer, UnitConsumerFactory, IndexOpts);
+  });
+
+  for (auto I = Collector.by_file_begin(), E = Collector.by_file_end(); I != E;
+       ++I) {
+    FileID FID = I->first;
+    const FileIndexData &FileData = *I->second;
+    if (Consumer->handleFileOccurrences(
+            FID, FileData.getDeclOccurrencesSortedByOffset(),
+            FileData.isSystem()))
+      return;
+  }
+
+  Consumer->finish();
+}
+
+namespace {
+/// Collects and provides the file and module dependency information for the
+/// supplied \c ModuleFile
+class ModuleFileIndexDependencyCollector : public IndexDependencyProvider {
+  serialization::ModuleFile &ModFile;
+  bool CollectSystemDependencies;
+
+public:
+  ModuleFileIndexDependencyCollector(serialization::ModuleFile &Mod,
+                                     bool CollectSystemDependencies)
+      : ModFile(Mod), CollectSystemDependencies(CollectSystemDependencies) {}
+
+  void visitFileDependencies(
+      const CompilerInstance &CI,
+      llvm::function_ref<void(const FileEntry *FE, bool IsSystem)> visitor)
+      const override {
+    auto Reader = CI.getModuleManager();
+    Reader->visitInputFiles(
+        ModFile, CollectSystemDependencies, /*Complain=*/false,
+        [&](const serialization::InputFile &IF, bool IsSystem) {
+          auto *FE = IF.getFile();
+          if (!FE)
+            return;
+          // Ignore module map files, they are not as important to track as
+          // source files and they may be auto-generated which would create an
+          // undesirable dependency on an intermediate build byproduct.
+          if (FE->getName().endswith("module.modulemap"))
+            return;
+
+          visitor(FE, IsSystem);
+        });
+  }
+
+  void
+  visitIncludes(llvm::function_ref<void(const FileEntry *Source, unsigned Line,
+                                        const FileEntry *Target)>
+                    visitor) const override {
+    // FIXME: Module files without a preprocessing record do not have info about
+    // include locations. Serialize enough data to be able to retrieve such
+    // info.
+  }
+
+  void visitModuleImports(
+      const CompilerInstance &CI,
+      llvm::function_ref<void(serialization::ModuleFile &Mod, bool IsSystem)>
+          visitor) const override {
+    HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
+    for (auto *Mod : ModFile.Imports) {
+      bool IsSystemMod = false;
+      if (auto *M = HS.lookupModule(Mod->ModuleName, /*AllowSearch=*/false))
+        IsSystemMod = M->IsSystem;
+      if (!IsSystemMod || CollectSystemDependencies)
+        visitor(*Mod, IsSystemMod);
+    }
+  }
+};
+} // anonymous namespace.
+
+
+bool UnitDataConsumerActionImpl::indexModule(
+      const CompilerInstance &CI, serialization::ModuleFile &Mod,
+      IndexUnitDataConsumer &ParentUnitConsumer,
+      IndexUnitDataConsumerFactory UnitConsumerFactory,
+      const UnitIndexingOptions &IndexOpts) {
+
+  if (!ParentUnitConsumer.shouldIndexModuleDependency(Mod))
+    return false;
+
+  IndexDataCollector Collector;
+  IndexingContext ModIndexCtx(IndexOpts, Collector);
+
+  auto &ASTCtx = CI.getASTContext();
+  Collector.initialize(ASTCtx);
+  Collector.setPreprocessor(CI.getPreprocessor());
+  ModIndexCtx.setASTContext(ASTCtx);
+  ModIndexCtx.setSysrootPath(CI.getHeaderSearchOpts().Sysroot);
+
+  for (const Decl *D : CI.getModuleManager()->getModuleFileLevelDecls(Mod))
+    ModIndexCtx.indexTopLevelDecl(D);
+
+  Collector.finish();
+
+  ModuleFileIndexDependencyCollector DepCollector(
+      Mod, IndexOpts.IncludeSystemDependencies);
+
+  reportData(CI, Collector, DepCollector,
+      getUnitDetails(Mod, CI, ModIndexCtx.getSysrootPath()),
+      UnitConsumerFactory, IndexOpts);
+  return true;
+}
+
+UnitDetails UnitDataConsumerActionImpl::getUnitDetails(
+    const CompilerInstance &CI, StringRef SysrootPath) {
+  SourceManager &SM = CI.getSourceManager();
+
+  std::string OutputFile = CI.getFrontendOpts().OutputFile;
+  if (OutputFile.empty()) {
+    OutputFile = CI.getFrontendOpts().Inputs[0].getFile();
+    OutputFile += ".o";
+  }
+
+  const FileEntry *RootFile = nullptr;
+  Module *UnitMod = nullptr;
+  bool IsModuleGeneration = CI.getLangOpts().isCompilingModule();
+  if (!IsModuleGeneration &&
+      CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH)
+    RootFile = SM.getFileEntryForID(SM.getMainFileID());
+
+  if (IsModuleGeneration) {
+    HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
+    UnitMod = HS.lookupModule(CI.getLangOpts().CurrentModule,
+                              /*AllowSearch=*/false);
+  }
+  return getUnitDetails(CI, std::move(OutputFile), RootFile, UnitMod,
+                        SysrootPath);
+}
+
+UnitDetails UnitDataConsumerActionImpl::getUnitDetails(
+    serialization::ModuleFile &Mod, const CompilerInstance &CI,
+    StringRef SysrootPath) {
+  HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
+  Module *UnitMod = HS.lookupModule(Mod.ModuleName, /*AllowSearch=*/false);
+  assert(UnitMod && "only loaded modules should be indexed");
+
+  return getUnitDetails(CI, /*OutputFile=*/Mod.FileName, /*RootFile=*/nullptr,
+                        UnitMod, SysrootPath);
+}
+
+UnitDetails UnitDataConsumerActionImpl::getUnitDetails(
+    const CompilerInstance &CI, std::string OutputFile,
+    const FileEntry *RootFile, Module *UnitMod, StringRef SysrootPath) {
+  std::string ModuleName =
+      UnitMod ? UnitMod->getFullModuleName() : std::string();
+  bool IsSystemUnit = UnitMod ? UnitMod->IsSystem : false;
+  bool IsModuleUnit = UnitMod != nullptr;
+  bool IsDebugCompilation = CI.getCodeGenOpts().OptimizationLevel == 0;
+
+  // Ignore sysroot path if it points to root, otherwise every header will be
+  // treated as system one.
+  if (llvm::sys::path::root_path(SysrootPath) == SysrootPath)
+    SysrootPath = "";
+
+  return {UnitMod, ModuleName, OutputFile, RootFile, SysrootPath, IsSystemUnit,
+      IsModuleUnit, IsDebugCompilation};
+}
+
+std::unique_ptr<FrontendAction>
+index::createUnitIndexingAction(
+    const UnitIndexingOptions &IndexOpts,
+    IndexUnitDataConsumerFactory ConsumerFactory,
+    std::unique_ptr<FrontendAction> WrappedAction) {
+  auto Base = llvm::make_unique<UnitDataConsumerActionImpl>(
+      std::move(IndexOpts), ConsumerFactory);
+  if (WrappedAction)
+    return llvm::make_unique<WrappingIndexAction>(std::move(WrappedAction),
+                                                  std::move(Base));
+  return llvm::make_unique<IndexAction>(std::move(Base));
+};
+
+std::unique_ptr<FrontendAction>
+index::createIndexDataRecordingAction(
+    const RecordingOptions &RecordOpts,
+    std::unique_ptr<FrontendAction> WrappedAction) {
+
+  class IndexUnitDataRecorder : public IndexUnitDataConsumer {
+  public:
+    IndexUnitDataRecorder() {}
+    // TODO: persist index data to the file system
+  };
+
+  auto ConsumerFactory =
+      [&](const CompilerInstance &CI, UnitDetails UnitInfo) ->
+          std::unique_ptr<IndexUnitDataConsumer> {
+        return llvm::make_unique<IndexUnitDataRecorder>();
+      };
+  auto Base = llvm::make_unique<UnitDataConsumerActionImpl>(
+      std::move(RecordOpts), ConsumerFactory);
+  if (WrappedAction)
+    return llvm::make_unique<WrappingIndexAction>(std::move(WrappedAction),
+                                                  std::move(Base));
+  return llvm::make_unique<IndexAction>(std::move(Base));
+};
+
+RecordingOptions
+index::getRecordingOptionsFromFrontendOptions(const FrontendOptions &FEOpts) {
+  RecordingOptions RecordOpts;
+  RecordOpts.DataDirPath = FEOpts.IndexStorePath;
+  if (FEOpts.IndexIgnoreSystemSymbols) {
+    RecordOpts.SystemSymbolFilter =
+        index::IndexingOptions::SystemSymbolFilterKind::None;
+  }
+  RecordOpts.RecordSymbolCodeGenName = FEOpts.IndexRecordCodegenName;
+  return RecordOpts;
+}
Index: lib/Index/FileIndexData.h
===================================================================
--- /dev/null
+++ lib/Index/FileIndexData.h
@@ -0,0 +1,56 @@
+//===--- FileIndexRecord.h - Index data per file --------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_LIB_INDEX_FILEINDEXRECORD_H
+#define LLVM_CLANG_LIB_INDEX_FILEINDEXRECORD_H
+
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Index/IndexSymbol.h"
+#include "clang/Index/DeclOccurrence.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallVector.h"
+#include <vector>
+
+namespace clang {
+class IdentifierInfo;
+
+namespace index {
+
+/// Stores the declaration occurrences seen in a particular source or header
+/// file of a translation unit
+class FileIndexData {
+private:
+  FileID FID;
+  bool IsSystem;
+  std::vector<DeclOccurrence> Decls;
+
+public:
+  FileIndexData(FileID FID, bool IsSystem) : FID(FID), IsSystem(IsSystem) {}
+
+  std::vector<DeclOccurrence> getDeclOccurrencesSortedByOffset() const;
+
+  FileID getFileID() const { return FID; }
+  bool isSystem() const { return IsSystem; }
+
+  /// Adds an occurrence of the canonical declaration \c D at the supplied
+  /// \c Offset
+  ///
+  /// \param Roles the roles the occurrence fulfills in this position.
+  /// \param Offset the offset in the file of this occurrence.
+  /// \param D the canonical declaration this is an occurrence of.
+  /// \param Relations the set of symbols related to this occurrence.
+  void addDeclOccurence(SymbolRoleSet Roles, unsigned Offset, const Decl *D,
+                        ArrayRef<SymbolRelation> Relations);
+  void print(llvm::raw_ostream &OS) const;
+};
+
+} // end namespace index
+} // end namespace clang
+
+#endif
Index: lib/Index/FileIndexData.cpp
===================================================================
--- /dev/null
+++ lib/Index/FileIndexData.cpp
@@ -0,0 +1,52 @@
+//===--- FileIndexRecord.cpp - Index data per file ------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "FileIndexData.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/DeclTemplate.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Path.h"
+
+using namespace clang;
+using namespace clang::index;
+
+void FileIndexData::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
+                                     const Decl *D,
+                                     ArrayRef<SymbolRelation> Relations) {
+  assert(D->isCanonicalDecl() &&
+         "Occurrences should be associated with their canonical decl");
+
+  Decls.emplace_back(Roles, Offset, D, Relations);
+}
+
+std::vector<DeclOccurrence>
+FileIndexData::getDeclOccurrencesSortedByOffset() const {
+    std::vector<DeclOccurrence> Sorted(Decls);
+    std::sort(Sorted.begin(), Sorted.end());
+    return Sorted;
+}
+
+void FileIndexData::print(llvm::raw_ostream &OS) const {
+  OS << "DECLS BEGIN ---\n";
+  for (auto &DclInfo : Decls) {
+    auto D = DclInfo.Dcl;
+    SourceManager &SM = D->getASTContext().getSourceManager();
+    SourceLocation Loc = SM.getFileLoc(D->getLocation());
+    PresumedLoc PLoc = SM.getPresumedLoc(Loc);
+    OS << llvm::sys::path::filename(PLoc.getFilename()) << ':' << PLoc.getLine()
+       << ':' << PLoc.getColumn();
+
+    if (auto ND = dyn_cast<NamedDecl>(D)) {
+      OS << ' ' << ND->getNameAsString();
+    }
+
+    OS << '\n';
+  }
+  OS << "DECLS END ---\n";
+}
Index: lib/Index/CMakeLists.txt
===================================================================
--- lib/Index/CMakeLists.txt
+++ lib/Index/CMakeLists.txt
@@ -6,6 +6,7 @@
 add_clang_library(clangIndex
   CodegenNameGenerator.cpp
   CommentToXML.cpp
+  FileIndexData.cpp
   IndexBody.cpp
   IndexDecl.cpp
   IndexingAction.cpp
@@ -23,6 +24,7 @@
   clangBasic
   clangFormat
   clangFrontend
+  clangLex
   clangRewrite
   clangSerialization
   clangToolingCore
Index: lib/FrontendTool/ExecuteCompilerInvocation.cpp
===================================================================
--- lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -23,6 +23,7 @@
 #include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/Frontend/FrontendPluginRegistry.h"
 #include "clang/Frontend/Utils.h"
+#include "clang/Index/IndexingAction.h"
 #include "clang/Rewrite/Frontend/FrontendActions.h"
 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
 #include "llvm/Option/OptTable.h"
@@ -164,6 +165,22 @@
   }
 #endif
 
+  if (!FEOpts.IndexStorePath.empty()) {
+    auto WrapWithIndexRecordAction =
+        [&](const FrontendOptions &opts,
+            std::unique_ptr<FrontendAction> WrappedAction) {
+          auto RecordOpts =
+              index::getRecordingOptionsFromFrontendOptions(FEOpts);
+          return index::createIndexDataRecordingAction(
+              RecordOpts, std::move(WrappedAction));
+    };
+
+    // Wrap the main action as well as any GenerateModuleActions created while
+    // loading modules
+    Act = WrapWithIndexRecordAction(FEOpts, std::move(Act));
+    CI.setGenModuleActionWrapper(WrapWithIndexRecordAction);
+  }
+
   // If there are any AST files to merge, create a frontend action
   // adaptor to perform the merge.
   if (!FEOpts.ASTMergeFiles.empty())
Index: lib/FrontendTool/CMakeLists.txt
===================================================================
--- lib/FrontendTool/CMakeLists.txt
+++ lib/FrontendTool/CMakeLists.txt
@@ -8,6 +8,7 @@
   clangCodeGen
   clangDriver
   clangFrontend
+  clangIndex
   clangRewriteFrontend
   )
 
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -1438,6 +1438,10 @@
       << "ARC migration" << "ObjC migration";
   }
 
+  Opts.IndexStorePath = Args.getLastArgValue(OPT_index_store_path);
+  Opts.IndexIgnoreSystemSymbols = Args.hasArg(OPT_index_ignore_system_symbols);
+  Opts.IndexRecordCodegenName = Args.hasArg(OPT_index_record_codegen_name);
+
   InputKind DashX(InputKind::Unknown);
   if (const Arg *A = Args.getLastArg(OPT_x)) {
     StringRef XValue = A->getValue();
Index: lib/Frontend/CompilerInstance.cpp
===================================================================
--- lib/Frontend/CompilerInstance.cpp
+++ lib/Frontend/CompilerInstance.cpp
@@ -28,6 +28,7 @@
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Frontend/Utils.h"
 #include "clang/Frontend/VerifyDiagnosticConsumer.h"
+#include "clang/Index/IndexingAction.h"
 #include "clang/Lex/HeaderSearch.h"
 #include "clang/Lex/PTHManager.h"
 #include "clang/Lex/Preprocessor.h"
@@ -1148,6 +1149,10 @@
   SourceMgr.pushModuleBuildStack(ModuleName,
     FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
 
+  // Pass along the GenModuleActionWrapper callback
+  auto WrapGenModuleAction = ImportingInstance.getGenModuleActionWrapper();
+  Instance.setGenModuleActionWrapper(WrapGenModuleAction);
+
   // If we're collecting module dependencies, we need to share a collector
   // between all of the module CompilerInstances. Other than that, we don't
   // want to produce any dependency output from the module build.
@@ -1166,8 +1171,11 @@
   llvm::CrashRecoveryContext CRC;
   CRC.RunSafelyOnThread(
       [&]() {
-        GenerateModuleFromModuleMapAction Action;
-        Instance.ExecuteAction(Action);
+        std::unique_ptr<FrontendAction> Action(
+            new GenerateModuleFromModuleMapAction);
+        if (WrapGenModuleAction)
+          Action = WrapGenModuleAction(FrontendOpts, std::move(Action));
+        Instance.ExecuteAction(*Action);
       },
       ThreadStackSize);
 
Index: lib/Driver/ToolChains/Darwin.cpp
===================================================================
--- lib/Driver/ToolChains/Darwin.cpp
+++ lib/Driver/ToolChains/Darwin.cpp
@@ -436,6 +436,10 @@
   // more information.
   ArgStringList CmdArgs;
 
+  Args.ClaimAllArgs(options::OPT_index_store_path);
+  Args.ClaimAllArgs(options::OPT_index_ignore_system_symbols);
+  Args.ClaimAllArgs(options::OPT_index_record_codegen_name);
+
   /// Hack(tm) to ignore linking errors when we are doing ARC migration.
   if (Args.hasArg(options::OPT_ccc_arcmt_check,
                   options::OPT_ccc_arcmt_migrate)) {
Index: lib/Driver/ToolChains/Clang.cpp
===================================================================
--- lib/Driver/ToolChains/Clang.cpp
+++ lib/Driver/ToolChains/Clang.cpp
@@ -3582,6 +3582,18 @@
 
   RenderARCMigrateToolOptions(D, Args, CmdArgs);
 
+  if (Args.hasArg(options::OPT_index_store_path)) {
+    Args.AddLastArg(CmdArgs, options::OPT_index_store_path);
+    Args.AddLastArg(CmdArgs, options::OPT_index_ignore_system_symbols);
+    Args.AddLastArg(CmdArgs, options::OPT_index_record_codegen_name);
+
+    // If '-o' is passed along with '-fsyntax-only' pass it along the cc1
+    // invocation so that the index action knows what the out file is.
+    if (isa<CompileJobAction>(JA) && JA.getType() == types::TY_Nothing) {
+      Args.AddLastArg(CmdArgs, options::OPT_o);
+    }
+  }
+
   // Add preprocessing options like -I, -D, etc. if we are using the
   // preprocessor.
   //
Index: lib/Driver/Job.cpp
===================================================================
--- lib/Driver/Job.cpp
+++ lib/Driver/Job.cpp
@@ -68,6 +68,8 @@
     .Default(false);
   if (IsInclude)
     return HaveCrashVFS ? false : true;
+  if (StringRef(Flag).startswith("-index-store-path"))
+    return true;
 
   // The remaining flags are treated as a single argument.
 
@@ -206,6 +208,21 @@
   IncFlags.push_back(std::move(NewInc));
 }
 
+/// The leftover modules from the crash are stored in
+///  <name>.cache/vfs/modules
+/// Provide a clean/empty directory named \c DirName adjacent to it for the
+/// future generated module cache and associated data, so the originals are left
+/// untouched for pcm inspection:
+///  <name>.cache/vfs/<DirName>
+static llvm::SmallString<128> buildDirAdjacentToModCache(
+    StringRef DirName, CrashReportInfo *CrashInfo) {
+  llvm::SmallString<128> RelModCacheDir = llvm::sys::path::parent_path(
+  llvm::sys::path::parent_path(CrashInfo->VFSPath));
+  llvm::sys::path::append(RelModCacheDir, DirName);
+
+  return RelModCacheDir;
+}
+
 void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
                     CrashReportInfo *CrashInfo) const {
   // Always quote the exe.
@@ -220,6 +237,7 @@
   }
 
   bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
+  bool HaveIndexStorePath = CrashInfo && !CrashInfo->IndexStorePath.empty();
   for (size_t i = 0, e = Args.size(); i < e; ++i) {
     const char *const Arg = Args[i];
 
@@ -267,22 +285,34 @@
     OS << ' ';
     printArg(OS, CrashInfo->VFSPath.str(), Quote);
 
-    // The leftover modules from the crash are stored in
-    //  <name>.cache/vfs/modules
-    // Leave it untouched for pcm inspection and provide a clean/empty dir
-    // path to contain the future generated module cache:
-    //  <name>.cache/vfs/repro-modules
-    SmallString<128> RelModCacheDir = llvm::sys::path::parent_path(
-        llvm::sys::path::parent_path(CrashInfo->VFSPath));
-    llvm::sys::path::append(RelModCacheDir, "repro-modules");
+    // Provide an empty dir path for the future generated module cache to
+    // leave the leftover modules from the crash untouched for pcm inspection
+    SmallString<128> RelModCacheDir =
+        buildDirAdjacentToModCache("repro-modules", CrashInfo);
 
     std::string ModCachePath = "-fmodules-cache-path=";
     ModCachePath.append(RelModCacheDir.c_str());
 
     OS << ' ';
     printArg(OS, ModCachePath, Quote);
   }
 
+  if (CrashInfo && HaveIndexStorePath) {
+    SmallString<128> IndexStoreDir;
+
+    if (HaveCrashVFS) {
+      // Provide a new index store, leaving the old one from the crash untouched
+      IndexStoreDir = buildDirAdjacentToModCache("index-store", CrashInfo);
+    } else {
+      IndexStoreDir = "index-store";
+    }
+
+    OS << ' ';
+    printArg(OS, "-index-store-path", Quote);
+    OS << ' ';
+    printArg(OS, IndexStoreDir.c_str(), Quote);
+  }
+
   if (ResponseFile != nullptr) {
     OS << "\n Arguments passed via response file:\n";
     writeResponseFile(OS);
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -993,7 +993,9 @@
   }
 
   // Assume associated files are based off of the first temporary file.
-  CrashReportInfo CrashInfo(TempFiles[0], VFS);
+  CrashReportInfo CrashInfo(
+      TempFiles[0], VFS,
+      C.getArgs().getLastArgValue(options::OPT_index_store_path));
 
   std::string Script = CrashInfo.Filename.rsplit('.').first.str() + ".sh";
   std::error_code EC;
Index: lib/Basic/DiagnosticIDs.cpp
===================================================================
--- lib/Basic/DiagnosticIDs.cpp
+++ lib/Basic/DiagnosticIDs.cpp
@@ -89,6 +89,7 @@
 VALIDATE_DIAG_SIZE(SEMA)
 VALIDATE_DIAG_SIZE(ANALYSIS)
 VALIDATE_DIAG_SIZE(REFACTORING)
+VALIDATE_DIAG_SIZE(INDEX)
 #undef VALIDATE_DIAG_SIZE
 #undef STRINGIFY_NAME
 
@@ -114,6 +115,7 @@
 #include "clang/Basic/DiagnosticSemaKinds.inc"
 #include "clang/Basic/DiagnosticAnalysisKinds.inc"
 #include "clang/Basic/DiagnosticRefactoringKinds.inc"
+#include "clang/Basic/DiagnosticIndexKinds.inc"
 #undef DIAG
 };
 
@@ -153,6 +155,7 @@
 CATEGORY(SEMA, CROSSTU)
 CATEGORY(ANALYSIS, SEMA)
 CATEGORY(REFACTORING, ANALYSIS)
+CATEGORY(INDEX, REFACTORING)
 #undef CATEGORY
 
   // Avoid out of bounds reads.
Index: include/clang/module.modulemap
===================================================================
--- include/clang/module.modulemap
+++ include/clang/module.modulemap
@@ -67,6 +67,7 @@
   module Comment { header "AST/CommentDiagnostic.h" export * }
   module Driver { header "Driver/DriverDiagnostic.h" export * }
   module Frontend { header "Frontend/FrontendDiagnostic.h" export * }
+  module Index { header "Index/IndexDiagnostic.h" export * }
   module Lex { header "Lex/LexDiagnostic.h" export * }
   module Parse { header "Parse/ParseDiagnostic.h" export * }
   module Sema { header "Sema/SemaDiagnostic.h" export * }
Index: include/clang/Index/IndexingAction.h
===================================================================
--- include/clang/Index/IndexingAction.h
+++ include/clang/Index/IndexingAction.h
@@ -13,33 +13,70 @@
 #include "clang/Basic/LLVM.h"
 #include "llvm/ADT/ArrayRef.h"
 #include <memory>
+#include <string>
 
 namespace clang {
   class ASTContext;
   class ASTReader;
   class ASTUnit;
+  class CompilerInstance;
   class Decl;
+  class FileEntry;
   class FrontendAction;
+  class FrontendOptions;
+  class Module;
 
 namespace serialization {
   class ModuleFile;
 }
 
 namespace index {
   class IndexDataConsumer;
+  class IndexUnitDataConsumer;
 
 struct IndexingOptions {
   enum class SystemSymbolFilterKind {
     None,
     DeclarationsOnly,
     All,
   };
 
-  SystemSymbolFilterKind SystemSymbolFilter
-    = SystemSymbolFilterKind::DeclarationsOnly;
+  SystemSymbolFilterKind SystemSymbolFilter =
+      SystemSymbolFilterKind::DeclarationsOnly;
   bool IndexFunctionLocals = false;
 };
 
+struct UnitIndexingOptions : IndexingOptions {
+  enum class FileIncludeFilterKind {
+    None,
+    UserOnly, // only record includes inside non-system files.
+    All,
+  };
+
+  bool IncludeSystemDependencies = true;
+  FileIncludeFilterKind FileIncludeFilter = FileIncludeFilterKind::UserOnly;
+};
+
+struct RecordingOptions : UnitIndexingOptions {
+  std::string DataDirPath;
+  bool RecordSymbolCodeGenName = false;
+};
+
+/// Information on the translation unit
+struct UnitDetails {
+  Module *UnitModule;
+  std::string ModuleName;
+  std::string OutputFile;
+  const FileEntry *RootFile;
+  StringRef SysrootPath;
+  bool IsSystemUnit;
+  bool IsModuleUnit;
+  bool IsDebugCompilation;
+};
+
+/// Creates a frontend action that provides decl occurrence information from the
+/// AST to the given \c IndexDataConsumer.
+///
 /// \param WrappedAction another frontend action to wrap over or null.
 std::unique_ptr<FrontendAction>
 createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
@@ -58,6 +95,38 @@
                      std::shared_ptr<IndexDataConsumer> DataConsumer,
                      IndexingOptions Opts);
 
+/// Factory function type for producing IndexUnitDataConsumers for a given
+/// translation unit
+typedef std::function<std::unique_ptr<IndexUnitDataConsumer>(
+    const CompilerInstance &CI, UnitDetails UnitInfo)>
+    IndexUnitDataConsumerFactory;
+
+/// \brief Creates a frontend action that provides dependency, file inclusion
+/// and decl ocurrence information for the translation unit, and optionally its
+/// module dependencies.
+///
+/// Decl occurrence information is provided per-file, sorted by offset.
+///
+/// \param ConsumerFactory provides an \c IndexUnitDataConsumer to use for a
+/// translation unit.
+/// \param WrappedAction another frontend action to wrap over or null.
+std::unique_ptr<FrontendAction>
+createUnitIndexingAction(const UnitIndexingOptions &IndexOpts,
+                         IndexUnitDataConsumerFactory ConsumerFactory,
+                         std::unique_ptr<FrontendAction> WrappedAction);
+
+/// Creates a frontend action that collects and persists dependency, file
+/// inclusion and decl occurrence information for the translation unit and its
+/// module dependencies to the file system.
+///
+/// \param WrappedAction another frontend action to wrap over or null.
+std::unique_ptr<FrontendAction>
+createIndexDataRecordingAction(const RecordingOptions &RecordOpts,
+                               std::unique_ptr<FrontendAction> WrappedAction);
+
+RecordingOptions
+getRecordingOptionsFromFrontendOptions(const FrontendOptions &FEOpts);
+
 } // namespace index
 } // namespace clang
 
Index: include/clang/Index/IndexUnitDataConsumer.h
===================================================================
--- /dev/null
+++ include/clang/Index/IndexUnitDataConsumer.h
@@ -0,0 +1,73 @@
+//===--- IndexUnitDataConsumer.h - Abstract index unit data consumer ---------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_INDEX_INDEXUNITDATACONSUMER_H
+#define LLVM_CLANG_INDEX_INDEXUNITDATACONSUMER_H
+
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Index/IndexSymbol.h"
+#include "clang/Index/DeclOccurrence.h"
+#include "llvm/ADT/ArrayRef.h"
+
+namespace clang {
+namespace serialization {
+  class ModuleFile;
+}
+
+namespace index {
+
+/// Consumer for the index data associated with a translation unit.
+class IndexUnitDataConsumer {
+public:
+
+  virtual ~IndexUnitDataConsumer() = default;
+
+  /// Called for each file dependency of the translation unit.
+  virtual void handleFileDependency(const FileEntry *FE, bool IsSystem) {}
+
+  /// Called for each file include in the translation unit.
+  virtual void handleInclude(const FileEntry *Source, unsigned Line,
+                             const FileEntry *Target) {}
+
+  /// Called for each each module imported by the translation unit.
+  virtual void handleModuleImport(const serialization::ModuleFile &Mod,
+                                  bool IsSystem) {}
+
+  /// Determines whether to collect the index data associated with the given
+  /// dependency of this translation unit or not.
+  ///
+  /// \param OutFilePath the output file path of the dependency.
+  /// \returns true to collect index data for \c Mod.
+  virtual bool shouldIndexModuleDependency(
+      const serialization::ModuleFile &Mod) {
+    return false;
+  }
+
+  /// Called with the decl occurrences in each file and AST file dependency,
+  /// sorted by offset.
+  ///
+  /// \returns true to cancel consuming data for this translation unit. Finish
+  /// will not be called.
+  virtual bool handleFileOccurrences(
+      FileID FID,
+      ArrayRef<DeclOccurrence> OccurrencesSortedByOffset, bool IsSystem) {
+    return false;
+  }
+
+  /// Called when there is no more data to handle.
+  virtual void finish() { }
+
+private:
+  virtual void _anchor();
+};
+
+} // namespace index
+} // namespace clang
+
+#endif
Index: include/clang/Index/IndexDiagnostic.h
===================================================================
--- /dev/null
+++ include/clang/Index/IndexDiagnostic.h
@@ -0,0 +1,29 @@
+//===--- IndexDiagnostic.h - ------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_INDEX_INDEXDIAGNOSTIC_H
+#define LLVM_CLANG_INDEX_INDEXDIAGNOSTIC_H
+
+#include "clang/Basic/Diagnostic.h"
+
+namespace clang {
+namespace diag {
+enum {
+#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
+             SHOWINSYSHEADER, CATEGORY)                                        \
+  ENUM,
+#define INDEXSTART
+#include "clang/Basic/DiagnosticIndexKinds.inc"
+#undef DIAG
+  NUM_BUILTIN_INDEX_DIAGNOSTICS
+};
+} // end namespace diag
+} // end namespace clang
+
+#endif // LLVM_CLANG_INDEX_INDEXDIAGNOSTIC_H
Index: include/clang/Index/IndexDataConsumer.h
===================================================================
--- include/clang/Index/IndexDataConsumer.h
+++ include/clang/Index/IndexDataConsumer.h
@@ -40,17 +40,18 @@
   virtual bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
                                    ArrayRef<SymbolRelation> Relations,
                                    FileID FID, unsigned Offset,
-                                   ASTNodeInfo ASTNode);
+                                   bool IsInSystemFile, ASTNodeInfo ASTNode);
 
   /// \returns true to continue indexing, or false to abort.
   virtual bool handleMacroOccurence(const IdentifierInfo *Name,
                                     const MacroInfo *MI, SymbolRoleSet Roles,
-                                    FileID FID, unsigned Offset);
+                                    FileID FID, unsigned Offset,
+                                    bool IsInSystemFile);
 
   /// \returns true to continue indexing, or false to abort.
   virtual bool handleModuleOccurence(const ImportDecl *ImportD,
-                                     SymbolRoleSet Roles,
-                                     FileID FID, unsigned Offset);
+                                     SymbolRoleSet Roles, FileID FID,
+                                     unsigned Offset, bool IsInSystemFile);
 
   virtual void finish() {}
 
Index: include/clang/Index/DeclOccurrence.h
===================================================================
--- /dev/null
+++ include/clang/Index/DeclOccurrence.h
@@ -0,0 +1,43 @@
+//===--- DeclOccurrence.h - An occurrence of a decl within a file ---------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_INDEX_DECLOCCURRENCE_H
+#define LLVM_CLANG_INDEX_DECLOCCURRENCE_H
+
+#include "clang/Basic/LLVM.h"
+#include "clang/Index/IndexSymbol.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallVector.h"
+
+namespace clang {
+  class Decl;
+
+namespace index {
+
+struct DeclOccurrence {
+    SymbolRoleSet Roles;
+    unsigned Offset;
+    const Decl *Dcl;
+    SmallVector<SymbolRelation, 3> Relations;
+
+    DeclOccurrence(SymbolRoleSet R, unsigned Offset, const Decl *D,
+                   ArrayRef<SymbolRelation> Relations)
+        : Roles(R), Offset(Offset), Dcl(D),
+          Relations(Relations.begin(), Relations.end()) {}
+
+    friend bool operator<(const DeclOccurrence &LHS,
+                          const DeclOccurrence &RHS) {
+      return LHS.Offset < RHS.Offset;
+    }
+  };
+
+} // namespace index
+} // namespace clang
+
+#endif
Index: include/clang/Frontend/FrontendOptions.h
===================================================================
--- include/clang/Frontend/FrontendOptions.h
+++ include/clang/Frontend/FrontendOptions.h
@@ -259,6 +259,13 @@
   std::string MTMigrateDir;
   std::string ARCMTMigrateReportOut;
 
+  /// The path to write index data to
+  std::string IndexStorePath;
+  /// Whether to ignore system files when writing out index data
+  unsigned IndexIgnoreSystemSymbols : 1;
+  /// Whether to include the codegen name of symbols in the index data
+  unsigned IndexRecordCodegenName : 1;
+
   /// The input files and their types.
   std::vector<FrontendInputFile> Inputs;
 
@@ -336,8 +343,9 @@
     SkipFunctionBodies(false), UseGlobalModuleIndex(true),
     GenerateGlobalModuleIndex(true), ASTDumpDecls(false), ASTDumpLookups(false),
     BuildingImplicitModule(false), ModulesEmbedAllFiles(false),
-    IncludeTimestamps(true), ARCMTAction(ARCMT_None),
-    ObjCMTAction(ObjCMT_None), ProgramAction(frontend::ParseSyntaxOnly)
+    IncludeTimestamps(true), ARCMTAction(ARCMT_None), ObjCMTAction(ObjCMT_None),
+    IndexIgnoreSystemSymbols(false), IndexRecordCodegenName(false),
+    ProgramAction(frontend::ParseSyntaxOnly)
   {}
 
   /// getInputKindForExtension - Return the appropriate input kind for a file
Index: include/clang/Frontend/CompilerInstance.h
===================================================================
--- include/clang/Frontend/CompilerInstance.h
+++ include/clang/Frontend/CompilerInstance.h
@@ -183,6 +183,14 @@
   /// The list of active output files.
   std::list<OutputFile> OutputFiles;
 
+  typedef std::function<std::unique_ptr<FrontendAction>(
+      const FrontendOptions &opts, std::unique_ptr<FrontendAction> action)>
+      ActionWrapperTy;
+
+  /// \brief An optional callback function used to wrap any
+  /// GenerateModuleActions created and executed when loading modules.
+  ActionWrapperTy GenModuleActionWrapper;
+
   CompilerInstance(const CompilerInstance &) = delete;
   void operator=(const CompilerInstance &) = delete;
 public:
@@ -796,6 +804,14 @@
 
   bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override;
 
+  void setGenModuleActionWrapper(ActionWrapperTy Wrapper) {
+    GenModuleActionWrapper = Wrapper;
+  };
+
+  ActionWrapperTy getGenModuleActionWrapper() const {
+    return GenModuleActionWrapper;
+  }
+
   void addDependencyCollector(std::shared_ptr<DependencyCollector> Listener) {
     DependencyCollectors.push_back(std::move(Listener));
   }
Index: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -324,6 +324,13 @@
 def : Joined<["-"], "objcmt-white-list-dir-path=">, Flags<[CC1Option]>,
     Alias<objcmt_whitelist_dir_path>;
 
+def index_store_path : Separate<["-"], "index-store-path">, Flags<[CC1Option]>,
+  HelpText<"Enable indexing with the specified data store path">;
+def index_ignore_system_symbols : Flag<["-"], "index-ignore-system-symbols">, Flags<[CC1Option]>,
+  HelpText<"Ignore symbols from system headers">;
+def index_record_codegen_name : Flag<["-"], "index-record-codegen-name">, Flags<[CC1Option]>,
+  HelpText<"Record the codegen name for symbols">;
+
 // Make sure all other -ccc- options are rejected.
 def ccc_ : Joined<["-"], "ccc-">, Group<internal_Group>, Flags<[Unsupported]>;
 
Index: include/clang/Driver/Job.h
===================================================================
--- include/clang/Driver/Job.h
+++ include/clang/Driver/Job.h
@@ -34,9 +34,11 @@
 struct CrashReportInfo {
   StringRef Filename;
   StringRef VFSPath;
+  StringRef IndexStorePath;
 
-  CrashReportInfo(StringRef Filename, StringRef VFSPath)
-      : Filename(Filename), VFSPath(VFSPath) {}
+  CrashReportInfo(StringRef Filename, StringRef VFSPath,
+                  StringRef IndexStorePath)
+      : Filename(Filename), VFSPath(VFSPath), IndexStorePath(IndexStorePath) {}
 };
 
 /// Command - An executable path/name and argument vector to
Index: include/clang/Basic/DiagnosticIndexKinds.td
===================================================================
--- /dev/null
+++ include/clang/Basic/DiagnosticIndexKinds.td
@@ -0,0 +1,31 @@
+//==--- DiagnosticIndexKinds.td - indexing diagnostics --------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+//===----------------------------------------------------------------------===//
+// Indexing Diagnostics
+//===----------------------------------------------------------------------===//
+
+let Component = "Index" in {
+
+let CategoryName = "Index Store Issue" in {
+
+def err_index_store_dir_create_failed : Error<"failed creating the index store "
+  "directory: %0">;
+def err_index_store_file_status_failed : Error<"failed file status check: %0">;
+def err_index_store_record_write_failed : Error<"failed writing record '%0': "
+  "%1">;
+def err_index_store_unit_write_failed : Error<"failed writing unit data: %0">;
+
+def remark_index_producing_module_file_data : Remark<"producing index data for "
+  "module file '%0'">,
+  InGroup<IndexStore>;
+
+}
+
+} // end of Indexing diagnostics
Index: include/clang/Basic/DiagnosticIDs.h
===================================================================
--- include/clang/Basic/DiagnosticIDs.h
+++ include/clang/Basic/DiagnosticIDs.h
@@ -40,6 +40,7 @@
       DIAG_SIZE_SEMA          = 3500,
       DIAG_SIZE_ANALYSIS      =  100,
       DIAG_SIZE_REFACTORING   = 1000,
+      DIAG_SIZE_INDEX         =  100,
     };
     // Start position for diagnostics.
     enum {
@@ -55,7 +56,8 @@
       DIAG_START_SEMA          = DIAG_START_CROSSTU       + DIAG_SIZE_COMMENT,
       DIAG_START_ANALYSIS      = DIAG_START_SEMA          + DIAG_SIZE_SEMA,
       DIAG_START_REFACTORING   = DIAG_START_ANALYSIS      + DIAG_SIZE_ANALYSIS,
-      DIAG_UPPER_LIMIT         = DIAG_START_REFACTORING   + DIAG_SIZE_REFACTORING
+      DIAG_START_INDEX         = DIAG_START_REFACTORING   + DIAG_SIZE_REFACTORING,
+      DIAG_UPPER_LIMIT         = DIAG_START_INDEX         + DIAG_SIZE_INDEX,
     };
 
     class CustomDiagInfo;
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td
+++ include/clang/Basic/DiagnosticGroups.td
@@ -327,6 +327,7 @@
 def ModuleBuild : DiagGroup<"module-build">;
 def ModuleConflict : DiagGroup<"module-conflict">;
 def ModuleFileExtension : DiagGroup<"module-file-extension">;
+def IndexStore : DiagGroup<"index-store">;
 def NewlineEOF : DiagGroup<"newline-eof">;
 def Nullability : DiagGroup<"nullability">;
 def NullabilityDeclSpec : DiagGroup<"nullability-declspec">;
Index: include/clang/Basic/Diagnostic.td
===================================================================
--- include/clang/Basic/Diagnostic.td
+++ include/clang/Basic/Diagnostic.td
@@ -136,6 +136,7 @@
 include "DiagnosticCrossTUKinds.td"
 include "DiagnosticDriverKinds.td"
 include "DiagnosticFrontendKinds.td"
+include "DiagnosticIndexKinds.td"
 include "DiagnosticLexKinds.td"
 include "DiagnosticParseKinds.td"
 include "DiagnosticRefactoringKinds.td"
Index: include/clang/Basic/CMakeLists.txt
===================================================================
--- include/clang/Basic/CMakeLists.txt
+++ include/clang/Basic/CMakeLists.txt
@@ -12,6 +12,7 @@
 clang_diag_gen(CrossTU)
 clang_diag_gen(Driver)
 clang_diag_gen(Frontend)
+clang_diag_gen(Index)
 clang_diag_gen(Lex)
 clang_diag_gen(Parse)
 clang_diag_gen(Refactoring)
Index: include/clang/Basic/AllDiagnostics.h
===================================================================
--- include/clang/Basic/AllDiagnostics.h
+++ include/clang/Basic/AllDiagnostics.h
@@ -21,6 +21,7 @@
 #include "clang/CrossTU/CrossTUDiagnostic.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/Index/IndexDiagnostic.h"
 #include "clang/Lex/LexDiagnostic.h"
 #include "clang/Parse/ParseDiagnostic.h"
 #include "clang/Sema/SemaDiagnostic.h"
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to