aboutsummaryrefslogtreecommitdiff
path: root/lib/Index/ASTLocation.cpp
blob: 091bc78c2669192f614127f83c565684a0d2e715 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  ASTLocation is Decl or a Stmt and its immediate Decl parent.
//
//===----------------------------------------------------------------------===//

#include "clang/Index/ASTLocation.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprObjC.h"
using namespace clang;
using namespace idx;

static Decl *getDeclFromExpr(Stmt *E) {
  if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
    return RefExpr->getDecl();
  if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
    return ME->getMemberDecl();
  if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
    return RE->getDecl();

  if (CallExpr *CE = dyn_cast<CallExpr>(E))
    return getDeclFromExpr(CE->getCallee());
  if (CastExpr *CE = dyn_cast<CastExpr>(E))
    return getDeclFromExpr(CE->getSubExpr());

  return 0;
}

Decl *ASTLocation::getReferencedDecl() {
  if (isInvalid())
    return 0;

  switch (getKind()) {
  default: assert(0 && "Invalid Kind");
  case N_Type:
    return 0;
  case N_Decl:
    return D;
  case N_NamedRef:
    return NDRef.ND;
  case N_Stmt:
    return getDeclFromExpr(Stm);
  }
  
  return 0;
}

SourceRange ASTLocation::getSourceRange() const {
  if (isInvalid())
    return SourceRange();

  switch (getKind()) {
  default: assert(0 && "Invalid Kind");
    return SourceRange();
  case N_Decl:
    return D->getSourceRange();
  case N_Stmt:
    return Stm->getSourceRange();
  case N_NamedRef:
    return SourceRange(AsNamedRef().Loc, AsNamedRef().Loc);
  case N_Type:
    return AsTypeLoc().getSourceRange();
  }
  
  return SourceRange();
}

void ASTLocation::print(llvm::raw_ostream &OS) const {
  if (isInvalid()) {
    OS << "<< Invalid ASTLocation >>\n";
    return;
  }
  
  ASTContext &Ctx = getParentDecl()->getASTContext();

  switch (getKind()) {
  case N_Decl:
    OS << "[Decl: " << AsDecl()->getDeclKindName() << " ";
    if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl()))
      OS << ND;
    break;

  case N_Stmt:
    OS << "[Stmt: " << AsStmt()->getStmtClassName() << " ";
    AsStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions()));
    break;
    
  case N_NamedRef:
    OS << "[NamedRef: " << AsNamedRef().ND->getDeclKindName() << " ";
    OS << AsNamedRef().ND;
    break;
    
  case N_Type: {
    QualType T = AsTypeLoc().getType();
    OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString();
  }
  }

  OS << "] <";

  SourceRange Range = getSourceRange();
  SourceManager &SourceMgr = Ctx.getSourceManager();
  Range.getBegin().print(OS, SourceMgr);
  OS << ", ";
  Range.getEnd().print(OS, SourceMgr);
  OS << ">\n";
}