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 | |
| 17 | #include "ASTConsumers.h" |
| 18 | #include "clang/AST/AST.h" |
| 19 | #include "clang/AST/ASTConsumer.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "llvm/System/Path.h" |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | a2bfb91 | 2007-10-24 19:06:02 +0000 | [diff] [blame] | 24 | #include "llvm/Bitcode/Serialize.h" |
| 25 | #include "llvm/Bitcode/Deserialize.h" |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 27 | #include <list> |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 29 | using namespace clang; |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 30 | using llvm::sys::TimeValue; |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // Utility classes |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 36 | namespace { |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 38 | template<typename T> class Janitor { |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 39 | T* Obj; |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 40 | public: |
| 41 | explicit Janitor(T* obj) : Obj(obj) {} |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 42 | ~Janitor() { delete Obj; } |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 43 | operator T*() const { return Obj; } |
| 44 | T* operator->() { return Obj; } |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 47 | class FileSP { |
| 48 | FILE* f; |
| 49 | public: |
| 50 | FileSP(const llvm::sys::Path& fname, const char* mode = "wb") |
| 51 | : f(fopen(fname.c_str(),mode)) {} |
| 52 | |
| 53 | ~FileSP() { if (f) fclose(f); } |
| 54 | |
| 55 | operator FILE*() const { return f; } |
| 56 | private: |
| 57 | void operator=(const FileSP& RHS) {} |
| 58 | FileSP(const FileSP& RHS) {} |
| 59 | }; |
| 60 | |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | // Driver code. |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 65 | class SerializationTest : public ASTConsumer { |
| 66 | ASTContext* Context; |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 67 | std::list<Decl*> Decls; |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 69 | enum { BasicMetadataBlock, |
| 70 | ASTContextBlock, |
| 71 | DeclsBlock }; |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 72 | |
| 73 | public: |
| 74 | SerializationTest() : Context(NULL) {}; |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 75 | ~SerializationTest(); |
| 76 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 77 | virtual void Initialize(ASTContext& context, unsigned) { |
| 78 | Context = &context; |
| 79 | } |
| 80 | |
| 81 | virtual void HandleTopLevelDecl(Decl *D) { |
| 82 | Decls.push_back(D); |
| 83 | } |
| 84 | |
| 85 | private: |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 86 | void Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint); |
| 87 | void Deserialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint); |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 90 | } // end anonymous namespace |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 92 | ASTConsumer* clang::CreateSerializationTest() { |
| 93 | return new SerializationTest(); |
| 94 | } |
| 95 | |
| 96 | static void WritePreamble(llvm::BitstreamWriter& Stream) { |
| 97 | Stream.Emit((unsigned)'B', 8); |
| 98 | Stream.Emit((unsigned)'C', 8); |
| 99 | Stream.Emit(0xC, 4); |
| 100 | Stream.Emit(0xF, 4); |
| 101 | Stream.Emit(0xE, 4); |
| 102 | Stream.Emit(0x0, 4); |
| 103 | } |
| 104 | |
Ted Kremenek | 07c0fd9 | 2007-11-06 23:52:19 +0000 | [diff] [blame] | 105 | static bool ReadPreamble(llvm::BitstreamReader& Stream) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 106 | return Stream.Read(8) != 'B' || |
| 107 | Stream.Read(8) != 'C' || |
| 108 | Stream.Read(4) != 0xC || |
| 109 | Stream.Read(4) != 0xF || |
| 110 | Stream.Read(4) != 0xE || |
| 111 | Stream.Read(4) != 0x0; |
| 112 | } |
| 113 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 114 | void SerializationTest::Serialize(llvm::sys::Path& Filename, |
| 115 | llvm::sys::Path& FNameDeclPrint) { |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 117 | // Reserve 256K for bitstream buffer. |
| 118 | std::vector<unsigned char> Buffer; |
| 119 | Buffer.reserve(256*1024); |
| 120 | |
| 121 | // Create bitstream and write preamble. |
| 122 | llvm::BitstreamWriter Stream(Buffer); |
| 123 | WritePreamble(Stream); |
| 124 | |
| 125 | // Create serializer. |
| 126 | llvm::Serializer Sezr(Stream); |
| 127 | |
| 128 | // ===---------------------------------------------------===/ |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 129 | // Serialize the top-level decls. |
| 130 | // ===---------------------------------------------------===/ |
| 131 | |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 132 | Sezr.EnterBlock(DeclsBlock); |
Ted Kremenek | e720198 | 2007-11-13 22:56:10 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 134 | { // Create a printer to "consume" our deserialized ASTS. |
| 135 | |
| 136 | Janitor<ASTConsumer> Printer(CreateASTPrinter()); |
| 137 | FileSP DeclFP(FNameDeclPrint,"w"); |
| 138 | assert (DeclFP && "Could not open file for printing out decls."); |
| 139 | Janitor<ASTConsumer> FilePrinter(CreateASTPrinter(DeclFP)); |
Ted Kremenek | e720198 | 2007-11-13 22:56:10 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 141 | for (std::list<Decl*>::iterator I=Decls.begin(), E=Decls.end(); I!=E; ++I) { |
| 142 | llvm::cerr << "Serializing: Decl.\n"; |
| 143 | |
| 144 | Printer->HandleTopLevelDecl(*I); |
| 145 | FilePrinter->HandleTopLevelDecl(*I); |
| 146 | |
| 147 | Sezr.EmitOwnedPtr(*I); |
| 148 | } |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | Sezr.ExitBlock(); |
| 152 | |
| 153 | // ===---------------------------------------------------===/ |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 154 | // Serialize the "Translation Unit" metadata. |
| 155 | // ===---------------------------------------------------===/ |
| 156 | |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 157 | // Emit ASTContext. |
| 158 | Sezr.EnterBlock(ASTContextBlock); |
| 159 | llvm::cerr << "Serializing: ASTContext.\n"; |
| 160 | Sezr.EmitOwnedPtr(Context); |
| 161 | Sezr.ExitBlock(); |
| 162 | |
| 163 | |
| 164 | Sezr.EnterBlock(BasicMetadataBlock); |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 165 | |
| 166 | // "Fake" emit the SourceManager. |
| 167 | llvm::cerr << "Faux-serializing: SourceManager.\n"; |
| 168 | Sezr.EmitPtr(&Context->SourceMgr); |
| 169 | |
| 170 | // "Fake" emit the Target. |
| 171 | llvm::cerr << "Faux-serializing: Target.\n"; |
| 172 | Sezr.EmitPtr(&Context->Target); |
| 173 | |
| 174 | // "Fake" emit Selectors. |
| 175 | llvm::cerr << "Faux-serializing: Selectors.\n"; |
| 176 | Sezr.EmitPtr(&Context->Selectors); |
| 177 | |
| 178 | // Emit the Identifier Table. |
| 179 | llvm::cerr << "Serializing: IdentifierTable.\n"; |
| 180 | Sezr.EmitOwnedPtr(&Context->Idents); |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 182 | Sezr.ExitBlock(); |
| 183 | |
| 184 | // ===---------------------------------------------------===/ |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 185 | // Finalize serialization: write the bits to disk. |
| 186 | { |
| 187 | FileSP fp(Filename); |
| 188 | |
| 189 | if (fp) |
| 190 | fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp); |
| 191 | else { |
| 192 | llvm::cerr << "Error: Cannot open " << Filename.c_str() << "\n"; |
| 193 | return; |
| 194 | } |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 195 | } |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 196 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 197 | llvm::cerr << "Commited bitstream to disk: " << Filename.c_str() << "\n"; |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 201 | void SerializationTest::Deserialize(llvm::sys::Path& Filename, |
| 202 | llvm::sys::Path& FNameDeclPrint) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 203 | |
| 204 | // Create the memory buffer that contains the contents of the file. |
| 205 | |
| 206 | using llvm::MemoryBuffer; |
| 207 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 208 | Janitor<MemoryBuffer> MBuffer(MemoryBuffer::getFile(Filename.c_str(), |
| 209 | strlen(Filename.c_str()))); |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 211 | if(!MBuffer) { |
| 212 | llvm::cerr << "ERROR: Cannot read file for deserialization.\n"; |
| 213 | return; |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 214 | } |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 215 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 216 | // Check if the file is of the proper length. |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 217 | if (MBuffer->getBufferSize() & 0x3) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 218 | llvm::cerr << "ERROR: AST file length should be a multiple of 4 bytes.\n"; |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 219 | return; |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 222 | // Create the bitstream reader. |
| 223 | unsigned char *BufPtr = (unsigned char *) MBuffer->getBufferStart(); |
| 224 | llvm::BitstreamReader Stream(BufPtr,BufPtr+MBuffer->getBufferSize()); |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 226 | // Sniff for the signature in the bitcode file. |
Ted Kremenek | 07c0fd9 | 2007-11-06 23:52:19 +0000 | [diff] [blame] | 227 | if (ReadPreamble(Stream)) { |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 228 | llvm::cerr << "ERROR: Invalid AST-bitcode signature.\n"; |
| 229 | return; |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Create the deserializer. |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 233 | llvm::Deserializer Dezr(Stream); |
| 234 | |
| 235 | // ===---------------------------------------------------===/ |
| 236 | // Deserialize the "Translation Unit" metadata. |
| 237 | // ===---------------------------------------------------===/ |
| 238 | |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 239 | // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock |
| 240 | // (which will appear earlier) and record its location. |
| 241 | |
| 242 | bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock); |
| 243 | assert (FoundBlock); |
| 244 | |
| 245 | llvm::Deserializer::Location ASTContextBlockLoc = |
| 246 | Dezr.getCurrentBlockLocation(); |
| 247 | |
| 248 | FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock); |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 249 | assert (FoundBlock); |
| 250 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 251 | // "Fake" read the SourceManager. |
| 252 | llvm::cerr << "Faux-Deserializing: SourceManager.\n"; |
| 253 | Dezr.RegisterPtr(&Context->SourceMgr); |
| 254 | |
| 255 | // "Fake" read the TargetInfo. |
| 256 | llvm::cerr << "Faux-Deserializing: Target.\n"; |
| 257 | Dezr.RegisterPtr(&Context->Target); |
| 258 | |
| 259 | // "Fake" read the Selectors. |
| 260 | llvm::cerr << "Faux-Deserializing: Selectors.\n"; |
| 261 | Dezr.RegisterPtr(&Context->Selectors); |
| 262 | |
| 263 | // Read the identifier table. |
| 264 | llvm::cerr << "Deserializing: IdentifierTable\n"; |
| 265 | Dezr.ReadOwnedPtr<IdentifierTable>(); |
| 266 | |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 267 | // Now jump back to ASTContextBlock and read the ASTContext. |
| 268 | Dezr.JumpTo(ASTContextBlockLoc); |
| 269 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 270 | // Read the ASTContext. |
| 271 | llvm::cerr << "Deserializing: ASTContext.\n"; |
| 272 | Dezr.ReadOwnedPtr<ASTContext>(); |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 273 | |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 274 | // "Rewind" the stream. Find the block with the serialized top-level decls. |
| 275 | Dezr.Rewind(); |
Ted Kremenek | b899ad2 | 2007-11-14 17:46:35 +0000 | [diff] [blame] | 276 | FoundBlock = Dezr.SkipToBlock(DeclsBlock); |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 277 | assert (FoundBlock); |
| 278 | llvm::Deserializer::Location DeclBlockLoc = Dezr.getCurrentBlockLocation(); |
| 279 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 280 | // Create a printer to "consume" our deserialized ASTS. |
| 281 | ASTConsumer* Printer = CreateASTPrinter(); |
| 282 | Janitor<ASTConsumer> PrinterJanitor(Printer); |
| 283 | FileSP DeclFP(FNameDeclPrint,"w"); |
| 284 | assert (DeclFP && "Could not open file for printing out decls."); |
| 285 | Janitor<ASTConsumer> FilePrinter(CreateASTPrinter(DeclFP)); |
| 286 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 287 | // The remaining objects in the file are top-level decls. |
Ted Kremenek | 7a1f4db | 2007-11-10 02:07:12 +0000 | [diff] [blame] | 288 | while (!Dezr.FinishedBlock(DeclBlockLoc)) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 289 | llvm::cerr << "Deserializing: Decl.\n"; |
| 290 | Decl* decl = Dezr.ReadOwnedPtr<Decl>(); |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 291 | Printer->HandleTopLevelDecl(decl); |
| 292 | FilePrinter->HandleTopLevelDecl(decl); |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 296 | namespace { |
| 297 | class TmpDirJanitor { |
| 298 | llvm::sys::Path& Dir; |
| 299 | public: |
| 300 | explicit TmpDirJanitor(llvm::sys::Path& dir) : Dir(dir) {} |
| 301 | |
| 302 | ~TmpDirJanitor() { |
| 303 | llvm::cerr << "Removing: " << Dir.c_str() << '\n'; |
| 304 | Dir.eraseFromDisk(true); |
| 305 | } |
| 306 | }; |
| 307 | } |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 308 | |
| 309 | SerializationTest::~SerializationTest() { |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 310 | |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 311 | std::string ErrMsg; |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 312 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 314 | if (Dir.isEmpty()) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 315 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 316 | return; |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 319 | TmpDirJanitor RemoveTmpOnExit(Dir); |
| 320 | |
| 321 | llvm::sys::Path FNameDeclBefore = Dir; |
| 322 | FNameDeclBefore.appendComponent("test.decl_before.txt"); |
| 323 | |
| 324 | if (FNameDeclBefore.makeUnique(true,&ErrMsg)) { |
Ted Kremenek | 018b395 | 2007-11-06 19:50:53 +0000 | [diff] [blame] | 325 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 326 | return; |
| 327 | } |
Ted Kremenek | 4ac8121 | 2007-11-05 21:39:35 +0000 | [diff] [blame] | 328 | |
Ted Kremenek | 79a2a26 | 2007-11-28 19:21:47 +0000 | [diff] [blame^] | 329 | llvm::sys::Path FNameDeclAfter = Dir; |
| 330 | FNameDeclAfter.appendComponent("test.decl_after.txt"); |
| 331 | |
| 332 | if (FNameDeclAfter.makeUnique(true,&ErrMsg)) { |
| 333 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | llvm::sys::Path ASTFilename = Dir; |
| 338 | ASTFilename.appendComponent("test.ast"); |
| 339 | |
| 340 | if (ASTFilename.makeUnique(true,&ErrMsg)) { |
| 341 | llvm::cerr << "Error: " << ErrMsg << "\n"; |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | // Serialize and then deserialize the ASTs. |
| 346 | Serialize(ASTFilename, FNameDeclBefore); |
| 347 | Deserialize(ASTFilename, FNameDeclAfter); |
| 348 | |
| 349 | // Read both pretty-printed files and compare them. |
| 350 | |
| 351 | using llvm::MemoryBuffer; |
| 352 | |
| 353 | Janitor<MemoryBuffer> |
| 354 | MBufferSer(MemoryBuffer::getFile(FNameDeclBefore.c_str(), |
| 355 | strlen(FNameDeclBefore.c_str()))); |
| 356 | |
| 357 | if(!MBufferSer) { |
| 358 | llvm::cerr << "ERROR: Cannot read pretty-printed file (pre-pickle).\n"; |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | Janitor<MemoryBuffer> |
| 363 | MBufferDSer(MemoryBuffer::getFile(FNameDeclAfter.c_str(), |
| 364 | strlen(FNameDeclAfter.c_str()))); |
| 365 | |
| 366 | if(!MBufferDSer) { |
| 367 | llvm::cerr << "ERROR: Cannot read pretty-printed file (post-pickle).\n"; |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | const char *p1 = MBufferSer->getBufferStart(); |
| 372 | const char *e1 = MBufferSer->getBufferEnd(); |
| 373 | const char *p2 = MBufferDSer->getBufferStart(); |
| 374 | const char *e2 = MBufferDSer->getBufferEnd(); |
| 375 | |
| 376 | if (MBufferSer->getBufferSize() == MBufferDSer->getBufferSize()) |
| 377 | for ( ; p1 != e1 ; ++p1, ++p2 ) |
| 378 | if (*p1 != *p2) break; |
| 379 | |
| 380 | if (p1 != e1 || p2 != e2 ) |
| 381 | llvm::cerr << "ERROR: Pretty-printed files are not the same.\n"; |
| 382 | else |
| 383 | llvm::cerr << "SUCCESS: Pretty-printed files are the same.\n"; |
Ted Kremenek | c637e6b | 2007-10-23 22:18:37 +0000 | [diff] [blame] | 384 | } |