blob: c41a6adc094c1ef16e7e6b61de4017b44718c0bf [file] [log] [blame]
Alexander Kornienko72f1e752014-06-18 09:33:46 +00001//===--- ExplicitConstructorCheck.cpp - clang-tidy ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ExplicitConstructorCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Lex/Lexer.h"
15
16using namespace clang::ast_matchers;
17
18namespace clang {
19namespace tidy {
20
21void ExplicitConstructorCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienkoad5074d2014-11-30 19:41:41 +000022 Finder->addMatcher(constructorDecl(unless(isInstantiated())).bind("ctor"),
23 this);
Alexander Kornienko72f1e752014-06-18 09:33:46 +000024}
25
26// Looks for the token matching the predicate and returns the range of the found
27// token including trailing whitespace.
28SourceRange FindToken(const SourceManager &Sources, LangOptions LangOpts,
29 SourceLocation StartLoc, SourceLocation EndLoc,
30 bool (*Pred)(const Token &)) {
31 if (StartLoc.isMacroID() || EndLoc.isMacroID())
32 return SourceRange();
33 FileID File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
34 StringRef Buf = Sources.getBufferData(File);
35 const char *StartChar = Sources.getCharacterData(StartLoc);
36 Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end());
37 Lex.SetCommentRetentionState(true);
38 Token Tok;
39 do {
40 Lex.LexFromRawLexer(Tok);
41 if (Pred(Tok)) {
42 Token NextTok;
43 Lex.LexFromRawLexer(NextTok);
44 return SourceRange(Tok.getLocation(), NextTok.getLocation());
45 }
46 } while (Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc);
47
48 return SourceRange();
49}
50
Alexander Kornienkodd2dad02015-02-05 12:49:07 +000051bool declIsStdInitializerList(const NamedDecl *D) {
52 // First use the fast getName() method to avoid unnecessary calls to the
53 // slow getQualifiedNameAsString().
54 return D->getName() == "initializer_list" &&
55 D->getQualifiedNameAsString() == "std::initializer_list";
56}
57
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000058bool isStdInitializerList(QualType Type) {
Alexander Kornienkodd2dad02015-02-05 12:49:07 +000059 Type = Type.getCanonicalType();
60 if (const auto *TS = Type->getAs<TemplateSpecializationType>()) {
61 if (const TemplateDecl *TD = TS->getTemplateName().getAsTemplateDecl())
62 return declIsStdInitializerList(TD);
63 }
64 if (const auto *RT = Type->getAs<RecordType>()) {
65 if (const auto *Specialization =
66 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
67 return declIsStdInitializerList(Specialization->getSpecializedTemplate());
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000068 }
69 return false;
70}
71
Alexander Kornienko72f1e752014-06-18 09:33:46 +000072void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
73 const CXXConstructorDecl *Ctor =
74 Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
75 // Do not be confused: isExplicit means 'explicit' keyword is present,
76 // isImplicit means that it's a compiler-generated constructor.
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000077 if (Ctor->isOutOfLine() || Ctor->isImplicit() || Ctor->isDeleted() ||
78 Ctor->getNumParams() == 0 || Ctor->getMinRequiredArguments() > 1)
Alexander Kornienko72f1e752014-06-18 09:33:46 +000079 return;
80
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000081 bool takesInitializerList = isStdInitializerList(
82 Ctor->getParamDecl(0)->getType().getNonReferenceType());
83 if (Ctor->isExplicit() &&
84 (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) {
Alexander Kornienko72f1e752014-06-18 09:33:46 +000085 auto isKWExplicit = [](const Token &Tok) {
86 return Tok.is(tok::raw_identifier) &&
87 Tok.getRawIdentifier() == "explicit";
88 };
89 SourceRange ExplicitTokenRange =
90 FindToken(*Result.SourceManager, Result.Context->getLangOpts(),
91 Ctor->getOuterLocStart(), Ctor->getLocEnd(), isKWExplicit);
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000092 StringRef ConstructorDescription;
93 if (Ctor->isMoveConstructor())
94 ConstructorDescription = "move";
95 else if (Ctor->isCopyConstructor())
96 ConstructorDescription = "copy";
97 else
98 ConstructorDescription = "initializer-list";
99
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000100 DiagnosticBuilder Diag =
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000101 diag(Ctor->getLocation(),
102 "%0 constructor should not be declared explicit")
103 << ConstructorDescription;
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000104 if (ExplicitTokenRange.isValid()) {
105 Diag << FixItHint::CreateRemoval(
106 CharSourceRange::getCharRange(ExplicitTokenRange));
107 }
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000108 return;
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000109 }
110
111 if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000112 takesInitializerList)
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000113 return;
114
115 SourceLocation Loc = Ctor->getLocation();
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000116 diag(Loc, "single-argument constructors must be explicit")
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000117 << FixItHint::CreateInsertion(Loc, "explicit ");
118}
119
120} // namespace tidy
121} // namespace clang