ASTLocation.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- 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. //
  10. // ASTLocation is Decl or a Stmt and its immediate Decl parent.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Index/ASTLocation.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/AST/DeclObjC.h"
  16. #include "clang/AST/Stmt.h"
  17. #include "clang/AST/Expr.h"
  18. #include "clang/AST/ExprObjC.h"
  19. using namespace clang;
  20. using namespace idx;
  21. static Decl *getDeclFromExpr(Stmt *E) {
  22. if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
  23. return RefExpr->getDecl();
  24. if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
  25. return ME->getMemberDecl();
  26. if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
  27. return RE->getDecl();
  28. if (CallExpr *CE = dyn_cast<CallExpr>(E))
  29. return getDeclFromExpr(CE->getCallee());
  30. if (CastExpr *CE = dyn_cast<CastExpr>(E))
  31. return getDeclFromExpr(CE->getSubExpr());
  32. return 0;
  33. }
  34. Decl *ASTLocation::getReferencedDecl() {
  35. if (isInvalid())
  36. return 0;
  37. switch (getKind()) {
  38. case N_Type:
  39. return 0;
  40. case N_Decl:
  41. return D;
  42. case N_NamedRef:
  43. return NDRef.ND;
  44. case N_Stmt:
  45. return getDeclFromExpr(Stm);
  46. }
  47. llvm_unreachable("Invalid ASTLocation Kind!");
  48. }
  49. SourceRange ASTLocation::getSourceRange() const {
  50. if (isInvalid())
  51. return SourceRange();
  52. switch (getKind()) {
  53. case N_Decl:
  54. return D->getSourceRange();
  55. case N_Stmt:
  56. return Stm->getSourceRange();
  57. case N_NamedRef:
  58. return SourceRange(AsNamedRef().Loc, AsNamedRef().Loc);
  59. case N_Type:
  60. return AsTypeLoc().getLocalSourceRange();
  61. }
  62. llvm_unreachable("Invalid ASTLocation Kind!");
  63. }
  64. void ASTLocation::print(raw_ostream &OS) const {
  65. if (isInvalid()) {
  66. OS << "<< Invalid ASTLocation >>\n";
  67. return;
  68. }
  69. ASTContext &Ctx = getParentDecl()->getASTContext();
  70. switch (getKind()) {
  71. case N_Decl:
  72. OS << "[Decl: " << AsDecl()->getDeclKindName() << " ";
  73. if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl()))
  74. OS << *ND;
  75. break;
  76. case N_Stmt:
  77. OS << "[Stmt: " << AsStmt()->getStmtClassName() << " ";
  78. AsStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOpts()));
  79. break;
  80. case N_NamedRef:
  81. OS << "[NamedRef: " << AsNamedRef().ND->getDeclKindName() << " ";
  82. OS << *AsNamedRef().ND;
  83. break;
  84. case N_Type: {
  85. QualType T = AsTypeLoc().getType();
  86. OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString();
  87. }
  88. }
  89. OS << "] <";
  90. SourceRange Range = getSourceRange();
  91. SourceManager &SourceMgr = Ctx.getSourceManager();
  92. Range.getBegin().print(OS, SourceMgr);
  93. OS << ", ";
  94. Range.getEnd().print(OS, SourceMgr);
  95. OS << ">\n";
  96. }