aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
diff options
context:
space:
mode:
authorBalazs Benics <balazs.benics@sigmatechnology.se>2021-11-29 09:56:43 +0100
committerBalazs Benics <balazs.benics@sigmatechnology.se>2021-11-29 09:56:43 +0100
commita8120a771143c15480b508c19a14c0c85a36378c (patch)
treea1a0a4e23ced33e5d85aa8cf35579eecc3c3ec58 /llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
parente1d0673aeeece138d4865385a24a86f6954dff72 (diff)
downloadllvm-a8120a771143c15480b508c19a14c0c85a36378c.zip
llvm-a8120a771143c15480b508c19a14c0c85a36378c.tar.gz
llvm-a8120a771143c15480b508c19a14c0c85a36378c.tar.bz2
[clang-tidy] Ignore narrowing conversions in case of bitfields
Bitfields are special. Due to integral promotion [conv.prom/5] bitfield member access expressions are frequently wrapped by an implicit cast to `int` if that type can represent all the values of the bitfield. Consider these examples: struct SmallBitfield { unsigned int id : 4; }; x.id & 1; (case-1) x.id & 1u; (case-2) x.id << 1u; (case-3) (unsigned)x.id << 1; (case-4) Due to the promotion rules, we would get a warning for case-1. It's debatable how useful this is, but the user at least has a convenient way of //fixing// it by adding the `u` unsigned-suffix to the literal as demonstrated by case-2. However, this won't work for shift operators like the one in case-3. In case of a normal binary operator, both operands contribute to the result type. However, the type of the shift expression is the promoted type of the left operand. One could still suppress this superfluous warning by explicitly casting the bitfield member access as case-4 demonstrates, but why? The compiler already knew that the value from the member access should safely fit into an `int`, why do we have this warning in the first place? So, hereby we suppress this specific scenario, when a bitfield's value is implicitly cast to int (likely due to integral promotion). Note that the bitshift operation might invoke unspecified/undefined behavior, but that's another topic, this checker is about detecting conversion-related defects. Example AST for `x.id << 1`: BinaryOperator 'int' '<<' |-ImplicitCastExpr 'int' <IntegralCast> | `-ImplicitCastExpr 'unsigned int' <LValueToRValue> | `-MemberExpr 'unsigned int' lvalue bitfield .id | `-DeclRefExpr 'SmallBitfield' lvalue ParmVar 'x' 'SmallBitfield' `-IntegerLiteral 'int' 1 Reviewed By: courbet Differential Revision: https://reviews.llvm.org/D114105
Diffstat (limited to 'llvm/lib/Transforms/Utils/BasicBlockUtils.cpp')
0 files changed, 0 insertions, 0 deletions