Sockke created this revision. Sockke added reviewers: aaron.ballman, gribozavr, flx, whisperity, steven.zhang, MTC. Herald added subscribers: shchenz, rnkovacs, kbarton, xazax.hun, nemanjai. Sockke requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
The overload of the constructor will repeatedly fix the member variables that need to be initialized. Removed the duplicate '{}'. struct A { A() {} A(int) {} int _var; // int _var{}{}; <-- wrong fix }; Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D107641 Files: clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp @@ -210,7 +210,7 @@ // FIXME: The fix-its here collide providing an erroneous fix int A, B; - // CHECK-FIXES: int A{}{}{}, B{}{}{}; + // CHECK-FIXES: int A{}, B{}; }; typedef struct { Index: clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h =================================================================== --- clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h +++ clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H #include "../ClangTidyCheck.h" +#include "llvm/ADT/DenseSet.h" namespace clang { namespace tidy { @@ -72,6 +73,10 @@ // instead of brace initialization. Only effective in C++11 mode. Default is // false. bool UseAssignment; + + // Record the member variables that have been initialized to prevent repeated + // initialization. + llvm::DenseSet<const FieldDecl *> HasRecordClasMemberSet; }; } // namespace cppcoreguidelines Index: clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp +++ clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp @@ -435,15 +435,24 @@ // Collect all the fields we need to initialize, including indirect fields. SmallPtrSet<const FieldDecl *, 16> AllFieldsToInit; forEachField(ClassDecl, FieldsToInit, - [&](const FieldDecl *F) { AllFieldsToInit.insert(F); }); - if (AllFieldsToInit.empty()) + [&](const FieldDecl *F) { + if (!HasRecordClasMemberSet.count(F)) + { + AllFieldsToInit.insert(F); + HasRecordClasMemberSet.insert(F); + } + }); + if (FieldsToInit.empty()) return; DiagnosticBuilder Diag = diag(Ctor ? Ctor->getBeginLoc() : ClassDecl.getLocation(), "%select{|union }0constructor %select{does not|should}0 initialize " "%select{|one of }0these fields: %1") - << IsUnion << toCommaSeparatedString(OrderedFields, AllFieldsToInit); + << IsUnion << toCommaSeparatedString(OrderedFields, FieldsToInit); + + if (AllFieldsToInit.empty()) + return; // Do not propose fixes for constructors in macros since we cannot place them // correctly.
Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-member-init.cpp @@ -210,7 +210,7 @@ // FIXME: The fix-its here collide providing an erroneous fix int A, B; - // CHECK-FIXES: int A{}{}{}, B{}{}{}; + // CHECK-FIXES: int A{}, B{}; }; typedef struct { Index: clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h =================================================================== --- clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h +++ clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_TYPE_MEMBER_INIT_H #include "../ClangTidyCheck.h" +#include "llvm/ADT/DenseSet.h" namespace clang { namespace tidy { @@ -72,6 +73,10 @@ // instead of brace initialization. Only effective in C++11 mode. Default is // false. bool UseAssignment; + + // Record the member variables that have been initialized to prevent repeated + // initialization. + llvm::DenseSet<const FieldDecl *> HasRecordClasMemberSet; }; } // namespace cppcoreguidelines Index: clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp +++ clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp @@ -435,15 +435,24 @@ // Collect all the fields we need to initialize, including indirect fields. SmallPtrSet<const FieldDecl *, 16> AllFieldsToInit; forEachField(ClassDecl, FieldsToInit, - [&](const FieldDecl *F) { AllFieldsToInit.insert(F); }); - if (AllFieldsToInit.empty()) + [&](const FieldDecl *F) { + if (!HasRecordClasMemberSet.count(F)) + { + AllFieldsToInit.insert(F); + HasRecordClasMemberSet.insert(F); + } + }); + if (FieldsToInit.empty()) return; DiagnosticBuilder Diag = diag(Ctor ? Ctor->getBeginLoc() : ClassDecl.getLocation(), "%select{|union }0constructor %select{does not|should}0 initialize " "%select{|one of }0these fields: %1") - << IsUnion << toCommaSeparatedString(OrderedFields, AllFieldsToInit); + << IsUnion << toCommaSeparatedString(OrderedFields, FieldsToInit); + + if (AllFieldsToInit.empty()) + return; // Do not propose fixes for constructors in macros since we cannot place them // correctly.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits