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 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 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 | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Streams.h" |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 25 | #include <fstream> |
Chris Lattner | 87cf5ac | 2008-03-10 17:04:53 +0000 | [diff] [blame] | 26 | #include <cstring> |
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 { |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame^] | 36 | TranslationUnit* TU; |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 37 | Diagnostic &Diags; |
| 38 | FileManager &FMgr; |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 39 | public: |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame^] | 40 | SerializationTest(Diagnostic &d, FileManager& fmgr) |
| 41 | : TU(0), Diags(d), FMgr(fmgr) {} |
Ted Kremenek | dca2466 | 2007-12-05 00:26:13 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 43 | ~SerializationTest(); |
| 44 | |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame^] | 45 | virtual void InitializeTU(TranslationUnit& tu) { TU = &tu; } |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 47 | private: |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 48 | bool Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint); |
| 49 | bool Deserialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint); |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 52 | } // end anonymous namespace |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | dca2466 | 2007-12-05 00:26:13 +0000 | [diff] [blame] | 54 | ASTConsumer* |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame^] | 55 | clang::CreateSerializationTest(Diagnostic &Diags, FileManager& FMgr) { |
| 56 | return new SerializationTest(Diags, FMgr); |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 60 | bool SerializationTest::Serialize(llvm::sys::Path& Filename, |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 61 | llvm::sys::Path& FNameDeclPrint) { |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 62 | { |
| 63 | // Pretty-print the decls to a temp file. |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 64 | std::ofstream DeclPP(FNameDeclPrint.c_str()); |
| 65 | assert (DeclPP && "Could not open file for printing out decls."); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 66 | llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP)); |
Ted Kremenek | e720198 | 2007-11-13 22:56:10 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 68 | for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I) |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 69 | FilePrinter->HandleTopLevelDecl(*I); |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 72 | // Serialize the translation unit. |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 73 | return EmitASTBitcodeFile(*TU,Filename); |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 76 | bool SerializationTest::Deserialize(llvm::sys::Path& Filename, |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 77 | llvm::sys::Path& FNameDeclPrint) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 79 | // Deserialize the translation unit. |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame^] | 80 | TranslationUnit* NewTU = ReadASTBitcodeFile(Filename, FMgr); |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 82 | if (!NewTU) |
| 83 | return false; |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 84 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 85 | { |
| 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 Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 89 | llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP)); |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 90 | |
| 91 | for (TranslationUnit::iterator I=NewTU->begin(), E=NewTU->end(); I!=E; ++I) |
| 92 | FilePrinter->HandleTopLevelDecl(*I); |
Ted Kremenek | c4ca288 | 2007-12-03 22:48:14 +0000 | [diff] [blame] | 93 | } |
Sam Bishop | 4ccea1a | 2008-04-03 05:03:34 +0000 | [diff] [blame] | 94 | |
| 95 | delete NewTU; |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 97 | return true; |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 100 | namespace { |
| 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 Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 112 | |
| 113 | SerializationTest::~SerializationTest() { |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 115 | std::string ErrMsg; |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 116 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 118 | if (Dir.isEmpty()) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 119 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 120 | return; |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 123 | 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 Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 129 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 130 | return; |
| 131 | } |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 133 | 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 Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 150 | bool status = Serialize(ASTFilename, FNameDeclBefore); |
| 151 | assert (status && "Serialization failed."); |
| 152 | status = Deserialize(ASTFilename, FNameDeclAfter); |
| 153 | assert (status && "Deserialization failed."); |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 154 | |
| 155 | // Read both pretty-printed files and compare them. |
| 156 | |
| 157 | using llvm::MemoryBuffer; |
| 158 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 159 | llvm::OwningPtr<MemoryBuffer> |
Chris Lattner | 35de512 | 2008-04-01 18:04:30 +0000 | [diff] [blame] | 160 | MBufferSer(MemoryBuffer::getFile(FNameDeclBefore.c_str())); |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 161 | |
| 162 | if(!MBufferSer) { |
| 163 | llvm::cerr << "ERROR: Cannot read pretty-printed file (pre-pickle).\n"; |
| 164 | return; |
| 165 | } |
| 166 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 167 | llvm::OwningPtr<MemoryBuffer> |
Chris Lattner | 35de512 | 2008-04-01 18:04:30 +0000 | [diff] [blame] | 168 | MBufferDSer(MemoryBuffer::getFile(FNameDeclAfter.c_str())); |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 169 | |
| 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 Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 188 | } |