r355035 - [clang][index-while-building] FileIndexRecord

Jan Korous via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 27 13:47:40 PST 2019


Author: jkorous
Date: Wed Feb 27 13:47:40 2019
New Revision: 355035

URL: http://llvm.org/viewvc/llvm-project?rev=355035&view=rev
Log:
[clang][index-while-building] FileIndexRecord

Basic data structures for index

Tests are missing from this patch - will be covered properly by tests for the whole feature.
I'm just trying to split into smaller patches to make it easier for reviewers.

Differential Revision: https://reviews.llvm.org/D58478

Added:
    cfe/trunk/include/clang/Index/DeclOccurrence.h
    cfe/trunk/lib/Index/FileIndexRecord.cpp
    cfe/trunk/lib/Index/FileIndexRecord.h
Modified:
    cfe/trunk/lib/Index/CMakeLists.txt

Added: cfe/trunk/include/clang/Index/DeclOccurrence.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/DeclOccurrence.h?rev=355035&view=auto
==============================================================================
--- cfe/trunk/include/clang/Index/DeclOccurrence.h (added)
+++ cfe/trunk/include/clang/Index/DeclOccurrence.h Wed Feb 27 13:47:40 2019
@@ -0,0 +1,42 @@
+//===--- 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

Modified: cfe/trunk/lib/Index/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/CMakeLists.txt?rev=355035&r1=355034&r2=355035&view=diff
==============================================================================
--- cfe/trunk/lib/Index/CMakeLists.txt (original)
+++ cfe/trunk/lib/Index/CMakeLists.txt Wed Feb 27 13:47:40 2019
@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
 add_clang_library(clangIndex
   CodegenNameGenerator.cpp
   CommentToXML.cpp
+  FileIndexRecord.cpp
   IndexBody.cpp
   IndexDecl.cpp
   IndexingAction.cpp

Added: cfe/trunk/lib/Index/FileIndexRecord.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/FileIndexRecord.cpp?rev=355035&view=auto
==============================================================================
--- cfe/trunk/lib/Index/FileIndexRecord.cpp (added)
+++ cfe/trunk/lib/Index/FileIndexRecord.cpp Wed Feb 27 13:47:40 2019
@@ -0,0 +1,59 @@
+//===--- 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 "FileIndexRecord.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 FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
+                                       const Decl *D,
+                                       ArrayRef<SymbolRelation> Relations) {
+  assert(D->isCanonicalDecl() &&
+         "Occurrences should be associated with their canonical decl");
+
+  auto IsNextOccurence = [&]() -> bool {
+    if (Decls.empty())
+      return true;
+    auto &Last = Decls.back();
+    return Last.Offset < Offset;
+  };
+
+  if (IsNextOccurence()) {
+    Decls.emplace_back(Roles, Offset, D, Relations);
+    return;
+  }
+
+  DeclOccurrence NewInfo(Roles, Offset, D, Relations);
+  auto It = std::upper_bound(Decls.begin(), Decls.end(), NewInfo);
+  Decls.insert(It, std::move(NewInfo));
+}
+
+void FileIndexRecord::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";
+}

Added: cfe/trunk/lib/Index/FileIndexRecord.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/FileIndexRecord.h?rev=355035&view=auto
==============================================================================
--- cfe/trunk/lib/Index/FileIndexRecord.h (added)
+++ cfe/trunk/lib/Index/FileIndexRecord.h Wed Feb 27 13:47:40 2019
@@ -0,0 +1,58 @@
+//===--- 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/DeclOccurrence.h"
+#include "clang/Index/IndexSymbol.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 FileIndexRecord {
+private:
+  FileID FID;
+  bool IsSystem;
+  std::vector<DeclOccurrence> Decls;
+
+public:
+  FileIndexRecord(FileID FID, bool IsSystem) : FID(FID), IsSystem(IsSystem) {}
+
+  ArrayRef<DeclOccurrence> getDeclOccurrencesSortedByOffset() const {
+    return Decls;
+  }
+
+  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




More information about the cfe-commits mailing list