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) {
24 if (!getLangOpts().CPlusPlus)
27 cxxConstructorDecl(unless(anyOf(isImplicit(),
28 isDeleted(), isInstantiated())))
32 cxxConversionDecl(unless(anyOf(isExplicit(),
34 isDeleted(), isInstantiated())))
42 static SourceRange
FindToken(
const SourceManager &Sources,
43 const LangOptions &LangOpts,
44 SourceLocation StartLoc, SourceLocation EndLoc,
45 bool (*Pred)(
const Token &)) {
46 if (StartLoc.isMacroID() || EndLoc.isMacroID())
48 FileID File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
49 StringRef Buf = Sources.getBufferData(File);
50 const char *StartChar = Sources.getCharacterData(StartLoc);
51 Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end());
52 Lex.SetCommentRetentionState(
true);
55 Lex.LexFromRawLexer(Tok);
58 Lex.LexFromRawLexer(NextTok);
59 return SourceRange(Tok.getLocation(), NextTok.getLocation());
61 }
while (Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc);
69 return D->getName() ==
"initializer_list" &&
70 D->getQualifiedNameAsString() ==
"std::initializer_list";
74 Type = Type.getCanonicalType();
75 if (
const auto *TS = Type->getAs<TemplateSpecializationType>()) {
76 if (
const TemplateDecl *TD = TS->getTemplateName().getAsTemplateDecl())
79 if (
const auto *RT = Type->getAs<RecordType>()) {
80 if (
const auto *Specialization =
81 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
87 void ExplicitConstructorCheck::check(
const MatchFinder::MatchResult &
Result) {
88 constexpr
char WarningMessage[] =
89 "%0 must be marked explicit to avoid unintentional implicit conversions";
91 if (
const auto *Conversion =
92 Result.Nodes.getNodeAs<CXXConversionDecl>(
"conversion")) {
93 if (Conversion->isOutOfLine())
95 SourceLocation
Loc = Conversion->getLocation();
100 diag(Loc, WarningMessage)
101 << Conversion << FixItHint::CreateInsertion(Loc,
"explicit ");
105 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
106 if (Ctor->isOutOfLine() || Ctor->getNumParams() == 0 ||
107 Ctor->getMinRequiredArguments() > 1)
111 Ctor->getParamDecl(0)->getType().getNonReferenceType());
112 if (Ctor->isExplicit() &&
113 (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) {
114 auto isKWExplicit = [](
const Token &Tok) {
115 return Tok.is(tok::raw_identifier) &&
116 Tok.getRawIdentifier() ==
"explicit";
118 SourceRange ExplicitTokenRange =
119 FindToken(*Result.SourceManager, getLangOpts(),
120 Ctor->getOuterLocStart(), Ctor->getEndLoc(), isKWExplicit);
121 StringRef ConstructorDescription;
122 if (Ctor->isMoveConstructor())
123 ConstructorDescription =
"move";
124 else if (Ctor->isCopyConstructor())
125 ConstructorDescription =
"copy";
127 ConstructorDescription =
"initializer-list";
129 auto Diag = diag(Ctor->getLocation(),
130 "%0 constructor should not be declared explicit")
131 << ConstructorDescription;
132 if (ExplicitTokenRange.isValid()) {
133 Diag << FixItHint::CreateRemoval(
134 CharSourceRange::getCharRange(ExplicitTokenRange));
139 if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
140 takesInitializerList)
143 bool SingleArgument =
144 Ctor->getNumParams() == 1 && !Ctor->getParamDecl(0)->isParameterPack();
145 SourceLocation
Loc = Ctor->getLocation();
146 diag(Loc, WarningMessage)
148 ?
"single-argument constructors" 149 :
"constructors that are callable with a single argument")
150 << 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++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
static bool declIsStdInitializerList(const NamedDecl *D)