2009-07-07 05:34:47 +08:00
|
|
|
//===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===//
|
2009-07-06 06:21:28 +08: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 05:34:20 +08:00
|
|
|
// ASTLocation is Decl or a Stmt and its immediate Decl parent.
|
2009-07-06 06:21:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-07 05:34:47 +08:00
|
|
|
#include "clang/Index/ASTLocation.h"
|
2009-07-06 06:21:28 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2009-07-18 08:33:46 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-07-06 06:21:28 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2009-07-21 08:05:38 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2009-07-06 06:21:28 +08:00
|
|
|
using namespace clang;
|
2009-07-07 05:34:47 +08:00
|
|
|
using namespace idx;
|
2009-07-06 06:21:28 +08:00
|
|
|
|
2009-07-19 05:17:58 +08: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 08:05:38 +08:00
|
|
|
if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
|
|
|
|
return RE->getDecl();
|
|
|
|
|
2009-07-19 05:17:58 +08: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 23:08:12 +08:00
|
|
|
|
2009-07-19 05:17:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl *ASTLocation::getReferencedDecl() {
|
|
|
|
if (isInvalid())
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-30 03:44:27 +08: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 07:40:48 +08:00
|
|
|
}
|
2009-09-30 03:44:27 +08:00
|
|
|
|
2009-07-06 06:21:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-07 05:35:20 +08:00
|
|
|
SourceRange ASTLocation::getSourceRange() const {
|
2009-07-19 05:17:43 +08:00
|
|
|
if (isInvalid())
|
|
|
|
return SourceRange();
|
2009-09-30 03:44:27 +08: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:
|
2010-05-20 18:00:11 +08:00
|
|
|
return AsTypeLoc().getLocalSourceRange();
|
2009-09-30 03:44:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return SourceRange();
|
2009-07-07 05:35:20 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void ASTLocation::print(raw_ostream &OS) const {
|
2009-07-19 05:17:43 +08:00
|
|
|
if (isInvalid()) {
|
|
|
|
OS << "<< Invalid ASTLocation >>\n";
|
|
|
|
return;
|
|
|
|
}
|
2009-09-30 03:44:27 +08:00
|
|
|
|
|
|
|
ASTContext &Ctx = getParentDecl()->getASTContext();
|
|
|
|
|
|
|
|
switch (getKind()) {
|
|
|
|
case N_Decl:
|
|
|
|
OS << "[Decl: " << AsDecl()->getDeclKindName() << " ";
|
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(AsDecl()))
|
2010-04-17 17:33:03 +08:00
|
|
|
OS << ND;
|
2009-09-30 03:44:27 +08:00
|
|
|
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() << " ";
|
2010-04-17 17:33:03 +08:00
|
|
|
OS << AsNamedRef().ND;
|
2009-09-30 03:44:27 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case N_Type: {
|
2009-10-18 09:05:36 +08:00
|
|
|
QualType T = AsTypeLoc().getType();
|
2009-09-30 03:44:27 +08:00
|
|
|
OS << "[Type: " << T->getTypeClassName() << " " << T.getAsString();
|
|
|
|
}
|
2009-07-06 06:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OS << "] <";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-07 05:35:20 +08:00
|
|
|
SourceRange Range = getSourceRange();
|
2009-09-30 03:44:27 +08:00
|
|
|
SourceManager &SourceMgr = Ctx.getSourceManager();
|
2009-07-06 06:21:28 +08:00
|
|
|
Range.getBegin().print(OS, SourceMgr);
|
|
|
|
OS << ", ";
|
|
|
|
Range.getEnd().print(OS, SourceMgr);
|
|
|
|
OS << ">\n";
|
|
|
|
}
|