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

    <tr>
        <th>Summary</th>
        <td>
            [clang] Lambda function debuginfo isn't created on the right scope
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    David Blaikie mentioned in a [2020 bug report](https://github.com/llvm/llvm-project/issues/44236) that lambda functions were not declared in the correct lexical block, but no bug report seems to have been filed. The example provided in the report doesn't work locally, so I've updated to the following code:

```
template <typename T> 
int f (T&& func) { 
    int x = 0;
    func(&x);
    return x;
}

int main(){
 volatile int x = 0;
    volatile int y = 1;
    if (y) {
        int s = 9;
        x = f( [&] (int *p) {
            *p += s;
 });
    }
    return 0;
}
```

In this situation, the lambda should be declared inside the lexical block for the if statement. However, using llvm-dwarfdump, we see the following dwarf:
```
0x00000023:   DW_TAG_subprogram
 DW_AT_low_pc    (0x0000000000001130)
                DW_AT_high_pc (0x000000000000117c)
                DW_AT_frame_base        (DW_OP_reg6 RBP)
                DW_AT_name      ("main")
 DW_AT_decl_file ("/home/blarsen/t.cc")
                DW_AT_decl_line (10)
                DW_AT_type      (0x000000b4 "int")
 DW_AT_external  (true)

0x00000033:     DW_TAG_variable
 DW_AT_location        (DW_OP_fbreg -8)
                  DW_AT_name ("x")
                  DW_AT_decl_file ("/home/blarsen/t.cc")
                  DW_AT_decl_line       (11)
 DW_AT_type    (0x0000167e "volatile int")

0x0000003f:     DW_TAG_variable
 DW_AT_location        (DW_OP_fbreg -12)
                  DW_AT_name ("y")
                  DW_AT_decl_file ("/home/blarsen/t.cc")
                  DW_AT_decl_line       (12)
 DW_AT_type    (0x0000167e "volatile int")

0x0000004b:     DW_TAG_lexical_block
 DW_AT_low_pc  (0x0000000000001159)
                  DW_AT_high_pc (0x0000000000001174)

0x00000051:       DW_TAG_variable
 DW_AT_location      (DW_OP_fbreg -16)
 DW_AT_name  ("s")
                    DW_AT_decl_file ("/home/blarsen/t.cc")
                    DW_AT_decl_line     (14)
 DW_AT_type  (0x000000b4 "int")

0x0000005c: NULL
0x0000005d:     DW_TAG_class_type
 DW_AT_calling_convention      (DW_CC_pass_by_value)
 DW_AT_byte_size       (0x08)
                  DW_AT_decl_file ("/home/blarsen/t.cc")
                  DW_AT_decl_line (15)

0x00000062:       DW_TAG_member
                    DW_AT_name ("s")
                    DW_AT_type  (0x000000af "int &")
 DW_AT_decl_file     ("/home/blarsen/t.cc")
 DW_AT_decl_line     (16)
                    DW_AT_data_member_location (0x00)

0x0000006b:       DW_TAG_subprogram
 DW_AT_name  ("operator()")
                    DW_AT_decl_file ("/home/blarsen/t.cc")
                    DW_AT_decl_line     (15)
 DW_AT_declaration   (true)
 DW_AT_accessibility (DW_ACCESS_public)

0x00000070: DW_TAG_formal_parameter
                      DW_AT_type (0x000000b8 "const class  *")
                      DW_AT_artificial (true)

0x00000075:         DW_TAG_formal_parameter
 DW_AT_type        (0x000000c2 "int *")

0x0000007a: NULL

0x0000007b:       NULL
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMmF2vmzgTxz-NczPqERgI5CIX55w0faqn2q22WfUSGTMk3hqMbJOX_fQrA0kgLyfd3a5UdJSjwIw9_s3fwzjMGLGuEOckeiHRYsIau1F6_mGH1WqD_98hVpNM5Yf5gm1FDi-SiW8CocTKClVhDqICBiR6oR71IGvWoLFW2pJoQWiysbY2JHgmdEnoci3spsmeuCoJXUq5Pf57V2v1B3JL6FIY06AhdBmGNJgSOgO7YRYkK7OcQdFU3E1rYIcaoVIWcuSS6S4Ou0HgSmvkFiTuBWcSMqn4N0JfIWssVGoQIRjE0oBVsGFbhAyxgkJIzJ9gtUHAPStriVBrtRX5eYLeOVdoKkJjCzulv4FUnEl5cBMZBR8JjbcITZ0zi7mb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