Converted AST Pretty-Printer to use iostreams instead of FILE*. This fixes

a bug where the statement pretty-printer used iostreams but the AST printer
did not.  This was an issue when dumping ASTs to something other than stderr.

Updated SerializationTest to use the new iostreams interface for the AST printer.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44417 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ted Kremenek 2007-11-28 21:32:21 +00:00
Родитель 251f7325b3
Коммит ea75c55990
3 изменённых файлов: 120 добавлений и 129 удалений

Просмотреть файл

@ -17,6 +17,7 @@
#include "clang/AST/CFG.h" #include "clang/AST/CFG.h"
#include "clang/Analysis/LiveVariables.h" #include "clang/Analysis/LiveVariables.h"
#include "clang/Analysis/LocalCheckers.h" #include "clang/Analysis/LocalCheckers.h"
#include "llvm/Support/Streams.h"
using namespace clang; using namespace clang;
@ -26,9 +27,10 @@ using namespace clang;
namespace { namespace {
class DeclPrinter { class DeclPrinter {
public: public:
FILE* FP; std::ostream& Out;
DeclPrinter(FILE* fp) : FP(fp ? fp : stderr) {} DeclPrinter(std::ostream* out) : Out(out ? *out : *llvm::cerr.stream()) {}
DeclPrinter() : Out(*llvm::cerr.stream()) {}
void PrintFunctionDeclStart(FunctionDecl *FD); void PrintFunctionDeclStart(FunctionDecl *FD);
void PrintTypeDefDecl(TypedefDecl *TD); void PrintTypeDefDecl(TypedefDecl *TD);
@ -45,17 +47,17 @@ namespace {
void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) { void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
bool HasBody = FD->getBody(); bool HasBody = FD->getBody();
fprintf(FP, "\n"); Out << '\n';
switch (FD->getStorageClass()) { switch (FD->getStorageClass()) {
default: assert(0 && "Unknown storage class"); default: assert(0 && "Unknown storage class");
case FunctionDecl::None: break; case FunctionDecl::None: break;
case FunctionDecl::Extern: fprintf(FP, "extern "); break; case FunctionDecl::Extern: Out << "extern "; break;
case FunctionDecl::Static: fprintf(FP, "static "); break; case FunctionDecl::Static: Out << "static "; break;
} }
if (FD->isInline()) if (FD->isInline())
fprintf(FP, "inline "); Out << "inline ";
std::string Proto = FD->getName(); std::string Proto = FD->getName();
FunctionType *AFT = cast<FunctionType>(FD->getType()); FunctionType *AFT = cast<FunctionType>(FD->getType());
@ -82,54 +84,52 @@ void DeclPrinter::PrintFunctionDeclStart(FunctionDecl *FD) {
} }
AFT->getResultType().getAsStringInternal(Proto); AFT->getResultType().getAsStringInternal(Proto);
fprintf(FP, "%s", Proto.c_str()); Out << Proto;
if (!FD->getBody()) if (!FD->getBody())
fprintf(FP, ";\n"); Out << ";\n";
// Doesn't print the body. // Doesn't print the body.
} }
void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) { void DeclPrinter::PrintTypeDefDecl(TypedefDecl *TD) {
std::string S = TD->getName(); std::string S = TD->getName();
TD->getUnderlyingType().getAsStringInternal(S); TD->getUnderlyingType().getAsStringInternal(S);
fprintf(FP, "typedef %s;\n", S.c_str()); Out << "typedef " << S << ";\n";
} }
void DeclPrinter::PrintObjcMethodDecl(ObjcMethodDecl *OMD) { void DeclPrinter::PrintObjcMethodDecl(ObjcMethodDecl *OMD) {
if (OMD->isInstance()) if (OMD->isInstance())
fprintf(FP, "\n- "); Out << "\n- ";
else else
fprintf(FP, "\n+ "); Out << "\n+ ";
if (!OMD->getResultType().isNull()) if (!OMD->getResultType().isNull())
fprintf(FP, "(%s) ", OMD->getResultType().getAsString().c_str()); Out << '(' << OMD->getResultType().getAsString() << ") ";
// FIXME: just print original selector name! // FIXME: just print original selector name!
fprintf(FP, "%s ", OMD->getSelector().getName().c_str()); Out << OMD->getSelector().getName();
for (int i = 0; i < OMD->getNumParams(); i++) { for (int i = 0; i < OMD->getNumParams(); i++) {
ParmVarDecl *PDecl = OMD->getParamDecl(i); ParmVarDecl *PDecl = OMD->getParamDecl(i);
// FIXME: selector is missing here! // FIXME: selector is missing here!
fprintf(FP, " :(%s) %s", PDecl->getType().getAsString().c_str(), Out << " :(" << PDecl->getType().getAsString() << ") " << PDecl->getName();
PDecl->getName());
} }
} }
void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) { void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) {
std::string I = OID->getName(); std::string I = OID->getName();
ObjcInterfaceDecl *SID = OID->getSuperClass(); ObjcInterfaceDecl *SID = OID->getSuperClass();
if (SID) {
std::string S = SID->getName(); if (SID)
fprintf(FP, "@implementation %s : %s", I.c_str(), S.c_str()); Out << "@implementation " << I << " : " << SID->getName();
}
else else
fprintf(FP, "@implementation %s", I.c_str()); Out << "@implementation " << I;
for (int i = 0; i < OID->getNumInstanceMethods(); i++) { for (int i = 0; i < OID->getNumInstanceMethods(); i++) {
PrintObjcMethodDecl(OID->getInstanceMethods()[i]); PrintObjcMethodDecl(OID->getInstanceMethods()[i]);
ObjcMethodDecl *OMD = OID->getInstanceMethods()[i]; ObjcMethodDecl *OMD = OID->getInstanceMethods()[i];
if (OMD->getBody()) { if (OMD->getBody()) {
fprintf(FP, " "); Out << ' ';
OMD->getBody()->dumpPretty(); OMD->getBody()->printPretty(Out);
fprintf(FP, "\n"); Out << '\n';
} }
} }
@ -137,145 +137,149 @@ void DeclPrinter::PrintObjcImplementationDecl(ObjcImplementationDecl *OID) {
PrintObjcMethodDecl(OID->getClassMethods()[i]); PrintObjcMethodDecl(OID->getClassMethods()[i]);
ObjcMethodDecl *OMD = OID->getClassMethods()[i]; ObjcMethodDecl *OMD = OID->getClassMethods()[i];
if (OMD->getBody()) { if (OMD->getBody()) {
fprintf(FP, " "); Out << ' ';
OMD->getBody()->dumpPretty(); OMD->getBody()->printPretty(Out);
fprintf(FP, "\n"); Out << '\n';
} }
} }
fprintf(FP,"@end\n"); Out << "@end\n";
} }
void DeclPrinter::PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) { void DeclPrinter::PrintObjcInterfaceDecl(ObjcInterfaceDecl *OID) {
std::string I = OID->getName(); std::string I = OID->getName();
ObjcInterfaceDecl *SID = OID->getSuperClass(); ObjcInterfaceDecl *SID = OID->getSuperClass();
if (SID) {
std::string S = SID->getName(); if (SID)
fprintf(FP, "@interface %s : %s", I.c_str(), S.c_str()); Out << "@interface " << I << " : " << SID->getName();
}
else else
fprintf(FP, "@interface %s", I.c_str()); Out << "@interface " << I;
// Protocols? // Protocols?
int count = OID->getNumIntfRefProtocols(); int count = OID->getNumIntfRefProtocols();
if (count > 0) { if (count > 0) {
ObjcProtocolDecl **refProtocols = OID->getReferencedProtocols(); ObjcProtocolDecl **refProtocols = OID->getReferencedProtocols();
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
fprintf(FP, "%c%s", (i == 0 ? '<' : ','), Out << (i == 0 ? '<' : ',') << refProtocols[i]->getName();
refProtocols[i]->getName());
} }
if (count > 0) if (count > 0)
fprintf(FP, ">\n"); Out << ">\n";
else else
fprintf(FP, "\n"); Out << '\n';
int NumIvars = OID->getNumInstanceVariables(); int NumIvars = OID->getNumInstanceVariables();
if (NumIvars > 0) { if (NumIvars > 0) {
ObjcIvarDecl **Ivars = OID->getInstanceVariables(); ObjcIvarDecl **Ivars = OID->getInstanceVariables();
fprintf(FP,"{"); Out << '{';
for (int i = 0; i < NumIvars; i++) { for (int i = 0; i < NumIvars; i++) {
fprintf(FP, "\t%s %s;\n", Ivars[i]->getType().getAsString().c_str(), Out << '\t' << Ivars[i]->getType().getAsString()
Ivars[i]->getName()); << ' ' << Ivars[i]->getName()
<< ";\n";
} }
fprintf(FP, "}\n"); Out << "}\n";
} }
int NumProperties = OID->getNumPropertyDecl(); int NumProperties = OID->getNumPropertyDecl();
if (NumProperties > 0) { if (NumProperties > 0) {
for (int i = 0; i < NumProperties; i++) { for (int i = 0; i < NumProperties; i++) {
ObjcPropertyDecl *PDecl = OID->getPropertyDecl()[i]; ObjcPropertyDecl *PDecl = OID->getPropertyDecl()[i];
fprintf(FP, "@property"); Out << "@property";
if (PDecl->getPropertyAttributes() != ObjcPropertyDecl::OBJC_PR_noattr) { if (PDecl->getPropertyAttributes() != ObjcPropertyDecl::OBJC_PR_noattr) {
bool first = true; bool first = true;
fprintf(FP, " ("); Out << " (";
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readonly) if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readonly)
{ {
fprintf(FP, "%creadonly", first ? ' ' : ','); Out << (first ? ' ' : ',') << "readonly";
first = false; first = false;
} }
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_getter) if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_getter)
{ {
fprintf(FP, "%cgetter = %s", first ? ' ' : ',' Out << (first ? ' ' : ',') << "getter = "
, PDecl->getGetterName()->getName()); << PDecl->getGetterName()->getName();
first = false; first = false;
} }
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_setter) if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_setter)
{ {
fprintf(FP, "%csetter = %s:", first ? ' ' : ',' Out << (first ? ' ' : ',') << "setter = "
, PDecl->getSetterName()->getName()); << PDecl->getSetterName()->getName();
first = false; first = false;
} }
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_assign) if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_assign)
{ {
fprintf(FP, "%cassign", first ? ' ' : ','); Out << (first ? ' ' : ',') << "assign";
first = false; first = false;
} }
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readwrite) if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_readwrite)
{ {
fprintf(FP, "%creadwrite", first ? ' ' : ','); Out << (first ? ' ' : ',') << "readwrite";
first = false; first = false;
} }
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_retain) if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_retain)
{ {
fprintf(FP, "%cretain", first ? ' ' : ','); Out << (first ? ' ' : ',') << "retain";
first = false; first = false;
} }
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_copy) if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_copy)
{ {
fprintf(FP, "%ccopy", first ? ' ' : ','); Out << (first ? ' ' : ',') << "copy";
first = false; first = false;
} }
if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_nonatomic) if (PDecl->getPropertyAttributes() & ObjcPropertyDecl::OBJC_PR_nonatomic)
{ {
fprintf(FP, "%cnonatomic", first ? ' ' : ','); Out << (first ? ' ' : ',') << "nonatomic";
first = false; first = false;
} }
fprintf(FP, " )"); Out << " )";
} }
ObjcIvarDecl **IDecl = PDecl->getPropertyDecls(); ObjcIvarDecl **IDecl = PDecl->getPropertyDecls();
fprintf(FP, " %s %s", IDecl[0]->getType().getAsString().c_str(),
IDecl[0]->getName()); Out << ' ' << IDecl[0]->getType().getAsString()
<< ' ' << IDecl[0]->getName();
for (int j = 1; j < PDecl->getNumPropertyDecls(); j++)
Out << ", " << IDecl[j]->getName();
for (int j = 1; j < PDecl->getNumPropertyDecls(); j++) { Out << ";\n";
fprintf(FP, ", %s", IDecl[j]->getName());
}
fprintf(FP, ";\n");
} }
} }
fprintf(FP,"@end\n");
Out << "@end\n";
// FIXME: implement the rest... // FIXME: implement the rest...
} }
void DeclPrinter::PrintObjcProtocolDecl(ObjcProtocolDecl *PID) { void DeclPrinter::PrintObjcProtocolDecl(ObjcProtocolDecl *PID) {
std::string S = PID->getName(); Out << "@protocol " << PID->getName() << '\n';
fprintf(FP, "@protocol %s;\n", S.c_str());
// FIXME: implement the rest... // FIXME: implement the rest...
} }
void DeclPrinter::PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID) { void DeclPrinter::PrintObjcCategoryImplDecl(ObjcCategoryImplDecl *PID) {
std::string S = PID->getName(); Out << "@implementation "
std::string I = PID->getClassInterface()->getName(); << PID->getClassInterface()->getName()
fprintf(FP, "@implementation %s(%s);\n", I.c_str(), S.c_str()); << '(' << PID->getName() << ");\n";
// FIXME: implement the rest... // FIXME: implement the rest...
} }
void DeclPrinter::PrintObjcCategoryDecl(ObjcCategoryDecl *PID) { void DeclPrinter::PrintObjcCategoryDecl(ObjcCategoryDecl *PID) {
std::string S = PID->getName(); Out << "@interface "
std::string I = PID->getClassInterface()->getName(); << PID->getClassInterface()->getName()
fprintf(FP, "@interface %s(%s);\n", I.c_str(), S.c_str()); << '(' << PID->getName() << ");\n";
// FIXME: implement the rest... // FIXME: implement the rest...
} }
void DeclPrinter::PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) { void DeclPrinter::PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) {
std::string A = AID->getName(); Out << "@compatibility_alias " << AID->getName()
std::string I = AID->getClassInterface()->getName(); << ' ' << AID->getClassInterface()->getName() << ";\n";
fprintf(FP, "@compatibility_alias %s %s;\n", A.c_str(), I.c_str());
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -284,16 +288,16 @@ void DeclPrinter::PrintObjcCompatibleAliasDecl(ObjcCompatibleAliasDecl *AID) {
namespace { namespace {
class ASTPrinter : public ASTConsumer, public DeclPrinter { class ASTPrinter : public ASTConsumer, public DeclPrinter {
public: public:
ASTPrinter(FILE* F = NULL) : DeclPrinter(F) {} ASTPrinter(std::ostream* o = NULL) : DeclPrinter(o) {}
virtual void HandleTopLevelDecl(Decl *D) { virtual void HandleTopLevelDecl(Decl *D) {
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
PrintFunctionDeclStart(FD); PrintFunctionDeclStart(FD);
if (FD->getBody()) { if (FD->getBody()) {
fprintf(FP, " "); Out << ' ';
FD->getBody()->dumpPretty(); FD->getBody()->printPretty(Out);
fprintf(FP, "\n"); Out << '\n';
} }
} else if (isa<ObjcMethodDecl>(D)) { } else if (isa<ObjcMethodDecl>(D)) {
// Do nothing, methods definitions are printed in // Do nothing, methods definitions are printed in
@ -306,13 +310,13 @@ namespace {
PrintObjcProtocolDecl(PID); PrintObjcProtocolDecl(PID);
} else if (ObjcForwardProtocolDecl *OFPD = } else if (ObjcForwardProtocolDecl *OFPD =
dyn_cast<ObjcForwardProtocolDecl>(D)) { dyn_cast<ObjcForwardProtocolDecl>(D)) {
fprintf(FP, "@protocol "); Out << "@protocol ";
for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) { for (unsigned i = 0, e = OFPD->getNumForwardDecls(); i != e; ++i) {
const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i); const ObjcProtocolDecl *D = OFPD->getForwardProtocolDecl(i);
if (i) fprintf(FP, ", "); if (i) Out << ", ";
fprintf(FP, "%s", D->getName()); Out << D->getName();
} }
fprintf(FP, ";\n"); Out << ";\n";
} else if (ObjcImplementationDecl *OID = } else if (ObjcImplementationDecl *OID =
dyn_cast<ObjcImplementationDecl>(D)) { dyn_cast<ObjcImplementationDecl>(D)) {
PrintObjcImplementationDecl(OID); PrintObjcImplementationDecl(OID);
@ -326,9 +330,9 @@ namespace {
dyn_cast<ObjcCompatibleAliasDecl>(D)) { dyn_cast<ObjcCompatibleAliasDecl>(D)) {
PrintObjcCompatibleAliasDecl(OID); PrintObjcCompatibleAliasDecl(OID);
} else if (isa<ObjcClassDecl>(D)) { } else if (isa<ObjcClassDecl>(D)) {
fprintf(FP, "@class [printing todo]\n"); Out << "@class [printing todo]\n";
} else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
fprintf(FP, "Read top-level variable decl: '%s'\n", SD->getName()); Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
} else { } else {
assert(0 && "Unknown decl type!"); assert(0 && "Unknown decl type!");
} }
@ -336,7 +340,9 @@ namespace {
}; };
} }
ASTConsumer *clang::CreateASTPrinter(FILE* fp) { return new ASTPrinter(fp); } ASTConsumer *clang::CreateASTPrinter(std::ostream* out) {
return new ASTPrinter(out);
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// ASTDumper - Low-level dumper of ASTs /// ASTDumper - Low-level dumper of ASTs
@ -345,7 +351,7 @@ namespace {
class ASTDumper : public ASTConsumer, public DeclPrinter { class ASTDumper : public ASTConsumer, public DeclPrinter {
SourceManager *SM; SourceManager *SM;
public: public:
ASTDumper(FILE* fp = NULL) : DeclPrinter(fp) {} ASTDumper() : DeclPrinter() {}
void Initialize(ASTContext &Context, unsigned MainFileID) { void Initialize(ASTContext &Context, unsigned MainFileID) {
SM = &Context.SourceMgr; SM = &Context.SourceMgr;
@ -356,24 +362,25 @@ namespace {
PrintFunctionDeclStart(FD); PrintFunctionDeclStart(FD);
if (FD->getBody()) { if (FD->getBody()) {
fprintf(FP, "\n"); Out << '\n';
// FIXME: convert dumper to use std::ostream?
FD->getBody()->dumpAll(*SM); FD->getBody()->dumpAll(*SM);
fprintf(FP, "\n"); Out << '\n';
} }
} else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
PrintTypeDefDecl(TD); PrintTypeDefDecl(TD);
} else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) { } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D)) {
fprintf(FP, "Read top-level variable decl: '%s'\n", SD->getName()); Out << "Read top-level variable decl: '" << SD->getName() << "'\n";
} else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) { } else if (ObjcInterfaceDecl *OID = dyn_cast<ObjcInterfaceDecl>(D)) {
fprintf(FP, "Read objc interface '%s'\n", OID->getName()); Out << "Read objc interface '" << OID->getName() << "'\n";
} else if (ObjcProtocolDecl *OPD = dyn_cast<ObjcProtocolDecl>(D)) { } else if (ObjcProtocolDecl *OPD = dyn_cast<ObjcProtocolDecl>(D)) {
fprintf(FP, "Read objc protocol '%s'\n", OPD->getName()); Out << "Read objc protocol '" << OPD->getName() << "'\n";
} else if (ObjcCategoryDecl *OCD = dyn_cast<ObjcCategoryDecl>(D)) { } else if (ObjcCategoryDecl *OCD = dyn_cast<ObjcCategoryDecl>(D)) {
fprintf(FP, "Read objc category '%s'\n", OCD->getName()); Out << "Read objc category '" << OCD->getName() << "'\n";
} else if (isa<ObjcForwardProtocolDecl>(D)) { } else if (isa<ObjcForwardProtocolDecl>(D)) {
fprintf(FP, "Read objc fwd protocol decl\n"); Out << "Read objc fwd protocol decl\n";
} else if (isa<ObjcClassDecl>(D)) { } else if (isa<ObjcClassDecl>(D)) {
fprintf(FP, "Read objc fwd class decl\n"); Out << "Read objc fwd class decl\n";
} else { } else {
assert(0 && "Unknown decl type!"); assert(0 && "Unknown decl type!");
} }
@ -396,12 +403,12 @@ namespace {
virtual void HandleTopLevelDecl(Decl *D) { virtual void HandleTopLevelDecl(Decl *D) {
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
DeclPrinter(stderr).PrintFunctionDeclStart(FD); DeclPrinter().PrintFunctionDeclStart(FD);
if (FD->getBody()) { if (FD->getBody()) {
fprintf(stderr, "\n"); llvm::cerr << '\n';
FD->getBody()->viewAST(); FD->getBody()->viewAST();
fprintf(stderr, "\n"); llvm::cerr << '\n';
} }
} }
} }
@ -434,8 +441,8 @@ void CFGVisitor::HandleTopLevelDecl(Decl *D) {
return; return;
if (printFuncDeclStart()) { if (printFuncDeclStart()) {
DeclPrinter(stderr).PrintFunctionDeclStart(FD); DeclPrinter().PrintFunctionDeclStart(FD);
fprintf(stderr,"\n"); llvm::cerr << '\n';
} }
CFG *C = CFG::buildCFG(FD->getBody()); CFG *C = CFG::buildCFG(FD->getBody());

Просмотреть файл

@ -14,7 +14,7 @@
#ifndef DRIVER_ASTCONSUMERS_H #ifndef DRIVER_ASTCONSUMERS_H
#define DRIVER_ASTCONSUMERS_H #define DRIVER_ASTCONSUMERS_H
#include <stdio.h> #include <iosfwd>
namespace clang { namespace clang {
@ -22,7 +22,7 @@ class ASTConsumer;
class Diagnostic; class Diagnostic;
struct LangOptions; struct LangOptions;
ASTConsumer *CreateASTPrinter(FILE* FP = NULL); ASTConsumer *CreateASTPrinter(std::ostream* OS = NULL);
ASTConsumer *CreateASTDumper(); ASTConsumer *CreateASTDumper();
ASTConsumer *CreateASTViewer(); ASTConsumer *CreateASTViewer();
ASTConsumer *CreateCFGDumper(bool ViewGraphs = false); ASTConsumer *CreateCFGDumper(bool ViewGraphs = false);

Просмотреть файл

@ -23,11 +23,11 @@
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Bitcode/Serialize.h" #include "llvm/Bitcode/Serialize.h"
#include "llvm/Bitcode/Deserialize.h" #include "llvm/Bitcode/Deserialize.h"
#include <fstream>
#include <stdio.h> #include <stdio.h>
#include <list> #include <list>
using namespace clang; using namespace clang;
using llvm::sys::TimeValue;
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Utility classes // Utility classes
@ -43,20 +43,6 @@ public:
operator T*() const { return Obj; } operator T*() const { return Obj; }
T* operator->() { return Obj; } T* operator->() { return Obj; }
}; };
class FileSP {
FILE* f;
public:
FileSP(const llvm::sys::Path& fname, const char* mode = "wb")
: f(fopen(fname.c_str(),mode)) {}
~FileSP() { if (f) fclose(f); }
operator FILE*() const { return f; }
private:
void operator=(const FileSP& RHS) {}
FileSP(const FileSP& RHS) {}
};
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Driver code. // Driver code.
@ -134,9 +120,9 @@ void SerializationTest::Serialize(llvm::sys::Path& Filename,
{ // Create a printer to "consume" our deserialized ASTS. { // Create a printer to "consume" our deserialized ASTS.
Janitor<ASTConsumer> Printer(CreateASTPrinter()); Janitor<ASTConsumer> Printer(CreateASTPrinter());
FileSP DeclFP(FNameDeclPrint,"w"); std::ofstream DeclPP(FNameDeclPrint.c_str());
assert (DeclFP && "Could not open file for printing out decls."); assert (DeclPP && "Could not open file for printing out decls.");
Janitor<ASTConsumer> FilePrinter(CreateASTPrinter(DeclFP)); Janitor<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
for (std::list<Decl*>::iterator I=Decls.begin(), E=Decls.end(); I!=E; ++I) { for (std::list<Decl*>::iterator I=Decls.begin(), E=Decls.end(); I!=E; ++I) {
llvm::cerr << "Serializing: Decl.\n"; llvm::cerr << "Serializing: Decl.\n";
@ -183,15 +169,13 @@ void SerializationTest::Serialize(llvm::sys::Path& Filename,
// ===---------------------------------------------------===/ // ===---------------------------------------------------===/
// Finalize serialization: write the bits to disk. // Finalize serialization: write the bits to disk.
{ if (FILE* fp = fopen(Filename.c_str(),"wb")) {
FileSP fp(Filename); fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp);
fclose(fp);
if (fp) }
fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp); else {
else { llvm::cerr << "Error: Cannot open " << Filename.c_str() << "\n";
llvm::cerr << "Error: Cannot open " << Filename.c_str() << "\n"; return;
return;
}
} }
llvm::cerr << "Commited bitstream to disk: " << Filename.c_str() << "\n"; llvm::cerr << "Commited bitstream to disk: " << Filename.c_str() << "\n";
@ -280,9 +264,9 @@ void SerializationTest::Deserialize(llvm::sys::Path& Filename,
// Create a printer to "consume" our deserialized ASTS. // Create a printer to "consume" our deserialized ASTS.
ASTConsumer* Printer = CreateASTPrinter(); ASTConsumer* Printer = CreateASTPrinter();
Janitor<ASTConsumer> PrinterJanitor(Printer); Janitor<ASTConsumer> PrinterJanitor(Printer);
FileSP DeclFP(FNameDeclPrint,"w"); std::ofstream DeclPP(FNameDeclPrint.c_str());
assert (DeclFP && "Could not open file for printing out decls."); assert (DeclPP && "Could not open file for printing out decls.");
Janitor<ASTConsumer> FilePrinter(CreateASTPrinter(DeclFP)); Janitor<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
// The remaining objects in the file are top-level decls. // The remaining objects in the file are top-level decls.
while (!Dezr.FinishedBlock(DeclBlockLoc)) { while (!Dezr.FinishedBlock(DeclBlockLoc)) {