10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
23 hasAnyConstructorInitializer(cxxCtorInitializer(
25 withInitializer(cxxConstructExpr(hasDeclaration(
26 cxxConstructorDecl(isDefaultConstructor())))))),
27 unless(isInstantiated()))
33 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
34 std::string ParamName = Ctor->getParamDecl(0)->getNameAsString();
37 std::string FixItInitList;
38 bool HasRelevantBaseInit =
false;
39 bool ShouldNotDoFixit =
false;
40 bool HasWrittenInitializer =
false;
41 SmallVector<FixItHint, 2> SafeFixIts;
42 for (
const auto *Init : Ctor->inits()) {
43 bool CtorInitIsWritten = Init->isWritten();
44 HasWrittenInitializer = HasWrittenInitializer || CtorInitIsWritten;
45 if (!Init->isBaseInitializer())
47 const Type *BaseType = Init->getBaseClass();
51 if (
const auto *TempSpecTy = dyn_cast<TemplateSpecializationType>(BaseType))
52 ShouldNotDoFixit = ShouldNotDoFixit || TempSpecTy->isTypeAlias();
53 ShouldNotDoFixit = ShouldNotDoFixit || isa<TypedefType>(BaseType);
54 ShouldNotDoFixit = ShouldNotDoFixit || CtorInitIsWritten;
55 const CXXRecordDecl *BaseClass =
56 BaseType->getAsCXXRecordDecl()->getDefinition();
57 if (BaseClass->field_empty() &&
58 BaseClass->forallBases(
59 [](
const CXXRecordDecl *Class) { return Class->field_empty(); }))
61 bool NonCopyableBase =
false;
62 for (
const auto *Ctor : BaseClass->ctors()) {
63 if (Ctor->isCopyConstructor() &&
64 (Ctor->getAccess() == AS_private || Ctor->isDeleted())) {
65 NonCopyableBase =
true;
71 const auto *CExpr = dyn_cast<CXXConstructExpr>(Init->getInit());
72 if (!CExpr || !CExpr->getConstructor()->isDefaultConstructor())
74 HasRelevantBaseInit =
true;
75 if (CtorInitIsWritten) {
76 if (!ParamName.empty())
78 FixItHint::CreateInsertion(CExpr->getEndLoc(), ParamName));
80 if (Init->getSourceLocation().isMacroID() ||
81 Ctor->getLocation().isMacroID() || ShouldNotDoFixit)
83 FixItInitList += BaseClass->getNameAsString();
84 FixItInitList +=
"(" + ParamName +
"), ";
87 if (!HasRelevantBaseInit)
90 auto Diag =
diag(Ctor->getLocation(),
91 "calling a base constructor other than the copy constructor")
94 if (FixItInitList.empty() || ParamName.empty() || ShouldNotDoFixit)
97 std::string FixItMsg{FixItInitList.substr(0, FixItInitList.size() - 2)};
98 SourceLocation FixItLoc;
100 if (!HasWrittenInitializer) {
101 FixItLoc = Ctor->getBody()->getBeginLoc();
102 FixItMsg =
" : " + FixItMsg;
105 FixItLoc = (*Ctor->init_begin())->getSourceLocation();
110 Diag << FixItHint::CreateInsertion(FixItLoc, FixItMsg);
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.