[Lldb-commits] [lldb] de10c1a - [lldb] Ignore libcxx std::ranges global variables in frame var

Michael Buch via lldb-commits lldb-commits at lists.llvm.org
Fri Mar 3 09:37:01 PST 2023


Author: Michael Buch
Date: 2023-03-03T17:36:43Z
New Revision: de10c1a824405833a0f49b22e7fa3f32a1393cc3

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

LOG: [lldb] Ignore libcxx std::ranges global variables in frame var

The motivation is to avoid cluttering LLDB's global variable view for
std::ranges users.

Before:
```
(lldb) frame var -g
...
(const std::ranges::__end::__fn) std::__1::ranges::__cpo::end = {}
(const std::ranges::views::__all::__fn) std::__1::ranges::views::__cpo::all = {}
(const std::ranges::__begin::__fn) std::__1::ranges::__cpo::begin = {}
(const std::ranges::views::__take::__fn) std::__1::ranges::views::__cpo::take = {}
(const std::ranges::__max_element::__fn) std::__1::ranges::__cpo::max_element = {}
(const std::ranges::__size::__fn) std::__1::ranges::__cpo::size = {}
(const std::ranges::__data::__fn) std::__1::ranges::__cpo::data = {}
```

After this patch none of these __cpo variables would show up.

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

Added: 
    lldb/test/API/lang/cpp/hide_global_ranges_vars/Makefile
    lldb/test/API/lang/cpp/hide_global_ranges_vars/TestHideGlobalRangesVars.py
    lldb/test/API/lang/cpp/hide_global_ranges_vars/main.cpp

Modified: 
    lldb/include/lldb/Target/LanguageRuntime.h
    lldb/source/Core/ValueObject.cpp
    lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp
    lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Target/LanguageRuntime.h b/lldb/include/lldb/Target/LanguageRuntime.h
