AMS21 created this revision. AMS21 added reviewers: PiotrZSL, njames93, carlosgalvezp. Herald added a subscriber: xazax.hun. Herald added a project: All. AMS21 requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
This was causes by `getValueOfValueInit` unconditionally calling `getScalarTypeKind` on the member type, which would then trigger an assertions since arrays are not scalar type. Repository: rG LLVM Github Monorepo 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.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,17 @@ // CHECK-FIXES: {{^ }}EmptyBracedIntDefault() {} // CHECK-FIXES-NEXT: {{^ }}int m_i{}; }; + +namespace PR63285 { + +class ArrayValueInit { + double bar[1]; + ArrayValueInit() : bar() {} +}; + +class ArrayBraceInit { + double bar[1]; + ArrayBraceInit() : bar{} {} +}; + +} // 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 @@ -267,18 +267,25 @@ 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(); + + // We do not handle array types at the moment + if (isa<ArrayType>(InitType)) { + return; + } + + const bool ValueInit = isa<ImplicitValueInitExpr>(InitExpression); + const bool CanAssign = + UseAssignment && (!ValueInit || !InitType->isEnumeralType()); auto Diag = diag(Field->getLocation(), "use default member initializer for %0") - << Field - << FixItHint::CreateInsertion(FieldEnd, CanAssign ? " = " : "{") + << Field << FixItHint::CreateInsertion(FieldEnd, CanAssign ? " = " : "{") << FixItHint::CreateInsertionFromRange(FieldEnd, InitRange); if (CanAssign && ValueInit) - Diag << FixItHint::CreateInsertion( - FieldEnd, getValueOfValueInit(Init->getInit()->getType())); + Diag << FixItHint::CreateInsertion(FieldEnd, getValueOfValueInit(InitType)); if (!CanAssign) Diag << FixItHint::CreateInsertion(FieldEnd, "}"); @@ -294,8 +301,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
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,17 @@ // CHECK-FIXES: {{^ }}EmptyBracedIntDefault() {} // CHECK-FIXES-NEXT: {{^ }}int m_i{}; }; + +namespace PR63285 { + +class ArrayValueInit { + double bar[1]; + ArrayValueInit() : bar() {} +}; + +class ArrayBraceInit { + double bar[1]; + ArrayBraceInit() : bar{} {} +}; + +} // 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 @@ -267,18 +267,25 @@ 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(); + + // We do not handle array types at the moment + if (isa<ArrayType>(InitType)) { + return; + } + + const bool ValueInit = isa<ImplicitValueInitExpr>(InitExpression); + const bool CanAssign = + UseAssignment && (!ValueInit || !InitType->isEnumeralType()); auto Diag = diag(Field->getLocation(), "use default member initializer for %0") - << Field - << FixItHint::CreateInsertion(FieldEnd, CanAssign ? " = " : "{") + << Field << FixItHint::CreateInsertion(FieldEnd, CanAssign ? " = " : "{") << FixItHint::CreateInsertionFromRange(FieldEnd, InitRange); if (CanAssign && ValueInit) - Diag << FixItHint::CreateInsertion( - FieldEnd, getValueOfValueInit(Init->getInit()->getType())); + Diag << FixItHint::CreateInsertion(FieldEnd, getValueOfValueInit(InitType)); if (!CanAssign) Diag << FixItHint::CreateInsertion(FieldEnd, "}"); @@ -294,8 +301,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