Make modm compileable with Clang for tooling support - #1349
Conversation
Clang doesn't provide constexpr built-ins for round, ceil and floor yet. GCC and libstd++ had support for constexpr std::round, std::ceil and std::floor before the feature had been added to C++23.
aa4cbe3 to
e3e9e4e
Compare
Rgb, Rgb565, Hsv and Brightness types all circularly depended on each other. The code is ill-formed, but compiled with gcc because of its non-conforming way of instantiating templates. Especially, the code doesn't compile with Clang. All of those classes implemented converting constructors to convert between each other. The dependency circle is broken up by replacing some of those constructors with conversion operators in the other type. Furthermore, conversions between types of different bit widths were disallowed where they were syntactically possible but yielded nonsensical results. In those cases conversion to a smaller type resulted in bitwise truncation. Conversion to a bigger type was done by simple assignment without scaling. TODO: - strongly consider removing error-prone implicit conversions - validate math for correctness
Functionality is provided by the standard library (std::numeric_limits, std::make_signed_t, std::make_unsigned_t)
The alignment was erroneously 1 instead of 2
Deprecate modm::aligned_storage_t which relies on the deprecated type std::aligned_storage. Remove all usages from modm and replace them with a properly aligned std::byte array as recommended by C++ paper P1413R3.
e3e9e4e to
c650a4d
Compare
salkinium
left a comment
There was a problem hiding this comment.
Thanks!
There is the concept of a plan where a modm:build:compiler option would be gcc or clang to help with the compile options here, but it was never necessary until now. That would probably also help with doing gcc_retarget.py correctly.
| modm_extern_c void vector ## _IRQHandler(void) \ | ||
| __attribute__((externally_visible)) __VA_ARGS__; \ | ||
| void vector ## _IRQHandler(void) | ||
| #endif // MODM_COMPILER_CLANG |
There was a problem hiding this comment.
Maybe we can add modm_externally_visible to the utils file, which is meant to abstract these compiler/platform differences.
|
|
||
| template<typename T> | ||
| using UnsignedType = typename detail::MakeUnsigned<T>::type; | ||
| using UnsignedType [[deprecated("use std::make_unsigned_t")]] = typename detail::MakeUnsigned<T>::type; // DEPRECATED: 2026q3 |
There was a problem hiding this comment.
| using UnsignedType [[deprecated("use std::make_unsigned_t")]] = typename detail::MakeUnsigned<T>::type; // DEPRECATED: 2026q3 | |
| using UnsignedType [[deprecated("use std::make_unsigned_t")]] = typename detail::MakeUnsigned<T>::type; // DEPRECATED: 2027q2 |
Historically we've given people 1y before we remove deprecation notices, due to them maybe skipping a few releases.
| using aligned_storage_t = typename aligned_storage<Cap, Align>::type; | ||
| template<std::size_t Cap, std::size_t Align = default_storage_alignment<Cap>> | ||
| using aligned_storage_t [[deprecated("see C++ standards paper P1413R3")]] = | ||
| typename aligned_storage<Cap, Align>::type; // DEPRECATED: 2026q3 |
There was a problem hiding this comment.
| typename aligned_storage<Cap, Align>::type; // DEPRECATED: 2026q3 | |
| typename aligned_storage<Cap, Align>::type; // DEPRECATED: 2027q2 |
|
I tested this PR with:
No more cyclic stuff with RGB, that's so nice. The list of required changes look familiar to my patches (hacks) just to make clangd happy. .clangd: ps: I did not compile my project with llvm, that's no particular goal of mine, just clangd. It doesn't even use the llvm arm-toolchain |
Enable modm to be compiled with Clang >= 22 to allow for use of Clang based tooling (e.g. Clang Tidy) in user projects.
Required changes:
typename,templatekeywords)constexprversions ofstd::round,std::floorandstd::ceilas clang still lacksconstexprsupport for more complex math functionsint32_t==inton Cortex-M with Clang instead ofint32_t==longwith GCC__atomic_is_lock_freefor Clang (If Clang was to be used for compiling binaries to run on devices the atomic implementation would need changes)Further changes:
ArithmeticTraitsavailable in the standard librarymodm::aligned_storage_tsincestd::aligned_storageis deprecated in C++23. Also remove all internal uses.TODO: