| OLD | NEW |
| 1 //===--- Bitcode/NaCl/TestUtils/NaClBitcodeMunge.cpp - Bitcode Munger -----===// | 1 //===--- Bitcode/NaCl/TestUtils/NaClBitcodeMunge.cpp - Bitcode Munger -----===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // Bitcode writer/munger implementation for testing. | 10 // Bitcode writer/munger implementation for testing. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #include "llvm/Bitcode/NaCl/NaClBitcodeMunge.h" | 14 #include "llvm/Bitcode/NaCl/NaClBitcodeMunge.h" |
| 15 | 15 |
| 16 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" | 16 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" |
| 17 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" | 17 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
| 18 #include "llvm/Bitcode/NaCl/NaClBitstreamWriter.h" | 18 #include "llvm/Bitcode/NaCl/NaClBitstreamWriter.h" |
| 19 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" | 19 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
| 20 #include "llvm/IR/LLVMContext.h" |
| 21 #include "llvm/IR/Module.h" |
| 20 #include "llvm/Support/ErrorHandling.h" | 22 #include "llvm/Support/ErrorHandling.h" |
| 21 #include "llvm/Support/MemoryBuffer.h" | 23 #include "llvm/Support/MemoryBuffer.h" |
| 22 | 24 |
| 23 #include <memory> | 25 #include <memory> |
| 24 | 26 |
| 25 using namespace llvm; | 27 using namespace llvm; |
| 26 | 28 |
| 27 // For debugging. When true, shows each PNaCl record that is | 29 // For debugging. When true, shows each PNaCl record that is |
| 28 // emitted to the bitcode file. | 30 // emitted to the bitcode file. |
| 29 static bool DebugEmitRecord = false; | 31 static bool DebugEmitRecord = false; |
| 30 | 32 |
| 31 bool NaClBitcodeMunger::runTestWithFlags( | 33 void NaClBitcodeMunger::setupTest( |
| 32 const char *Name, const uint64_t Munges[], size_t MungesSize, | 34 const char *TestName, const uint64_t Munges[], size_t MungesSize, |
| 33 bool AddHeader, bool NoRecords, bool NoAssembly) { | 35 bool AddHeader) { |
| 34 assert(DumpStream == NULL && "Test run with DumpStream already defined"); | 36 assert(DumpStream == nullptr && "Test run with DumpStream already defined"); |
| 37 assert(MungedInput == nullptr && "Test run with MungedInput already defined"); |
| 35 FoundErrors = false; | 38 FoundErrors = false; |
| 36 DumpResults.clear(); // Throw away any previous results. | 39 DumpResults.clear(); // Throw away any previous results. |
| 37 std::string DumpBuffer; | 40 std::string DumpBuffer; |
| 38 raw_string_ostream MungedDumpStream(DumpResults); | 41 DumpStream = new raw_string_ostream(DumpResults); |
| 39 DumpStream = &MungedDumpStream; | |
| 40 SmallVector<char, 0> StreamBuffer; | 42 SmallVector<char, 0> StreamBuffer; |
| 41 StreamBuffer.reserve(256*1024); | 43 StreamBuffer.reserve(256*1024); |
| 42 NaClBitstreamWriter OutStream(StreamBuffer); | 44 NaClBitstreamWriter OutStream(StreamBuffer); |
| 43 Writer = &OutStream; | 45 Writer = &OutStream; |
| 44 | 46 |
| 45 if (DebugEmitRecord) { | 47 if (DebugEmitRecord) { |
| 46 errs() << "*** Run test: " << Name << "\n"; | 48 errs() << "*** Run test: " << TestName << "\n"; |
| 47 } | 49 } |
| 48 | 50 |
| 49 WriteBlockID = -1; | 51 WriteBlockID = -1; |
| 50 SetBID = -1; | 52 SetBID = -1; |
| 51 writeMungedData(Munges, MungesSize, AddHeader); | 53 writeMungedData(Munges, MungesSize, AddHeader); |
| 52 | 54 |
| 53 std::string OutBuffer; | 55 std::string OutBuffer; |
| 54 raw_string_ostream BitcodeStrm(OutBuffer); | 56 raw_string_ostream BitcodeStrm(OutBuffer); |
| 55 for (SmallVectorImpl<char>::const_iterator | 57 for (SmallVectorImpl<char>::const_iterator |
| 56 Iter = StreamBuffer.begin(), IterEnd = StreamBuffer.end(); | 58 Iter = StreamBuffer.begin(), IterEnd = StreamBuffer.end(); |
| 57 Iter != IterEnd; ++Iter) { | 59 Iter != IterEnd; ++Iter) { |
| 58 BitcodeStrm << *Iter; | 60 BitcodeStrm << *Iter; |
| 59 } | 61 } |
| 62 MungedInput = MemoryBuffer::getMemBufferCopy(BitcodeStrm.str(), TestName); |
| 63 } |
| 60 | 64 |
| 61 std::unique_ptr<MemoryBuffer> Input( | 65 void NaClBitcodeMunger::cleanupTest() { |
| 62 MemoryBuffer::getMemBufferCopy(BitcodeStrm.str(), Name)); | 66 delete MungedInput; |
| 63 if (NaClObjDump(Input.get(), *DumpStream, NoRecords, NoAssembly)) | 67 MungedInput = nullptr; |
| 64 FoundErrors = true; | 68 assert(DumpStream && "Dump stream removed before cleanup!"); |
| 65 DumpStream = NULL; | 69 DumpStream->flush(); |
| 66 Writer = NULL; | 70 delete DumpStream; |
| 67 MungedDumpStream.flush(); | 71 DumpStream = nullptr; |
| 68 return !FoundErrors; | 72 Writer = nullptr; |
| 69 } | 73 } |
| 70 | 74 |
| 71 // Return the next line of input (including eoln), starting from | 75 // Return the next line of input (including eoln), starting from |
| 72 // Pos. Then increment Pos past the end of that line. | 76 // Pos. Then increment Pos past the end of that line. |
| 73 static std::string getLine(const std::string &Input, size_t &Pos) { | 77 static std::string getLine(const std::string &Input, size_t &Pos) { |
| 74 std::string Line; | 78 std::string Line; |
| 75 if (Pos >= Input.size()) { | 79 if (Pos >= Input.size()) { |
| 76 Pos = std::string::npos; | 80 Pos = std::string::npos; |
| 77 return Line; | 81 return Line; |
| 78 } | 82 } |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 break; | 335 break; |
| 332 } | 336 } |
| 333 default: | 337 default: |
| 334 Fatal() << "Error: Bad literal flag in abbreviation. Found: " | 338 Fatal() << "Error: Bad literal flag in abbreviation. Found: " |
| 335 << Values[Index]; | 339 << Values[Index]; |
| 336 ReportFatalError(); | 340 ReportFatalError(); |
| 337 } | 341 } |
| 338 } | 342 } |
| 339 return Abbrev; | 343 return Abbrev; |
| 340 } | 344 } |
| 345 |
| 346 bool NaClObjDumpMunger::runTestWithFlags( |
| 347 const char *Name, const uint64_t Munges[], size_t MungesSize, |
| 348 bool AddHeader, bool NoRecords, bool NoAssembly) { |
| 349 setupTest(Name, Munges, MungesSize, AddHeader); |
| 350 if (NaClObjDump(MungedInput, *DumpStream, NoRecords, NoAssembly)) |
| 351 FoundErrors = true; |
| 352 cleanupTest(); |
| 353 return !FoundErrors; |
| 354 } |
| 355 |
| 356 bool NaClParseBitcodeMunger::runTest( |
| 357 const char *Name, const uint64_t Munges[], size_t MungesSize, |
| 358 bool VerboseErrors) { |
| 359 bool AddHeader = true; |
| 360 setupTest(Name, Munges, MungesSize, AddHeader); |
| 361 LLVMContext &Context = getGlobalContext(); |
| 362 raw_ostream *VerboseStrm = VerboseErrors ? DumpStream : nullptr; |
| 363 ErrorOr<Module *> ModuleOrError = |
| 364 NaClParseBitcodeFile(MungedInput, Context, VerboseStrm); |
| 365 if (ModuleOrError) { |
| 366 delete ModuleOrError.get(); |
| 367 if (VerboseErrors) |
| 368 *DumpStream << "Successful parse!\n"; |
| 369 // If there was a successful parse, MungedInput was deleted by the |
| 370 // parser. Hence, we null it out here so that cleanupTest doesn't |
| 371 // double delete it. |
| 372 MungedInput = nullptr; |
| 373 } else { |
| 374 Error() << ModuleOrError.getError().message() << "\n"; |
| 375 } |
| 376 cleanupTest(); |
| 377 return !FoundErrors; |
| 378 } |
| OLD | NEW |