This improves upon https://reviews.llvm.org/D21099, which taught -Wcast-align to look at the aligned attribute of variables. I added a function that computes a more accurate alignment to ExprConstant.cpp.
rdar://problem/59242343
Differential D78767
[Sema] Teach -Wcast-align to compute a more accurate alignment when the source expression has array subscript or pointer arithmetic operators ahatanak on Apr 23 2020, 3:43 PM. Authored by
Details This improves upon https://reviews.llvm.org/D21099, which taught -Wcast-align to look at the aligned attribute of variables. I added a function that computes a more accurate alignment to ExprConstant.cpp. rdar://problem/59242343
Diff Detail
Event Timeline
Comment Actions The method has to compute the correct alignment for variables captured by lambdas or blocks too. Comment Actions Define two helper functions in Sema that compute the alignment of a VarDecl and a constant offset from it and use them instead of the classes for evaluating lvalue and pointer expressions in ExprConstant.cpp. Using the classes in ExprConstant.cpp to compute an expression alignment as I did in the previous patch is probably not a good idea since they are for evaluating constant expressions. They don't return the lvalue base variables and offsets in some cases ( for example, (A *)&m_ref in the test case) and using them for this purpose can make it harder to make changes to the way constant expressions are evaluated. The current patch is far from perfect as it misses many cases that could be detected by the classes in ExprConstant.cpp, but is at least an improvement over what we have now. I was also thinking about fixing the alignment computation of captured variables, but I think I should do that separately in a follow-up as it might not be trivial. It will probably require looking up the captured variables in all the enclosing CapturingScopeInfos.
Comment Actions Address review comments. Handle derived-to-base cast expressions and array subscript expressions that don't have constant indices.
Comment Actions Address most of the review comments.
Comment Actions Other changes look good to me, thanks.
|
Oh, this — and all the other places that do presumed alignment based on a pointee type — needs a special case for C++ records with virtual bases, because you need to get its presumed alignment as a base sub-object, not its presumed alignment as a complete object, which is what getTypeAlignInChars will return. The right way to merge that information is to get the normal alignment — which may be lower than expected if there's a typedef in play — and then min that with the base sub-object alignment.