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);
32 static SourceRange
FindToken(
const SourceManager &Sources,
34 SourceLocation StartLoc, SourceLocation EndLoc,
35 bool (*Pred)(
const Token &)) {
36 if (StartLoc.isMacroID() || EndLoc.isMacroID())
38 FileID
File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
39 StringRef Buf = Sources.getBufferData(File);
40 const char *StartChar = Sources.getCharacterData(StartLoc);
41 Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end());
42 Lex.SetCommentRetentionState(
true);
45 Lex.LexFromRawLexer(Tok);
48 Lex.LexFromRawLexer(NextTok);
49 return SourceRange(Tok.getLocation(), NextTok.getLocation());
51 }
while (Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc);
59 return D->getName() ==
"initializer_list" &&
60 D->getQualifiedNameAsString() ==
"std::initializer_list";
64 Type = Type.getCanonicalType();
65 if (
const auto *TS = Type->getAs<TemplateSpecializationType>()) {
66 if (
const TemplateDecl *TD = TS->getTemplateName().getAsTemplateDecl())
69 if (
const auto *RT = Type->getAs<RecordType>()) {
70 if (
const auto *Specialization =
71 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
77 void ExplicitConstructorCheck::check(
const MatchFinder::MatchResult &
Result) {
78 const CXXConstructorDecl *Ctor =
79 Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
82 if (Ctor->isOutOfLine() || Ctor->isImplicit() || Ctor->isDeleted() ||
83 Ctor->getNumParams() == 0 || Ctor->getMinRequiredArguments() > 1)
87 Ctor->getParamDecl(0)->getType().getNonReferenceType());
88 if (Ctor->isExplicit() &&
89 (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) {
90 auto isKWExplicit = [](
const Token &Tok) {
91 return Tok.is(tok::raw_identifier) &&
92 Tok.getRawIdentifier() ==
"explicit";
94 SourceRange ExplicitTokenRange =
95 FindToken(*Result.SourceManager, Result.Context->getLangOpts(),
96 Ctor->getOuterLocStart(), Ctor->getLocEnd(), isKWExplicit);
97 StringRef ConstructorDescription;
98 if (Ctor->isMoveConstructor())
99 ConstructorDescription =
"move";
100 else if (Ctor->isCopyConstructor())
101 ConstructorDescription =
"copy";
103 ConstructorDescription =
"initializer-list";
105 DiagnosticBuilder Diag =
106 diag(Ctor->getLocation(),
107 "%0 constructor should not be declared explicit")
108 << ConstructorDescription;
109 if (ExplicitTokenRange.isValid()) {
110 Diag << FixItHint::CreateRemoval(
111 CharSourceRange::getCharRange(ExplicitTokenRange));
116 if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
117 takesInitializerList)
120 bool SingleArgument =
121 Ctor->getNumParams() == 1 && !Ctor->getParamDecl(0)->isParameterPack();
122 SourceLocation
Loc = Ctor->getLocation();
124 "%0 must be marked explicit to avoid unintentional implicit conversions")
126 ?
"single-argument constructors"
127 :
"constructors that are callable with a single argument")
128 << FixItHint::CreateInsertion(Loc,
"explicit ");
SourceLocation Loc
'#' location in the include directive
static bool isStdInitializerList(QualType Type)
std::unique_ptr< ast_matchers::MatchFinder > Finder
static SourceRange FindToken(const SourceManager &Sources, const LangOptions &LangOpts, SourceLocation StartLoc, SourceLocation EndLoc, bool(*Pred)(const Token &))
static bool declIsStdInitializerList(const NamedDecl *D)