[llvm] [DWARF] Generalize DWARFTypePrinter to a template class (PR #109459)

Pavel Labath via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 24 01:26:03 PDT 2024


================
@@ -31,37 +49,739 @@ struct DWARFTypePrinter {
   /// Dump the name encoded in the type tag.
   void appendTypeTagName(dwarf::Tag T);
 
-  void appendArrayType(const DWARFDie &D);
+  void appendArrayType(const DieType &D);
 
-  DWARFDie skipQualifiers(DWARFDie D);
+  DieType skipQualifiers(DieType D);
 
-  bool needsParens(DWARFDie D);
+  bool needsParens(DieType D);
 
-  void appendPointerLikeTypeBefore(DWARFDie D, DWARFDie Inner, StringRef Ptr);
+  void appendPointerLikeTypeBefore(DieType D, DieType Inner, StringRef Ptr);
 
-  DWARFDie appendUnqualifiedNameBefore(DWARFDie D,
-                                       std::string *OriginalFullName = nullptr);
+  DieType appendUnqualifiedNameBefore(DieType D,
+                                      std::string *OriginalFullName = nullptr);
 
-  void appendUnqualifiedNameAfter(DWARFDie D, DWARFDie Inner,
+  void appendUnqualifiedNameAfter(DieType D, DieType Inner,
                                   bool SkipFirstParamIfArtificial = false);
-  void appendQualifiedName(DWARFDie D);
-  DWARFDie appendQualifiedNameBefore(DWARFDie D);
-  bool appendTemplateParameters(DWARFDie D, bool *FirstParameter = nullptr);
-  void decomposeConstVolatile(DWARFDie &N, DWARFDie &T, DWARFDie &C,
-                              DWARFDie &V);
-  void appendConstVolatileQualifierAfter(DWARFDie N);
-  void appendConstVolatileQualifierBefore(DWARFDie N);
+  void appendQualifiedName(DieType D);
+  DieType appendQualifiedNameBefore(DieType D);
+  bool appendTemplateParameters(DieType D, bool *FirstParameter = nullptr);
+  void appendAndTerminateTemplateParameters(DieType D);
+  void decomposeConstVolatile(DieType &N, DieType &T, DieType &C, DieType &V);
+  void appendConstVolatileQualifierAfter(DieType N);
+  void appendConstVolatileQualifierBefore(DieType N);
 
   /// Recursively append the DIE type name when applicable.
-  void appendUnqualifiedName(DWARFDie D,
+  void appendUnqualifiedName(DieType D,
                              std::string *OriginalFullName = nullptr);
 
-  void appendSubroutineNameAfter(DWARFDie D, DWARFDie Inner,
+  void appendSubroutineNameAfter(DieType D, DieType Inner,
                                  bool SkipFirstParamIfArtificial, bool Const,
                                  bool Volatile);
-  void appendScopes(DWARFDie D);
+  void appendScopes(DieType D);
 };
 
+template <typename DieType>
+void DWARFTypePrinter<DieType>::appendTypeTagName(dwarf::Tag T) {
+  StringRef TagStr = TagString(T);
+  static constexpr StringRef Prefix = "DW_TAG_";
+  static constexpr StringRef Suffix = "_type";
+  if (!TagStr.starts_with(Prefix) || !TagStr.ends_with(Suffix))
+    return;
+  OS << TagStr.substr(Prefix.size(),
+                      TagStr.size() - (Prefix.size() + Suffix.size()))
+     << " ";
+}
+
+template <typename DieType>
+void DWARFTypePrinter<DieType>::appendArrayType(const DieType &D) {
+  for (const DieType &C : D.children()) {
+    if (C.getTag() != dwarf::DW_TAG_subrange_type)
+      continue;
+    std::optional<uint64_t> LB;
+    std::optional<uint64_t> Count;
+    std::optional<uint64_t> UB;
+    std::optional<unsigned> DefaultLB;
+    if (std::optional<DWARFFormValue> L = C.find(dwarf::DW_AT_lower_bound))
+      LB = L->getAsUnsignedConstant();
+    if (std::optional<DWARFFormValue> CountV = C.find(dwarf::DW_AT_count))
+      Count = CountV->getAsUnsignedConstant();
+    if (std::optional<DWARFFormValue> UpperV = C.find(dwarf::DW_AT_upper_bound))
+      UB = UpperV->getAsUnsignedConstant();
+    if (std::optional<DWARFFormValue> LV =
+            D.getDwarfUnit()->getUnitDIE().find(dwarf::DW_AT_language))
+      if (std::optional<uint64_t> LC = LV->getAsUnsignedConstant())
+        if ((DefaultLB =
+                 LanguageLowerBound(static_cast<dwarf::SourceLanguage>(*LC))))
+          if (LB && *LB == *DefaultLB)
+            LB = std::nullopt;
+    if (!LB && !Count && !UB)
+      OS << "[]";
+    else if (!LB && (Count || UB) && DefaultLB)
+      OS << '[' << (Count ? *Count : *UB - *DefaultLB + 1) << ']';
+    else {
+      OS << "[[";
+      if (LB)
+        OS << *LB;
+      else
+        OS << '?';
+      OS << ", ";
+      if (Count)
+        if (LB)
+          OS << *LB + *Count;
+        else
+          OS << "? + " << *Count;
+      else if (UB)
+        OS << *UB + 1;
+      else
+        OS << '?';
+      OS << ")]";
+    }
+  }
+  EndedWithTemplate = false;
+}
+
+namespace detail {
----------------
labath wrote:

Now that this is in a header, these might be better off as private static member functions

https://github.com/llvm/llvm-project/pull/109459


More information about the llvm-commits mailing list