[PATCH] D114105: [clang-tidy] Ignore narrowing conversions in case of bitfields

Balázs Benics via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 17 10:17:26 PST 2021


steakhal created this revision.
steakhal added reviewers: aaron.ballman, whisperity, alexfh, hokein, JonasToth, courbet, ymandel.
Herald added subscribers: carlosgalvezp, martong, rnkovacs, kbarton, kristof.beyls, xazax.hun, nemanjai.
steakhal requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Consider this example:

  struct SmallBitfield { unsigned int id : 4; } x;
  x.id << 1;

The corresponding AST looks like this:

  BinaryOperator 'int' '<<'
  |-ImplicitCastExpr 'int' <IntegralCast>
  | `-ImplicitCastExpr 'unsigned int' <LValueToRValue>
  |   `-MemberExpr 'unsigned int' lvalue bitfield .id
  |     `-DeclRefExpr 'SmallBitfield' lvalue ParmVar 'x' 'SmallBitfield'
  `-IntegerLiteral 'int' 1

There are two implicit casts:

1. LValueToRValue, loading from the bitfield
2. IntegralCast, casting the 'unsigned int' to 'int' to process the bitshift operation

This patch aims to detect this case and ignore it.

The patch might be too restrictive in terms of matching for only this
exact pattern, but I did not find any other similar occurrences of this
kind.


https://reviews.llvm.org/D114105

Files:
  clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions-bitfields.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114105.387976.patch
Type: text/x-patch
Size: 5717 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211117/9ba886ed/attachment.bin>


More information about the cfe-commits mailing list