11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 21 void CopyConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
22 if (!getLangOpts().CPlusPlus)
29 hasAnyConstructorInitializer(cxxCtorInitializer(
31 withInitializer(cxxConstructExpr(hasDeclaration(
32 cxxConstructorDecl(isDefaultConstructor())))))),
33 unless(isInstantiated()))
38 void CopyConstructorInitCheck::check(
const MatchFinder::MatchResult &Result) {
39 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
40 std::string ParamName = Ctor->getParamDecl(0)->getNameAsString();
43 std::string FixItInitList;
44 bool HasRelevantBaseInit =
false;
45 bool ShouldNotDoFixit =
false;
46 bool HasWrittenInitializer =
false;
47 SmallVector<FixItHint, 2> SafeFixIts;
48 for (
const auto *Init : Ctor->inits()) {
49 bool CtorInitIsWritten = Init->isWritten();
50 HasWrittenInitializer = HasWrittenInitializer || CtorInitIsWritten;
51 if (!Init->isBaseInitializer())
53 const Type *BaseType = Init->getBaseClass();
57 if (
const auto *TempSpecTy = dyn_cast<TemplateSpecializationType>(BaseType))
58 ShouldNotDoFixit = ShouldNotDoFixit || TempSpecTy->isTypeAlias();
59 ShouldNotDoFixit = ShouldNotDoFixit || isa<TypedefType>(BaseType);
60 ShouldNotDoFixit = ShouldNotDoFixit || CtorInitIsWritten;
61 const CXXRecordDecl *BaseClass =
62 BaseType->getAsCXXRecordDecl()->getDefinition();
63 if (BaseClass->field_empty() &&
64 BaseClass->forallBases(
65 [](
const CXXRecordDecl *Class) {
return Class->field_empty(); }))
67 bool NonCopyableBase =
false;
68 for (
const auto *Ctor : BaseClass->ctors()) {
69 if (Ctor->isCopyConstructor() &&
70 (Ctor->getAccess() == AS_private || Ctor->isDeleted())) {
71 NonCopyableBase =
true;
77 const auto *CExpr = dyn_cast<CXXConstructExpr>(Init->getInit());
78 if (!CExpr || !CExpr->getConstructor()->isDefaultConstructor())
80 HasRelevantBaseInit =
true;
81 if (CtorInitIsWritten) {
82 if (!ParamName.empty())
84 FixItHint::CreateInsertion(CExpr->getLocEnd(), ParamName));
86 if (Init->getSourceLocation().isMacroID() ||
87 Ctor->getLocation().isMacroID() || ShouldNotDoFixit)
89 FixItInitList += BaseClass->getNameAsString();
90 FixItInitList +=
"(" + ParamName +
"), ";
93 if (!HasRelevantBaseInit)
96 auto Diag = diag(Ctor->getLocation(),
97 "calling a base constructor other than the copy constructor")
100 if (FixItInitList.empty() || ParamName.empty() || ShouldNotDoFixit)
103 std::string FixItMsg{FixItInitList.substr(0, FixItInitList.size() - 2)};
104 SourceLocation FixItLoc;
106 if (!HasWrittenInitializer) {
107 FixItLoc = Ctor->getBody()->getLocStart();
108 FixItMsg =
" : " + FixItMsg;
111 FixItLoc = (*Ctor->init_begin())->getSourceLocation();
116 Diag << FixItHint::CreateInsertion(FixItLoc, FixItMsg);