Skip to content

Conversation

@Michael137
Copy link
Member

We almost had RTTI support for DWARFASTParserClang, but because classof was protected, using llvm::cast/etc. on it would fail to compile with:

llvm/include/llvm/Support/Casting.h:64:57: error: 'classof' is a protected member of 'DWARFASTParserClang'
   64 |   static inline bool doit(const From &Val) { return To::classof(&Val); }
      |                                                         ^
llvm/include/llvm/Support/Casting.h:110:32: note: in instantiation of member function 'llvm::isa_impl<DWARFASTParserClang, lldb_private::plugin::dwarf::DWARFASTParser>::doit' requested here
  110 |     return isa_impl<To, From>::doit(*Val);

This patch makes classof public and turns static_casts of DWARFASTParserClang into llvm::casts.

We almost had RTTI support for `DWARFASTParserClang`, but because
`classof` was protected, using `llvm::cast`/etc. on it would fail to
compile with:
```
llvm/include/llvm/Support/Casting.h:64:57: error: 'classof' is a protected member of 'DWARFASTParserClang'
   64 |   static inline bool doit(const From &Val) { return To::classof(&Val); }
      |                                                         ^
llvm/include/llvm/Support/Casting.h:110:32: note: in instantiation of member function 'llvm::isa_impl<DWARFASTParserClang, lldb_private::plugin::dwarf::DWARFASTParser>::doit' requested here
  110 |     return isa_impl<To, From>::doit(*Val);
```

This patch makes `classof` public and turns `static_cast`s of
`DWARFASTParserClang` into `llvm::cast`s.
@llvmbot
Copy link
Member

llvmbot commented Dec 2, 2025

@llvm/pr-subscribers-lldb

Author: Michael Buch (Michael137)

Changes

We almost had RTTI support for DWARFASTParserClang, but because classof was protected, using llvm::cast/etc. on it would fail to compile with:

llvm/include/llvm/Support/Casting.h:64:57: error: 'classof' is a protected member of 'DWARFASTParserClang'
   64 |   static inline bool doit(const From &amp;Val) { return To::classof(&amp;Val); }
      |                                                         ^
llvm/include/llvm/Support/Casting.h:110:32: note: in instantiation of member function 'llvm::isa_impl&lt;DWARFASTParserClang, lldb_private::plugin::dwarf::DWARFASTParser&gt;::doit' requested here
  110 |     return isa_impl&lt;To, From&gt;::doit(*Val);

This patch makes classof public and turns static_casts of DWARFASTParserClang into llvm::casts.


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

5 Files Affected:

  • (modified) lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp (+4-6)
  • (modified) lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h (+5-4)
  • (modified) lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp (+5-6)
  • (modified) lldb/unittests/Symbol/TestClangASTImporter.cpp (+1-1)
  • (modified) lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp (+2-2)
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 36aa49ac3de95..7160c6eec564b 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -3707,12 +3707,10 @@ bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
     }
   }
 
