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

Side by Side Diff: lib/Bitcode/NaCl/Analysis/NaClObjDumpStream.cpp

Issue 986453002: Additional clean ups on errors in bitcode parsing. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix nit. Created 5 years, 9 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
OLDNEW
1 //===-- NaClObjDumpStream.cpp --------------------------------------------===// 1 //===-- NaClObjDumpStream.cpp --------------------------------------------===//
2 // Implements an objdump stream (bitcode records/assembly code). 2 // Implements an objdump stream (bitcode records/assembly code).
3 // 3 //
4 // The LLVM Compiler Infrastructure 4 // The LLVM Compiler Infrastructure
5 // 5 //
6 // This file is distributed under the University of Illinois Open Source 6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details. 7 // License. See LICENSE.TXT for details.
8 // 8 //
9 //===----------------------------------------------------------------------===// 9 //===----------------------------------------------------------------------===//
10 10
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 } 145 }
146 146
147 147
148 TextFormatter::Directive *TextFormatter::GetTokenDirective:: 148 TextFormatter::Directive *TextFormatter::GetTokenDirective::
149 Allocate(TextFormatter *Formatter, const std::string &Text) { 149 Allocate(TextFormatter *Formatter, const std::string &Text) {
150 GetTokenDirective *Dir = Formatter->GetTokenFreeList.Allocate(Formatter); 150 GetTokenDirective *Dir = Formatter->GetTokenFreeList.Allocate(Formatter);
151 Dir->Text = Text; 151 Dir->Text = Text;
152 return Dir; 152 return Dir;
153 } 153 }
154 154
155 std::string RecordTextFormatter::getBitAddress(uint64_t Bit) {
156 std::string Address = naclbitc::getBitAddress(Bit);
157 size_t AddressSize = Address.size();
158 if (AddressSize >= AddressWriteWidth)
159 return Address;
160
161 // Pad address with leading spaces.
162 std::string Buffer;
163 raw_string_ostream StrBuf(Buffer);
164 for (size_t i = AddressWriteWidth - AddressSize; i > 0; --i) {
165 StrBuf << " ";
166 }
167 StrBuf << Address;
168 return StrBuf.str();
169 }
170
155 RecordTextFormatter::RecordTextFormatter(ObjDumpStream *ObjDump) 171 RecordTextFormatter::RecordTextFormatter(ObjDumpStream *ObjDump)
156 : TextFormatter(ObjDump->Records(), 0, " "), 172 : TextFormatter(ObjDump->Records(), 0, " "),
157 OpenBrace(this, "<"), 173 OpenBrace(this, "<"),
158 CloseBrace(this, ">"), 174 CloseBrace(this, ">"),
159 Comma(this, ","), 175 Comma(this, ","),
160 Space(this), 176 Space(this),
161 Endline(this), 177 Endline(this),
162 StartCluster(this), 178 StartCluster(this),
163 FinishCluster(this) { 179 FinishCluster(this) {
164 // Handle fact that 64-bit values can take up to 21 characters. 180 // Handle fact that 64-bit values can take up to 21 characters.
165 MinLineWidth = 21; 181 MinLineWidth = 21;
166 Label = NaClBitstreamReader::getBitAddress(0, AddressWriteWidth); 182 Label = getBitAddress(0);
167 } 183 }
168 184
169 std::string RecordTextFormatter::GetEmptyLabelColumn() { 185 std::string RecordTextFormatter::GetEmptyLabelColumn() {
170 std::string Buffer; 186 std::string Buffer;
171 raw_string_ostream StrmBuffer(Buffer); 187 raw_string_ostream StrmBuffer(Buffer);
172 for (size_t i = 0; i < Label.size(); ++i) { 188 for (size_t i = 0; i < Label.size(); ++i) {
173 StrmBuffer << ' '; 189 StrmBuffer << ' ';
174 } 190 }
175 StrmBuffer << '|'; 191 StrmBuffer << '|';
176 return StrmBuffer.str(); 192 return StrmBuffer.str();
177 } 193 }
178 194
179 void RecordTextFormatter::WriteLineIndents() { 195 void RecordTextFormatter::WriteLineIndents() {
180 if (AtInstructionBeginning) { 196 if (AtInstructionBeginning) {
181 BaseStream << Label << '|'; 197 BaseStream << Label << '|';
182 } else { 198 } else {
183 BaseStream << GetEmptyLabelColumn(); 199 BaseStream << GetEmptyLabelColumn();
184 } 200 }
185 LinePosition += Label.size() + 1; 201 LinePosition += Label.size() + 1;
186 TextFormatter::WriteLineIndents(); 202 TextFormatter::WriteLineIndents();
187 } 203 }
188 204
189 void RecordTextFormatter::WriteValues(uint64_t Bit, 205 void RecordTextFormatter::WriteValues(uint64_t Bit,
190 const llvm::NaClBitcodeValues &Values, 206 const llvm::NaClBitcodeValues &Values,
191 int32_t AbbrevIndex) { 207 int32_t AbbrevIndex) {
192 Label = NaClBitstreamReader::getBitAddress( 208 Label = getBitAddress(Bit);
193 Bit, RecordTextFormatter::AddressWriteWidth);
194 if (AbbrevIndex != ABBREV_INDEX_NOT_SPECIFIED) { 209 if (AbbrevIndex != ABBREV_INDEX_NOT_SPECIFIED) {
195 TextStream << AbbrevIndex << ":" << Space; 210 TextStream << AbbrevIndex << ":" << Space;
196 } 211 }
197 TextStream << OpenBrace; 212 TextStream << OpenBrace;
198 for (size_t i = 0; i < Values.size(); ++i) { 213 for (size_t i = 0; i < Values.size(); ++i) {
199 if (i > 0) { 214 if (i > 0) {
200 TextStream << Comma << FinishCluster << Space; 215 TextStream << Comma << FinishCluster << Space;
201 } 216 }
202 TextStream << StartCluster << Values[i]; 217 TextStream << StartCluster << Values[i];
203 } 218 }
(...skipping 26 matching lines...) Expand all
230 RecordStream(RecordBuffer), 245 RecordStream(RecordBuffer),
231 RecordFormatter(this) { 246 RecordFormatter(this) {
232 if (DumpRecords) { 247 if (DumpRecords) {
233 RecordWidth = DumpAssembly 248 RecordWidth = DumpAssembly
234 ? ComboObjDumpSeparatorColumn 249 ? ComboObjDumpSeparatorColumn
235 : RecordObjectDumpLength; 250 : RecordObjectDumpLength;
236 RecordFormatter.SetLineWidth(RecordWidth); 251 RecordFormatter.SetLineWidth(RecordWidth);
237 } 252 }
238 } 253 }
239 254
240 raw_ostream &ObjDumpStream::Error(uint64_t Bit) { 255 raw_ostream &ObjDumpStream::ErrorAt(naclbitc::ErrorLevel Level, uint64_t Bit) {
256 // Note: Since this method only prints the error line prefix, and
257 // lets the caller fill in the rest of the error, we can't stop the
258 // executable at this point. Instead, we let Flush empty the buffers,
259 // and print out existing errors, and then exit the executable.
260 if (NumErrors >= MaxErrors)
261 Flush();
jvoung (off chromium) 2015/03/09 18:50:23 How does the caller know that the MaxErrors is hit
Karl 2015/03/09 19:49:25 I guess my comment wasn't clear enough. I thought
jvoung (off chromium) 2015/03/09 20:03:01 Okay, I think I understand now. I didn't look at F
Karl 2015/03/12 16:32:26 Followed your suggestion and used your suggested c
241 LastKnownBit = Bit; 262 LastKnownBit = Bit;
242 if (NumErrors >= MaxErrors) 263 if (Level >= naclbitc::Error)
243 Fatal(Bit, "Too many errors"); 264 ++NumErrors;
244 ++NumErrors; 265 return naclbitc::ErrorAt(Comments(), Level, Bit);
245 return PrintMessagePrefix("Error", Bit);
246 }
247
248 void ObjDumpStream::Fatal(uint64_t Bit, const std::string &Message) {
249 LastKnownBit = Bit;
250 if (!Message.empty())
251 PrintMessagePrefix("Error", Bit) << Message << "\n";
252 Flush();
253 llvm::report_fatal_error("Unable to continue");
254 }
255
256 void ObjDumpStream::Fatal(uint64_t Bit,
257 const llvm::NaClBitcodeRecordData &Record,
258 const std::string &Message) {
259 LastKnownBit = Bit;
260 PrintMessagePrefix("Error", Bit) << Message;
261 Write(Bit, Record);
262 Flush();
263 llvm::report_fatal_error("Unable to continue");
264 } 266 }
265 267
266 // Dumps the next line of text in the buffer. Returns the number of characters 268 // Dumps the next line of text in the buffer. Returns the number of characters
267 // printed. 269 // printed.
268 static size_t DumpLine(raw_ostream &Stream, 270 static size_t DumpLine(raw_ostream &Stream,
269 const std::string &Buffer, 271 const std::string &Buffer,
270 size_t &Index, 272 size_t &Index,
271 size_t Size) { 273 size_t Size) {
272 size_t Count = 0; 274 size_t Count = 0;
273 while (Index < Size) { 275 while (Index < Size) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 Stream << ColumnSeparator; 319 Stream << ColumnSeparator;
318 } 320 }
319 // Dump next assembly line. 321 // Dump next assembly line.
320 DumpLine(Stream, AssemblyBuffer, AssemblyIndex, AssemblySize); 322 DumpLine(Stream, AssemblyBuffer, AssemblyIndex, AssemblySize);
321 Stream << '\n'; 323 Stream << '\n';
322 } 324 }
323 } 325 }
324 326
325 // Print out messages and reset buffers. 327 // Print out messages and reset buffers.
326 Stream << MessageBuffer; 328 Stream << MessageBuffer;
329 Stream.flush();
327 ResetBuffers(); 330 ResetBuffers();
328 if (NumErrors >= MaxErrors) { 331 if (NumErrors >= MaxErrors)
329 // Note: we don't call Fatal here because that will call Flush, causing 332 report_fatal_error("Too many errors, Unable to continue");
330 // an infinite loop. 333 }
331 Stream << "Error(" << NaClBitstreamReader::getBitAddress(LastKnownBit) 334
332 << "): Too many errors\n"; 335 void ObjDumpStream::FlushThenQuit() {
333 llvm::report_fatal_error("Unable to continue"); 336 Flush();
334 } 337 report_fatal_error("Unable to continue");
335 Stream.flush();
336 } 338 }
337 339
338 } 340 }
339 } 341 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698