[Lldb-commits] [lldb] [lldb] Fix dwim-print's arrow-operator check to match the substring (PR #225778)

via lldb-commits lldb-commits at lists.llvm.org
Wed Sep 23 07:00:44 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Yao Qi (qiyao)

<details>
<summary>Changes</summary>

`CommandObjectDWIMPrint::DoExecute` decides whether to try `expr` as a
limited, dot-only frame variable path before falling back to full
expression evaluation. The comment above the check says a variable
path is not attempted if `expr` contains the arrow operator (`->`) or
the subscript operator (`[]`), or a bare `*` or `&`:
```
    const bool try_variable_path =
        expr.find_first_of("*&->[]") == StringRef::npos;
```
`find_first_of` treats its argument as a set of characters so this also
matches a lone `-` or a lone `>` anywhere in `expr`, not only the
two-character `->` sequence the comment describes.

Fix the check to test for the `->` substring directly with
`StringRef::contains`, keeping `find_first_of` only for the four
characters that are genuinely excluded one at a time (`*`, `&`, `[`,
`]`).

By itself, this widens which expressions attempt the variable-path
fast path (anything with a lone `-` or `>` now does).
Ordinary arithmetic and comparison expressions containing a lone `-`
or `>` now correctly attempt, and correctly fail, the variable-path
fast path before falling through to `expression`, shown here
```
    frame #<!-- -->0: liblldb.24.0git.dylib`lldb_private::CommandObjectDWIMPrint::DoExecute(this=<unavailable>, command=<unavailable>, result=<unavailable>) at CommandObjectDWIMPrint.cpp:169:13 [opt]
    (lldb) settings set dwim-print-verbosity full
    (lldb) dwim-print expr
    note: ran `frame variable expr`
    (llvm::StringRef)  (Data = "1+1", Length = 3)
    (lldb) dwim-print expr.Length-1
    warning: `this' is not accessible (substituting 0). Couldn't load 'this' because its value couldn't be evaluated
    note: ran `expression expr.Length-1`
    (size_t) 2
    (lldb) dwim-print expr.Length>0
    warning: `this' is not accessible (substituting 0). Couldn't load 'this' because its value couldn't be evaluated
    note: ran `expression expr.Length>0`
    (bool) true
    (lldb) dwim-print frame->m_frame_index
    warning: `this' is not accessible (substituting 0). Couldn't load 'this' because its value couldn't be evaluated
    note: ran `expression frame->m_frame_index`
    (uint32_t) 0
```
Add more expressions in `TestDWIMPrint.test_expressions`, to cover expressions
containing a lone `-` or `>`.  Note that these tests can't tell "the
expression is evaluated by DIL but failed" and "expression isn't
evaluated by DIL at all".


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


2 Files Affected:

- (modified) lldb/source/Commands/CommandObjectDWIMPrint.cpp (+1-1) 
- (modified) lldb/test/API/commands/dwim-print/TestDWIMPrint.py (+1-1) 


``````````diff
diff --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index 1b0b4c7881cfc1..fd3c84f4cd268e 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.cpp
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
@@ -163,7 +163,7 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
   // both operators can be overloaded in C++, and could result in ambiguity in
   // how the expression is handled. Additionally, `*` and `&` are not supported.
   const bool try_variable_path =
-      expr.find_first_of("*&->[]") == StringRef::npos;
+      !expr.contains("->") && expr.find_first_of("*&[]") == StringRef::npos;
   if (frame && try_variable_path) {
     VariableSP var_sp;
     Status status;
diff --git a/lldb/test/API/commands/dwim-print/TestDWIMPrint.py b/lldb/test/API/commands/dwim-print/TestDWIMPrint.py
index d50c707f33de92..36098bc5941f84 100644
--- a/lldb/test/API/commands/dwim-print/TestDWIMPrint.py
+++ b/lldb/test/API/commands/dwim-print/TestDWIMPrint.py
@@ -79,7 +79,7 @@ def test_expressions(self):
         """Test dwim-print with expressions."""
         self.build()
         lldbutil.run_to_name_breakpoint(self, "main")
-        exprs = ("argc + 1", "(void)argc", "(int)abs(argc)")
+        exprs = ("argc + 1", "(void)argc", "(int)abs(argc)", "argc - 1", "argc > 1")
         for expr in exprs:
             self._expect_cmd(f"dwim-print {expr}", "expression")
 

``````````

</details>


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


More information about the lldb-commits mailing list