Skip to content

Commit

Permalink
[clang-tidy] Keep parentheses when replacing index access in sizeof
Browse files Browse the repository at this point in the history
… calls (#82166)

Fixes #56021.
  • Loading branch information
SimplyDanny committed Feb 18, 2024
1 parent 536d78c commit 5e83b26
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,17 @@ void LoopConvertCheck::doConversion(
ReplaceText = Usage.Kind == Usage::UK_MemberThroughArrow
? VarNameOrStructuredBinding + "."
: VarNameOrStructuredBinding;
auto Parents = Context->getParents(*Usage.Expression);
const DynTypedNodeList Parents = Context->getParents(*Usage.Expression);
if (Parents.size() == 1) {
if (const auto *Paren = Parents[0].get<ParenExpr>()) {
// Usage.Expression will be replaced with the new index variable,
// and parenthesis around a simple DeclRefExpr can always be
// removed.
Range = Paren->getSourceRange();
// removed except in case of a `sizeof` operator call.
const DynTypedNodeList GrandParents = Context->getParents(*Paren);
if (GrandParents.size() != 1 ||
GrandParents[0].get<UnaryExprOrTypeTraitExpr>() == nullptr) {
Range = Paren->getSourceRange();
}
} else if (const auto *UOP = Parents[0].get<UnaryOperator>()) {
// If we are taking the address of the loop variable, then we must
// not use a copy, as it would mean taking the address of the loop's
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ Changes in existing checks
`AllowStringArrays` option, enabling the exclusion of array types with deduced
length initialized from string literals.

- Improved :doc:`modernize-loop-convert
<clang-tidy/checks/modernize/loop-convert>` check by ensuring that fix-its
don't remove parentheses used in ``sizeof`` calls when they have array index
accesses as arguments.

- Improved :doc:`modernize-use-override
<clang-tidy/checks/modernize/use-override>` check to also remove any trailing
whitespace when deleting the ``virtual`` keyword.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ void f() {
// CHECK-FIXES-NEXT: printf("Fibonacci number %d has address %p\n", I, &I);
// CHECK-FIXES-NEXT: Sum += I + 2;

int Matrix[N][12];
unsigned size = 0;
for (int I = 0; I < N; ++I) {
size += sizeof(Matrix[I]);
size += sizeof Matrix[I];
size += sizeof((Matrix[I]));
}
// CHECK-MESSAGES: :[[@LINE-5]]:3: warning: use range-based for loop instead
// CHECK-FIXES: for (auto & I : Matrix)
// CHECK-FIXES-NEXT: size += sizeof(I);
// CHECK-FIXES-NEXT: size += sizeof I;
// CHECK-FIXES-NEXT: size += sizeof(I);

Val Teas[N];
for (int I = 0; I < N; ++I) {
Teas[I].g();
Expand Down

0 comments on commit 5e83b26

Please sign in to comment.