diff options
author | Danny Mösch <danny.moesch@icloud.com> | 2024-02-18 22:42:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-18 22:42:17 +0100 |
commit | 5e83b26584c9a02f8a2923651d08cad2dcc8479a (patch) | |
tree | d869a409cbc36e235795e1f29b6e453506aa7365 /clang-tools-extra | |
parent | 536d78c213e127e00aa3b120582a3c133e6b0e52 (diff) | |
download | llvm-5e83b26584c9a02f8a2923651d08cad2dcc8479a.zip llvm-5e83b26584c9a02f8a2923651d08cad2dcc8479a.tar.gz llvm-5e83b26584c9a02f8a2923651d08cad2dcc8479a.tar.bz2 |
[clang-tidy] Keep parentheses when replacing index access in `sizeof` calls (#82166)
Fixes #56021.
Diffstat (limited to 'clang-tools-extra')
3 files changed, 25 insertions, 3 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp index f0791da..3229e30 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp @@ -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 diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7ca7037..5862942 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -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. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-basic.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-basic.cpp index c29fbc9f..695925a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-basic.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/loop-convert-basic.cpp @@ -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(); |