<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/84806>84806</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            LLDB: Inconsistant existance of expression global variable (`use of undeclared identifier`)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          royitaqi
      </td>
    </tr>
</table>

<pre>
    # Problem

During LLDB debug session, global variables that are declared in a previous expression evaluation is sometimes reported as undeclared identifier _after_ a program restart.

TL;DR LLDB output (see full output at the end):
```
(lldb) p int $i = 32; $i
(int) 32
...
(lldb) r
...
(lldb) p $i
(int) 32
(lldb) p $i + a
error: <user expression 2>:1:1: use of undeclared identifier '$i'
    1 | $i + a
 | ^
(lldb) p $i
error: <user expression 3>:1:1: use of undeclared identifier '$i'
    1 | $i
      | ^
(lldb) 
```

In the above, the behavior of finding/not finding `$i` seems to change based on two factors:
1. Whether or not the said global variables are used in an expression (instead of just priting a single variable).
2. The order of evaluate a single variable vs. evaluate an expression.


# Repro steps

Create a `main.cpp`:
```
int main() { 
    int a = 1;
    return a;
}
```

Compile:
```
clang++ -g -O0 main.cpp
```

Start `lldb a.out` then type the following comands. The output from LLDB follows.
```
b /main/
r

p a
p int $i = 32; $i
p $i + a
r

p $i
p $i + a
p $i
```

Full LLDB output:
```
(lldb) target create "a.out"
Current executable set to '/Users/royshi/tmp2/a.out' (arm64).
(lldb) b /main/
Breakpoint 1: where = a.out`main + 12 at main.cpp:2:9, address = 0x0000000100003f98
(lldb) r

Process 73517 launched: '/Users/royshi/tmp2/a.out' (arm64)

Process 73517 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
 frame #0: 0x0000000100003f98 a.out`main at main.cpp:2:9
   1    int main() {
-> 2             int a = 1;
   3        return a;
   4    }
(lldb) p a
(int) -2035101492
(lldb) p int $i = 32; $i
(int) 32
(lldb) p $i + a
(int) -2035101460
(lldb) r
There is a running process, kill it and restart?: [Y/n] 
Process 73517 exited with status = 9 (0x00000009) killed
Process 73523 launched: '/Users/royshi/tmp2/a.out' (arm64)
Process 73523 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100003f98 a.out`main at main.cpp:2:9
   1    int main() {
-> 2             int a = 1;
   3        return a;
   4    }
(lldb) p $i
(int) 32
(lldb) p $i + a
error: <user expression 2>:1:1: use of undeclared identifier '$i'
    1 | $i + a
      | ^
(lldb) p $i
error: <user expression 3>:1:1: use of undeclared identifier '$i'
 1 | $i
      | ^
(lldb) 
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUV1tv2zoS_jX0y8CCRPr64IfYroECBbbodrHYp4KSxhZbilR5cZJ_vxjKt9SXc4pTFGiQKBY5HA6_-ebjWHqvdgZxwcZLNl4PZAyNdQtnX1WQ39WgtPXrgnEBH50tNbYsX7P8qX-uo1NmBx8-rJdQYxl34NF7ZQ3jK9hpW0oNe-mULDV6CI0MIB1CjZWWDmtQBiR0DvfKRg_40rl-OeBe6igDfVQevG0xqBY9OOysC1iD9BDN2U-NJqitQgdf5Dag-5L82p2TLTj0QbqQXQb--QMTy_WnPnIbQxcDMD7ziLCNWh-HZIDQIKCpGZ8zcVjMJvnht3_lM63rkvE5dKAMORopYGINgjOxTK8nS2UCGQrej2RZduXE3Z_qHjq7MgTGlyD7SXTOOiaegIlV9Ogu0eZMvGPiqTj8QfQIdnsHYManKQg-7R0DABTApqurLfvB8buHx3gUl_h1cZ3H4G5Yt7Obnu9NIoIs7R6J2_RSYiP3yjoKaatMrcyO8Y2x4fgG5IT2nuTgEVsPwULVSLNDKKXHGqyB8GxhK6tgnT8RrMjgvw2GBh1YB-SR9vNS1ddFRfUU_aGWzCV6iSA-oKwpwq_RB-icChSYBK_MTuPJD-PzA9t4Bp8bBOtqTCc7VCJer4G9z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