Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DWARF] Fix DWARTTypePrinter unable to print qualified name for DW_TAG_typedef DIE #117239

Merged
merged 1 commit into from
Nov 25, 2024

Conversation

ZequanWu
Copy link
Contributor

Fix a bug introduced in #117071.

Ideally the DWARTTypePrinter test should go to llvm/unittests/DebugInfo/DWARF/DWARTTypePrinterTest.cpp.

@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

@llvm/pr-subscribers-debuginfo

Author: Zequan Wu (ZequanWu)

Changes

Fix a bug introduced in #117071.

Ideally the DWARTTypePrinter test should go to llvm/unittests/DebugInfo/DWARF/DWARTTypePrinterTest.cpp.


Full diff: https://github.com/llvm/llvm-project/pull/117239.diff

2 Files Affected:

  • (modified) lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp (+35-13)
  • (modified) llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h (+1)
diff --git a/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp b/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
index ae63e286cc1551..3a03ed283a98d4 100644
--- a/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
+++ b/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
@@ -23,6 +23,26 @@ using namespace lldb_private;
 using namespace lldb_private::plugin::dwarf;
 using namespace lldb_private::dwarf;
 
+namespace {
+void Test_appendAndTerminateTemplateParameters(const DWARFDIE &die,
+                                               const std::string &expected) {
+  std::string template_name;
+  llvm::raw_string_ostream template_name_os(template_name);
+  llvm::DWARFTypePrinter<DWARFDIE> template_name_printer(template_name_os);
+  template_name_printer.appendAndTerminateTemplateParameters(die);
+  EXPECT_THAT(template_name, expected);
+}
+
+void Test_appendQualifiedName(const DWARFDIE &die,
+                              const std::string &expected) {
+  std::string qualified_name;
+  llvm::raw_string_ostream template_name_os(qualified_name);
+  llvm::DWARFTypePrinter<DWARFDIE> template_name_printer(template_name_os);
+  template_name_printer.appendQualifiedName(die);
+  EXPECT_THAT(qualified_name, expected);
+}
+} // namespace
+
 TEST(DWARFDIETest, ChildIteration) {
   // Tests DWARFDIE::child_iterator.
 
@@ -466,6 +486,14 @@ TEST(DWARFDIETest, TestDWARFTypePrinter) {
           Attributes:
             - Attribute:       DW_AT_type
               Form:            DW_FORM_ref4
+        - Code:            0x8
+          Tag:             DW_TAG_typedef
+          Children:        DW_CHILDREN_no
+          Attributes:
+            - Attribute:       DW_AT_type
+              Form:            DW_FORM_ref4
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_string
   debug_info:
     - Version:         4
       AddrSize:        8
@@ -494,6 +522,10 @@ TEST(DWARFDIETest, TestDWARFTypePrinter) {
         - AbbrCode:        0x7
           Values:
             - Value:            0x0000000c # update
+        - AbbrCode:        0x8
+          Values:
+            - Value:            0x0000000c
+            - CStr:            my_int
         - AbbrCode:        0x0
         - AbbrCode:        0x0)";
   YAMLModuleTester t(yamldata);
@@ -505,17 +537,7 @@ TEST(DWARFDIETest, TestDWARFTypePrinter) {
   unit->Dump(&debug_os);
   ASSERT_TRUE(unit);
 
-  DWARFDIE t1_die = unit->GetDIE(0x11);
-  std::string template_name;
-  llvm::raw_string_ostream template_name_os(template_name);
-  llvm::DWARFTypePrinter<DWARFDIE> template_name_printer(template_name_os);
-  template_name_printer.appendAndTerminateTemplateParameters(t1_die);
-  EXPECT_THAT(template_name, "<t3<int> >");
-
-  DWARFDIE t2_die = unit->GetDIE(0x1a);
-  std::string qualified_name;
-  llvm::raw_string_ostream qualified_name_os(qualified_name);
-  llvm::DWARFTypePrinter<DWARFDIE> qualified_name_printer(qualified_name_os);
-  qualified_name_printer.appendQualifiedName(t2_die);
-  EXPECT_THAT(qualified_name, "t1<t3<int> >::t2");
+  Test_appendAndTerminateTemplateParameters(unit->GetDIE(0x11), "<t3<int> >");
+  Test_appendQualifiedName(unit->GetDIE(0x1a), "t1<t3<int> >::t2");
+  Test_appendQualifiedName(unit->GetDIE(0x28), "t3<int>::my_int");
 }
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h
index 962462b8278259..3c936b93865045 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h
@@ -70,6 +70,7 @@ template <typename DieType> struct DWARFTypePrinter {
     case dwarf::DW_TAG_union_type:
     case dwarf::DW_TAG_namespace:
     case dwarf::DW_TAG_enumeration_type:
+    case dwarf::DW_TAG_typedef:
       return true;
     default:
       break;

@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

@llvm/pr-subscribers-lldb

Author: Zequan Wu (ZequanWu)

Changes

Fix a bug introduced in #117071.

Ideally the DWARTTypePrinter test should go to llvm/unittests/DebugInfo/DWARF/DWARTTypePrinterTest.cpp.


Full diff: https://github.com/llvm/llvm-project/pull/117239.diff

2 Files Affected:

  • (modified) lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp (+35-13)
  • (modified) llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h (+1)
diff --git a/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp b/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
index ae63e286cc1551..3a03ed283a98d4 100644
--- a/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
+++ b/lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
@@ -23,6 +23,26 @@ using namespace lldb_private;
 using namespace lldb_private::plugin::dwarf;
 using namespace lldb_private::dwarf;
 
+namespace {
+void Test_appendAndTerminateTemplateParameters(const DWARFDIE &die,
+                                               const std::string &expected) {
+  std::string template_name;
+  llvm::raw_string_ostream template_name_os(template_name);
+  llvm::DWARFTypePrinter<DWARFDIE> template_name_printer(template_name_os);
+  template_name_printer.appendAndTerminateTemplateParameters(die);
+  EXPECT_THAT(template_name, expected);
+}
+
+void Test_appendQualifiedName(const DWARFDIE &die,
+                              const std::string &expected) {
+  std::string qualified_name;
+  llvm::raw_string_ostream template_name_os(qualified_name);
+  llvm::DWARFTypePrinter<DWARFDIE> template_name_printer(template_name_os);
+  template_name_printer.appendQualifiedName(die);
+  EXPECT_THAT(qualified_name, expected);
+}
+} // namespace
+
 TEST(DWARFDIETest, ChildIteration) {
   // Tests DWARFDIE::child_iterator.
 
@@ -466,6 +486,14 @@ TEST(DWARFDIETest, TestDWARFTypePrinter) {
           Attributes:
             - Attribute:       DW_AT_type
               Form:            DW_FORM_ref4
+        - Code:            0x8
+          Tag:             DW_TAG_typedef
+          Children:        DW_CHILDREN_no
+          Attributes:
+            - Attribute:       DW_AT_type
+              Form:            DW_FORM_ref4
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_string
   debug_info:
     - Version:         4
       AddrSize:        8
@@ -494,6 +522,10 @@ TEST(DWARFDIETest, TestDWARFTypePrinter) {
         - AbbrCode:        0x7
           Values:
             - Value:            0x0000000c # update
+        - AbbrCode:        0x8
+          Values:
+            - Value:            0x0000000c
+            - CStr:            my_int
         - AbbrCode:        0x0
         - AbbrCode:        0x0)";
   YAMLModuleTester t(yamldata);
@@ -505,17 +537,7 @@ TEST(DWARFDIETest, TestDWARFTypePrinter) {
   unit->Dump(&debug_os);
   ASSERT_TRUE(unit);
 
-  DWARFDIE t1_die = unit->GetDIE(0x11);
-  std::string template_name;
-  llvm::raw_string_ostream template_name_os(template_name);
-  llvm::DWARFTypePrinter<DWARFDIE> template_name_printer(template_name_os);
-  template_name_printer.appendAndTerminateTemplateParameters(t1_die);
-  EXPECT_THAT(template_name, "<t3<int> >");
-
-  DWARFDIE t2_die = unit->GetDIE(0x1a);
-  std::string qualified_name;
-  llvm::raw_string_ostream qualified_name_os(qualified_name);
-  llvm::DWARFTypePrinter<DWARFDIE> qualified_name_printer(qualified_name_os);
-  qualified_name_printer.appendQualifiedName(t2_die);
-  EXPECT_THAT(qualified_name, "t1<t3<int> >::t2");
+  Test_appendAndTerminateTemplateParameters(unit->GetDIE(0x11), "<t3<int> >");
+  Test_appendQualifiedName(unit->GetDIE(0x1a), "t1<t3<int> >::t2");
+  Test_appendQualifiedName(unit->GetDIE(0x28), "t3<int>::my_int");
 }
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h
index 962462b8278259..3c936b93865045 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h
@@ -70,6 +70,7 @@ template <typename DieType> struct DWARFTypePrinter {
     case dwarf::DW_TAG_union_type:
     case dwarf::DW_TAG_namespace:
     case dwarf::DW_TAG_enumeration_type:
+    case dwarf::DW_TAG_typedef:
       return true;
     default:
       break;

@dwblaikie
Copy link
Collaborator

Rather than a unit test, perhaps this could be tested with llvm-dwarfdump? (I think that's how I tested the DWARFTypePrinter during its initial development) It's a feature improvement to llvm-dwarfdump - being able to print out names better.

I guess most of what I did was more specifically about simplified-template-names roundtripping, which this new typedef case isn't relevant to (typedefs aren't canonical names, don't themselves get simplified/have to be rebuilt).

But the testing was in llvm/test/tools/llvm-dwarfdump/X86/simplified-template-names.s (and there's similar testing in the cross_project_tests and in clang - so adding the test coverage in those places too would be good, if this test file's one that could be used for this patch). I guess maybe this testing all only does "verify" testing, which means it won't print the typedef qualified name you want to test.

In that case, we have some testing for type printing prior to the DWARFTypePrinter work...

Oh, also - LLVM fixes should be tested in LLVM, not in lldb...

Probably the most common way to test this would be to add something in llvm/test/DebugInfo/X86/dwarfdump-*.ll

Oh, this test might be where we test most of this in the past: llvm/test/tools/llvm-dwarfdump/X86/prettyprint_types.s

@ZequanWu
Copy link
Contributor Author

ZequanWu commented Nov 22, 2024

Just noticed that this breaks llvm/test/tools/dsymutil/X86/DWARFLinkerParallel/odr-string.test because for DW_TAG_typedef, llvm-dwarfdump will print the fully qualified name instead of the base name in DW_AT_type that refers to it(here). Do you think we should keep the original bahavior by using the base name of typedef? Or update the test to accept fully qualified name?

metaflow added a commit that referenced this pull request Nov 22, 2024
…mplate names with DWARFTypePrinter (#117071)"

This reverts commit f06c187.

Temporary revert: there is #117239 that is suppose to fix the issue.
Reverting to keep things rolling.
@metaflow
Copy link
Contributor

FYI I have temporary reverted f06c187 to keep things rolling

@dwblaikie
Copy link
Collaborator

Just noticed that this breaks llvm/test/tools/dsymutil/X86/DWARFLinkerParallel/odr-string.test because for DW_TAG_typedef, llvm-dwarfdump will print the fully qualified name instead of the base name in DW_AT_type that refers to it(here). Do you think we should keep the original bahavior by using the base name of typedef? Or update the test to accept fully qualified name?

@JDevlieghere can probably speak more to that, but sounds very golden-file-y & can probably be updated.

I take it it's this part of the test?

CHECK:DW_TAG_reference_type

CHECK: 0x[[CONST_STR_REF:[0-9a-f]*]]: DW_TAG_reference_type{{.*[[:space:]].*}}DW_AT_type{{.*}}0x[[CONST_STRING:[0-9a-f]*]] "const string"

CHECK:DW_TAG_const_type

CHECK: 0x[[CONST_STRING]]: DW_TAG_const_type{{.*[[:space:]].*}}DW_AT_type{{.*}}0x[[STRING]] "string"

Where those names go from "const string" to "const std::__1::string" (and from "string" to "std::__1::string")? or something like that.

@ZequanWu
Copy link
Contributor Author

Where those names go from "const string" to "const std::__1::string" (and from "string" to "std::__1::string")? or something like that.

Yes. In some cases, it's getting super verbose, e.g: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::value_type.

Probably the most common way to test this would be to add something in llvm/test/DebugInfo/X86/dwarfdump-*.ll

Oh, this test might be where we test most of this in the past: llvm/test/tools/llvm-dwarfdump/X86/prettyprint_types.s

Because DWARTTypePrinter is used by both llvm-dwarfump and lldb (after a reland), how about just test DWARTTypePrinter in llvm/unittests/DebugInfo/DWARF/DWARFDieTest.cpp as an unit test?

@dwblaikie
Copy link
Collaborator

Where those names go from "const string" to "const std::__1::string" (and from "string" to "std::__1::string")? or something like that.

Yes. In some cases, it's getting super verbose, e.g: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::value_type.

Oh yeah, still good though.

Probably the most common way to test this would be to add something in llvm/test/DebugInfo/X86/dwarfdump-*.ll
Oh, this test might be where we test most of this in the past: llvm/test/tools/llvm-dwarfdump/X86/prettyprint_types.s

Because DWARTTypePrinter is used by both llvm-dwarfump and lldb (after a reland), how about just test DWARTTypePrinter in llvm/unittests/DebugInfo/DWARF/DWARFDieTest.cpp as an unit test?

Perhaps - if it's easy to write/legible. Though I don't think we should feel bad about somewhat "indirectly" testing libDebugInfoDWARF via llvm-dwarfdump tests, rather than restricting ourselves to API tests for bugs in libDebugInfoDWARF. So whichever's more legible/maintainable.

@ZequanWu ZequanWu force-pushed the fix-dwarf-type-printer branch from 5ecdcda to f006f03 Compare November 25, 2024 19:25
@ZequanWu
Copy link
Contributor Author

Moved the unit tests for DWARFTypePrinter from lldb to llvm/unittests/DebugInfo/DWARF/DWARFDieTest.cpp.

@ZequanWu ZequanWu merged commit 9de73b2 into llvm:main Nov 25, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants