FileIndexRecord.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "FileIndexRecord.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/AST/DeclTemplate.h"
  11. #include "clang/Basic/SourceManager.h"
  12. #include "llvm/ADT/SmallString.h"
  13. #include "llvm/Support/Path.h"
  14. using namespace clang;
  15. using namespace clang::index;
  16. ArrayRef<DeclOccurrence>
  17. FileIndexRecord::getDeclOccurrencesSortedByOffset() const {
  18. if (!IsSorted) {
  19. llvm::stable_sort(Decls,
  20. [](const DeclOccurrence &A, const DeclOccurrence &B) {
  21. return A.Offset < B.Offset;
  22. });
  23. IsSorted = true;
  24. }
  25. return Decls;
  26. }
  27. void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
  28. const Decl *D,
  29. ArrayRef<SymbolRelation> Relations) {
  30. assert(D->isCanonicalDecl() &&
  31. "Occurrences should be associated with their canonical decl");
  32. IsSorted = false;
  33. Decls.emplace_back(Roles, Offset, D, Relations);
  34. }
  35. void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles, unsigned Offset,
  36. const IdentifierInfo *Name,
  37. const MacroInfo *MI) {
  38. IsSorted = false;
  39. Decls.emplace_back(Roles, Offset, Name, MI);
  40. }
  41. void FileIndexRecord::removeHeaderGuardMacros() {
  42. auto It =
  43. std::remove_if(Decls.begin(), Decls.end(), [](const DeclOccurrence &D) {
  44. if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
  45. return MI->isUsedForHeaderGuard();
  46. return false;
  47. });
  48. Decls.erase(It, Decls.end());
  49. }
  50. void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {
  51. OS << "DECLS BEGIN ---\n";
  52. for (auto &DclInfo : Decls) {
  53. if (const auto *D = DclInfo.DeclOrMacro.dyn_cast<const Decl *>()) {
  54. SourceLocation Loc = SM.getFileLoc(D->getLocation());
  55. PresumedLoc PLoc = SM.getPresumedLoc(Loc);
  56. OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
  57. << PLoc.getLine() << ':' << PLoc.getColumn();
  58. if (const auto *ND = dyn_cast<NamedDecl>(D)) {
  59. OS << ' ' << ND->getDeclName();
  60. }
  61. } else {
  62. const auto *MI = DclInfo.DeclOrMacro.get<const MacroInfo *>();
  63. SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());
  64. PresumedLoc PLoc = SM.getPresumedLoc(Loc);
  65. OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
  66. << PLoc.getLine() << ':' << PLoc.getColumn();
  67. OS << ' ' << DclInfo.MacroName->getName();
  68. }
  69. OS << '\n';
  70. }
  71. OS << "DECLS END ---\n";
  72. }