blob: e489a19ff83ff0c180dd642befd3d6a975fd88a2 [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"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Ted Kremenekc4ca2882007-12-03 22:48:14 +000020#include "clang.h"
Ted Kremenek35537662007-12-05 22:08:43 +000021#include "ASTConsumers.h"
Ted Kremenek525fdbc2007-12-18 21:36:21 +000022#include "clang/AST/TranslationUnit.h"
Ted Kremenekc637e6b2007-10-23 22:18:37 +000023#include "llvm/Support/MemoryBuffer.h"
Ted Kremenekee533642007-12-20 19:47:16 +000024#include "llvm/ADT/OwningPtr.h"
Ted Kremenek35537662007-12-05 22:08:43 +000025#include "llvm/Support/Streams.h"
Ted Kremenekea75c552007-11-28 21:32:21 +000026#include <fstream>
Chris Lattner87cf5ac2008-03-10 17:04:53 +000027#include <cstring>
Ted Kremenekbfa82c42007-10-16 23:37:27 +000028using namespace clang;
Ted Kremenekbfa82c42007-10-16 23:37:27 +000029
Ted Kremenek79a2a262007-11-28 19:21:47 +000030//===----------------------------------------------------------------------===//
Ted Kremenek79a2a262007-11-28 19:21:47 +000031// Driver code.
32//===----------------------------------------------------------------------===//
33
Ted Kremenek35537662007-12-05 22:08:43 +000034namespace {
Ted Kremenek4ac81212007-11-05 21:39:35 +000035
Ted Kremenek35537662007-12-05 22:08:43 +000036class SerializationTest : public ASTConsumer {
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)
Ted Kremeneke452e0f2008-08-08 04:15:52 +000041 : Diags(d), FMgr(fmgr) {}
Ted Kremenekdca24662007-12-05 00:26:13 +000042
Ted Kremeneke452e0f2008-08-08 04:15:52 +000043 ~SerializationTest() {}
44
45 virtual void HandleTranslationUnit(TranslationUnit& TU);
Ted Kremenek63ea8632007-12-19 19:27:38 +000046
Ted Kremenek018b3952007-11-06 19:50:53 +000047private:
Ted Kremeneke452e0f2008-08-08 04:15:52 +000048 bool Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint,
49 TranslationUnit& TU);
50
Ted Kremenek35537662007-12-05 22:08:43 +000051 bool Deserialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint);
Ted Kremenek4ac81212007-11-05 21:39:35 +000052};
53
Ted Kremenekc637e6b2007-10-23 22:18:37 +000054} // end anonymous namespace
Ted Kremenekbfa82c42007-10-16 23:37:27 +000055
Ted Kremenekdca24662007-12-05 00:26:13 +000056ASTConsumer*
Ted Kremeneke7d07d12008-06-04 15:55:15 +000057clang::CreateSerializationTest(Diagnostic &Diags, FileManager& FMgr) {
58 return new SerializationTest(Diags, FMgr);
Ted Kremenek018b3952007-11-06 19:50:53 +000059}
60
Ted Kremenek018b3952007-11-06 19:50:53 +000061
Ted Kremenek35537662007-12-05 22:08:43 +000062bool SerializationTest::Serialize(llvm::sys::Path& Filename,
Ted Kremeneke452e0f2008-08-08 04:15:52 +000063 llvm::sys::Path& FNameDeclPrint,
64 TranslationUnit& TU) {
Ted Kremenek35537662007-12-05 22:08:43 +000065 {
66 // Pretty-print the decls to a temp file.
Ted Kremeneka95d3752008-09-13 05:16:45 +000067 std::string Err;
Daniel Dunbar26fb2722008-11-13 05:09:21 +000068 llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), true, Err);
Ted Kremeneka95d3752008-09-13 05:16:45 +000069 assert (Err.empty() && "Could not open file for printing out decls.");
Ted Kremenekee533642007-12-20 19:47:16 +000070 llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
Ted Kremeneke7201982007-11-13 22:56:10 +000071
Ted Kremeneke452e0f2008-08-08 04:15:52 +000072 for (TranslationUnit::iterator I=TU.begin(), E=TU.end(); I!=E; ++I)
Ted Kremenek35537662007-12-05 22:08:43 +000073 FilePrinter->HandleTopLevelDecl(*I);
Ted Kremenek7a1f4db2007-11-10 02:07:12 +000074 }
75
Ted Kremenek35537662007-12-05 22:08:43 +000076 // Serialize the translation unit.
Ted Kremeneke452e0f2008-08-08 04:15:52 +000077 return EmitASTBitcodeFile(TU,Filename);
Ted Kremenekbfa82c42007-10-16 23:37:27 +000078}
79
Ted Kremenek35537662007-12-05 22:08:43 +000080bool SerializationTest::Deserialize(llvm::sys::Path& Filename,
Ted Kremenek79a2a262007-11-28 19:21:47 +000081 llvm::sys::Path& FNameDeclPrint) {
Ted Kremenek018b3952007-11-06 19:50:53 +000082
Ted Kremenek35537662007-12-05 22:08:43 +000083 // Deserialize the translation unit.
Ted Kremeneke7d07d12008-06-04 15:55:15 +000084 TranslationUnit* NewTU = ReadASTBitcodeFile(Filename, FMgr);
Ted Kremenekb899ad22007-11-14 17:46:35 +000085
Ted Kremenek35537662007-12-05 22:08:43 +000086 if (!NewTU)
87 return false;
Ted Kremenekb899ad22007-11-14 17:46:35 +000088
Ted Kremeneke452e0f2008-08-08 04:15:52 +000089 {
Ted Kremenek35537662007-12-05 22:08:43 +000090 // Pretty-print the deserialized decls to a temp file.
Ted Kremeneka95d3752008-09-13 05:16:45 +000091 std::string Err;
Daniel Dunbar26fb2722008-11-13 05:09:21 +000092 llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), true, Err);
Ted Kremeneka95d3752008-09-13 05:16:45 +000093 assert (Err.empty() && "Could not open file for printing out decls.");
Ted Kremenekee533642007-12-20 19:47:16 +000094 llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP));
Ted Kremenek35537662007-12-05 22:08:43 +000095
96 for (TranslationUnit::iterator I=NewTU->begin(), E=NewTU->end(); I!=E; ++I)
97 FilePrinter->HandleTopLevelDecl(*I);
Ted Kremenekc4ca2882007-12-03 22:48:14 +000098 }
Sam Bishop4ccea1a2008-04-03 05:03:34 +000099
100 delete NewTU;
Ted Kremenek018b3952007-11-06 19:50:53 +0000101
Ted Kremenek35537662007-12-05 22:08:43 +0000102 return true;
Ted Kremenek018b3952007-11-06 19:50:53 +0000103}
104
Ted Kremenek79a2a262007-11-28 19:21:47 +0000105namespace {
106 class TmpDirJanitor {
107 llvm::sys::Path& Dir;
108 public:
109 explicit TmpDirJanitor(llvm::sys::Path& dir) : Dir(dir) {}
110
111 ~TmpDirJanitor() {
112 llvm::cerr << "Removing: " << Dir.c_str() << '\n';
113 Dir.eraseFromDisk(true);
114 }
115 };
116}
Ted Kremenek018b3952007-11-06 19:50:53 +0000117
Ted Kremeneke452e0f2008-08-08 04:15:52 +0000118void SerializationTest::HandleTranslationUnit(TranslationUnit& TU) {
Ted Kremenek79a2a262007-11-28 19:21:47 +0000119
Ted Kremenek018b3952007-11-06 19:50:53 +0000120 std::string ErrMsg;
Ted Kremenek79a2a262007-11-28 19:21:47 +0000121 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenek018b3952007-11-06 19:50:53 +0000122
Ted Kremenek79a2a262007-11-28 19:21:47 +0000123 if (Dir.isEmpty()) {
Ted Kremenek018b3952007-11-06 19:50:53 +0000124 llvm::cerr << "Error: " << ErrMsg << "\n";
125 return;
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000126 }
127
Ted Kremenek79a2a262007-11-28 19:21:47 +0000128 TmpDirJanitor RemoveTmpOnExit(Dir);
129
130 llvm::sys::Path FNameDeclBefore = Dir;
131 FNameDeclBefore.appendComponent("test.decl_before.txt");
132
133 if (FNameDeclBefore.makeUnique(true,&ErrMsg)) {
Ted Kremenek018b3952007-11-06 19:50:53 +0000134 llvm::cerr << "Error: " << ErrMsg << "\n";
135 return;
136 }
Ted Kremenek4ac81212007-11-05 21:39:35 +0000137
Ted Kremenek79a2a262007-11-28 19:21:47 +0000138 llvm::sys::Path FNameDeclAfter = Dir;
139 FNameDeclAfter.appendComponent("test.decl_after.txt");
140
141 if (FNameDeclAfter.makeUnique(true,&ErrMsg)) {
142 llvm::cerr << "Error: " << ErrMsg << "\n";
143 return;
144 }
145
146 llvm::sys::Path ASTFilename = Dir;
147 ASTFilename.appendComponent("test.ast");
148
149 if (ASTFilename.makeUnique(true,&ErrMsg)) {
150 llvm::cerr << "Error: " << ErrMsg << "\n";
151 return;
152 }
153
154 // Serialize and then deserialize the ASTs.
Ted Kremeneke452e0f2008-08-08 04:15:52 +0000155 bool status = Serialize(ASTFilename, FNameDeclBefore, TU);
Ted Kremenek35537662007-12-05 22:08:43 +0000156 assert (status && "Serialization failed.");
157 status = Deserialize(ASTFilename, FNameDeclAfter);
158 assert (status && "Deserialization failed.");
Ted Kremenek79a2a262007-11-28 19:21:47 +0000159
160 // Read both pretty-printed files and compare them.
161
162 using llvm::MemoryBuffer;
163
Ted Kremenekee533642007-12-20 19:47:16 +0000164 llvm::OwningPtr<MemoryBuffer>
Chris Lattner35de5122008-04-01 18:04:30 +0000165 MBufferSer(MemoryBuffer::getFile(FNameDeclBefore.c_str()));
Ted Kremenek79a2a262007-11-28 19:21:47 +0000166
167 if(!MBufferSer) {
168 llvm::cerr << "ERROR: Cannot read pretty-printed file (pre-pickle).\n";
169 return;
170 }
171
Ted Kremenekee533642007-12-20 19:47:16 +0000172 llvm::OwningPtr<MemoryBuffer>
Chris Lattner35de5122008-04-01 18:04:30 +0000173 MBufferDSer(MemoryBuffer::getFile(FNameDeclAfter.c_str()));
Ted Kremenek79a2a262007-11-28 19:21:47 +0000174
175 if(!MBufferDSer) {
176 llvm::cerr << "ERROR: Cannot read pretty-printed file (post-pickle).\n";
177 return;
178 }
179
180 const char *p1 = MBufferSer->getBufferStart();
181 const char *e1 = MBufferSer->getBufferEnd();
182 const char *p2 = MBufferDSer->getBufferStart();
183 const char *e2 = MBufferDSer->getBufferEnd();
184
185 if (MBufferSer->getBufferSize() == MBufferDSer->getBufferSize())
186 for ( ; p1 != e1 ; ++p1, ++p2 )
187 if (*p1 != *p2) break;
188
189 if (p1 != e1 || p2 != e2 )
190 llvm::cerr << "ERROR: Pretty-printed files are not the same.\n";
191 else
192 llvm::cerr << "SUCCESS: Pretty-printed files are the same.\n";
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000193}