2007-10-17 07:37:27 +08:00
|
|
|
//===--- SerializationTest.cpp - Experimental Object Serialization --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-10-17 07:37:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements prototype code for serialization of objects in clang.
|
|
|
|
// It is not intended yet for public use, but simply is a placeholder to
|
|
|
|
// experiment with new serialization features. Serialization will eventually
|
|
|
|
// be integrated as a proper component of the clang libraries.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2007-11-29 09:24:25 +08:00
|
|
|
#include "clang/AST/CFG.h"
|
2007-12-04 06:48:14 +08:00
|
|
|
#include "clang.h"
|
2007-12-06 06:08:43 +08:00
|
|
|
#include "ASTConsumers.h"
|
2007-12-19 05:36:21 +08:00
|
|
|
#include "clang/AST/TranslationUnit.h"
|
2007-10-24 06:18:37 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2007-12-21 03:47:16 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2007-12-06 06:08:43 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2007-11-29 05:32:21 +08:00
|
|
|
#include <fstream>
|
2008-03-11 01:04:53 +08:00
|
|
|
#include <cstring>
|
2007-10-17 07:37:27 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2007-11-29 03:21:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-12-06 06:08:43 +08:00
|
|
|
// Driver code.
|
2007-11-29 03:21:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-24 06:18:37 +08:00
|
|
|
namespace {
|
2007-11-29 03:21:47 +08:00
|
|
|
|
2007-11-06 05:39:35 +08:00
|
|
|
class SerializationTest : public ASTConsumer {
|
2008-04-24 00:25:39 +08:00
|
|
|
llvm::OwningPtr<TranslationUnit> TU;
|
2007-12-04 06:48:14 +08:00
|
|
|
Diagnostic &Diags;
|
2007-12-06 06:08:43 +08:00
|
|
|
FileManager &FMgr;
|
2008-04-24 00:25:39 +08:00
|
|
|
const LangOptions& lopts;
|
2007-11-07 03:50:53 +08:00
|
|
|
public:
|
2007-12-20 06:24:34 +08:00
|
|
|
SerializationTest(Diagnostic &d, FileManager& fmgr, const LangOptions& LOpts)
|
2008-04-24 00:25:39 +08:00
|
|
|
: Diags(d), FMgr(fmgr), lopts(LOpts) {}
|
2007-12-05 08:26:13 +08:00
|
|
|
|
2007-11-07 03:50:53 +08:00
|
|
|
~SerializationTest();
|
2007-10-17 07:37:27 +08:00
|
|
|
|
2007-12-20 06:51:13 +08:00
|
|
|
virtual void Initialize(ASTContext& context) {
|
2008-04-24 00:25:39 +08:00
|
|
|
if (!TU) TU.reset(new TranslationUnit(context, lopts));
|
2007-12-06 06:08:43 +08:00
|
|
|
}
|
|
|
|
|
2007-11-07 03:50:53 +08:00
|
|
|
virtual void HandleTopLevelDecl(Decl *D) {
|
2008-04-24 00:25:39 +08:00
|
|
|
TU->AddTopLevelDecl(D);
|
2007-10-17 07:37:27 +08:00
|
|
|
}
|
2007-12-20 03:27:38 +08:00
|
|
|
|
2007-11-07 03:50:53 +08:00
|
|
|
private:
|
2007-12-06 06:08:43 +08:00
|
|
|
bool Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint);
|
|
|
|
bool Deserialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint);
|
2007-11-07 03:50:53 +08:00
|
|
|
};
|
2007-10-17 07:37:27 +08:00
|
|
|
|
2007-11-07 03:50:53 +08:00
|
|
|
} // end anonymous namespace
|
2007-10-17 07:37:27 +08:00
|
|
|
|
2007-12-05 08:26:13 +08:00
|
|
|
ASTConsumer*
|
2007-12-20 06:24:34 +08:00
|
|
|
clang::CreateSerializationTest(Diagnostic &Diags, FileManager& FMgr,
|
|
|
|
const LangOptions &LOpts) {
|
|
|
|
|
|
|
|
return new SerializationTest(Diags,FMgr,LOpts);
|
2007-11-06 05:39:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-06 06:08:43 +08:00
|
|
|
bool SerializationTest::Serialize(llvm::sys::Path& Filename,
|
2007-11-29 03:21:47 +08:00
|
|
|
llvm::sys::Path& FNameDeclPrint) {
|
2007-12-06 06:08:43 +08:00
|
|
|
{
|
|
|
|
// Pretty-print the decls to a temp file.
|
2007-11-29 05:32:21 +08:00
|
|
|
std::ofstream DeclPP(FNameDeclPrint.c_str());
|
|
|
|
assert (DeclPP && "Could not open file for printing out decls.");
|
2007-12-21 03:47:16 +08:00
|
|
|
llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
|
2007-11-14 06:56:10 +08:00
|
|
|
|
2008-04-24 00:25:39 +08:00
|
|
|
for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
|
2007-12-06 06:08:43 +08:00
|
|
|
FilePrinter->HandleTopLevelDecl(*I);
|
2007-10-17 07:37:27 +08:00
|
|
|
}
|
2007-10-24 06:18:37 +08:00
|
|
|
|
2007-12-06 06:08:43 +08:00
|
|
|
// Serialize the translation unit.
|
2008-04-24 00:25:39 +08:00
|
|
|
return EmitASTBitcodeFile(*TU,Filename);
|
2007-10-17 07:37:27 +08:00
|
|
|
}
|
|
|
|
|
2007-12-06 06:08:43 +08:00
|
|
|
bool SerializationTest::Deserialize(llvm::sys::Path& Filename,
|
2007-11-29 03:21:47 +08:00
|
|
|
llvm::sys::Path& FNameDeclPrint) {
|
2007-11-07 03:50:53 +08:00
|
|
|
|
2007-12-06 06:08:43 +08:00
|
|
|
// Deserialize the translation unit.
|
2007-12-19 05:44:50 +08:00
|
|
|
TranslationUnit* NewTU = ReadASTBitcodeFile(Filename,FMgr);
|
2007-11-15 01:46:35 +08:00
|
|
|
|
2007-12-06 06:08:43 +08:00
|
|
|
if (!NewTU)
|
|
|
|
return false;
|
2007-11-15 01:46:35 +08:00
|
|
|
|
2007-12-06 06:08:43 +08:00
|
|
|
{
|
|
|
|
// Pretty-print the deserialized decls to a temp file.
|
|
|
|
std::ofstream DeclPP(FNameDeclPrint.c_str());
|
|
|
|
assert (DeclPP && "Could not open file for printing out decls.");
|
2007-12-21 03:47:16 +08:00
|
|
|
llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
|
2007-11-29 09:24:25 +08:00
|
|
|
|
2007-12-06 06:08:43 +08:00
|
|
|
for (TranslationUnit::iterator I=NewTU->begin(), E=NewTU->end(); I!=E; ++I)
|
|
|
|
FilePrinter->HandleTopLevelDecl(*I);
|
2007-11-07 03:50:53 +08:00
|
|
|
}
|
2008-04-03 13:03:34 +08:00
|
|
|
|
|
|
|
delete NewTU;
|
2007-12-06 06:08:43 +08:00
|
|
|
|
|
|
|
return true;
|
2007-11-07 03:50:53 +08:00
|
|
|
}
|
2007-11-06 05:39:35 +08:00
|
|
|
|
2007-11-29 03:21:47 +08:00
|
|
|
namespace {
|
|
|
|
class TmpDirJanitor {
|
|
|
|
llvm::sys::Path& Dir;
|
|
|
|
public:
|
|
|
|
explicit TmpDirJanitor(llvm::sys::Path& dir) : Dir(dir) {}
|
|
|
|
|
|
|
|
~TmpDirJanitor() {
|
|
|
|
llvm::cerr << "Removing: " << Dir.c_str() << '\n';
|
|
|
|
Dir.eraseFromDisk(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2007-10-17 07:37:27 +08:00
|
|
|
|
2007-11-07 03:50:53 +08:00
|
|
|
SerializationTest::~SerializationTest() {
|
2007-11-29 03:21:47 +08:00
|
|
|
|
2007-11-07 03:50:53 +08:00
|
|
|
std::string ErrMsg;
|
2007-11-29 03:21:47 +08:00
|
|
|
llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
|
2007-11-07 03:50:53 +08:00
|
|
|
|
2007-11-29 03:21:47 +08:00
|
|
|
if (Dir.isEmpty()) {
|
2007-11-07 03:50:53 +08:00
|
|
|
llvm::cerr << "Error: " << ErrMsg << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-29 03:21:47 +08:00
|
|
|
TmpDirJanitor RemoveTmpOnExit(Dir);
|
|
|
|
|
|
|
|
llvm::sys::Path FNameDeclBefore = Dir;
|
|
|
|
FNameDeclBefore.appendComponent("test.decl_before.txt");
|
|
|
|
|
|
|
|
if (FNameDeclBefore.makeUnique(true,&ErrMsg)) {
|
|
|
|
llvm::cerr << "Error: " << ErrMsg << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::sys::Path FNameDeclAfter = Dir;
|
|
|
|
FNameDeclAfter.appendComponent("test.decl_after.txt");
|
|
|
|
|
|
|
|
if (FNameDeclAfter.makeUnique(true,&ErrMsg)) {
|
|
|
|
llvm::cerr << "Error: " << ErrMsg << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::sys::Path ASTFilename = Dir;
|
|
|
|
ASTFilename.appendComponent("test.ast");
|
2007-11-07 03:50:53 +08:00
|
|
|
|
2007-11-29 03:21:47 +08:00
|
|
|
if (ASTFilename.makeUnique(true,&ErrMsg)) {
|
2007-11-07 03:50:53 +08:00
|
|
|
llvm::cerr << "Error: " << ErrMsg << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-29 03:21:47 +08:00
|
|
|
// Serialize and then deserialize the ASTs.
|
2007-12-06 06:08:43 +08:00
|
|
|
bool status = Serialize(ASTFilename, FNameDeclBefore);
|
|
|
|
assert (status && "Serialization failed.");
|
|
|
|
status = Deserialize(ASTFilename, FNameDeclAfter);
|
|
|
|
assert (status && "Deserialization failed.");
|
2007-11-29 03:21:47 +08:00
|
|
|
|
|
|
|
// Read both pretty-printed files and compare them.
|
|
|
|
|
|
|
|
using llvm::MemoryBuffer;
|
|
|
|
|
2007-12-21 03:47:16 +08:00
|
|
|
llvm::OwningPtr<MemoryBuffer>
|
2008-04-02 02:04:30 +08:00
|
|
|
MBufferSer(MemoryBuffer::getFile(FNameDeclBefore.c_str()));
|
2007-11-29 03:21:47 +08:00
|
|
|
|
|
|
|
if(!MBufferSer) {
|
|
|
|
llvm::cerr << "ERROR: Cannot read pretty-printed file (pre-pickle).\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-21 03:47:16 +08:00
|
|
|
llvm::OwningPtr<MemoryBuffer>
|
2008-04-02 02:04:30 +08:00
|
|
|
MBufferDSer(MemoryBuffer::getFile(FNameDeclAfter.c_str()));
|
2007-11-29 03:21:47 +08:00
|
|
|
|
|
|
|
if(!MBufferDSer) {
|
|
|
|
llvm::cerr << "ERROR: Cannot read pretty-printed file (post-pickle).\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *p1 = MBufferSer->getBufferStart();
|
|
|
|
const char *e1 = MBufferSer->getBufferEnd();
|
|
|
|
const char *p2 = MBufferDSer->getBufferStart();
|
|
|
|
const char *e2 = MBufferDSer->getBufferEnd();
|
|
|
|
|
|
|
|
if (MBufferSer->getBufferSize() == MBufferDSer->getBufferSize())
|
|
|
|
for ( ; p1 != e1 ; ++p1, ++p2 )
|
|
|
|
if (*p1 != *p2) break;
|
|
|
|
|
|
|
|
if (p1 != e1 || p2 != e2 )
|
|
|
|
llvm::cerr << "ERROR: Pretty-printed files are not the same.\n";
|
|
|
|
else
|
|
|
|
llvm::cerr << "SUCCESS: Pretty-printed files are the same.\n";
|
2007-10-24 06:18:37 +08:00
|
|
|
}
|