10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Lex/Lexer.h"
21 void ExplicitConstructorCheck::registerMatchers(MatchFinder *Finder) {
23 cxxConstructorDecl(unless(anyOf(isImplicit(),
24 isDeleted(), isInstantiated())))
28 cxxConversionDecl(unless(anyOf(isExplicit(),
30 isDeleted(), isInstantiated())))
38 static SourceRange
FindToken(
const SourceManager &Sources,
39 const LangOptions &LangOpts,
40 SourceLocation StartLoc, SourceLocation EndLoc,
41 bool (*Pred)(
const Token &)) {
42 if (StartLoc.isMacroID() || EndLoc.isMacroID())
44 FileID File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
45 StringRef Buf = Sources.getBufferData(File);
46 const char *StartChar = Sources.getCharacterData(StartLoc);
47 Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end());
48 Lex.SetCommentRetentionState(
true);
51 Lex.LexFromRawLexer(Tok);
54 Lex.LexFromRawLexer(NextTok);
55 return SourceRange(Tok.getLocation(), NextTok.getLocation());
57 }
while (Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc);
65 return D->getName() ==
"initializer_list" &&
66 D->getQualifiedNameAsString() ==
"std::initializer_list";
71 if (
const auto *TS =
Type->getAs<TemplateSpecializationType>()) {
72 if (
const TemplateDecl *TD = TS->getTemplateName().getAsTemplateDecl())
75 if (
const auto *RT =
Type->getAs<RecordType>()) {
76 if (
const auto *Specialization =
77 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
83 void ExplicitConstructorCheck::check(
const MatchFinder::MatchResult &Result) {
85 "%0 must be marked explicit to avoid unintentional implicit conversions";
87 if (
const auto *Conversion =
88 Result.Nodes.getNodeAs<CXXConversionDecl>(
"conversion")) {
89 if (Conversion->isOutOfLine())
91 SourceLocation
Loc = Conversion->getLocation();
97 << Conversion << FixItHint::CreateInsertion(
Loc,
"explicit ");
101 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
102 if (Ctor->isOutOfLine() || Ctor->getNumParams() == 0 ||
103 Ctor->getMinRequiredArguments() > 1)
107 Ctor->getParamDecl(0)->getType().getNonReferenceType());
108 if (Ctor->isExplicit() &&
109 (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) {
110 auto isKWExplicit = [](
const Token &Tok) {
111 return Tok.is(tok::raw_identifier) &&
112 Tok.getRawIdentifier() ==
"explicit";
114 SourceRange ExplicitTokenRange =
115 FindToken(*Result.SourceManager, getLangOpts(),
116 Ctor->getOuterLocStart(), Ctor->getEndLoc(), isKWExplicit);
117 StringRef ConstructorDescription;
118 if (Ctor->isMoveConstructor())
119 ConstructorDescription =
"move";
120 else if (Ctor->isCopyConstructor())
121 ConstructorDescription =
"copy";
123 ConstructorDescription =
"initializer-list";
125 auto Diag = diag(Ctor->getLocation(),
126 "%0 constructor should not be declared explicit")
127 << ConstructorDescription;
128 if (ExplicitTokenRange.isValid()) {
129 Diag << FixItHint::CreateRemoval(
130 CharSourceRange::getCharRange(ExplicitTokenRange));
135 if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
136 takesInitializerList)
139 bool SingleArgument =
140 Ctor->getNumParams() == 1 && !Ctor->getParamDecl(0)->isParameterPack();
141 SourceLocation
Loc = Ctor->getLocation();
144 ?
"single-argument constructors"
145 :
"constructors that are callable with a single argument")
146 << FixItHint::CreateInsertion(
Loc,
"explicit ");