2009-07-07 01:34:47 +04:00
|
|
|
//===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===//
|
2009-07-06 02:21:28 +04:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-07-07 01:34:20 +04:00
|
|
|
// ASTLocation is Decl or a Stmt and its immediate Decl parent.
|
2009-07-06 02:21:28 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-07 01:34:47 +04:00
|
|
|
#include "clang/Index/ASTLocation.h"
|
2009-07-06 02:21:28 +04:00
|
|
|
#include "clang/AST/Decl.h"
|
2009-07-18 04:33:46 +04:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-07-06 02:21:28 +04:00
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2009-07-21 04:05:38 +04:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2009-07-06 02:21:28 +04:00
|
|
|
using namespace clang;
|
2009-07-07 01:34:47 +04:00
|
|
|
using namespace idx;
|
2009-07-06 02:21:28 +04:00
|
|
|
|
2009-07-19 01:17:58 +04:00
|
|
|
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();
|
2009-07-21 04:05:38 +04:00
|
|
|
if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
|
|
|
|
return RE->getDecl();
|
|
|
|
|
2009-07-19 01:17:58 +04:00
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(E))
|
|
|
|
return getDeclFromExpr(CE->getCallee());
|
|
|
|
if (CastExpr *CE = dyn_cast<CastExpr>(E))
|
|
|
|
return getDeclFromExpr(CE->getSubExpr());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-19 01:17:58 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl *ASTLocation::getReferencedDecl() {
|
|
|
|
if (isInvalid())
|
|
|
|
return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-29 23:44:27 +04:00
|
|
|
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);
|
2009-07-30 03:40:48 +04:00
|
|
|
}
|
2009-09-29 23:44:27 +04:00
|
|
|
|
2009-07-06 02:21:28 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-07 01:35:20 +04:00
|
|
|
SourceRange ASTLocation::getSourceRange() const {
|
2009-07-19 01:17:43 +04:00
|
|
|
if (isInvalid())
|
|
|
|
return SourceRange();
|
2009-09-29 23:44:27 +04:00
|
|
|
|
|
|
|
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();
|
2009-07-07 01:35:20 +04:00
|
|
|
}
|
|
|
|
|
2009-07-30 03:39:35 +04:00
|
|
|
void ASTLocation::print(llvm::raw_ostream &OS) const {
|
2009-07-19 01:17:43 +04:00
|
|
|
if (isInvalid()) {
|
|
|
|
OS << "<< Invalid ASTLocation >>\n";
|
|
|
|
return;
|
|
|
|
}
|
2009-09-29 23:44:27 +04:00
|
|
|
|
|
|
|
ASTContext &Ctx = getParentDecl()->getASTContext();
|
|
|
|
|
|
|
|
switch (getKind()) {
|
|
|
|
case N_Decl:
|
|
|
|
OS << "[Decl: " << AsDecl()->getDeclKindName() << " ";
|
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl()))
|
|
|
|
OS << ND->getNameAsString();
|
|
|
|
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->getNameAsString();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case N_Type: {
|
|
|
|
QualType T = AsTypeLoc().getSourceType();
|
|
|
|
OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString();
|
|
|
|
}
|
2009-07-06 02:21:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
OS << "] <";
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-07 01:35:20 +04:00
|
|
|
SourceRange Range = getSourceRange();
|
2009-09-29 23:44:27 +04:00
|
|
|
SourceManager &SourceMgr = Ctx.getSourceManager();
|
2009-07-06 02:21:28 +04:00
|
|
|
Range.getBegin().print(OS, SourceMgr);
|
|
|
|
OS << ", ";
|
|
|
|
Range.getEnd().print(OS, SourceMgr);
|
|
|
|
OS << ">\n";
|
|
|
|
}
|