[clang] Handle constant "pointers" for `__atomic_always_lock_free`/`__atomic_is_lock_free`. (PR #99340)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 17 09:23:09 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), "");
----------------
AaronBallman wrote:
Should we also add some test coverage where the fake pointer is a constant expression but utter nonsense? e.g.,
```
__atomic_always_lock_free(1, (void*)"well would you look at this")
__atomic_always_lock_free(1, (void*)42.0f)
```
(Just to make sure that the conversion correctly ignores these as valid hints.)
https://github.com/llvm/llvm-project/pull/99340
More information about the cfe-commits
mailing list