FileIndexRecord.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. llvm::erase_if(Decls, [](const DeclOccurrence &D) {
  43. if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
  44. return MI->isUsedForHeaderGuard();
  45. return false;
  46. });
  47. }
  48. void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {
  49. OS << "DECLS BEGIN ---\n";
  50. for (auto &DclInfo : Decls) {
  51. if (const auto *D = DclInfo.DeclOrMacro.dyn_cast<const Decl *>()) {
  52. SourceLocation Loc = SM.getFileLoc(D->getLocation());
  53. PresumedLoc PLoc = SM.getPresumedLoc(Loc);
  54. OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
  55. << PLoc.getLine() << ':' << PLoc.getColumn();
  56. if (const auto *ND = dyn_cast<NamedDecl>(D)) {
  57. OS << ' ' << ND->getDeclName();
  58. }
  59. } else {
  60. const auto *MI = DclInfo.DeclOrMacro.get<const MacroInfo *>();
  61. SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());
  62. PresumedLoc PLoc = SM.getPresumedLoc(Loc);
  63. OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
  64. << PLoc.getLine() << ':' << PLoc.getColumn();
  65. OS << ' ' << DclInfo.MacroName->getName();
  66. }
  67. OS << '\n';
  68. }
  69. OS << "DECLS END ---\n";
  70. }