blob: 3beff3f31a8f96de5ff16cbb8e11c07c0142978d [file] [log] [blame]
Argyrios Kyrtzidisccbcb702009-07-06 21:34:47 +00001//===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===//
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +00002//
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//
Argyrios Kyrtzidis874012b2009-07-06 21:34:20 +000010// ASTLocation is Decl or a Stmt and its immediate Decl parent.
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000011//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidisccbcb702009-07-06 21:34:47 +000014#include "clang/Index/ASTLocation.h"
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000015#include "clang/AST/Decl.h"
16#include "clang/AST/Stmt.h"
17#include "clang/AST/Expr.h"
18using namespace clang;
Argyrios Kyrtzidisccbcb702009-07-06 21:34:47 +000019using namespace idx;
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000020
21static bool isContainedInStatement(Stmt *Node, Stmt *Parent) {
22 assert(Node && Parent && "Passed null Node or Parent");
23
24 if (Node == Parent)
25 return true;
26
27 for (Stmt::child_iterator
28 I = Parent->child_begin(), E = Parent->child_end(); I != E; ++I) {
29 if (isContainedInStatement(Node, *I))
30 return true;
31 }
32
33 return false;
34}
35
36static Decl *FindImmediateParent(Decl *D, Stmt *Node) {
37 assert(D && Node && "Passed null Decl or null Stmt");
38
39 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
40 Expr *Init = VD->getInit();
41 if (Init == 0)
42 return 0;
43 return isContainedInStatement(Node, Init) ? D : 0;
44 }
45
46 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
47 if (!FD->isThisDeclarationADefinition())
48 return 0;
49
50 for (DeclContext::decl_iterator
51 I = FD->decls_begin(), E = FD->decls_end(); I != E; ++I) {
52 Decl *Child = FindImmediateParent(*I, Node);
53 if (Child)
54 return Child;
55 }
56
57 assert(FD->getBody() && "If not definition we should have exited already");
58 return isContainedInStatement(Node, FD->getBody()) ? D : 0;
59 }
60
61 return 0;
62}
63
Zhongxing Xuadfc8d12009-07-17 06:58:08 +000064ASTLocation::ASTLocation(const Decl *d, const Stmt *stm)
65 : D(const_cast<Decl*>(d)), Stm(const_cast<Stmt*>(stm)) {
66 if (Stm) {
67 Decl *Parent = FindImmediateParent(D, Stm);
68 assert(Parent);
69 D = Parent;
70 }
71}
72
73
Argyrios Kyrtzidis874012b2009-07-06 21:34:20 +000074bool ASTLocation::isImmediateParent(Decl *D, Stmt *Node) {
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000075 assert(D && Node && "Passed null Decl or null Stmt");
76 return D == FindImmediateParent(D, Node);
77}
78
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +000079SourceRange ASTLocation::getSourceRange() const {
80 return isDecl() ? getDecl()->getSourceRange() : getStmt()->getSourceRange();
81}
82
Argyrios Kyrtzidis874012b2009-07-06 21:34:20 +000083void ASTLocation::print(llvm::raw_ostream &OS) {
84 assert(isValid() && "ASTLocation is not valid");
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000085
86 OS << "[Decl: " << getDecl()->getDeclKindName() << " ";
87 if (NamedDecl *ND = dyn_cast<NamedDecl>(getDecl()))
88 OS << ND->getNameAsString();
89
90 if (getStmt()) {
91 ASTContext &Ctx = getDecl()->getASTContext();
92 OS << " | Stmt: " << getStmt()->getStmtClassName() << " ";
93 getStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions()));
94 }
95
96 OS << "] <";
97
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +000098 SourceRange Range = getSourceRange();
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000099 SourceManager &SourceMgr = getDecl()->getASTContext().getSourceManager();
100 Range.getBegin().print(OS, SourceMgr);
101 OS << ", ";
102 Range.getEnd().print(OS, SourceMgr);
103 OS << ">\n";
104}