| OLD | NEW |
| 1 //===-- NaClBitcodeAnalyzer.cpp - Bitcode Analyzer ------------------------===// | 1 //===-- NaClBitcodeAnalyzer.cpp - Bitcode Analyzer ------------------------===// |
| 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 #define DEBUG_TYPE "nacl-bitcode-analyzer" | 10 #define DEBUG_TYPE "nacl-bitcode-analyzer" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "llvm/Support/MemoryBuffer.h" | 23 #include "llvm/Support/MemoryBuffer.h" |
| 24 #include "llvm/Support/raw_ostream.h" | 24 #include "llvm/Support/raw_ostream.h" |
| 25 #include <algorithm> | 25 #include <algorithm> |
| 26 #include <map> | 26 #include <map> |
| 27 #include <system_error> | 27 #include <system_error> |
| 28 | 28 |
| 29 // TODO(kschimpf): Separate out into two bitcode parsers, one for | 29 // TODO(kschimpf): Separate out into two bitcode parsers, one for |
| 30 // dumping records, and one for collecting distribution stats for | 30 // dumping records, and one for collecting distribution stats for |
| 31 // printing. This should simplify the code. | 31 // printing. This should simplify the code. |
| 32 | 32 |
| 33 /// Error - All bitcode analysis errors go through this function, making this a | 33 namespace { |
| 34 /// good place to breakpoint if debugging. | 34 |
| 35 static bool Error(const llvm::Twine &Err) { | 35 // Generates an error message when outside parsing, and no |
| 36 // corresponding bit position is known. |
| 37 bool Error(const llvm::Twine &Err) { |
| 36 llvm::errs() << Err << "\n"; | 38 llvm::errs() << Err << "\n"; |
| 37 return true; | 39 return true; |
| 38 } | 40 } |
| 39 | 41 |
| 42 } // End of anonymous namespace. |
| 43 |
| 40 namespace llvm { | 44 namespace llvm { |
| 41 | 45 |
| 42 // Parses all bitcode blocks, and collects distribution of records in | 46 // Parses all bitcode blocks, and collects distribution of records in |
| 43 // each block. Also dumps bitcode structure if specified (via global | 47 // each block. Also dumps bitcode structure if specified (via global |
| 44 // variables). | 48 // variables). |
| 45 class PNaClBitcodeAnalyzerParser : public NaClBitcodeParser { | 49 class PNaClBitcodeAnalyzerParser : public NaClBitcodeParser { |
| 46 public: | 50 public: |
| 47 PNaClBitcodeAnalyzerParser(NaClBitstreamCursor &Cursor, | 51 PNaClBitcodeAnalyzerParser(NaClBitstreamCursor &Cursor, |
| 48 raw_ostream &OS, | 52 raw_ostream &OS, |
| 49 const AnalysisDumpOptions &DumpOptions, | 53 const AnalysisDumpOptions &DumpOptions, |
| 50 NaClBitcodeDist *Dist) | 54 NaClBitcodeDist *Dist) |
| 51 : NaClBitcodeParser(Cursor), | 55 : NaClBitcodeParser(Cursor), |
| 52 IndentLevel(0), | 56 IndentLevel(0), |
| 53 OS(OS), | 57 OS(OS), |
| 54 DumpOptions(DumpOptions), | 58 DumpOptions(DumpOptions), |
| 55 Dist(Dist), | 59 Dist(Dist), |
| 56 AbbrevListener(this) | 60 AbbrevListener(this) |
| 57 { | 61 { |
| 58 SetListener(&AbbrevListener); | 62 SetListener(&AbbrevListener); |
| 59 } | 63 } |
| 60 | 64 |
| 61 virtual ~PNaClBitcodeAnalyzerParser() {} | 65 virtual ~PNaClBitcodeAnalyzerParser() {} |
| 62 | 66 |
| 63 virtual bool Error(const std::string &Message) { | |
| 64 // Use local error routine so that all errors are treated uniformly. | |
| 65 return ::Error(Message); | |
| 66 } | |
| 67 | |
| 68 virtual bool ParseBlock(unsigned BlockID); | 67 virtual bool ParseBlock(unsigned BlockID); |
| 69 | 68 |
| 70 // Returns the string defining the indentation to use with respect | 69 // Returns the string defining the indentation to use with respect |
| 71 // to the current indent level. | 70 // to the current indent level. |
| 72 const std::string &GetIndentation() { | 71 const std::string &GetIndentation() { |
| 73 size_t Size = IndentationCache.size(); | 72 size_t Size = IndentationCache.size(); |
| 74 if (IndentLevel >= Size) { | 73 if (IndentLevel >= Size) { |
| 75 IndentationCache.resize(IndentLevel+1); | 74 IndentationCache.resize(IndentLevel+1); |
| 76 for (size_t i = Size; i <= IndentLevel; ++i) { | 75 for (size_t i = Size; i <= IndentLevel; ++i) { |
| 77 IndentationCache[i] = std::string(i*2, ' '); | 76 IndentationCache[i] = std::string(i*2, ' '); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 Context->IndentLevel++; | 239 Context->IndentLevel++; |
| 241 Indent = Context->GetIndentation(); | 240 Indent = Context->GetIndentation(); |
| 242 } | 241 } |
| 243 | 242 |
| 244 // Increment the indentation level for dumping. | 243 // Increment the indentation level for dumping. |
| 245 void DecrementIndent() { | 244 void DecrementIndent() { |
| 246 Context->IndentLevel--; | 245 Context->IndentLevel--; |
| 247 Indent = Context->GetIndentation(); | 246 Indent = Context->GetIndentation(); |
| 248 } | 247 } |
| 249 | 248 |
| 250 virtual bool Error(const std::string &Message) { | |
| 251 // Use local error routine so that all errors are treated uniformly. | |
| 252 return ::Error(Message); | |
| 253 } | |
| 254 | |
| 255 // Called once the block has been entered by the bitstream reader. | 249 // Called once the block has been entered by the bitstream reader. |
| 256 // Argument NumWords is set to the number of words in the | 250 // Argument NumWords is set to the number of words in the |
| 257 // corresponding block. | 251 // corresponding block. |
| 258 virtual void EnterBlock(unsigned NumberWords) { | 252 virtual void EnterBlock(unsigned NumberWords) { |
| 259 NumWords = NumberWords; | 253 NumWords = NumberWords; |
| 260 if (Context->DumpOptions.DumpRecords) { | 254 if (Context->DumpOptions.DumpRecords) { |
| 261 unsigned BlockID = GetBlockID(); | 255 unsigned BlockID = GetBlockID(); |
| 262 EmitBeginStartTag(); | 256 EmitBeginStartTag(); |
| 263 EmitEnterBlockTagName(BlockID); | 257 EmitEnterBlockTagName(BlockID); |
| 264 if (Context->DumpOptions.DumpDetails) { | 258 if (Context->DumpOptions.DumpDetails) { |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile = | 460 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile = |
| 467 MemoryBuffer::getFileOrSTDIN(InputFilename); | 461 MemoryBuffer::getFileOrSTDIN(InputFilename); |
| 468 if (std::error_code EC = ErrOrFile.getError()) | 462 if (std::error_code EC = ErrOrFile.getError()) |
| 469 return Error(Twine("Error reading '") + InputFilename + "': " + | 463 return Error(Twine("Error reading '") + InputFilename + "': " + |
| 470 EC.message()); | 464 EC.message()); |
| 471 | 465 |
| 472 return AnalyzeBitcodeInBuffer(ErrOrFile.get(), OS, DumpOptions); | 466 return AnalyzeBitcodeInBuffer(ErrOrFile.get(), OS, DumpOptions); |
| 473 } | 467 } |
| 474 | 468 |
| 475 } // namespace llvm | 469 } // namespace llvm |
| OLD | NEW |