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

Side by Side Diff: include/llvm/Bitcode/NaCl/NaClObjDumpStream.h

Issue 932953002: Fix the NaCl bitstream reader to report fatal errors. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Merge with master 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.h --------------------------------------*- C++ -*-===// 1 //===- NaClObjDumpStream.h --------------------------------------*- C++ -*-===//
2 // Models an objdump stream (bitcode records/assembly code). 2 // Models 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 651 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 }; 662 };
663 663
664 class ObjDumpStream; 664 class ObjDumpStream;
665 665
666 /// Models that an abbreviation index is not specified when dumping a 666 /// Models that an abbreviation index is not specified when dumping a
667 /// bitcode record. 667 /// bitcode record.
668 static int32_t ABBREV_INDEX_NOT_SPECIFIED = -1; 668 static int32_t ABBREV_INDEX_NOT_SPECIFIED = -1;
669 669
670 /// The formatter used for dumping records in ObjDumpStream. 670 /// The formatter used for dumping records in ObjDumpStream.
671 class RecordTextFormatter : public TextFormatter { 671 class RecordTextFormatter : public TextFormatter {
672 RecordTextFormatter(const RecordTextFormatter&) = delete;
673 RecordTextFormatter &operator=(const RecordTextFormatter&) = delete;
672 public: 674 public:
673 /// The address write width used to print the number of 675 /// The address write width used to print the number of
674 /// bytes in the record bit address, when printing records. 676 /// bytes in the record bit address, when printing records.
675 static const unsigned AddressWriteWidth = 8; 677 static const unsigned AddressWriteWidth = 8;
676 678
677 explicit RecordTextFormatter(ObjDumpStream *ObjDump); 679 explicit RecordTextFormatter(ObjDumpStream *ObjDump);
678 680
679 ~RecordTextFormatter() override {} 681 ~RecordTextFormatter() override {}
680 682
681 /// Writes out the given record of values as an instruction. 683 /// Writes out the given record of values as an instruction.
682 void WriteValues(uint64_t Bit, 684 void WriteValues(uint64_t Bit,
683 const llvm::NaClBitcodeValues &Values, 685 const llvm::NaClBitcodeValues &Values,
684 int32_t AbbrevIndex = ABBREV_INDEX_NOT_SPECIFIED); 686 int32_t AbbrevIndex = ABBREV_INDEX_NOT_SPECIFIED);
685 687
686 /// Returns text corresponding to an empty label column. 688 /// Returns text corresponding to an empty label column.
687 std::string GetEmptyLabelColumn(); 689 std::string GetEmptyLabelColumn();
688 690
689 // Converts the given start bit to the corresponding address to
690 // print. That is, generate "Bit/8:Bit%8" value.
691 static std::string RecordAddress(uint64_t Bit, unsigned MinByteWidth);
692
693 // Returns the record address (when printing records) associated with
694 // the given bit.
695 static std::string RecordAddress(uint64_t Bit) {
696 return RecordAddress(Bit, AddressWriteWidth);
697 }
698
699 protected: 691 protected:
700 void WriteLineIndents() override; 692 void WriteLineIndents() override;
701 693
702 private: 694 private:
703 // The object dumper this formatter is associated with.
704 ObjDumpStream *ObjDump;
705 // The address label associated with the current line. 695 // The address label associated with the current line.
706 std::string Label; 696 std::string Label;
707 // The open brace '<' for a record. 697 // The open brace '<' for a record.
708 OpenTextDirective OpenBrace; 698 OpenTextDirective OpenBrace;
709 // The close brace '>' for a record. 699 // The close brace '>' for a record.
710 CloseTextDirective CloseBrace; 700 CloseTextDirective CloseBrace;
711 // A comma between elements in a record. 701 // A comma between elements in a record.
712 TokenTextDirective Comma; 702 TokenTextDirective Comma;
713 // A space inside a record. 703 // A space inside a record.
714 SpaceTextDirective Space; 704 SpaceTextDirective Space;
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 827
838 /// Prints "Error(Bit/8:Bit%8): " onto the comments stream, records 828 /// Prints "Error(Bit/8:Bit%8): " onto the comments stream, records
839 /// that an error has occurred, and then returns the comments 829 /// that an error has occurred, and then returns the comments
840 /// stream. In general errors will be printed after the next record, 830 /// stream. In general errors will be printed after the next record,
841 /// unless a call to Flush is made. 831 /// unless a call to Flush is made.
842 raw_ostream &Error(uint64_t Bit); 832 raw_ostream &Error(uint64_t Bit);
843 833
844 /// Write a fatal error message to the dump stream, and then 834 /// Write a fatal error message to the dump stream, and then
845 /// stop the executable. If any assembly, comments, or errors have 835 /// stop the executable. If any assembly, comments, or errors have
846 /// been buffered, they will be printed first. 836 /// been buffered, they will be printed first.
837 LLVM_ATTRIBUTE_NORETURN
847 void Fatal(const std::string &Message) { 838 void Fatal(const std::string &Message) {
848 Fatal(LastKnownBit, Message); 839 Fatal(LastKnownBit, Message);
849 } 840 }
850 841
851 /// Write a fatal error message to the dump stream, and then 842 /// Write a fatal error message to the dump stream, and then
852 /// stop the executable. If any assembly, comments, or errors have 843 /// stop the executable. If any assembly, comments, or errors have
853 /// been buffered, they will be printed first. Associates fatal error 844 /// been buffered, they will be printed first. Associates fatal error
854 /// Message with the given Bit. 845 /// Message with the given Bit.
846 LLVM_ATTRIBUTE_NORETURN
855 void Fatal(uint64_t Bit, const std::string &Message); 847 void Fatal(uint64_t Bit, const std::string &Message);
856 848
857 /// Write a fatal error message to the dump stream, and then 849 /// Write a fatal error message to the dump stream, and then
858 /// stop the executable. If any assembly, comments, or errors have 850 /// stop the executable. If any assembly, comments, or errors have
859 /// been buffered, they will be printed first, along with the given record. 851 /// been buffered, they will be printed first, along with the given record.
852 LLVM_ATTRIBUTE_NORETURN
860 void Fatal(uint64_t Bit, 853 void Fatal(uint64_t Bit,
861 const llvm::NaClBitcodeRecordData &Record, 854 const llvm::NaClBitcodeRecordData &Record,
862 const std::string &Message); 855 const std::string &Message);
863 856
864 /// Dumps a record (at the given bit), along with all buffered assembly, 857 /// Dumps a record (at the given bit), along with all buffered assembly,
865 /// comments, and errors, into the objdump stream. 858 /// comments, and errors, into the objdump stream.
866 void Write(uint64_t Bit, 859 void Write(uint64_t Bit,
867 const llvm::NaClBitcodeRecordData &Record, 860 const llvm::NaClBitcodeRecordData &Record,
868 int32_t AbbrevIndex = ABBREV_INDEX_NOT_SPECIFIED) { 861 int32_t AbbrevIndex = ABBREV_INDEX_NOT_SPECIFIED) {
869 LastKnownBit = Bit; 862 LastKnownBit = Bit;
(...skipping 30 matching lines...) Expand all
900 // Returns true if dumping record. 893 // Returns true if dumping record.
901 bool GetDumpRecords() const { 894 bool GetDumpRecords() const {
902 return DumpRecords; 895 return DumpRecords;
903 } 896 }
904 897
905 // Returns true if dumping assembly. 898 // Returns true if dumping assembly.
906 bool GetDumpAssembly() const { 899 bool GetDumpAssembly() const {
907 return DumpAssembly; 900 return DumpAssembly;
908 } 901 }
909 902
910 /// Changes the default assumption that bit addresses start
911 /// at index 0.
912 void SetStartOffset(uint64_t Offset) {
913 StartOffset = Offset;
914 }
915
916 /// Changes the maximum number of errors allowed. 903 /// Changes the maximum number of errors allowed.
917 void SetMaxErrors(unsigned NewMax) { 904 void SetMaxErrors(unsigned NewMax) {
918 MaxErrors = NewMax; 905 MaxErrors = NewMax;
919 } 906 }
920 907
921 /// Changes the width allowed for records (from the default). 908 /// Changes the width allowed for records (from the default).
922 void SetRecordWidth(unsigned Width) { 909 void SetRecordWidth(unsigned Width) {
923 RecordWidth = Width; 910 RecordWidth = Width;
924 } 911 }
925 912
926 unsigned GetRecordWidth() const { 913 unsigned GetRecordWidth() const {
927 return RecordWidth; 914 return RecordWidth;
928 } 915 }
929 916
930 /// Changes the column separator character to the given value. 917 /// Changes the column separator character to the given value.
931 void SetColumnSeparator(char Separator) { 918 void SetColumnSeparator(char Separator) {
932 ColumnSeparator = Separator; 919 ColumnSeparator = Separator;
933 } 920 }
934 921
935 /// Changes the internal state, to assume one is processing a record 922 /// Changes the internal state, to assume one is processing a record
936 /// at the given bit. Used by Error() and Fatal(Message) to fill in 923 /// at the given bit. Used by Error() and Fatal(Message) to fill in
937 /// the corresponding bit to associate with the error message. 924 /// the corresponding bit to associate with the error message.
938 void SetRecordBitAddress(uint64_t Bit) { 925 void SetRecordBitAddress(uint64_t Bit) {
939 LastKnownBit = Bit; 926 LastKnownBit = Bit;
940 } 927 }
941 928
942 // Converts the given start bit to the corresponding address to
943 // print. That is, generate "Bit/8:Bit%8" value.
944 std::string ObjDumpAddress(uint64_t Bit, unsigned MinByteWidth=1) {
945 return RecordFormatter.RecordAddress(Bit + StartOffset, MinByteWidth);
946 }
947
948 // Returns the record address (when printing records) associated with
949 // the given bit.
950 std::string RecordAddress(uint64_t Bit) {
951 return RecordFormatter.RecordAddress(Bit + StartOffset);
952 }
953
954 private: 929 private:
955 // The stream to dump to. 930 // The stream to dump to.
956 raw_ostream &Stream; 931 raw_ostream &Stream;
957 // True if records should be dumped to the dump stream. 932 // True if records should be dumped to the dump stream.
958 bool DumpRecords; 933 bool DumpRecords;
959 // True if assembly text should be dumped to the dump stream. 934 // True if assembly text should be dumped to the dump stream.
960 bool DumpAssembly; 935 bool DumpAssembly;
961 // The number of errors reported. 936 // The number of errors reported.
962 unsigned NumErrors; 937 unsigned NumErrors;
963 // The maximum number of errors before quitting. 938 // The maximum number of errors before quitting.
964 unsigned MaxErrors; 939 unsigned MaxErrors;
965 // The number of columns available to print bitcode records. 940 // The number of columns available to print bitcode records.
966 unsigned RecordWidth; 941 unsigned RecordWidth;
967 // The number of bits to add to the record bit address, to correct
968 // the record bit address passed to the write routines.
969 uint64_t StartOffset;
970 // The buffer for assembly to be printed during the next write. 942 // The buffer for assembly to be printed during the next write.
971 std::string AssemblyBuffer; 943 std::string AssemblyBuffer;
972 // The stream to buffer assembly into the assembly buffer. 944 // The stream to buffer assembly into the assembly buffer.
973 raw_string_ostream AssemblyStream; 945 raw_string_ostream AssemblyStream;
974 // The buffer for comments and errors. 946 // The buffer for comments and errors.
975 std::string MessageBuffer; 947 std::string MessageBuffer;
976 // The stream to buffer comments and errors into the message. 948 // The stream to buffer comments and errors into the message.
977 raw_string_ostream MessageStream; 949 raw_string_ostream MessageStream;
978 // The character used to separate records from assembly. 950 // The character used to separate records from assembly.
979 char ColumnSeparator; 951 char ColumnSeparator;
980 // The last known bit passed to the objdump object. Used as default 952 // The last known bit passed to the objdump object. Used as default
981 // for automatically generated errors. 953 // for automatically generated errors.
982 uint64_t LastKnownBit; 954 uint64_t LastKnownBit;
983 /// The buffer for records to be printed to during the next write. 955 /// The buffer for records to be printed to during the next write.
984 std::string RecordBuffer; 956 std::string RecordBuffer;
985 /// The stream to buffer recordds into the record buffer. 957 /// The stream to buffer recordds into the record buffer.
986 raw_string_ostream RecordStream; 958 raw_string_ostream RecordStream;
987 /// The text formatter for generating records. 959 /// The text formatter for generating records.
988 RecordTextFormatter RecordFormatter; 960 RecordTextFormatter RecordFormatter;
989 961
990 // Resets assembly and buffers. 962 // Resets assembly and buffers.
991 void ResetBuffers() { 963 void ResetBuffers() {
992 RecordBuffer.clear(); 964 RecordBuffer.clear();
993 AssemblyBuffer.clear(); 965 AssemblyBuffer.clear();
994 MessageBuffer.clear(); 966 MessageBuffer.clear();
995 } 967 }
996 968
997 // Returns the message stream with 'Label(Bit/8:Bit%8): '. 969 // Returns the message stream with 'Label(Bit/8:Bit%8): '.
998 raw_ostream &PrintMessagePrefix(const char *Label, uint64_t Bit) { 970 raw_ostream &PrintMessagePrefix(const char *Label, uint64_t Bit) {
999 return Comments() << Label << "(" << ObjDumpAddress(Bit) << "): "; 971 return Comments() << Label << "(" << NaClBitstreamReader::getBitAddress(Bit)
972 << "): ";
1000 } 973 }
1001 }; 974 };
1002 975
1003 } 976 }
1004 } 977 }
1005 978
1006 #endif 979 #endif
OLDNEW
« no previous file with comments | « include/llvm/Bitcode/NaCl/NaClBitstreamReader.h ('k') | lib/Bitcode/NaCl/Analysis/NaClObjDump.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698