11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 14 #include "clang/Lex/Lexer.h" 22 void ExplicitConstructorCheck::registerMatchers(MatchFinder *Finder) {
25 if (!getLangOpts().CPlusPlus)
28 cxxConstructorDecl(unless(anyOf(isImplicit(),
29 isDeleted(), isInstantiated())))
33 cxxConversionDecl(unless(anyOf(isExplicit(),
35 isDeleted(), isInstantiated())))
43 static SourceRange
FindToken(
const SourceManager &Sources,
44 const LangOptions &LangOpts,
45 SourceLocation StartLoc, SourceLocation EndLoc,
46 bool (*Pred)(
const Token &)) {
47 if (StartLoc.isMacroID() || EndLoc.isMacroID())
49 FileID
File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
50 StringRef Buf = Sources.getBufferData(File);
51 const char *StartChar = Sources.getCharacterData(StartLoc);
52 Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end());
53 Lex.SetCommentRetentionState(
true);
56 Lex.LexFromRawLexer(Tok);
59 Lex.LexFromRawLexer(NextTok);
60 return SourceRange(Tok.getLocation(), NextTok.getLocation());
62 }
while (Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc);
70 return D->getName() ==
"initializer_list" &&
71 D->getQualifiedNameAsString() ==
"std::initializer_list";
75 Type = Type.getCanonicalType();
76 if (
const auto *TS = Type->getAs<TemplateSpecializationType>()) {
77 if (
const TemplateDecl *TD = TS->getTemplateName().getAsTemplateDecl())
80 if (
const auto *RT = Type->getAs<RecordType>()) {
81 if (
const auto *Specialization =
82 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
88 void ExplicitConstructorCheck::check(
const MatchFinder::MatchResult &Result) {
89 constexpr
char WarningMessage[] =
90 "%0 must be marked explicit to avoid unintentional implicit conversions";
92 if (
const auto *Conversion =
93 Result.Nodes.getNodeAs<CXXConversionDecl>(
"conversion")) {
94 if (Conversion->isOutOfLine())
96 SourceLocation
Loc = Conversion->getLocation();
101 diag(Loc, WarningMessage)
102 << Conversion << FixItHint::CreateInsertion(Loc,
"explicit ");
106 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
107 if (Ctor->isOutOfLine() || Ctor->getNumParams() == 0 ||
108 Ctor->getMinRequiredArguments() > 1)
112 Ctor->getParamDecl(0)->getType().getNonReferenceType());
113 if (Ctor->isExplicit() &&
114 (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) {
115 auto isKWExplicit = [](
const Token &Tok) {
116 return Tok.is(tok::raw_identifier) &&
117 Tok.getRawIdentifier() ==
"explicit";
119 SourceRange ExplicitTokenRange =
120 FindToken(*Result.SourceManager, getLangOpts(),
121 Ctor->getOuterLocStart(), Ctor->getLocEnd(), isKWExplicit);
122 StringRef ConstructorDescription;
123 if (Ctor->isMoveConstructor())
124 ConstructorDescription =
"move";
125 else if (Ctor->isCopyConstructor())
126 ConstructorDescription =
"copy";
128 ConstructorDescription =
"initializer-list";
130 auto Diag = diag(Ctor->getLocation(),
131 "%0 constructor should not be declared explicit")
132 << ConstructorDescription;
133 if (ExplicitTokenRange.isValid()) {
134 Diag << FixItHint::CreateRemoval(
135 CharSourceRange::getCharRange(ExplicitTokenRange));
140 if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
141 takesInitializerList)
144 bool SingleArgument =
145 Ctor->getNumParams() == 1 && !Ctor->getParamDecl(0)->isParameterPack();
146 SourceLocation
Loc = Ctor->getLocation();
147 diag(Loc, WarningMessage)
149 ?
"single-argument constructors" 150 :
"constructors that are callable with a single argument")
151 << FixItHint::CreateInsertion(Loc,
"explicit ");
SourceLocation Loc
'#' location in the include directive
static bool isStdInitializerList(QualType Type)
static SourceRange FindToken(const SourceManager &Sources, const LangOptions &LangOpts, SourceLocation StartLoc, SourceLocation EndLoc, bool(*Pred)(const Token &))
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static bool declIsStdInitializerList(const NamedDecl *D)