10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
20 void CopyConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
25 hasAnyConstructorInitializer(cxxCtorInitializer(
27 withInitializer(cxxConstructExpr(hasDeclaration(
28 cxxConstructorDecl(isDefaultConstructor())))))),
29 unless(isInstantiated()))
34 void CopyConstructorInitCheck::check(
const MatchFinder::MatchResult &Result) {
35 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
36 std::string ParamName = Ctor->getParamDecl(0)->getNameAsString();
39 std::string FixItInitList;
40 bool HasRelevantBaseInit =
false;
41 bool ShouldNotDoFixit =
false;
42 bool HasWrittenInitializer =
false;
43 SmallVector<FixItHint, 2> SafeFixIts;
44 for (
const auto *Init : Ctor->inits()) {
45 bool CtorInitIsWritten = Init->isWritten();
46 HasWrittenInitializer = HasWrittenInitializer || CtorInitIsWritten;
47 if (!Init->isBaseInitializer())
49 const Type *BaseType = Init->getBaseClass();
53 if (
const auto *TempSpecTy = dyn_cast<TemplateSpecializationType>(BaseType))
54 ShouldNotDoFixit = ShouldNotDoFixit || TempSpecTy->isTypeAlias();
55 ShouldNotDoFixit = ShouldNotDoFixit || isa<TypedefType>(BaseType);
56 ShouldNotDoFixit = ShouldNotDoFixit || CtorInitIsWritten;
57 const CXXRecordDecl *BaseClass =
58 BaseType->getAsCXXRecordDecl()->getDefinition();
59 if (BaseClass->field_empty() &&
60 BaseClass->forallBases(
61 [](
const CXXRecordDecl *Class) {
return Class->field_empty(); }))
63 bool NonCopyableBase =
false;
64 for (
const auto *Ctor : BaseClass->ctors()) {
65 if (Ctor->isCopyConstructor() &&
66 (Ctor->getAccess() == AS_private || Ctor->isDeleted())) {
67 NonCopyableBase =
true;
73 const auto *CExpr = dyn_cast<CXXConstructExpr>(Init->getInit());
74 if (!CExpr || !CExpr->getConstructor()->isDefaultConstructor())
76 HasRelevantBaseInit =
true;
77 if (CtorInitIsWritten) {
78 if (!ParamName.empty())
80 FixItHint::CreateInsertion(CExpr->getEndLoc(), ParamName));
82 if (Init->getSourceLocation().isMacroID() ||
83 Ctor->getLocation().isMacroID() || ShouldNotDoFixit)
85 FixItInitList += BaseClass->getNameAsString();
86 FixItInitList +=
"(" + ParamName +
"), ";
89 if (!HasRelevantBaseInit)
92 auto Diag = diag(Ctor->getLocation(),
93 "calling a base constructor other than the copy constructor")
96 if (FixItInitList.empty() || ParamName.empty() || ShouldNotDoFixit)
99 std::string FixItMsg{FixItInitList.substr(0, FixItInitList.size() - 2)};
100 SourceLocation FixItLoc;
102 if (!HasWrittenInitializer) {
103 FixItLoc = Ctor->getBody()->getBeginLoc();
104 FixItMsg =
" : " + FixItMsg;
107 FixItLoc = (*Ctor->init_begin())->getSourceLocation();
112 Diag << FixItHint::CreateInsertion(FixItLoc, FixItMsg);