From 6d9a685d75b62a6e152b76a600794e72cbdde8a6 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 25 Oct 2006 05:11:20 +0000 Subject: [PATCH] Make the driver print function bodies at -parse-print-ast llvm-svn: 39048 --- clang/AST/Decl.cpp | 5 +++++ clang/AST/Sema.cpp | 2 +- clang/AST/Stmt.cpp | 9 +++++---- clang/Driver/clang.cpp | 8 ++++++-- clang/Sema/Sema.cpp | 2 +- clang/include/clang/AST/Decl.h | 11 +++++++++++ clang/include/clang/AST/Expr.h | 3 +++ clang/include/clang/AST/Stmt.h | 3 +++ 8 files changed, 35 insertions(+), 8 deletions(-) diff --git a/clang/AST/Decl.cpp b/clang/AST/Decl.cpp index a3760ebc4d61..db3d632bdeb9 100644 --- a/clang/AST/Decl.cpp +++ b/clang/AST/Decl.cpp @@ -12,4 +12,9 @@ //===----------------------------------------------------------------------===// #include "clang/AST/Decl.h" +using namespace llvm; +using namespace clang; +// Out-of-line virtual method providing a home for Decl. +Decl::~Decl() { +} diff --git a/clang/AST/Sema.cpp b/clang/AST/Sema.cpp index e62de8bd609b..fe833aa5cd17 100644 --- a/clang/AST/Sema.cpp +++ b/clang/AST/Sema.cpp @@ -196,7 +196,7 @@ ASTBuilder::ParseCompoundStmt(SourceLocation L, SourceLocation R, Action::StmtResult ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) { - + return new ReturnStmt((Expr*)RetValExp); } //===--------------------------------------------------------------------===// diff --git a/clang/AST/Stmt.cpp b/clang/AST/Stmt.cpp index 70992d5cc074..204bbc9646b3 100644 --- a/clang/AST/Stmt.cpp +++ b/clang/AST/Stmt.cpp @@ -22,18 +22,19 @@ void Stmt::dump() const { std::cerr << ""; return; } - bool isExpr = dynamic_cast(this) != 0; - if (isExpr) std::cerr << "("; + if (isExpr()) std::cerr << "("; dump_impl(); - if (isExpr) std::cerr << ")"; + if (isExpr()) std::cerr << ")"; } void CompoundStmt::dump_impl() const { std::cerr << "{\n"; - for (unsigned i = 0, e = Body.size(); i != e; ++i) + for (unsigned i = 0, e = Body.size(); i != e; ++i) { Body[i]->dump(); + std::cerr << "\n"; + } std::cerr << "}"; } diff --git a/clang/Driver/clang.cpp b/clang/Driver/clang.cpp index e74603c51428..148f8c0d8f7c 100644 --- a/clang/Driver/clang.cpp +++ b/clang/Driver/clang.cpp @@ -790,11 +790,15 @@ static void PrintASTs(Preprocessor &PP, unsigned MainFileID) { ASTStreamerTy *Streamer = ASTStreamer_Init(PP, MainFileID, true); while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) { - std::cerr << "Read top-level decl: "; + std::cerr << "Read top-level decl: '"; if (const IdentifierInfo *II = D->getIdentifier()) - std::cerr << II->getName() << "\n"; + std::cerr << II->getName() << "'\n"; else std::cerr << "\n"; + if (FunctionDecl *FD = D->isFunctionDecl()) { + FD->getBody()->dump(); + std::cerr << "\n"; + } } ASTStreamer_Terminate(Streamer); diff --git a/clang/Sema/Sema.cpp b/clang/Sema/Sema.cpp index e62de8bd609b..fe833aa5cd17 100644 --- a/clang/Sema/Sema.cpp +++ b/clang/Sema/Sema.cpp @@ -196,7 +196,7 @@ ASTBuilder::ParseCompoundStmt(SourceLocation L, SourceLocation R, Action::StmtResult ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) { - + return new ReturnStmt((Expr*)RetValExp); } //===--------------------------------------------------------------------===// diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 43c5e1055765..c3bd357c6bde 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -21,6 +21,7 @@ namespace llvm { namespace clang { class IdentifierInfo; class Stmt; +class FunctionDecl; /// Decl - This represents one declaration (or definition), e.g. a variable, /// typedef, function, struct, etc. @@ -43,12 +44,17 @@ class Decl { public: Decl(IdentifierInfo *Id, const Declarator &D, Decl *next) : Identifier(Id), DeclarationSpecifier(D.getDeclSpec()), Next(next) {} + virtual ~Decl(); const IdentifierInfo *getIdentifier() const { return Identifier; } const DeclSpec &getDeclSpec() const { return DeclarationSpecifier; } Decl *getNext() const { return Next; } + + // FIXME: Implement cast/dyn_cast/etc + virtual FunctionDecl *isFunctionDecl() { return 0; } + virtual const FunctionDecl *isFunctionDecl() const { return 0; } }; /// FunctionDecl - An instance of this class is created to represent a function @@ -62,6 +68,11 @@ public: Stmt *getBody() const { return Body; } void setBody(Stmt *B) { Body = B; } + + + // FIXME: Implement cast/dyn_cast/etc + virtual FunctionDecl *isFunctionDecl() { return this; } + virtual const FunctionDecl *isFunctionDecl() const { return this; } }; /// VarDecl - An instance of this class is created to represent a variable diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h index f96eec73343a..e9d01f733e5b 100644 --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -32,6 +32,9 @@ public: Expr() {} ~Expr() {} + + // FIXME: move to isa/dyncast etc. + virtual bool isExpr() const { return true; } }; //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h index 2f7148438e72..c3a0a17f4e4f 100644 --- a/clang/include/clang/AST/Stmt.h +++ b/clang/include/clang/AST/Stmt.h @@ -33,6 +33,9 @@ public: // FIXME: Change to non-virtual method that uses visitor pattern to do this. void dump() const; + // FIXME: move to isa/dyncast etc. + virtual bool isExpr() const { return false; } + private: virtual void dump_impl() const = 0; };