[clang] Handle constant "pointers" for `__atomic_always_lock_free`/`__atomic_is_lock_free`. (PR #99340)
James Y Knight via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 17 16:32:22 PDT 2024
================
@@ -124,6 +124,24 @@ _Static_assert(__atomic_always_lock_free(4, &i64), "");
_Static_assert(!__atomic_always_lock_free(8, &i32), "");
_Static_assert(__atomic_always_lock_free(8, &i64), "");
+// Validate use with fake pointers constants. This mechanism is used to allow
+// validating atomicity of a given size and alignment.
+_Static_assert(__atomic_is_lock_free(1, (void*)1), "");
+_Static_assert(__atomic_is_lock_free(1, (void*)-1), "");
+_Static_assert(__atomic_is_lock_free(4, (void*)2), ""); // expected-error {{not an integral constant expression}}
+_Static_assert(__atomic_is_lock_free(4, (void*)-2), ""); // expected-error {{not an integral constant expression}}
+_Static_assert(__atomic_is_lock_free(4, (void*)4), "");
+_Static_assert(__atomic_is_lock_free(4, (void*)-4), "");
+
+_Static_assert(__atomic_always_lock_free(1, (void*)1), "");
+_Static_assert(__atomic_always_lock_free(1, (void*)-1), "");
+_Static_assert(!__atomic_always_lock_free(4, (void*)2), "");
+_Static_assert(!__atomic_always_lock_free(4, (void*)-2), "");
+_Static_assert(__atomic_always_lock_free(4, (void*)4), "");
+_Static_assert(__atomic_always_lock_free(4, (void*)-4), "");
----------------
jyknight wrote:
Neat, found a pre-existing bug: `_Static_assert(__atomic_always_lock_free(2, "string"), "");` triggers an assertion failure in clang at head because the type of `"string"` is ConstantArrayType not PointerType, in this cast:
https://github.com/llvm/llvm-project/blob/83fbd79319a4d997520c85ab41997692a58cd958/clang/lib/AST/ExprConstant.cpp#L12960
The argument AST looks like:
```
(gdb) p E->getArg(1)->dump()
ImplicitCastExpr 0x1463b528 'const volatile void *' <BitCast>
`-ImplicitCastExpr 0x1463b510 'char *' <ArrayToPointerDecay>
`-StringLiteral 0x1463b440 'char[7]' lvalue "string"
```
I suppose it shouldn't do "IgnoreImpCasts", and instead only ignore a single level of BitCast ImplicitCastExpr?
https://github.com/llvm/llvm-project/pull/99340
More information about the cfe-commits
mailing list