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

    <tr>
        <th>Summary</th>
        <td>
            `DILocation::getMergedLocation` produces invalid result while merging locations from `DILexicalBlockFile`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            wrong-debug,
            debuginfo
      </td>
    </tr>

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

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

<pre>
    Consider the following input:
```c
# 1 "1.c" 1
# 1 "1.c" 2
int foo(int a) {
  int i = 0;
  if ((a & 1) == 1) {
    a -= 1;
# 1 "m.c" 1
# 40 "m.c"
i += a;
i -= 10*a;
i *= a*a;
# 6 "1.c" 2
 } else {
    a += 3;
# 1 "m.c" 1
# 40 "m.c"
i += a;
i -= 10*a;
i *= a*a;
# 9 "1.c" 2
 }
  return i;
}
```

The LLVM IR looks like as follows:
```llvm
...
!10 = !DIFile(filename: "1.c", directory: "/Users/asl/Projects/llvm/build/bin")
...
!27 = !DIFile(filename: "m.c", directory: "/Users/asl/Projects/llvm/build/bin")
...
!39 = distinct !DILexicalBlock(scope: !20, file: !10, line: 6, column: 9)
!40 = !DILocation(line: 40, column: 6, scope: !41)
!41 = !DILexicalBlockFile(scope: !39, file: !27, discriminator: 0)
```

Note that here we're having two nested scopes from different files.

The resulting code looks like as:
```
        .loc    1 42 7 prologue_end is_stmt 1   ; 1.c:42:7
        mul     w9, w8, w8
        .loc    1 41 3                          ; 1.c:41:3
        mul     w8, w9, w8
Ltmp2:
        .loc    1 42 3                          ; 1.c:42:3
        add     w8, w8, w8, lsl #3
        neg     w0, w8
Ltmp3:
        ;DEBUG_VALUE: foo:i <- $w0
        .loc    1 10 3                          ; 1.c:10:3
        ret
```
and:
```llvm
!10 = !DIFile(filename: "1.c", directory: "/Users/asl/Projects/llvm/build/bin")
...
!19 = distinct !DILexicalBlock(scope: !9, file: !10, line: 3, column: 7)
...
!22 = !DILocation(line: 42, column: 7, scope: !19)
```
So, after merging we end with invalid location info: line numbers are from one file, but file itself is different – from enclosing scope.

Indeed, it seems that `DILocation::getMergedLocation` completely ignores possibility that file could be changed and recreates the scope with invalid location info. It already has the code that handles `inlinedAt`. It think `DILexicalBlockFile` should be treated in a same way as this is essentially "textual inclusion".
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEVk1v4zYT_jX0ZRCDHCr-OPigJOsXAbIvira71wUtji02FGmQVLz59wUpJZG92e320NYwLHPEmefhfDySitEcHNGGXd-w67uZ6lPrw0ZFO9t5_by59S4aTQFSS7D31vqTcQcw7tgnJmvGa7bgw7fJC5QggCGKecMQQbxjQ8Zr4xLsvWe4yv8UwzWw5Q3jNUA2GGDyDjiTo2kPDFcMVwoYLkCU7fIu7xFTVwAFV4O1eL4id-dsKv5mzGSA4U12U4ObGYNwhvWriWE9bHm15UiLi3MBW94B2UhnnMbw8t9itX6HVSETKPXBgRm3Futr_fJ_Xv_eEjw8fP4I97-C9f4xgjWPBCqO1Y8XVbf2qWO8ns_nBVwIXmrHUNzdb40lhqu9seRUR0zWb8QY3oI2gZrkw_N4h-H2U6QQGW5VtAy3vwT_BzUpGwoObne9sTpfjSsO6yk2Lv8Ku_vHsGVpSdAmJuOaNJB4oK-mUfbG-uaR4So2_jgyEcgzi0xvNIhisMYVwyIvGm_7zuXlekBjKKpJfh98o5LxjuHqxa_i544lzhS3Em-hxCTUhOmYuqmXXF-wxeWQxNgE0xmnkg_5Dh-Dn_fU_30iSK1K0FIgOBHDZSBo1VPWknTy4Cgm0gPRCPvgO9Bmv6dAWSiMpTh_a89AsbcpuzZe03mXXnTnOIL5M7e-yVcBFcISjsFbf-jpCzkNJn6JqUsgAIDJG8gtKusKmayXkxBdb8v1VLJxWo2_72IIkPDdzwRDMFnL9zCG6OtXjIfUHXE43rtH-jk4vIBTWk_hJr82WmAop5sdHYbN_IyVPGfF5M3dh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