blob: 41846055da0992285a82a34f7aea7f0fc9488fe8 [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
Zhongxing Xu7f66bd22009-07-17 07:36:20 +000036Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) {
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000037 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
Argyrios Kyrtzidis874012b2009-07-06 21:34:20 +000064bool ASTLocation::isImmediateParent(Decl *D, Stmt *Node) {
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000065 assert(D && Node && "Passed null Decl or null Stmt");
66 return D == FindImmediateParent(D, Node);
67}
68
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +000069SourceRange ASTLocation::getSourceRange() const {
70 return isDecl() ? getDecl()->getSourceRange() : getStmt()->getSourceRange();
71}
72
Argyrios Kyrtzidis874012b2009-07-06 21:34:20 +000073void ASTLocation::print(llvm::raw_ostream &OS) {
74 assert(isValid() && "ASTLocation is not valid");
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000075
76 OS << "[Decl: " << getDecl()->getDeclKindName() << " ";
77 if (NamedDecl *ND = dyn_cast<NamedDecl>(getDecl()))
78 OS << ND->getNameAsString();
79
80 if (getStmt()) {
81 ASTContext &Ctx = getDecl()->getASTContext();
82 OS << " | Stmt: " << getStmt()->getStmtClassName() << " ";
83 getStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions()));
84 }
85
86 OS << "] <";
87
Argyrios Kyrtzidis755c6b42009-07-06 21:35:20 +000088 SourceRange Range = getSourceRange();
Argyrios Kyrtzidisc0824632009-07-05 22:21:28 +000089 SourceManager &SourceMgr = getDecl()->getASTContext().getSourceManager();
90 Range.getBegin().print(OS, SourceMgr);
91 OS << ", ";
92 Range.getEnd().print(OS, SourceMgr);
93 OS << ">\n";
94}