11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Lex/Lexer.h"
16 using namespace clang::ast_matchers;
22 void ExplicitConstructorCheck::registerMatchers(MatchFinder *
Finder) {
25 if (getLangOpts().CPlusPlus)
27 cxxConstructorDecl(unless(isInstantiated())).bind(
"ctor"),
this);
33 SourceLocation StartLoc, SourceLocation EndLoc,
34 bool (*Pred)(
const Token &)) {
35 if (StartLoc.isMacroID() || EndLoc.isMacroID())
37 FileID
File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
38 StringRef Buf = Sources.getBufferData(File);
39 const char *StartChar = Sources.getCharacterData(StartLoc);
40 Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end());
41 Lex.SetCommentRetentionState(
true);
44 Lex.LexFromRawLexer(Tok);
47 Lex.LexFromRawLexer(NextTok);
48 return SourceRange(Tok.getLocation(), NextTok.getLocation());
50 }
while (Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc);
58 return D->getName() ==
"initializer_list" &&
59 D->getQualifiedNameAsString() ==
"std::initializer_list";
63 Type = Type.getCanonicalType();
64 if (
const auto *TS = Type->getAs<TemplateSpecializationType>()) {
65 if (
const TemplateDecl *TD = TS->getTemplateName().getAsTemplateDecl())
68 if (
const auto *RT = Type->getAs<RecordType>()) {
69 if (
const auto *Specialization =
70 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
76 void ExplicitConstructorCheck::check(
const MatchFinder::MatchResult &
Result) {
77 const CXXConstructorDecl *Ctor =
78 Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
81 if (Ctor->isOutOfLine() || Ctor->isImplicit() || Ctor->isDeleted() ||
82 Ctor->getNumParams() == 0 || Ctor->getMinRequiredArguments() > 1)
86 Ctor->getParamDecl(0)->getType().getNonReferenceType());
87 if (Ctor->isExplicit() &&
88 (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) {
89 auto isKWExplicit = [](
const Token &Tok) {
90 return Tok.is(tok::raw_identifier) &&
91 Tok.getRawIdentifier() ==
"explicit";
93 SourceRange ExplicitTokenRange =
94 FindToken(*Result.SourceManager, Result.Context->getLangOpts(),
95 Ctor->getOuterLocStart(), Ctor->getLocEnd(), isKWExplicit);
96 StringRef ConstructorDescription;
97 if (Ctor->isMoveConstructor())
98 ConstructorDescription =
"move";
99 else if (Ctor->isCopyConstructor())
100 ConstructorDescription =
"copy";
102 ConstructorDescription =
"initializer-list";
104 DiagnosticBuilder Diag =
105 diag(Ctor->getLocation(),
106 "%0 constructor should not be declared explicit")
107 << ConstructorDescription;
108 if (ExplicitTokenRange.isValid()) {
109 Diag << FixItHint::CreateRemoval(
110 CharSourceRange::getCharRange(ExplicitTokenRange));
115 if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
116 takesInitializerList)
119 bool SingleArgument =
120 Ctor->getNumParams() == 1 && !Ctor->getParamDecl(0)->isParameterPack();
121 SourceLocation
Loc = Ctor->getLocation();
123 "%0 must be marked explicit to avoid unintentional implicit conversions")
125 ?
"single-argument constructors"
126 :
"constructors that are callable with a single argument")
127 << FixItHint::CreateInsertion(Loc,
"explicit ");
static SourceRange FindToken(const SourceManager &Sources, LangOptions LangOpts, SourceLocation StartLoc, SourceLocation EndLoc, bool(*Pred)(const Token &))
SourceLocation Loc
'#' location in the include directive
static bool isStdInitializerList(QualType Type)
std::unique_ptr< ast_matchers::MatchFinder > Finder
static bool declIsStdInitializerList(const NamedDecl *D)