index 9cc79dc976c4d..34f0fdb9cfbc9 100644
--- a/lldb/include/lldb/Target/LanguageRuntime.h
+++ b/lldb/include/lldb/Target/LanguageRuntime.h
@@ -151,6 +151,11 @@ class LanguageRuntime : public Runtime, public PluginInterface {
   /// from the user interface.
   virtual bool IsAllowedRuntimeValue(ConstString name) { return false; }
 
+  /// Returns 'true' if we the variable with the specified 'name'
+  /// should be hidden from variable views (e.g., when listing variables in
+  /// 'frame variable' or 'target variable')
+  virtual bool ShouldHideVariable(llvm::StringRef name) const { return false; }
+
   virtual std::optional<CompilerType> GetRuntimeType(CompilerType base_type) {
     return std::nullopt;
   }

diff  --git a/lldb/source/Core/ValueObject.cpp b/lldb/source/Core/ValueObject.cpp
index 6e79a04d024e5..f3ca16781e9ad 100644
--- a/lldb/source/Core/ValueObject.cpp
+++ b/lldb/source/Core/ValueObject.cpp
@@ -1600,12 +1600,20 @@ bool ValueObject::IsRuntimeSupportValue() {
   if (!process)
     return false;
 
+  if (!GetVariable())
+    return false;
+
+  auto *runtime = process->GetLanguageRuntime(GetVariable()->GetLanguage());
+  if (runtime)
+    if (runtime->ShouldHideVariable(GetName().GetStringRef()))
+      return true;
+
   // We trust that the compiler did the right thing and marked runtime support
   // values as artificial.
-  if (!GetVariable() || !GetVariable()->IsArtificial())
+  if (!GetVariable()->IsArtificial())
     return false;
 
-  if (auto *runtime = process->GetLanguageRuntime(GetVariable()->GetLanguage()))
+  if (runtime)
     if (runtime->IsAllowedRuntimeValue(GetName()))
       return false;
 

diff  --git a/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp b/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp
index 0028a51412873..5945c57e5aa74 100644
--- a/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp
@@ -28,6 +28,7 @@
 #include "lldb/Target/StackFrame.h"
 #include "lldb/Target/ThreadPlanRunToAddress.h"
 #include "lldb/Target/ThreadPlanStepInRange.h"
+#include "lldb/Utility/RegularExpression.h"
 #include "lldb/Utility/Timer.h"
 
 using namespace lldb;
@@ -40,6 +41,17 @@ char CPPLanguageRuntime::ID = 0;
 CPPLanguageRuntime::CPPLanguageRuntime(Process *process)
     : LanguageRuntime(process) {}
 
+bool CPPLanguageRuntime::ShouldHideVariable(llvm::StringRef name) const {
+  // Matches the global function objects in std::ranges/std::ranges::views
+  // E.g.,
+  //   std::__1::ranges::views::__cpo::take
+  //   std::__1::ranges::__cpo::max_element
+  static RegularExpression ignore_global_ranges_pattern(
+      "std::__[[:alnum:]]+::ranges(::views)*::__cpo");
+
+  return ignore_globale_ranges_pattern.Execute(name);
+}
+
 bool CPPLanguageRuntime::IsAllowedRuntimeValue(ConstString name) {
   return name == g_this;
 }

diff  --git a/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h b/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h
index 1be58b7bf9ea9..097d1da6e909a 100644
--- a/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h
+++ b/lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h
@@ -77,6 +77,8 @@ class CPPLanguageRuntime : public LanguageRuntime {
   lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread,
                                                   bool stop_others) override;
 
+  bool ShouldHideVariable(llvm::StringRef name) const override;
+
   bool IsAllowedRuntimeValue(ConstString name) override;
 protected:
   // Classes that inherit from CPPLanguageRuntime can see and modify these

diff  --git a/lldb/test/API/lang/cpp/hide_global_ranges_vars/Makefile b/lldb/test/API/lang/cpp/hide_global_ranges_vars/Makefile
new file mode 100644
index 0000000000000..4f79c0a900c3a
--- /dev/null
+++ b/lldb/test/API/lang/cpp/hide_global_ranges_vars/Makefile
@@ -0,0 +1,4 @@
+CXX_SOURCES := main.cpp
+CXXFLAGS_EXTRAS := -std=c++20
+
+include Makefile.rules

diff  --git a/lldb/test/API/lang/cpp/hide_global_ranges_vars/TestHideGlobalRangesVars.py b/lldb/test/API/lang/cpp/hide_global_ranges_vars/TestHideGlobalRangesVars.py
new file mode 100644
index 0000000000000..f7b98a55341df
--- /dev/null
+++ b/lldb/test/API/lang/cpp/hide_global_ranges_vars/TestHideGlobalRangesVars.py
@@ -0,0 +1,27 @@
+"""Test that frame var and target var hide
+the global function objects in the libc++
+ranges implementation"""
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+class HideGlobalRangesVarsTestCase(TestBase):
+
+    @add_test_categories(["libc++"])
+    @skipIf(compiler=no_match("clang"))
+    @skipIf(compiler="clang", compiler_version=['<', '16.0'])
+    def test(self):
+        self.build()
+
+        lldbutil.run_to_source_breakpoint(self, "return", lldb.SBFileSpec('main.cpp', False))
+
+        self.expect("frame variable --show-globals",
+                    substrs=["::ranges::views::__cpo",
+                             "::ranges::__cpo"],
+                    matching=False)
+
+        self.expect("target variable",
+                    substrs=["::ranges::views::__cpo",
+                             "::ranges::__cpo"],
+                    matching=False)

diff  --git a/lldb/test/API/lang/cpp/hide_global_ranges_vars/main.cpp b/lldb/test/API/lang/cpp/hide_global_ranges_vars/main.cpp
new file mode 100644
index 0000000000000..96a05f846396a
--- /dev/null
+++ b/lldb/test/API/lang/cpp/hide_global_ranges_vars/main.cpp
@@ -0,0 +1,9 @@
+#include <ranges>
+
+int main(int argc, char const *argv[]) {
+  int arr[3] = {1, 2, 3};
+  auto arr_view = std::ranges::views::all(arr);
+  auto arr_max = std::ranges::max_element(arr);
+
+  return 0;
+}


        


More information about the lldb-commits mailing list