Michael137 created this revision.
Michael137 added a reviewer: aprantl.
Herald added a project: All.
Michael137 requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145245

Files:
  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
  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

Index: lldb/test/API/lang/cpp/hide_global_ranges_vars/main.cpp
===================================================================
--- /dev/null
+++ 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;
+}
Index: lldb/test/API/lang/cpp/hide_global_ranges_vars/TestHideGlobalRangesVars.py
===================================================================
--- /dev/null
+++ 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)
Index: lldb/test/API/lang/cpp/hide_global_ranges_vars/Makefile
===================================================================
--- /dev/null
+++ 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
Index: lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h
===================================================================
--- lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h
+++ lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h
@@ -77,6 +77,8 @@
   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
Index: lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp
===================================================================
--- lldb/source/Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.cpp
+++ 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,18 @@
 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_globale_ranges_pattern(
+      "std::__[[:alnum:]]+::ranges(::views)*::__cpo");
+
+  // return ignore_globale_ranges_pattern.Execute(name);
+  return false;
+}
+
 bool CPPLanguageRuntime::IsAllowedRuntimeValue(ConstString name) {
   return name == g_this;
 }
Index: lldb/source/Core/ValueObject.cpp
===================================================================
--- lldb/source/Core/ValueObject.cpp
+++ lldb/source/Core/ValueObject.cpp
@@ -1589,12 +1589,20 @@
   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;
 
Index: lldb/include/lldb/Target/LanguageRuntime.h
===================================================================
--- lldb/include/lldb/Target/LanguageRuntime.h
+++ lldb/include/lldb/Target/LanguageRuntime.h
@@ -151,6 +151,8 @@
   /// from the user interface.
   virtual bool IsAllowedRuntimeValue(ConstString name) { return false; }
 
+  virtual bool ShouldHideVariable(llvm::StringRef name) const { return false; }
+
   virtual std::optional<CompilerType> GetRuntimeType(CompilerType base_type) {
     return std::nullopt;
   }
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to