FileIndexRecord.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. auto It = std::upper_bound(Decls.begin(), Decls.end(), NewInfo);
  33. Decls.insert(It, std::move(NewInfo));
  34. }
  35. void FileIndexRecord::print(llvm::raw_ostream &OS) const {
  36. OS << "DECLS BEGIN ---\n";
  37. for (auto &DclInfo : Decls) {
  38. const Decl *D = DclInfo.Dcl;
  39. SourceManager &SM = D->getASTContext().getSourceManager();
  40. SourceLocation Loc = SM.getFileLoc(D->getLocation());
  41. PresumedLoc PLoc = SM.getPresumedLoc(Loc);
  42. OS << llvm::sys::path::filename(PLoc.getFilename()) << ':' << PLoc.getLine()
  43. << ':' << PLoc.getColumn();
  44. if (auto ND = dyn_cast<NamedDecl>(D)) {
  45. OS << ' ' << ND->getNameAsString();
  46. }
  47. OS << '\n';
  48. }
  49. OS << "DECLS END ---\n";
  50. }