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" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Ted Kremenek | c4ca288 | 2007-12-03 22:48:14 +0000 | [diff] [blame] | 20 | #include "clang.h" |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 21 | #include "ASTConsumers.h" |
Ted Kremenek | 525fdbc | 2007-12-18 21:36:21 +0000 | [diff] [blame] | 22 | #include "clang/AST/TranslationUnit.h" |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Streams.h" |
Ted Kremenek | ea75c55 | 2007-11-28 21:32:21 +0000 | [diff] [blame] | 26 | #include <fstream> |
Chris Lattner | 87cf5ac | 2008-03-10 17:04:53 +0000 | [diff] [blame] | 27 | #include <cstring> |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 28 | using namespace clang; |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 31 | // Driver code. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 34 | namespace { |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 36 | class SerializationTest : public ASTConsumer { |
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) |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 41 | : Diags(d), FMgr(fmgr) {} |
Ted Kremenek | dca2466 | 2007-12-05 00:26:13 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 43 | ~SerializationTest() {} |
| 44 | |
| 45 | virtual void HandleTranslationUnit(TranslationUnit& 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 | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 48 | bool Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint, |
| 49 | TranslationUnit& TU); |
| 50 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 51 | bool Deserialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint); |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 54 | } // end anonymous namespace |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | dca2466 | 2007-12-05 00:26:13 +0000 | [diff] [blame] | 56 | ASTConsumer* |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 57 | clang::CreateSerializationTest(Diagnostic &Diags, FileManager& FMgr) { |
| 58 | return new SerializationTest(Diags, FMgr); |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 61 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 62 | bool SerializationTest::Serialize(llvm::sys::Path& Filename, |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 63 | llvm::sys::Path& FNameDeclPrint, |
| 64 | TranslationUnit& TU) { |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 65 | { |
| 66 | // Pretty-print the decls to a temp file. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 67 | std::string Err; |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 68 | llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), true, Err); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 69 | assert (Err.empty() && "Could not open file for printing out decls."); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 70 | llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP)); |
Ted Kremenek | e720198 | 2007-11-13 22:56:10 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 72 | for (TranslationUnit::iterator I=TU.begin(), E=TU.end(); I!=E; ++I) |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 73 | FilePrinter->HandleTopLevelDecl(*I); |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 76 | // Serialize the translation unit. |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 77 | return EmitASTBitcodeFile(TU,Filename); |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 80 | bool SerializationTest::Deserialize(llvm::sys::Path& Filename, |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 81 | llvm::sys::Path& FNameDeclPrint) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 83 | // Deserialize the translation unit. |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 84 | TranslationUnit* NewTU = ReadASTBitcodeFile(Filename, FMgr); |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 86 | if (!NewTU) |
| 87 | return false; |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 89 | { |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 90 | // Pretty-print the deserialized decls to a temp file. |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 91 | std::string Err; |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 92 | llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), true, Err); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 93 | assert (Err.empty() && "Could not open file for printing out decls."); |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 94 | llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP)); |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 95 | |
| 96 | for (TranslationUnit::iterator I=NewTU->begin(), E=NewTU->end(); I!=E; ++I) |
| 97 | FilePrinter->HandleTopLevelDecl(*I); |
Ted Kremenek | c4ca288 | 2007-12-03 22:48:14 +0000 | [diff] [blame] | 98 | } |
Sam Bishop | 4ccea1a | 2008-04-03 05:03:34 +0000 | [diff] [blame] | 99 | |
| 100 | delete NewTU; |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 102 | return true; |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 105 | namespace { |
| 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 Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 118 | void SerializationTest::HandleTranslationUnit(TranslationUnit& TU) { |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 120 | std::string ErrMsg; |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 121 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 123 | if (Dir.isEmpty()) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 124 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 125 | return; |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 128 | 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 Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 134 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 135 | return; |
| 136 | } |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 138 | 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 Kremenek | e452e0f | 2008-08-08 04:15:52 +0000 | [diff] [blame] | 155 | bool status = Serialize(ASTFilename, FNameDeclBefore, TU); |
Ted Kremenek | 3553766 | 2007-12-05 22:08:43 +0000 | [diff] [blame] | 156 | assert (status && "Serialization failed."); |
| 157 | status = Deserialize(ASTFilename, FNameDeclAfter); |
| 158 | assert (status && "Deserialization failed."); |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 159 | |
| 160 | // Read both pretty-printed files and compare them. |
| 161 | |
| 162 | using llvm::MemoryBuffer; |
| 163 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 164 | llvm::OwningPtr<MemoryBuffer> |
Chris Lattner | 35de512 | 2008-04-01 18:04:30 +0000 | [diff] [blame] | 165 | MBufferSer(MemoryBuffer::getFile(FNameDeclBefore.c_str())); |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 166 | |
| 167 | if(!MBufferSer) { |
| 168 | llvm::cerr << "ERROR: Cannot read pretty-printed file (pre-pickle).\n"; |
| 169 | return; |
| 170 | } |
| 171 | |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 172 | llvm::OwningPtr<MemoryBuffer> |
Chris Lattner | 35de512 | 2008-04-01 18:04:30 +0000 | [diff] [blame] | 173 | MBufferDSer(MemoryBuffer::getFile(FNameDeclAfter.c_str())); |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame] | 174 | |
| 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 Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 193 | } |