diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDataExtractor.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDataExtractor.h --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDataExtractor.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDataExtractor.h @@ -55,8 +55,6 @@ /// reflect the absolute address of this pointer. Optional getEncodedPointer(uint64_t *Offset, uint8_t Encoding, uint64_t AbsPosOffset = 0) const; - - size_t size() const { return Section == nullptr ? 0 : Section->Data.size(); } }; } // end namespace llvm diff --git a/llvm/include/llvm/Support/DataExtractor.h b/llvm/include/llvm/Support/DataExtractor.h --- a/llvm/include/llvm/Support/DataExtractor.h +++ b/llvm/include/llvm/Support/DataExtractor.h @@ -534,14 +534,14 @@ /// error state of the cursor. The only way both eof and error states can be /// true is if one attempts a read while the cursor is at the very end of the /// data buffer. - bool eof(const Cursor &C) const { return Data.size() == C.Offset; } + bool eof(const Cursor &C) const { return size() == C.Offset; } /// Test the validity of \a offset. /// /// @return /// \b true if \a offset is a valid offset into the data in this /// object, \b false otherwise. - bool isValidOffset(uint64_t offset) const { return Data.size() > offset; } + bool isValidOffset(uint64_t offset) const { return size() > offset; } /// Test the availability of \a length bytes of data from \a offset. /// @@ -563,6 +563,9 @@ return isValidOffsetForDataOfSize(offset, AddressSize); } + /// Return the number of bytes in the underlying buffer. + size_t size() const { return Data.size(); } + protected: // Make it possible for subclasses to access these fields without making them // public. diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp --- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp @@ -48,15 +48,19 @@ return true; } - void generate() { + void generate(bool UseSection = false) { Context = createContext(); assert(Context != nullptr && "test state is not valid"); const DWARFObject &Obj = Context->getDWARFObj(); uint8_t TargetAddrSize = AddressSize == 0 ? 8 : AddressSize; - LineData = DWARFDataExtractor( - Obj, Obj.getLineSection(), - getDefaultTargetTripleForAddrSize(TargetAddrSize).isLittleEndian(), - AddressSize); + bool IsLittleEndian = + getDefaultTargetTripleForAddrSize(TargetAddrSize).isLittleEndian(); + if (UseSection) + LineData = DWARFDataExtractor(Obj, Obj.getLineSection(), IsLittleEndian, + AddressSize); + else + LineData = DWARFDataExtractor(Obj.getLineSection().Data, IsLittleEndian, + AddressSize); } std::unique_ptr createContext() { @@ -162,6 +166,13 @@ DwarfFormat Format; }; +struct DebugLineSectionFixture : public TestWithParam, + public CommonFixture { + void SetUp() { UseSection = GetParam(); } + + bool UseSection; +}; + void checkDefaultPrologue(uint16_t Version, DwarfFormat Format, DWARFDebugLine::Prologue Prologue, uint64_t BodyLength) { @@ -514,7 +525,7 @@ EXPECT_EQ((*ExpectedLineTable)->Rows[2].IsStmt, 1u); } -TEST_F(DebugLineBasicFixture, ErrorForUnitLengthTooLarge) { +TEST_P(DebugLineSectionFixture, ErrorForUnitLengthTooLarge) { if (!setupGenerator()) return; @@ -531,7 +542,7 @@ Prologue.TotalLength += 6; LT.setPrologue(Prologue); - generate(); + generate(UseSection); auto ExpectedLineTable = Line.getOrParseLineTable(LineData, 1, *Context, nullptr, RecordRecoverable); @@ -543,6 +554,9 @@ EXPECT_EQ((*ExpectedLineTable)->Sequences.size(), 1u); } +INSTANTIATE_TEST_CASE_P(LineTableSectionParams, DebugLineSectionFixture, + Values(true, false), ); + TEST_F(DebugLineBasicFixture, ErrorForMismatchedAddressSize) { if (!setupGenerator(4, 8)) return; diff --git a/llvm/unittests/Support/DataExtractorTest.cpp b/llvm/unittests/Support/DataExtractorTest.cpp --- a/llvm/unittests/Support/DataExtractorTest.cpp +++ b/llvm/unittests/Support/DataExtractorTest.cpp @@ -269,4 +269,13 @@ EXPECT_TRUE(DE.eof(C)); EXPECT_THAT_ERROR(C.takeError(), Succeeded()); } + +TEST(DataExtractorTest, size) { + uint8_t Data[] = {'A', 'B', 'C', 'D'}; + DataExtractor DE1(StringRef(reinterpret_cast(Data), sizeof(Data)), + false, 8); + EXPECT_EQ(DE1.size(), sizeof(Data)); + DataExtractor DE2(ArrayRef(Data), false, 8); + EXPECT_EQ(DE2.size(), sizeof(Data)); +} }