OLD | NEW |
1 //===- NaClBitstreamReader.cpp --------------------------------------------===// | 1 //===- NaClBitstreamReader.cpp --------------------------------------------===// |
2 // NaClBitstreamReader implementation | 2 // NaClBitstreamReader implementation |
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 |
| 11 #include "llvm/ADT/STLExtras.h" |
11 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h" | 12 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h" |
12 #include "llvm/Support/Format.h" | 13 #include "llvm/Support/Format.h" |
13 #include "llvm/Support/raw_ostream.h" | 14 #include "llvm/Support/raw_ostream.h" |
14 | 15 |
15 using namespace llvm; | 16 using namespace llvm; |
16 | 17 |
17 std::string NaClBitstreamReader::getBitAddress(uint64_t Bit, | 18 namespace { |
18 unsigned MinByteWidth) { | 19 |
| 20 static const char *ErrorLevelName[] = { |
| 21 "Warning", |
| 22 "Error", |
| 23 "Fatal" |
| 24 }; |
| 25 |
| 26 } // End of anonymous namespace. |
| 27 |
| 28 std::string llvm::naclbitc::getBitAddress(uint64_t Bit) { |
19 std::string Buffer; | 29 std::string Buffer; |
20 raw_string_ostream Stream(Buffer); | 30 raw_string_ostream Stream(Buffer); |
21 Stream << '%' << MinByteWidth << PRIu64 << ":%u"; | 31 Stream << (Bit / 8) << ":" << (Bit % 8); |
22 Stream.flush(); | |
23 std::string FormatString(Buffer); | |
24 Buffer.clear(); | |
25 Stream << format(FormatString.c_str(), | |
26 (Bit / 8), | |
27 static_cast<unsigned>(Bit % 8)); | |
28 return Stream.str(); | 32 return Stream.str(); |
29 } | 33 } |
30 | 34 |
| 35 raw_ostream &llvm::naclbitc::ErrorAt( |
| 36 raw_ostream &Out, ErrorLevel Level, uint64_t BitPosition) { |
| 37 assert(Level < array_lengthof(::ErrorLevelName)); |
| 38 return Out << ErrorLevelName[Level] << "(" |
| 39 << naclbitc::getBitAddress(BitPosition) << "): "; |
| 40 } |
| 41 |
31 //===----------------------------------------------------------------------===// | 42 //===----------------------------------------------------------------------===// |
32 // NaClBitstreamCursor implementation | 43 // NaClBitstreamCursor implementation |
33 //===----------------------------------------------------------------------===// | 44 //===----------------------------------------------------------------------===// |
34 | 45 |
35 void NaClBitstreamCursor::ErrorHandler:: | 46 void NaClBitstreamCursor::ErrorHandler:: |
36 Fatal(const std::string &ErrorMessage) const { | 47 Fatal(const std::string &ErrorMessage) const { |
37 // Default implementation is simply print message, and the bit where | 48 // Default implementation is simply print message, and the bit where |
38 // the error occurred. | 49 // the error occurred. |
39 std::string Buffer; | 50 std::string Buffer; |
40 raw_string_ostream StrBuf(Buffer); | 51 raw_string_ostream StrBuf(Buffer); |
41 StrBuf << "Error(" | 52 naclbitc::ErrorAt(StrBuf, naclbitc::Fatal, Cursor.GetCurrentBitNo()) |
42 << NaClBitstreamReader::getBitAddress(Cursor.GetCurrentBitNo()) | 53 << ErrorMessage; |
43 << "): " << ErrorMessage; | |
44 report_fatal_error(StrBuf.str()); | 54 report_fatal_error(StrBuf.str()); |
45 } | 55 } |
46 | 56 |
47 void NaClBitstreamCursor::freeState() { | 57 void NaClBitstreamCursor::freeState() { |
48 // Free all the Abbrevs. | 58 // Free all the Abbrevs. |
49 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i) | 59 for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i) |
50 CurAbbrevs[i]->dropRef(); | 60 CurAbbrevs[i]->dropRef(); |
51 CurAbbrevs.clear(); | 61 CurAbbrevs.clear(); |
52 | 62 |
53 // Free all the Abbrevs in the block scope. | 63 // Free all the Abbrevs in the block scope. |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 if (Record.size() < 1) return true; | 402 if (Record.size() < 1) return true; |
393 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); | 403 CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]); |
394 if (Listener) { | 404 if (Listener) { |
395 Listener->Values = Record; | 405 Listener->Values = Record; |
396 Listener->SetBID(); | 406 Listener->SetBID(); |
397 } | 407 } |
398 break; | 408 break; |
399 } | 409 } |
400 } | 410 } |
401 } | 411 } |
OLD | NEW |