blob: 946a4c87d6ae83560702955245a375256283d8c8 [file] [log] [blame]
Ted Kremenekbfa82c42007-10-16 23:37:27 +00001//===--- SerializationTest.cpp - Experimental Object Serialization --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekbfa82c42007-10-16 23:37:27 +00007//
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 Kremenekee533642007-12-20 19:47:16 +000023#include "llvm/ADT/OwningPtr.h"
Ted Kremenek35537662007-12-05 22:08:43 +000024#include "llvm/Support/Streams.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000025#include <fstream>
Chris Lattner87cf5ac2008-03-10 17:04:53 +000026#include <cstring>
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 {
Ted Kremeneke7d07d12008-06-04 15:55:15 +000036 TranslationUnit* TU;
Ted Kremenek35537662007-12-05 22:08:43 +000037 Diagnostic &Diags;
38 FileManager &FMgr;
Ted Kremenek018b3952007-11-06 19:50:53 +000039public:
Ted Kremeneke7d07d12008-06-04 15:55:15 +000040 SerializationTest(Diagnostic &d, FileManager& fmgr)
41 : TU(0), Diags(d), FMgr(fmgr) {}
Ted Kremenekdca24662007-12-05 00:26:13 +000042
Ted Kremenek4ac81212007-11-05 21:39:35 +000043 ~SerializationTest();
44
Ted Kremeneke7d07d12008-06-04 15:55:15 +000045 virtual void InitializeTU(TranslationUnit& tu) { TU = &tu; }
Ted Kremenek63ea8632007-12-19 19:27:38 +000046
Ted Kremenek018b3952007-11-06 19:50:53 +000047private:
Ted Kremenek35537662007-12-05 22:08:43 +000048 bool Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint);
49 bool Deserialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint);
Ted Kremenek4ac81212007-11-05 21:39:35 +000050};
51
Ted Kremenekc637e6b2007-10-23 22:18:37 +000052} // end anonymous namespace
Ted Kremenekbfa82c42007-10-16 23:37:27 +000053
Ted Kremenekdca24662007-12-05 00:26:13 +000054ASTConsumer*
Ted Kremeneke7d07d12008-06-04 15:55:15 +000055clang::CreateSerializationTest(Diagnostic &Diags, FileManager& FMgr) {
56 return new SerializationTest(Diags, FMgr);
Ted Kremenek018b3952007-11-06 19:50:53 +000057}
58
Ted Kremenek018b3952007-11-06 19:50:53 +000059
Ted Kremenek35537662007-12-05 22:08:43 +000060bool SerializationTest::Serialize(llvm::sys::Path& Filename,
Ted Kremenek79a2a262007-11-28 19:21:47 +000061 llvm::sys::Path& FNameDeclPrint) {
Ted Kremenek35537662007-12-05 22:08:43 +000062 {
63 // Pretty-print the decls to a temp file.
Ted Kremenekea75c552007-11-28 21:32:21 +000064 std::ofstream DeclPP(FNameDeclPrint.c_str());
65 assert (DeclPP && "Could not open file for printing out decls.");
Ted Kremenekee533642007-12-20 19:47:16 +000066 llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
Ted Kremeneke7201982007-11-13 22:56:10 +000067
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000068 for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I)
Ted Kremenek35537662007-12-05 22:08:43 +000069 FilePrinter->HandleTopLevelDecl(*I);
Ted Kremenek7a1f4db2007-11-10 02:07:12 +000070 }
71
Ted Kremenek35537662007-12-05 22:08:43 +000072 // Serialize the translation unit.
Ted Kremenekc1e9dea2008-04-23 16:25:39 +000073 return EmitASTBitcodeFile(*TU,Filename);
Ted Kremenekbfa82c42007-10-16 23:37:27 +000074}
75
Ted Kremenek35537662007-12-05 22:08:43 +000076bool SerializationTest::Deserialize(llvm::sys::Path& Filename,
Ted Kremenek79a2a262007-11-28 19:21:47 +000077 llvm::sys::Path& FNameDeclPrint) {
Ted Kremenek018b3952007-11-06 19:50:53 +000078
Ted Kremenek35537662007-12-05 22:08:43 +000079 // Deserialize the translation unit.
Ted Kremeneke7d07d12008-06-04 15:55:15 +000080 TranslationUnit* NewTU = ReadASTBitcodeFile(Filename, FMgr);
Ted Kremenekb899ad22007-11-14 17:46:35 +000081
Ted Kremenek35537662007-12-05 22:08:43 +000082 if (!NewTU)
83 return false;
Ted Kremenekb899ad22007-11-14 17:46:35 +000084
Ted Kremenek35537662007-12-05 22:08:43 +000085 {
86 // Pretty-print the deserialized decls to a temp file.
87 std::ofstream DeclPP(FNameDeclPrint.c_str());
88 assert (DeclPP && "Could not open file for printing out decls.");
Ted Kremenekee533642007-12-20 19:47:16 +000089 llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
Ted Kremenek35537662007-12-05 22:08:43 +000090
91 for (TranslationUnit::iterator I=NewTU->begin(), E=NewTU->end(); I!=E; ++I)
92 FilePrinter->HandleTopLevelDecl(*I);
Ted Kremenekc4ca2882007-12-03 22:48:14 +000093 }
Sam Bishop4ccea1a2008-04-03 05:03:34 +000094
95 delete NewTU;
Ted Kremenek018b3952007-11-06 19:50:53 +000096
Ted Kremenek35537662007-12-05 22:08:43 +000097 return true;
Ted Kremenek018b3952007-11-06 19:50:53 +000098}
99
Ted Kremenek79a2a262007-11-28 19:21:47 +0000100namespace {
101 class TmpDirJanitor {
102 llvm::sys::Path& Dir;
103 public:
104 explicit TmpDirJanitor(llvm::sys::Path& dir) : Dir(dir) {}
105
106 ~TmpDirJanitor() {
107 llvm::cerr << "Removing: " << Dir.c_str() << '\n';
108 Dir.eraseFromDisk(true);
109 }
110 };
111}
Ted Kremenek018b3952007-11-06 19:50:53 +0000112
113SerializationTest::~SerializationTest() {
Ted Kremenek79a2a262007-11-28 19:21:47 +0000114
Ted Kremenek018b3952007-11-06 19:50:53 +0000115 std::string ErrMsg;
Ted Kremenek79a2a262007-11-28 19:21:47 +0000116 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenek018b3952007-11-06 19:50:53 +0000117
Ted Kremenek79a2a262007-11-28 19:21:47 +0000118 if (Dir.isEmpty()) {
Ted Kremenek018b3952007-11-06 19:50:53 +0000119 llvm::cerr << "Error: " << ErrMsg << "\n";
120 return;
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000121 }
122
Ted Kremenek79a2a262007-11-28 19:21:47 +0000123 TmpDirJanitor RemoveTmpOnExit(Dir);
124
125 llvm::sys::Path FNameDeclBefore = Dir;
126 FNameDeclBefore.appendComponent("test.decl_before.txt");
127
128 if (FNameDeclBefore.makeUnique(true,&ErrMsg)) {
Ted Kremenek018b3952007-11-06 19:50:53 +0000129 llvm::cerr << "Error: " << ErrMsg << "\n";
130 return;
131 }
Ted Kremenek4ac81212007-11-05 21:39:35 +0000132
Ted Kremenek79a2a262007-11-28 19:21:47 +0000133 llvm::sys::Path FNameDeclAfter = Dir;
134 FNameDeclAfter.appendComponent("test.decl_after.txt");
135
136 if (FNameDeclAfter.makeUnique(true,&ErrMsg)) {
137 llvm::cerr << "Error: " << ErrMsg << "\n";
138 return;
139 }
140
141 llvm::sys::Path ASTFilename = Dir;
142 ASTFilename.appendComponent("test.ast");
143
144 if (ASTFilename.makeUnique(true,&ErrMsg)) {
145 llvm::cerr << "Error: " << ErrMsg << "\n";
146 return;
147 }
148
149 // Serialize and then deserialize the ASTs.
Ted Kremenek35537662007-12-05 22:08:43 +0000150 bool status = Serialize(ASTFilename, FNameDeclBefore);
151 assert (status && "Serialization failed.");
152 status = Deserialize(ASTFilename, FNameDeclAfter);
153 assert (status && "Deserialization failed.");
Ted Kremenek79a2a262007-11-28 19:21:47 +0000154
155 // Read both pretty-printed files and compare them.
156
157 using llvm::MemoryBuffer;
158
Ted Kremenekee533642007-12-20 19:47:16 +0000159 llvm::OwningPtr<MemoryBuffer>
Chris Lattner35de5122008-04-01 18:04:30 +0000160 MBufferSer(MemoryBuffer::getFile(FNameDeclBefore.c_str()));
Ted Kremenek79a2a262007-11-28 19:21:47 +0000161
162 if(!MBufferSer) {
163 llvm::cerr << "ERROR: Cannot read pretty-printed file (pre-pickle).\n";
164 return;
165 }
166
Ted Kremenekee533642007-12-20 19:47:16 +0000167 llvm::OwningPtr<MemoryBuffer>
Chris Lattner35de5122008-04-01 18:04:30 +0000168 MBufferDSer(MemoryBuffer::getFile(FNameDeclAfter.c_str()));
Ted Kremenek79a2a262007-11-28 19:21:47 +0000169
170 if(!MBufferDSer) {
171 llvm::cerr << "ERROR: Cannot read pretty-printed file (post-pickle).\n";
172 return;
173 }
174
175 const char *p1 = MBufferSer->getBufferStart();
176 const char *e1 = MBufferSer->getBufferEnd();
177 const char *p2 = MBufferDSer->getBufferStart();
178 const char *e2 = MBufferDSer->getBufferEnd();
179
180 if (MBufferSer->getBufferSize() == MBufferDSer->getBufferSize())
181 for ( ; p1 != e1 ; ++p1, ++p2 )
182 if (*p1 != *p2) break;
183
184 if (p1 != e1 || p2 != e2 )
185 llvm::cerr << "ERROR: Pretty-printed files are not the same.\n";
186 else
187 llvm::cerr << "SUCCESS: Pretty-printed files are the same.\n";
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000188}