<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/120307>120307</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[flang][debug] Incorrect module variable scope when applying "use, only"
</td>
</tr>
<tr>
<th>Labels</th>
<td>
flang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jieljiel
</td>
</tr>
</table>
<pre>
Code example,
```
module mod1
integer :: a = 1
integer :: b = 2
end module
subroutine sub1()
use mod1, only : a
end subroutine
```
When using debugger to inspect variable from inside sub1, only `a` should be visible, but currently flang is giving the whole module imported to sub1.
```
!2 = !DIModule(scope: !4, name: "mod1", file: !3, line: 1)
!11 = distinct !DISubprogram(name: "sub1", linkageName: "sub1_", scope: !3, file: !3, line: 6, type: !12, scopeLine: 6, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !4, retainedNodes: !14)
!15 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !11, entity: !2, file: !3, line: 1)
```
The solution should be straight forward,
(1) change the `tag:` field of `DIImportedEntity` from `DW_TAG_imported_module` to `DW_TAG_imported_declaration`;
(2) change the `entity:` field of `DIImportedEntity` from referencing the module to refencing the variable;
```
!1 = distinct !DIGlobalVariable(name: "a", linkageName: "_QMmod1Ea", scope: !2, file: !3, line: 2, type: !8, isLocal: false, isDefinition: true)
!2 = !DIModule(scope: !4, name: "mod1", file: !3, line: 1)
!11 = distinct !DISubprogram(name: "sub1", linkageName: "sub1_", scope: !3, file: !3, line: 6, type: !12, scopeLine: 6, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !4, retainedNodes: !14)
!15 = !DIImportedEntity(tag: DW_TAG_imported_declaration, scope: !11, entity: !1, file: !3, line: 1)
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVk2P2zYQ_TXUhchCHEm296CDdh0HCyRpiwTN0aDEkcSUIgV-eOv--oKS_LEbN01vPQTY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