Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: lib/Bitcode/NaCl/TestUtils/NaClBitcodeMunge.cpp

Issue 1140153004: Clean up bitcode munging tests. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix issues in patch set 2. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/llvm/Bitcode/NaCl/NaClBitcodeMunge.h ('k') | unittests/Bitcode/NaClCompressTests.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.
(...skipping 11 matching lines...) Expand all
22 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MemoryBuffer.h" 23 #include "llvm/Support/MemoryBuffer.h"
24 24
25 #include <memory> 25 #include <memory>
26 26
27 using namespace llvm; 27 using namespace llvm;
28 28
29 // For debugging. When true, shows each test being run. 29 // For debugging. When true, shows each test being run.
30 static bool TraceTestRuns = false; 30 static bool TraceTestRuns = false;
31 31
32 bool NaClBitcodeMunger::setupTest( 32 bool NaClBitcodeMunger::setupTest(const uint64_t Munges[], size_t MungesSize,
33 const char *TestName, const uint64_t Munges[], size_t MungesSize, 33 bool AddHeader) {
34 bool AddHeader) {
35 assert(DumpStream == nullptr && "Test run with DumpStream already defined"); 34 assert(DumpStream == nullptr && "Test run with DumpStream already defined");
36 assert(MungedInput.get() == nullptr 35 assert(MungedInput.get() == nullptr
37 && "Test run with MungedInput already defined"); 36 && "Test run with MungedInput already defined");
38 FoundErrors = false; 37 FoundErrors = false;
39 DumpResults.clear(); // Throw away any previous results. 38 DumpResults.clear(); // Throw away any previous results.
40 std::string DumpBuffer; 39 std::string DumpBuffer;
41 DumpStream = new raw_string_ostream(DumpResults); 40 DumpStream = new raw_string_ostream(DumpResults);
42 MungedInputBuffer.clear(); 41 MungedInputBuffer.clear();
43 42
44 if (TraceTestRuns) { 43 if (TraceTestRuns) {
45 errs() << "*** Run test: " << TestName << "\n"; 44 errs() << "*** Run test:\n";
46 } 45 }
47 46
48 MungedBitcode.munge(Munges, MungesSize, RecordTerminator); 47 MungedBitcode.munge(Munges, MungesSize, RecordTerminator);
49 WriteFlags.setErrStream(getDumpStream()); 48 WriteFlags.setErrStream(getDumpStream());
50 NaClMungedBitcode::WriteResults Results = 49 NaClMungedBitcode::WriteResults Results =
51 MungedBitcode.writeMaybeRepair(MungedInputBuffer, AddHeader, WriteFlags); 50 MungedBitcode.writeMaybeRepair(MungedInputBuffer, AddHeader, WriteFlags);
52 if (Results.NumErrors != 0 51 if (Results.NumErrors != 0
53 && !(WriteFlags.getTryToRecover() 52 && !(WriteFlags.getTryToRecover()
54 && Results.NumRepairs == Results.NumErrors) 53 && Results.NumRepairs == Results.NumErrors)
55 && !(WriteFlags.getWriteBadAbbrevIndex() 54 && !(WriteFlags.getWriteBadAbbrevIndex()
56 && Results.WroteBadAbbrevIndex && Results.NumErrors == 1)) { 55 && Results.WroteBadAbbrevIndex && Results.NumErrors == 1)) {
57 Error() << "Unable to generate bitcode file due to write errors\n"; 56 Error() << "Unable to generate bitcode file due to write errors\n";
58 return false; 57 return false;
59 } 58 }
60 59
61 // Add null terminator, so that we meet the requirements of the 60 // Add null terminator, so that we meet the requirements of the
62 // MemoryBuffer API. 61 // MemoryBuffer API.
63 MungedInputBuffer.push_back('\0'); 62 MungedInputBuffer.push_back('\0');
64 63
65 MungedInput = MemoryBuffer::getMemBuffer( 64 MungedInput = MemoryBuffer::getMemBuffer(
66 StringRef(MungedInputBuffer.data(), MungedInputBuffer.size()-1), 65 StringRef(MungedInputBuffer.data(), MungedInputBuffer.size()-1));
67 TestName);
68 return true; 66 return true;
69 } 67 }
70 68
71 bool NaClBitcodeMunger::cleanupTest() { 69 bool NaClBitcodeMunger::cleanupTest() {
72 RunAsDeathTest = false; 70 RunAsDeathTest = false;
73 WriteFlags.reset(); 71 WriteFlags.reset();
74 MungedBitcode.removeEdits(); 72 MungedBitcode.removeEdits();
75 MungedInput.reset(); 73 MungedInput.reset();
76 assert(DumpStream && "Dump stream removed before cleanup!"); 74 assert(DumpStream && "Dump stream removed before cleanup!");
77 DumpStream->flush(); 75 DumpStream->flush();
(...skipping 29 matching lines...) Expand all
107 std::string Line = getLine(DumpResults, LastLinePos); 105 std::string Line = getLine(DumpResults, LastLinePos);
108 if (LastLinePos == std::string::npos) break; 106 if (LastLinePos == std::string::npos) break;
109 size_t Pos = Line.find(Substring); 107 size_t Pos = Line.find(Substring);
110 if (Pos != std::string::npos && (!MustBePrefix || Pos == 0)) { 108 if (Pos != std::string::npos && (!MustBePrefix || Pos == 0)) {
111 Messages.append(Line); 109 Messages.append(Line);
112 } 110 }
113 } 111 }
114 return Messages; 112 return Messages;
115 } 113 }
116 114
117 bool NaClWriteMunger::runTest(const char* Name, const uint64_t Munges[], 115 bool NaClWriteMunger::runTest(const uint64_t Munges[], size_t MungesSize) {
118 size_t MungesSize) {
119 bool AddHeader = true; 116 bool AddHeader = true;
120 if (!setupTest(Name, Munges, MungesSize, AddHeader)) 117 if (!setupTest(Munges, MungesSize, AddHeader))
121 return cleanupTest(); 118 return cleanupTest();
122 MemoryBufferRef InputRef(MungedInput->getMemBufferRef()); 119 MemoryBufferRef InputRef(MungedInput->getMemBufferRef());
123 NaClMungedBitcode WrittenBitcode(MemoryBuffer::getMemBuffer(InputRef)); 120 NaClMungedBitcode WrittenBitcode(MemoryBuffer::getMemBuffer(InputRef));
124 WrittenBitcode.print(WriteFlags.getErrStream()); 121 WrittenBitcode.print(WriteFlags.getErrStream());
125 return cleanupTest(); 122 return cleanupTest();
126 } 123 }
127 124
128 bool NaClObjDumpMunger::runTestWithFlags( 125 bool NaClObjDumpMunger::runTestWithFlags(
129 const char *Name, const uint64_t Munges[], size_t MungesSize, 126 const uint64_t Munges[], size_t MungesSize, bool AddHeader,
130 bool AddHeader, bool NoRecords, bool NoAssembly) { 127 bool NoRecords, bool NoAssembly) {
131 if (!setupTest(Name, Munges, MungesSize, AddHeader)) 128 if (!setupTest(Munges, MungesSize, AddHeader))
132 return cleanupTest(); 129 return cleanupTest();
133 130
134 if (NaClObjDump(MungedInput.get()->getMemBufferRef(), 131 if (NaClObjDump(MungedInput.get()->getMemBufferRef(),
135 getDumpStream(), NoRecords, NoAssembly)) 132 getDumpStream(), NoRecords, NoAssembly))
136 FoundErrors = true; 133 FoundErrors = true;
137 return cleanupTest(); 134 return cleanupTest();
138 } 135 }
139 136
140 bool NaClParseBitcodeMunger::runTest( 137 bool NaClParseBitcodeMunger::runTest(const uint64_t Munges[], size_t MungesSize,
141 const char *Name, const uint64_t Munges[], size_t MungesSize, 138 bool VerboseErrors) {
142 bool VerboseErrors) {
143 bool AddHeader = true; 139 bool AddHeader = true;
144 if (!setupTest(Name, Munges, MungesSize, AddHeader)) 140 if (!setupTest(Munges, MungesSize, AddHeader))
145 return cleanupTest(); 141 return cleanupTest();
146 142
147 LLVMContext &Context = getGlobalContext(); 143 LLVMContext &Context = getGlobalContext();
148 144
149 raw_ostream *VerboseStrm = VerboseErrors ? &getDumpStream() : nullptr; 145 raw_ostream *VerboseStrm = VerboseErrors ? &getDumpStream() : nullptr;
150 ErrorOr<Module *> ModuleOrError = 146 ErrorOr<Module *> ModuleOrError =
151 NaClParseBitcodeFile(MungedInput->getMemBufferRef(), Context, 147 NaClParseBitcodeFile(MungedInput->getMemBufferRef(), Context,
152 VerboseStrm); 148 VerboseStrm);
153 if (ModuleOrError) { 149 if (ModuleOrError) {
154 if (VerboseErrors) 150 if (VerboseErrors)
155 getDumpStream() << "Successful parse!\n"; 151 getDumpStream() << "Successful parse!\n";
156 delete ModuleOrError.get(); 152 delete ModuleOrError.get();
157 } else { 153 } else {
158 Error() << ModuleOrError.getError().message() << "\n"; 154 Error() << ModuleOrError.getError().message() << "\n";
159 } 155 }
160 return cleanupTest(); 156 return cleanupTest();
161 } 157 }
162 158
163 bool NaClCompressMunger::runTest(const char* Name, const uint64_t Munges[], 159 bool NaClCompressMunger::runTest(const uint64_t Munges[], size_t MungesSize) {
164 size_t MungesSize) {
165 bool AddHeader = true; 160 bool AddHeader = true;
166 if (!setupTest(Name, Munges, MungesSize, AddHeader)) 161 if (!setupTest(Munges, MungesSize, AddHeader))
167 return cleanupTest(); 162 return cleanupTest();
168 163
169 NaClBitcodeCompressor Compressor; 164 NaClBitcodeCompressor Compressor;
170 if (!Compressor.compress(MungedInput.get(), getDumpStream())) 165 if (!Compressor.compress(MungedInput.get(), getDumpStream()))
171 Error() << "Unable to compress\n"; 166 Error() << "Unable to compress\n";
172 return cleanupTest(); 167 return cleanupTest();
173 } 168 }
OLDNEW
« no previous file with comments | « include/llvm/Bitcode/NaCl/NaClBitcodeMunge.h ('k') | unittests/Bitcode/NaClCompressTests.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698