blob: acb91642d32d43c90553882e4834c02e24fb7a7a [file] [log] [blame]
Ted Kremenekbfa82c42007-10-16 23:37:27 +00001//===--- SerializationTest.cpp - Experimental Object Serialization --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Ted Kremenek and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements prototype code for serialization of objects in clang.
11// It is not intended yet for public use, but simply is a placeholder to
12// experiment with new serialization features. Serialization will eventually
13// be integrated as a proper component of the clang libraries.
14//
15//===----------------------------------------------------------------------===//
16
Ted Kremenekbfa82c42007-10-16 23:37:27 +000017#include "clang/AST/ASTConsumer.h"
Ted Kremenek5656b782007-11-29 01:24:25 +000018#include "clang/AST/CFG.h"
Ted Kremenekc4ca2882007-12-03 22:48:14 +000019#include "clang.h"
Ted Kremenek35537662007-12-05 22:08:43 +000020#include "ASTConsumers.h"
Ted Kremenek525fdbc2007-12-18 21:36:21 +000021#include "clang/AST/TranslationUnit.h"
Ted Kremenekc637e6b2007-10-23 22:18:37 +000022#include "llvm/Support/MemoryBuffer.h"
Ted Kremenek35537662007-12-05 22:08:43 +000023#include "llvm/ADT/scoped_ptr.h"
24#include "llvm/Support/Streams.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000025#include <fstream>
Ted Kremenekc637e6b2007-10-23 22:18:37 +000026
Ted Kremenekbfa82c42007-10-16 23:37:27 +000027using namespace clang;
Ted Kremenekbfa82c42007-10-16 23:37:27 +000028
Ted Kremenek79a2a262007-11-28 19:21:47 +000029//===----------------------------------------------------------------------===//
Ted Kremenek79a2a262007-11-28 19:21:47 +000030// Driver code.
31//===----------------------------------------------------------------------===//
32
Ted Kremenek35537662007-12-05 22:08:43 +000033namespace {
Ted Kremenek4ac81212007-11-05 21:39:35 +000034
Ted Kremenek35537662007-12-05 22:08:43 +000035class SerializationTest : public ASTConsumer {
36 TranslationUnit TU;
37 Diagnostic &Diags;
38 FileManager &FMgr;
Ted Kremenek018b3952007-11-06 19:50:53 +000039public:
Ted Kremenek63ea8632007-12-19 19:27:38 +000040 SerializationTest(const std::string& SourceFile, Diagnostic &d,
41 FileManager& fmgr, const LangOptions& LOpts)
42 : TU(SourceFile, LOpts), Diags(d), FMgr(fmgr) {}
Ted Kremenekdca24662007-12-05 00:26:13 +000043
Ted Kremenek4ac81212007-11-05 21:39:35 +000044 ~SerializationTest();
45
Ted Kremenek018b3952007-11-06 19:50:53 +000046 virtual void Initialize(ASTContext& context, unsigned) {
Ted Kremenek35537662007-12-05 22:08:43 +000047 TU.setContext(&context);
48 }
Ted Kremenek018b3952007-11-06 19:50:53 +000049
Ted Kremenek35537662007-12-05 22:08:43 +000050 virtual void HandleTopLevelDecl(Decl *D) {
51 TU.AddTopLevelDecl(D);
52 }
Ted Kremenek63ea8632007-12-19 19:27:38 +000053
Ted Kremenek018b3952007-11-06 19:50:53 +000054private:
Ted Kremenek35537662007-12-05 22:08:43 +000055 bool Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint);
56 bool Deserialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint);
Ted Kremenek4ac81212007-11-05 21:39:35 +000057};
58
Ted Kremenekc637e6b2007-10-23 22:18:37 +000059} // end anonymous namespace
Ted Kremenekbfa82c42007-10-16 23:37:27 +000060
Ted Kremenekdca24662007-12-05 00:26:13 +000061ASTConsumer*
Ted Kremenek63ea8632007-12-19 19:27:38 +000062clang::CreateSerializationTest(const std::string& SourceFile, Diagnostic &Diags,
63 FileManager& FMgr, const LangOptions &LOpts) {
64 return new SerializationTest(SourceFile,Diags,FMgr,LOpts);
Ted Kremenek018b3952007-11-06 19:50:53 +000065}
66
Ted Kremenek018b3952007-11-06 19:50:53 +000067
Ted Kremenek35537662007-12-05 22:08:43 +000068bool SerializationTest::Serialize(llvm::sys::Path& Filename,
Ted Kremenek79a2a262007-11-28 19:21:47 +000069 llvm::sys::Path& FNameDeclPrint) {
Ted Kremenek35537662007-12-05 22:08:43 +000070 {
71 // Pretty-print the decls to a temp file.
Ted Kremenekea75c552007-11-28 21:32:21 +000072 std::ofstream DeclPP(FNameDeclPrint.c_str());
73 assert (DeclPP && "Could not open file for printing out decls.");
Ted Kremenek35537662007-12-05 22:08:43 +000074 llvm::scoped_ptr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
Ted Kremeneke7201982007-11-13 22:56:10 +000075
Ted Kremenek35537662007-12-05 22:08:43 +000076 for (TranslationUnit::iterator I=TU.begin(), E=TU.end(); I!=E; ++I)
77 FilePrinter->HandleTopLevelDecl(*I);
Ted Kremenek7a1f4db2007-11-10 02:07:12 +000078 }
79
Ted Kremenek35537662007-12-05 22:08:43 +000080 // Serialize the translation unit.
Ted Kremenekdca29272007-12-18 21:44:50 +000081 return EmitASTBitcodeFile(TU,Filename);
Ted Kremenekbfa82c42007-10-16 23:37:27 +000082}
83
Ted Kremenek35537662007-12-05 22:08:43 +000084bool SerializationTest::Deserialize(llvm::sys::Path& Filename,
Ted Kremenek79a2a262007-11-28 19:21:47 +000085 llvm::sys::Path& FNameDeclPrint) {
Ted Kremenek018b3952007-11-06 19:50:53 +000086
Ted Kremenek35537662007-12-05 22:08:43 +000087 // Deserialize the translation unit.
Ted Kremenekdca29272007-12-18 21:44:50 +000088 TranslationUnit* NewTU = ReadASTBitcodeFile(Filename,FMgr);
Ted Kremenekb899ad22007-11-14 17:46:35 +000089
Ted Kremenek35537662007-12-05 22:08:43 +000090 if (!NewTU)
91 return false;
Ted Kremenekb899ad22007-11-14 17:46:35 +000092
Ted Kremenek35537662007-12-05 22:08:43 +000093 {
94 // Pretty-print the deserialized decls to a temp file.
95 std::ofstream DeclPP(FNameDeclPrint.c_str());
96 assert (DeclPP && "Could not open file for printing out decls.");
97 llvm::scoped_ptr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
98
99 for (TranslationUnit::iterator I=NewTU->begin(), E=NewTU->end(); I!=E; ++I)
100 FilePrinter->HandleTopLevelDecl(*I);
Ted Kremenekc4ca2882007-12-03 22:48:14 +0000101 }
Ted Kremenek018b3952007-11-06 19:50:53 +0000102
Ted Kremenek35537662007-12-05 22:08:43 +0000103 return true;
Ted Kremenek018b3952007-11-06 19:50:53 +0000104}
105
Ted Kremenek79a2a262007-11-28 19:21:47 +0000106namespace {
107 class TmpDirJanitor {
108 llvm::sys::Path& Dir;
109 public:
110 explicit TmpDirJanitor(llvm::sys::Path& dir) : Dir(dir) {}
111
112 ~TmpDirJanitor() {
113 llvm::cerr << "Removing: " << Dir.c_str() << '\n';
114 Dir.eraseFromDisk(true);
115 }
116 };
117}
Ted Kremenek018b3952007-11-06 19:50:53 +0000118
119SerializationTest::~SerializationTest() {
Ted Kremenek79a2a262007-11-28 19:21:47 +0000120
Ted Kremenek018b3952007-11-06 19:50:53 +0000121 std::string ErrMsg;
Ted Kremenek79a2a262007-11-28 19:21:47 +0000122 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenek018b3952007-11-06 19:50:53 +0000123
Ted Kremenek79a2a262007-11-28 19:21:47 +0000124 if (Dir.isEmpty()) {
Ted Kremenek018b3952007-11-06 19:50:53 +0000125 llvm::cerr << "Error: " << ErrMsg << "\n";
126 return;
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000127 }
128
Ted Kremenek79a2a262007-11-28 19:21:47 +0000129 TmpDirJanitor RemoveTmpOnExit(Dir);
130
131 llvm::sys::Path FNameDeclBefore = Dir;
132 FNameDeclBefore.appendComponent("test.decl_before.txt");
133
134 if (FNameDeclBefore.makeUnique(true,&ErrMsg)) {
Ted Kremenek018b3952007-11-06 19:50:53 +0000135 llvm::cerr << "Error: " << ErrMsg << "\n";
136 return;
137 }
Ted Kremenek4ac81212007-11-05 21:39:35 +0000138
Ted Kremenek79a2a262007-11-28 19:21:47 +0000139 llvm::sys::Path FNameDeclAfter = Dir;
140 FNameDeclAfter.appendComponent("test.decl_after.txt");
141
142 if (FNameDeclAfter.makeUnique(true,&ErrMsg)) {
143 llvm::cerr << "Error: " << ErrMsg << "\n";
144 return;
145 }
146
147 llvm::sys::Path ASTFilename = Dir;
148 ASTFilename.appendComponent("test.ast");
149
150 if (ASTFilename.makeUnique(true,&ErrMsg)) {
151 llvm::cerr << "Error: " << ErrMsg << "\n";
152 return;
153 }
154
155 // Serialize and then deserialize the ASTs.
Ted Kremenek35537662007-12-05 22:08:43 +0000156 bool status = Serialize(ASTFilename, FNameDeclBefore);
157 assert (status && "Serialization failed.");
158 status = Deserialize(ASTFilename, FNameDeclAfter);
159 assert (status && "Deserialization failed.");
Ted Kremenek79a2a262007-11-28 19:21:47 +0000160
161 // Read both pretty-printed files and compare them.
162
163 using llvm::MemoryBuffer;
164
Ted Kremenek35537662007-12-05 22:08:43 +0000165 llvm::scoped_ptr<MemoryBuffer>
Ted Kremenek79a2a262007-11-28 19:21:47 +0000166 MBufferSer(MemoryBuffer::getFile(FNameDeclBefore.c_str(),
167 strlen(FNameDeclBefore.c_str())));
168
169 if(!MBufferSer) {
170 llvm::cerr << "ERROR: Cannot read pretty-printed file (pre-pickle).\n";
171 return;
172 }
173
Ted Kremenek35537662007-12-05 22:08:43 +0000174 llvm::scoped_ptr<MemoryBuffer>
Ted Kremenek79a2a262007-11-28 19:21:47 +0000175 MBufferDSer(MemoryBuffer::getFile(FNameDeclAfter.c_str(),
176 strlen(FNameDeclAfter.c_str())));
177
178 if(!MBufferDSer) {
179 llvm::cerr << "ERROR: Cannot read pretty-printed file (post-pickle).\n";
180 return;
181 }
182
183 const char *p1 = MBufferSer->getBufferStart();
184 const char *e1 = MBufferSer->getBufferEnd();
185 const char *p2 = MBufferDSer->getBufferStart();
186 const char *e2 = MBufferDSer->getBufferEnd();
187
188 if (MBufferSer->getBufferSize() == MBufferDSer->getBufferSize())
189 for ( ; p1 != e1 ; ++p1, ++p2 )
190 if (*p1 != *p2) break;
191
192 if (p1 != e1 || p2 != e2 )
193 llvm::cerr << "ERROR: Pretty-printed files are not the same.\n";
194 else
195 llvm::cerr << "SUCCESS: Pretty-printed files are the same.\n";
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000196}