-  DWARFASTParserClang *src_dwarf_ast_parser =
-      static_cast<DWARFASTParserClang *>(
-          SymbolFileDWARF::GetDWARFParser(*src_class_die.GetCU()));
-  DWARFASTParserClang *dst_dwarf_ast_parser =
-      static_cast<DWARFASTParserClang *>(
-          SymbolFileDWARF::GetDWARFParser(*dst_class_die.GetCU()));
+  auto *src_dwarf_ast_parser = llvm::cast<DWARFASTParserClang>(
+      SymbolFileDWARF::GetDWARFParser(*src_class_die.GetCU()));
+  auto *dst_dwarf_ast_parser = llvm::cast<DWARFASTParserClang>(
+      SymbolFileDWARF::GetDWARFParser(*dst_class_die.GetCU()));
   auto link = [&](DWARFDIE src, DWARFDIE dst) {
     auto &die_to_type = dst_class_die.GetDWARF()->GetDIEToType();
     clang::DeclContext *dst_decl_ctx =
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h
index f5f707129d67d..6eb2b6b48787b 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h
@@ -47,6 +47,11 @@ class DWARFASTParserClang : public lldb_private::plugin::dwarf::DWARFASTParser {
 
   ~DWARFASTParserClang() override;
 
+  // LLVM RTTI support
+  static bool classof(const DWARFASTParser *Parser) {
+    return Parser->GetKind() == Kind::DWARFASTParserClang;
+  }
+
   // DWARFASTParser interface.
   lldb::TypeSP
   ParseTypeFromDWARF(const lldb_private::SymbolContext &sc,
@@ -264,10 +269,6 @@ class DWARFASTParserClang : public lldb_private::plugin::dwarf::DWARFASTParser {
   lldb::ModuleSP
   GetModuleForType(const lldb_private::plugin::dwarf::DWARFDIE &die);
 
-  static bool classof(const DWARFASTParser *Parser) {
-    return Parser->GetKind() == Kind::DWARFASTParserClang;
-  }
-
 private:
   struct FieldInfo {
     /// Size in bits that this field occupies. Can but
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
index a8654869d6093..69951ee03357e 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -1560,8 +1560,8 @@ bool SymbolFileDWARF::HasForwardDeclForCompilerType(
   auto clang_type_system = compiler_type.GetTypeSystem<TypeSystemClang>();
   if (!clang_type_system)
     return false;
-  DWARFASTParserClang *ast_parser =
-      static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
+  auto *ast_parser =
+      llvm::cast<DWARFASTParserClang>(clang_type_system->GetDWARFParser());
   return ast_parser->GetClangASTImporter().CanImport(compiler_type);
 }
 
@@ -1569,8 +1569,8 @@ bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
   auto clang_type_system = compiler_type.GetTypeSystem<TypeSystemClang>();
   if (clang_type_system) {
-    DWARFASTParserClang *ast_parser =
-        static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser());
+    auto *ast_parser =
+        llvm::cast<DWARFASTParserClang>(clang_type_system->GetDWARFParser());
     if (ast_parser &&
         ast_parser->GetClangASTImporter().CanImport(compiler_type))
       return ast_parser->GetClangASTImporter().CompleteType(compiler_type);
@@ -1614,8 +1614,7 @@ bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) {
 
   if (decl_die != def_die) {
     GetDIEToType()[def_die.GetDIE()] = type;
-    DWARFASTParserClang *ast_parser =
-        static_cast<DWARFASTParserClang *>(dwarf_ast);
+    auto *ast_parser = llvm::cast<DWARFASTParserClang>(dwarf_ast);
     ast_parser->MapDeclDIEToDefDIE(decl_die, def_die);
   }
 
diff --git a/lldb/unittests/Symbol/TestClangASTImporter.cpp b/lldb/unittests/Symbol/TestClangASTImporter.cpp
index f1b3d7911c4bd..07c42088b9101 100644
--- a/lldb/unittests/Symbol/TestClangASTImporter.cpp
+++ b/lldb/unittests/Symbol/TestClangASTImporter.cpp
@@ -287,7 +287,7 @@ TEST_F(TestClangASTImporter, RecordLayoutFromOrigin) {
   clang_utils::SourceASTWithRecord source;
 
   auto *dwarf_parser =
-      static_cast<DWARFASTParserClang *>(source.ast->GetDWARFParser());
+      llvm::cast<DWARFASTParserClang>(source.ast->GetDWARFParser());
   auto &importer = dwarf_parser->GetClangASTImporter();
 
   // Set the layout for the origin decl in the origin ClangASTImporter.
diff --git a/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp b/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp
index cef3a25a4a960..298dfe3a6fdd5 100644
--- a/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp
+++ b/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp
@@ -1257,7 +1257,7 @@ TEST_F(DWARFASTParserClangTests, TestParseSubroutine_ExplicitObjectParameter) {
   ASSERT_TRUE(static_cast<bool>(ts_or_err));
   llvm::consumeError(ts_or_err.takeError());
   auto *parser =
-      static_cast<DWARFASTParserClang *>((*ts_or_err)->GetDWARFParser());
+      llvm::cast<DWARFASTParserClang>((*ts_or_err)->GetDWARFParser());
 
   auto context_die = cu_die.GetFirstChild();
   ASSERT_TRUE(context_die.IsValid());
@@ -1434,7 +1434,7 @@ TEST_F(DWARFASTParserClangTests, TestParseSubroutine_ParameterCreation) {
   llvm::consumeError(ts_or_err.takeError());
 
   auto *ts = static_cast<TypeSystemClang *>(ts_or_err->get());
-  auto *parser = static_cast<DWARFASTParserClang *>(ts->GetDWARFParser());
+  auto *parser = llvm::cast<DWARFASTParserClang>(ts->GetDWARFParser());
 
   auto subprogram = cu_die.GetFirstChild();
   ASSERT_TRUE(subprogram.IsValid());

Michael137 added a commit to Michael137/llvm-project that referenced this pull request Dec 2, 2025
… structure

Depends on:
* llvm#170249

We keep repeating the boilerplate of creating a `DWARFASTParserClangStub` and `TypeSystemClangHolder` in all the unit-test cases. Lets extract this into a helper to make the tests easier to grok.
@Michael137 Michael137 merged commit ac19d38 into llvm:main Dec 3, 2025
12 checks passed
@Michael137 Michael137 deleted the lldb/dwarf-rtti branch December 3, 2025 00:50
Michael137 added a commit to Michael137/llvm-project that referenced this pull request Dec 3, 2025
… structure

Depends on:
* llvm#170249

We keep repeating the boilerplate of creating a `DWARFASTParserClangStub` and `TypeSystemClangHolder` in all the unit-test cases. Lets extract this into a helper to make the tests easier to grok.
Michael137 added a commit that referenced this pull request Dec 3, 2025
… structure (#170132)

Depends on:
* #170249

We keep repeating the boilerplate of creating a
`DWARFASTParserClangStub` and `TypeSystemClangHolder` in all the
unit-test cases. Lets extract this into a helper to make the tests
easier to grok.

We actually only need the `DWARFASTParserClangStub` and a
`TypeSystemClangHolder` in one of the test cases. For the rest, we can
just re-use the typesystem/parser that the `YAMLModuleTester` created.
Re-using them makes it more straightforward to write test-cases because
we don't need to worry about which TypeSystem which DWARFParser created
types into.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Dec 3, 2025
…into helper structure (#170132)

Depends on:
* llvm/llvm-project#170249

We keep repeating the boilerplate of creating a
`DWARFASTParserClangStub` and `TypeSystemClangHolder` in all the
unit-test cases. Lets extract this into a helper to make the tests
easier to grok.

We actually only need the `DWARFASTParserClangStub` and a
`TypeSystemClangHolder` in one of the test cases. For the rest, we can
just re-use the typesystem/parser that the `YAMLModuleTester` created.
Re-using them makes it more straightforward to write test-cases because
we don't need to worry about which TypeSystem which DWARFParser created
types into.
Michael137 added a commit to Michael137/llvm-project that referenced this pull request Dec 3, 2025
… structure

Depends on:
* llvm#170249

We keep repeating the boilerplate of creating a `DWARFASTParserClangStub` and `TypeSystemClangHolder` in all the unit-test cases. Lets extract this into a helper to make the tests easier to grok.
Michael137 added a commit to swiftlang/llvm-project that referenced this pull request Dec 3, 2025
…ort (llvm#170249)

We almost had RTTI support for `DWARFASTParserClang`, but because
`classof` was protected, using `llvm::cast`/etc. on it would fail to
compile with:
```
llvm/include/llvm/Support/Casting.h:64:57: error: 'classof' is a protected member of 'DWARFASTParserClang'
   64 |   static inline bool doit(const From &Val) { return To::classof(&Val); }
      |                                                         ^
llvm/include/llvm/Support/Casting.h:110:32: note: in instantiation of member function 'llvm::isa_impl<DWARFASTParserClang, lldb_private::plugin::dwarf::DWARFASTParser>::doit' requested here
  110 |     return isa_impl<To, From>::doit(*Val);
```

This patch makes `classof` public and turns `static_cast`s of
`DWARFASTParserClang` into `llvm::cast`s.

(cherry picked from commit ac19d38)
Michael137 added a commit to swiftlang/llvm-project that referenced this pull request Dec 3, 2025
… structure (llvm#170132)

Depends on:
* llvm#170249

We keep repeating the boilerplate of creating a
`DWARFASTParserClangStub` and `TypeSystemClangHolder` in all the
unit-test cases. Lets extract this into a helper to make the tests
easier to grok.

We actually only need the `DWARFASTParserClangStub` and a
`TypeSystemClangHolder` in one of the test cases. For the rest, we can
just re-use the typesystem/parser that the `YAMLModuleTester` created.
Re-using them makes it more straightforward to write test-cases because
we don't need to worry about which TypeSystem which DWARFParser created
types into.

(cherry picked from commit 7685e1f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants