AMS21 updated this revision to Diff 530921. AMS21 marked an inline comment as done. AMS21 added a comment.
Should now properly handle arrays Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D152802/new/ https://reviews.llvm.org/D152802 Files: clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init-assignment.cpp clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp +++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp @@ -482,3 +482,39 @@ // CHECK-FIXES: {{^ }}EmptyBracedIntDefault() {} // CHECK-FIXES-NEXT: {{^ }}int m_i{}; }; + +namespace PR63285 { + +class ArrayValueInit { + ArrayValueInit() : m_array() {} + double m_array[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init] + // CHECK-FIXES: {{^ }}ArrayValueInit() {} + // CHECK-FIXES-NEXT: {{^ }}double m_array[1]{}; +}; + +class ArrayBraceInit { + ArrayBraceInit() : m_array{} {} + double m_array[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init] + // CHECK-FIXES: {{^ }}ArrayBraceInit() {} + // CHECK-FIXES-NEXT: {{^ }}double m_array[1]{}; +}; + +class ArrayBraceInitWithValue { + ArrayBraceInitWithValue() : m_array{3.14} {} + double m_array[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init] + // CHECK-FIXES: {{^ }}ArrayBraceInitWithValue() {} + // CHECK-FIXES-NEXT: {{^ }}double m_array[1]{3.14}; +}; + +class ArrayBraceInitMultipleValues { + ArrayBraceInitMultipleValues() : m_array{1.0, 2.0, 3.0} {} + double m_array[3]; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init] + // CHECK-FIXES: {{^ }}ArrayBraceInitMultipleValues() {} + // CHECK-FIXES-NEXT: {{^ }}double m_array[3]{1.0, 2.0, 3.0}; +}; + +} // namespace PR63285 Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init-assignment.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init-assignment.cpp +++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init-assignment.cpp @@ -190,3 +190,39 @@ NegativeTemplate<int> nti; NegativeTemplate<double> ntd; + +namespace PR63285 { + +class ArrayValueInit { + ArrayValueInit() : m_array() {} + double m_array[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init] + // CHECK-FIXES: {{^ }}ArrayValueInit() {} + // CHECK-FIXES-NEXT: {{^ }}double m_array[1] = {}; +}; + +class ArrayBraceInit { + ArrayBraceInit() : m_array{} {} + double m_array[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init] + // CHECK-FIXES: {{^ }}ArrayBraceInit() {} + // CHECK-FIXES-NEXT: {{^ }}double m_array[1] = {}; +}; + +class ArrayBraceInitWithValue { + ArrayBraceInitWithValue() : m_array{3.14} {} + double m_array[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init] + // CHECK-FIXES: {{^ }}ArrayBraceInitWithValue() {} + // CHECK-FIXES-NEXT: {{^ }}double m_array[1] = {3.14}; +}; + +class ArrayBraceInitMultipleValues { + ArrayBraceInitMultipleValues() : m_array{1.0, 2.0, 3.0} {} + double m_array[3]; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init] + // CHECK-FIXES: {{^ }}ArrayBraceInitMultipleValues() {} + // CHECK-FIXES-NEXT: {{^ }}double m_array[3] = {1.0, 2.0, 3.0}; +}; + +} // namespace PR63285 Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -325,6 +325,10 @@ constructors toward hand written constructors so that they are skipped if more than one exists. +- Fixed crash in :doc:`modernize-use-default-member-init + <clang-tidy/checks/modernize/use-default-member-init>` with array members which + are value initialized. + - Fixed false positive in :doc:`modernize-use-equals-default <clang-tidy/checks/modernize/use-equals-default>` check for special member functions containing macros or preprocessor directives, and out-of-line special Index: clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp +++ clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp @@ -206,7 +206,7 @@ auto Init = anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)), - initCountIs(0))), + initCountIs(0), hasType(arrayType()))), InitBase); Finder->addMatcher( @@ -267,20 +267,30 @@ CharSourceRange InitRange = CharSourceRange::getCharRange(LParenEnd, Init->getRParenLoc()); - bool ValueInit = isa<ImplicitValueInitExpr>(Init->getInit()); - bool CanAssign = UseAssignment && (!ValueInit || !Init->getInit()->getType()->isEnumeralType()); + const Expr *InitExpression = Init->getInit(); + const QualType InitType = InitExpression->getType(); + + const bool ValueInit = + isa<ImplicitValueInitExpr>(InitExpression) && !isa<ArrayType>(InitType); + const bool CanAssign = + UseAssignment && (!ValueInit || !InitType->isEnumeralType()); + const bool NeedsBraces = !CanAssign || isa<ArrayType>(InitType); auto Diag = diag(Field->getLocation(), "use default member initializer for %0") - << Field - << FixItHint::CreateInsertion(FieldEnd, CanAssign ? " = " : "{") - << FixItHint::CreateInsertionFromRange(FieldEnd, InitRange); + << Field; + + if (CanAssign) + Diag << FixItHint::CreateInsertion(FieldEnd, " = "); + if (NeedsBraces) + Diag << FixItHint::CreateInsertion(FieldEnd, "{"); if (CanAssign && ValueInit) - Diag << FixItHint::CreateInsertion( - FieldEnd, getValueOfValueInit(Init->getInit()->getType())); + Diag << FixItHint::CreateInsertion(FieldEnd, getValueOfValueInit(InitType)); + else + Diag << FixItHint::CreateInsertionFromRange(FieldEnd, InitRange); - if (!CanAssign) + if (NeedsBraces) Diag << FixItHint::CreateInsertion(FieldEnd, "}"); Diag << FixItHint::CreateRemoval(Init->getSourceRange()); @@ -294,8 +304,7 @@ return; diag(Init->getSourceLocation(), "member initializer for %0 is redundant") - << Field - << FixItHint::CreateRemoval(Init->getSourceRange()); + << Field << FixItHint::CreateRemoval(Init->getSourceRange()); } } // namespace clang::tidy::modernize
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits