Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 1 | //===--- 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 Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Ted Kremenek | 5656b78 | 2007-11-29 01:24:25 +0000 | [diff] [blame] | 18 | #include "clang/AST/CFG.h" |
Ted Kremenek | c4ca288 | 2007-12-03 22:48:14 +0000 | [diff] [blame] | 19 | #include "clang.h" |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 20 | #include "ASTConsumers.h" |
Ted Kremenek | 525fdbc | 2007-12-18 21:36:21 +0000 | [diff] [blame] | 21 | #include "clang/AST/TranslationUnit.h" |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/scoped_ptr.h" |
| 24 | #include "llvm/Support/Streams.h" |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 25 | #include <fstream> |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 27 | using namespace clang; |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 30 | // Driver code. |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 33 | namespace { |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 35 | class SerializationTest : public ASTConsumer { |
| 36 | TranslationUnit TU; |
| 37 | Diagnostic &Diags; |
| 38 | FileManager &FMgr; |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 39 | public: |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame^] | 40 | SerializationTest(const std::string& SourceFile, Diagnostic &d, |
| 41 | FileManager& fmgr, const LangOptions& LOpts) |
| 42 | : TU(SourceFile, LOpts), Diags(d), FMgr(fmgr) {} |
Ted Kremenek | dca2466 | 2007-12-05 00:26:13 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 44 | ~SerializationTest(); |
| 45 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 46 | virtual void Initialize(ASTContext& context, unsigned) { |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 47 | TU.setContext(&context); |
| 48 | } |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 50 | virtual void HandleTopLevelDecl(Decl *D) { |
| 51 | TU.AddTopLevelDecl(D); |
| 52 | } |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame^] | 53 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 54 | private: |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 55 | bool Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint); |
| 56 | bool Deserialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint); |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 59 | } // end anonymous namespace |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | dca2466 | 2007-12-05 00:26:13 +0000 | [diff] [blame] | 61 | ASTConsumer* |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame^] | 62 | clang::CreateSerializationTest(const std::string& SourceFile, Diagnostic &Diags, |
| 63 | FileManager& FMgr, const LangOptions &LOpts) { |
| 64 | return new SerializationTest(SourceFile,Diags,FMgr,LOpts); |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 68 | bool SerializationTest::Serialize(llvm::sys::Path& Filename, |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 69 | llvm::sys::Path& FNameDeclPrint) { |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 70 | { |
| 71 | // Pretty-print the decls to a temp file. |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 72 | std::ofstream DeclPP(FNameDeclPrint.c_str()); |
| 73 | assert (DeclPP && "Could not open file for printing out decls."); |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 74 | llvm::scoped_ptr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP)); |
Ted Kremenek | e720198 | 2007-11-13 22:56:10 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 76 | for (TranslationUnit::iterator I=TU.begin(), E=TU.end(); I!=E; ++I) |
| 77 | FilePrinter->HandleTopLevelDecl(*I); |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 80 | // Serialize the translation unit. |
Ted Kremenek | dca2927 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 81 | return EmitASTBitcodeFile(TU,Filename); |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 84 | bool SerializationTest::Deserialize(llvm::sys::Path& Filename, |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 85 | llvm::sys::Path& FNameDeclPrint) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 87 | // Deserialize the translation unit. |
Ted Kremenek | dca2927 | 2007-12-18 21:44:50 +0000 | [diff] [blame] | 88 | TranslationUnit* NewTU = ReadASTBitcodeFile(Filename,FMgr); |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 90 | if (!NewTU) |
| 91 | return false; |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 93 | { |
| 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 Kremenek | c4ca288 | 2007-12-03 22:48:14 +0000 | [diff] [blame] | 101 | } |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 103 | return true; |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 106 | namespace { |
| 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 Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 118 | |
| 119 | SerializationTest::~SerializationTest() { |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 121 | std::string ErrMsg; |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 122 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 124 | if (Dir.isEmpty()) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 125 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 126 | return; |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 129 | 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 Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 135 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 136 | return; |
| 137 | } |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 139 | 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 Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 156 | bool status = Serialize(ASTFilename, FNameDeclBefore); |
| 157 | assert (status && "Serialization failed."); |
| 158 | status = Deserialize(ASTFilename, FNameDeclAfter); |
| 159 | assert (status && "Deserialization failed."); |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 160 | |
| 161 | // Read both pretty-printed files and compare them. |
| 162 | |
| 163 | using llvm::MemoryBuffer; |
| 164 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 165 | llvm::scoped_ptr<MemoryBuffer> |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 166 | 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 Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 174 | llvm::scoped_ptr<MemoryBuffer> |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 175 | 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 Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 196 | } |