<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/135437>135437</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Lock analysis doesn't work with static locks that are member variables
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
afoxley
</td>
</tr>
</table>
<pre>
Little standalone example showing the issue: https://godbolt.org/z/6EvMbrvsr
Given:
```
struct B {
int resource GUARDED_BY(mutex) = 0;
static inline std::mutex mutex;
};
struct A {
void acquire() {
std::lock_guard lock(b.mutex);
b.resource++;
}
B b;
};
int main() {
A a;
a.acquire();
}
```
We get a lock analysis failure:
```
<source>:17:11: warning: writing variable 'resource' requires holding mutex 'mutex' exclusively [-Wthread-safety-analysis]
17 | b.resource++;
| ^
1 warning generated.
ASM generation compiler returned: 0
<source>:17:11: warning: writing variable 'resource' requires holding mutex 'mutex' exclusively [-Wthread-safety-analysis]
17 | b.resource++;
```
Even though we are holding the lock.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUlMGO4zYMhp-GvhAxZMmJJwcflE2yl91Li2LR00K2Oba6sjSVZGfSpy_kcZLOTvsABYSEECjy40-aKgTdW6IatgfYHjM1xcH5Wj27V0PXrHHdtf6iYzSEISrbKeMsIb2q8SVdDe6ibY9xINQhTARC4hDjSwAhgZ-Bn3vXNc7E3Pke-Pkv4Ofdaf7a-Dl4YBKY_KxnssmdSdix9TAZop_aiAeE6gBMIiJqG9FTcJNvCT__Jn85no7fD78DfxqnSK_A9wjiiAzE7UWIKuoWtTXapgK6lEfIxR3fHi2-UB1X455YPhKvwWanO1Ttn5P2BPxpSXdne8u2xjeu_fG9n5TvMJnAn5r8hrjCNfmtEuCHdO7MieXNOmDzES-JMCptPxBIVGsQlb-jfMR4rzAw-Y2wp4hqwURllbkGHfBZaTN5-tgUEJ9WaHECIYsq_RSp6Rflrbb9Ynod01DMymvVGELg1aPYCj0tcAEHZ7rk-NYO4NWqUYX02pop6JnMFWF72HyLgyfVbYJ6pnjd3EBhu0pVVAjVp1sf_kvcf7ogwvYETBY3cuzJkleRuhyYlL9-vV1oZ7F144s25NFTnLyl1Gf8Hwjy70L8NASnmSzGwU39gBdC5ekOkr7rNBp51tWi24u9yqguqrIsWVnsWTbU9LxjZam6hlNZNWX7JHZC7SviLTEudizTNWd8y8qi4KwUBcubXbtv97tWFG3Jiq6AktGotMmNmce0JrJlk9SF2JaiyoxqyIRlPXFu6bLuGc7TtvJ1erRppj5AyYwOMTzCRB0N1V_eTXbnKFjgVcSL8z_wouNwWxKpzoBxUHGRYKSxIX_vWcgmb-qfVpuOw9TkrRuBn1Pa9W_z4t0f1Ebg5wU2AD-v1cw1_zsAAP__0qWowA">