To help answer my own question, is this to be used with the new Index library, or does it serve a different purpose?
On Jul 5, 2009, at 11:19 PM, Ted Kremenek wrote: > Hi Argiris, > > I was a little surprised to see this. What is the purpose of this > class? Just curious. I easily could have missed something in > previous emails, but the description from the commit message doesn't > tell me what problem ASTNode helps address. > > Ted > > On Jul 5, 2009, at 3:21 PM, Argiris Kirtzidis wrote: > >> Author: akirtzidis >> Date: Sun Jul 5 17:21:28 2009 >> New Revision: 74797 >> >> URL: http://llvm.org/viewvc/llvm-project?rev=74797&view=rev >> Log: >> Introduce ASTNode class into the AST library. >> >> ASTNode is an immutable pair of a Decl and Stmt. If Stmt is not >> null, Decl should be its immediate parent. >> >> Added: >> cfe/trunk/include/clang/AST/ASTNode.h >> cfe/trunk/lib/AST/ASTNode.cpp >> Modified: >> cfe/trunk/lib/AST/CMakeLists.txt >> >> Added: cfe/trunk/include/clang/AST/ASTNode.h >> URL: >> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTNode.h?rev=74797&view=auto >> >> = >> = >> = >> = >> = >> = >> = >> = >> = >> ===================================================================== >> --- cfe/trunk/include/clang/AST/ASTNode.h (added) >> +++ cfe/trunk/include/clang/AST/ASTNode.h Sun Jul 5 17:21:28 2009 >> @@ -0,0 +1,66 @@ >> +//===--- ASTNode.h - 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. >> +// >> +// >> = >> = >> = >> ----------------------------------------------------------------------= >> ==// >> +// >> +// ASTNode is Decl or a Stmt and its immediate Decl parent. >> +// >> +// >> = >> = >> = >> ----------------------------------------------------------------------= >> ==// >> + >> +#ifndef LLVM_CLANG_AST_ASTNODE_H >> +#define LLVM_CLANG_AST_ASTNODE_H >> + >> +#include <cassert> >> + >> +namespace llvm { >> + class raw_ostream; >> +} >> + >> +namespace clang { >> + class Decl; >> + class Stmt; >> + >> +/// \brief Represents a Decl or a Stmt and its immediate Decl >> parent. It's >> +/// immutable. >> +class ASTNode { >> + Decl *D; >> + Stmt *Stm; >> + >> +public: >> + ASTNode() : D(0), Stm(0) {} >> + >> + explicit ASTNode(const Decl *d, const Stmt *stm = 0) >> + : D(const_cast<Decl*>(d)), Stm(const_cast<Stmt*>(stm)) { >> + assert((Stm == 0 || isImmediateParent(D, Stm)) && >> + "The Decl is not the immediate parent of the Stmt."); >> + } >> + >> + const Decl *getDecl() const { return D; } >> + const Stmt *getStmt() const { return Stm; } >> + Decl *getDecl() { return D; } >> + Stmt *getStmt() { return Stm; } >> + >> + bool isValid() const { return D != 0; } >> + bool isInvalid() const { return !isValid(); } >> + bool hasStmt() const { return Stm != 0; } >> + >> + /// \brief Checks that D is the immediate Decl parent of Node. >> + static bool isImmediateParent(Decl *D, Stmt *Node); >> + >> + friend bool operator==(const ASTNode &L, const ASTNode &R) { >> + return L.D == R.D && L.Stm == R.Stm; >> + } >> + friend bool operator!=(const ASTNode &L, const ASTNode &R) { >> + return !(L == R); >> + } >> + >> + void print(llvm::raw_ostream &OS); >> +}; >> + >> +} // namespace clang >> + >> +#endif >> >> Added: cfe/trunk/lib/AST/ASTNode.cpp >> URL: >> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTNode.cpp?rev=74797&view=auto >> >> = >> = >> = >> = >> = >> = >> = >> = >> = >> ===================================================================== >> --- cfe/trunk/lib/AST/ASTNode.cpp (added) >> +++ cfe/trunk/lib/AST/ASTNode.cpp Sun Jul 5 17:21:28 2009 >> @@ -0,0 +1,90 @@ >> +//===--- ASTNode.h - 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. >> +// >> +// >> = >> = >> = >> ----------------------------------------------------------------------= >> ==// >> +// >> +// ASTNode is Decl or a Stmt and its immediate Decl parent. >> +// >> +// >> = >> = >> = >> ----------------------------------------------------------------------= >> ==// >> + >> +#include "clang/AST/ASTNode.h" >> +#include "clang/AST/Decl.h" >> +#include "clang/AST/Stmt.h" >> +#include "clang/AST/Expr.h" >> +using namespace clang; >> + >> +static bool isContainedInStatement(Stmt *Node, Stmt *Parent) { >> + assert(Node && Parent && "Passed null Node or Parent"); >> + >> + if (Node == Parent) >> + return true; >> + >> + for (Stmt::child_iterator >> + I = Parent->child_begin(), E = Parent->child_end(); I != >> E; ++I) { >> + if (isContainedInStatement(Node, *I)) >> + return true; >> + } >> + >> + return false; >> +} >> + >> +static Decl *FindImmediateParent(Decl *D, Stmt *Node) { >> + assert(D && Node && "Passed null Decl or null Stmt"); >> + >> + if (VarDecl *VD = dyn_cast<VarDecl>(D)) { >> + Expr *Init = VD->getInit(); >> + if (Init == 0) >> + return 0; >> + return isContainedInStatement(Node, Init) ? D : 0; >> + } >> + >> + if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { >> + if (!FD->isThisDeclarationADefinition()) >> + return 0; >> + >> + for (DeclContext::decl_iterator >> + I = FD->decls_begin(), E = FD->decls_end(); I != E; + >> +I) { >> + Decl *Child = FindImmediateParent(*I, Node); >> + if (Child) >> + return Child; >> + } >> + >> + assert(FD->getBody() && "If not definition we should have >> exited already"); >> + return isContainedInStatement(Node, FD->getBody()) ? D : 0; >> + } >> + >> + return 0; >> +} >> + >> +bool ASTNode::isImmediateParent(Decl *D, Stmt *Node) { >> + assert(D && Node && "Passed null Decl or null Stmt"); >> + return D == FindImmediateParent(D, Node); >> +} >> + >> +void ASTNode::print(llvm::raw_ostream &OS) { >> + assert(isValid() && "ASTNode is not valid"); >> + >> + OS << "[Decl: " << getDecl()->getDeclKindName() << " "; >> + if (NamedDecl *ND = dyn_cast<NamedDecl>(getDecl())) >> + OS << ND->getNameAsString(); >> + >> + if (getStmt()) { >> + ASTContext &Ctx = getDecl()->getASTContext(); >> + OS << " | Stmt: " << getStmt()->getStmtClassName() << " "; >> + getStmt()->printPretty(OS, Ctx, 0, PrintingPolicy >> (Ctx.getLangOptions())); >> + } >> + >> + OS << "] <"; >> + >> + SourceRange Range = hasStmt() ? getStmt()->getSourceRange() >> + : getDecl()->getSourceRange(); >> + SourceManager &SourceMgr = getDecl()->getASTContext >> ().getSourceManager(); >> + Range.getBegin().print(OS, SourceMgr); >> + OS << ", "; >> + Range.getEnd().print(OS, SourceMgr); >> + OS << ">\n"; >> +} >> >> Modified: cfe/trunk/lib/AST/CMakeLists.txt >> URL: >> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/CMakeLists.txt?rev=74797&r1=74796&r2=74797&view=diff >> >> = >> = >> = >> = >> = >> = >> = >> = >> = >> ===================================================================== >> --- cfe/trunk/lib/AST/CMakeLists.txt (original) >> +++ cfe/trunk/lib/AST/CMakeLists.txt Sun Jul 5 17:21:28 2009 >> @@ -4,6 +4,7 @@ >> APValue.cpp >> ASTConsumer.cpp >> ASTContext.cpp >> + ASTNode.cpp >> CFG.cpp >> DeclarationName.cpp >> DeclBase.cpp >> >> >> _______________________________________________ >> cfe-commits mailing list >> [email protected] >> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits > _______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
