Skip to content

[lldb][NFC] Simplify DWARRFDeclContext::GetQualifiedName #74788

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

Merged

Conversation

felipepiovezan
Copy link
Contributor

This commit factors out the logic building each component of a qualified name into its own function so that it may be reused by a future commit, while also simplifying the logic of assembling these pieces together by using llvm::interleave.

@llvmbot
Copy link
Member

llvmbot commented Dec 7, 2023

@llvm/pr-subscribers-lldb

Author: Felipe de Azevedo Piovezan (felipepiovezan)

Changes

This commit factors out the logic building each component of a qualified name into its own function so that it may be reused by a future commit, while also simplifying the logic of assembling these pieces together by using llvm::interleave.


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

1 Files Affected:

  • (modified) lldb/source/Plugins/SymbolFile/DWARF/DWARFDeclContext.cpp (+20-20)
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDeclContext.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDeclContext.cpp
index 44e76022790130..eda2ff3e73d47c 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDeclContext.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDeclContext.cpp
@@ -7,10 +7,28 @@
 //===----------------------------------------------------------------------===//
 
 #include "DWARFDeclContext.h"
+#include "llvm/Support/raw_ostream.h"
+
 
 using namespace lldb_private::dwarf;
 using namespace lldb_private::plugin::dwarf;
 
+/// Returns the name of `entry` if it has one, or the appropriate "anonymous
+/// {namespace, class, struct, union}".
+static const char *GetName(const DWARFDeclContext::Entry &entry) {
+  if (entry.name != nullptr)
+    return entry.name;
+  if (entry.tag == DW_TAG_namespace)
+    return "(anonymous namespace)";
+  if (entry.tag == DW_TAG_class_type)
+    return "(anonymous class)";
+  if (entry.tag == DW_TAG_structure_type)
+    return "(anonymous struct)";
+  if (entry.tag == DW_TAG_union_type)
+    return "(anonymous union)";
+  return "(anonymous)";
+}
+
 const char *DWARFDeclContext::GetQualifiedName() const {
   if (m_qualified_name.empty()) {
     // The declaration context array for a class named "foo" in namespace
@@ -26,26 +44,8 @@ const char *DWARFDeclContext::GetQualifiedName() const {
           m_qualified_name.append(m_entries[0].name);
         }
       } else {
-        collection::const_reverse_iterator pos;
-        collection::const_reverse_iterator begin = m_entries.rbegin();
-        collection::const_reverse_iterator end = m_entries.rend();
-        for (pos = begin; pos != end; ++pos) {
-          if (pos != begin)
-            m_qualified_name.append("::");
-          if (pos->name == nullptr) {
-            if (pos->tag == DW_TAG_namespace)
-              m_qualified_name.append("(anonymous namespace)");
-            else if (pos->tag == DW_TAG_class_type)
-              m_qualified_name.append("(anonymous class)");
-            else if (pos->tag == DW_TAG_structure_type)
-              m_qualified_name.append("(anonymous struct)");
-            else if (pos->tag == DW_TAG_union_type)
-              m_qualified_name.append("(anonymous union)");
-            else
-              m_qualified_name.append("(anonymous)");
-          } else
-            m_qualified_name.append(pos->name);
-        }
+        llvm::raw_string_ostream string_stream(m_qualified_name);
+        llvm::interleave(llvm::reverse(m_entries), string_stream, GetName, "::");
       }
     }
   }

Copy link

github-actions bot commented Dec 7, 2023

:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.

@felipepiovezan felipepiovezan force-pushed the felipe/simplify_get_qualified_name branch from 89c88d1 to c10c3f0 Compare December 8, 2023 00:29
Copy link
Member

@bulbazord bulbazord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about this overload of llvm::interleave!!!

@felipepiovezan felipepiovezan force-pushed the felipe/simplify_get_qualified_name branch from c10c3f0 to 5a5309c Compare December 11, 2023 12:02
This commit factors out the logic building each component of a qualified name
into its own function so that it may be reused by a future commit, while also
simplifying the logic of assembling these pieces together by using
llvm::interleave.
@felipepiovezan felipepiovezan force-pushed the felipe/simplify_get_qualified_name branch from 5a5309c to 19452a7 Compare December 11, 2023 12:03
Copy link
Member

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@felipepiovezan felipepiovezan merged commit 06d6af7 into llvm:main Dec 11, 2023
@felipepiovezan felipepiovezan deleted the felipe/simplify_get_qualified_name branch December 11, 2023 14:16
felipepiovezan added a commit to felipepiovezan/llvm-project that referenced this pull request Jan 31, 2024
This commit factors out the logic building each component of a qualified
name into its own function so that it may be reused by a future commit,
while also simplifying the logic of assembling these pieces together by
using llvm::interleave.

(cherry picked from commit 06d6af7)
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.

4 participants