FileIndexRecord.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FileIndexRecord.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/AST/DeclTemplate.h"
  12. #include "llvm/ADT/SmallString.h"
  13. #include "llvm/Support/Path.h"
  14. using namespace clang;
  15. using namespace clang::index;
  16. void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
  17. const Decl *D,
  18. ArrayRef<SymbolRelation> Relations) {
  19. assert(D->isCanonicalDecl() &&
  20. "Occurrences should be associated with their canonical decl");
  21. auto IsNextOccurence = [&]() -> bool {
  22. if (Decls.empty())
  23. return true;
  24. auto &Last = Decls.back();
  25. return Last.Offset < Offset;
  26. };
  27. if (IsNextOccurence()) {
  28. Decls.emplace_back(Roles, Offset, D, Relations);
  29. return;
  30. }
  31. DeclOccurrence NewInfo(Roles, Offset, D, Relations);
  32. // We keep Decls in order as we need to access them in this order in all cases.
  33. auto It = llvm::upper_bound(Decls, NewInfo);
  34. Decls.insert(It, std::move(NewInfo));
  35. }
  36. void FileIndexRecord::print(llvm::raw_ostream &OS) const {
  37. OS << "DECLS BEGIN ---\n";
  38. for (auto &DclInfo : Decls) {
  39. const Decl *D = DclInfo.Dcl;
  40. SourceManager &SM = D->getASTContext().getSourceManager();
  41. SourceLocation Loc = SM.getFileLoc(D->getLocation());
  42. PresumedLoc PLoc = SM.getPresumedLoc(Loc);
  43. OS << llvm::sys::path::filename(PLoc.getFilename()) << ':' << PLoc.getLine()
  44. << ':' << PLoc.getColumn();
  45. if (auto ND = dyn_cast<NamedDecl>(D)) {
  46. OS << ' ' << ND->getNameAsString();
  47. }
  48. OS << '\n';
  49. }
  50. OS << "DECLS END ---\n";
  51. }