[llvm] ca1b994 - [DebugInfo][NFCI] Add unittest for DWARFAbbreviationDeclarationSet

Alex Langford via llvm-commits llvm-commits at lists.llvm.org
Mon May 22 15:11:20 PDT 2023


Author: Alex Langford
Date: 2023-05-22T15:11:10-07:00
New Revision: ca1b9943e1e44ce61b604e4186023eee00a02d47

URL: https://github.com/llvm/llvm-project/commit/ca1b9943e1e44ce61b604e4186023eee00a02d47
DIFF: https://github.com/llvm/llvm-project/commit/ca1b9943e1e44ce61b604e4186023eee00a02d47.diff

LOG: [DebugInfo][NFCI] Add unittest for DWARFAbbreviationDeclarationSet

The classes relevant to DWARFDebugAbbrev do not have any unittests
verifying their behavior. Seeing as there is not much error handling
around these classes right now, I want to add some testing as I plan on
making changes to these classes in the near future.

Differential Revision: https://reviews.llvm.org/D151001

Added: 
    llvm/unittests/DebugInfo/DWARF/DWARFDebugAbbrevTest.cpp

Modified: 
    llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h
    llvm/unittests/DebugInfo/DWARF/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h
index 52a88f2c390aa..aa3885d7a9d6b 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h
@@ -49,6 +49,8 @@ class DWARFAbbreviationDeclarationSet {
 
   std::string getCodeRange() const;
 
+  uint32_t getFirstAbbrCode() const { return FirstAbbrCode; }
+
 private:
   void clear();
 };

diff  --git a/llvm/unittests/DebugInfo/DWARF/CMakeLists.txt b/llvm/unittests/DebugInfo/DWARF/CMakeLists.txt
index a6c37c6a1519c..1ba3786387594 100644
--- a/llvm/unittests/DebugInfo/DWARF/CMakeLists.txt
+++ b/llvm/unittests/DebugInfo/DWARF/CMakeLists.txt
@@ -16,6 +16,7 @@ add_llvm_unittest(DebugInfoDWARFTests
   DwarfUtils.cpp
   DWARFAcceleratorTableTest.cpp
   DWARFDataExtractorTest.cpp
+  DWARFDebugAbbrevTest.cpp
   DWARFDebugArangeSetTest.cpp
   DWARFDebugFrameTest.cpp
   DWARFDebugInfoTest.cpp

diff  --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugAbbrevTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugAbbrevTest.cpp
new file mode 100644
index 0000000000000..5f304e9329c3e
--- /dev/null
+++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugAbbrevTest.cpp
@@ -0,0 +1,105 @@
+//===- llvm/unittest/DebugInfo/DWARFDebugAbbrevTest.cpp -------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
+#include "llvm/BinaryFormat/Dwarf.h"
+#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
+#include "llvm/Support/DataExtractor.h"
+#include "llvm/Support/LEB128.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Testing/Support/Error.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace dwarf;
+
+enum Order : bool { InOrder, OutOfOrder };
+
+void WriteAbbreviationDeclarations(raw_ostream &OS, uint32_t FirstCode,
+                                   Order Ord) {
+  encodeULEB128(FirstCode, OS);
+  encodeULEB128(DW_TAG_compile_unit, OS);
+  OS << static_cast<uint8_t>(DW_CHILDREN_yes);
+  encodeULEB128(DW_AT_name, OS);
+  encodeULEB128(DW_FORM_strp, OS);
+  encodeULEB128(0, OS);
+  encodeULEB128(0, OS);
+
+  uint32_t SecondCode = Ord == Order::InOrder ? FirstCode + 1 : FirstCode - 1;
+
+  encodeULEB128(SecondCode, OS);
+  encodeULEB128(DW_TAG_subprogram, OS);
+  OS << static_cast<uint8_t>(DW_CHILDREN_no);
+  encodeULEB128(DW_AT_name, OS);
+  encodeULEB128(DW_FORM_strp, OS);
+  encodeULEB128(0, OS);
+  encodeULEB128(0, OS);
+}
+
+TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetExtractSuccess) {
+  SmallString<64> RawData;
+  raw_svector_ostream OS(RawData);
+  uint32_t FirstCode = 5;
+
+  WriteAbbreviationDeclarations(OS, FirstCode, InOrder);
+  encodeULEB128(0, OS);
+
+  uint64_t Offset = 0;
+  DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
+  DWARFAbbreviationDeclarationSet AbbrevSet;
+  const bool DataWasExtracted = AbbrevSet.extract(Data, &Offset);
+  EXPECT_TRUE(DataWasExtracted);
+  // The Abbreviation Declarations are in order and contiguous, so we want to
+  // make sure that FirstAbbrCode was correctly set
+  EXPECT_EQ(AbbrevSet.getFirstAbbrCode(), FirstCode);
+
+  const DWARFAbbreviationDeclaration *Abbrev5 =
+      AbbrevSet.getAbbreviationDeclaration(FirstCode);
+  EXPECT_TRUE(Abbrev5);
+  EXPECT_EQ(Abbrev5->getTag(), DW_TAG_compile_unit);
+  EXPECT_TRUE(Abbrev5->hasChildren());
+  EXPECT_EQ(Abbrev5->getNumAttributes(), 1u);
+
+  const DWARFAbbreviationDeclaration *Abbrev6 =
+      AbbrevSet.getAbbreviationDeclaration(FirstCode + 1);
+  EXPECT_TRUE(Abbrev6);
+  EXPECT_EQ(Abbrev6->getTag(), DW_TAG_subprogram);
+  EXPECT_FALSE(Abbrev6->hasChildren());
+  EXPECT_EQ(Abbrev6->getNumAttributes(), 1u);
+}
+
+TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetExtractSuccessOutOfOrder) {
+  SmallString<64> RawData;
+  raw_svector_ostream OS(RawData);
+  uint32_t FirstCode = 2;
+
+  WriteAbbreviationDeclarations(OS, FirstCode, OutOfOrder);
+  encodeULEB128(0, OS);
+
+  uint64_t Offset = 0;
+  DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
+  DWARFAbbreviationDeclarationSet AbbrevSet;
+  const bool DataWasExtracted = AbbrevSet.extract(Data, &Offset);
+  EXPECT_TRUE(DataWasExtracted);
+  // The declarations are out of order, ensure that FirstAbbrCode is UINT32_MAX
+  EXPECT_EQ(AbbrevSet.getFirstAbbrCode(), UINT32_MAX);
+
+  const DWARFAbbreviationDeclaration *Abbrev2 =
+      AbbrevSet.getAbbreviationDeclaration(FirstCode);
+  EXPECT_TRUE(Abbrev2);
+  EXPECT_EQ(Abbrev2->getTag(), DW_TAG_compile_unit);
+  EXPECT_TRUE(Abbrev2->hasChildren());
+  EXPECT_EQ(Abbrev2->getNumAttributes(), 1u);
+
+  const DWARFAbbreviationDeclaration *Abbrev1 =
+      AbbrevSet.getAbbreviationDeclaration(FirstCode - 1);
+  EXPECT_TRUE(Abbrev1);
+  EXPECT_EQ(Abbrev1->getTag(), DW_TAG_subprogram);
+  EXPECT_FALSE(Abbrev1->hasChildren());
+  EXPECT_EQ(Abbrev1->getNumAttributes(), 1u);
+}


        


More information about the llvm-commits mailing list