blob: 3960dac6842e3da69ee79a3d1d448e1d742bf0e7 [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 Kornienko15c5e6a2014-11-27 11:11:47 +000051bool isStdInitializerList(QualType Type) {
52 if (const RecordType *RT = Type.getCanonicalType()->getAs<RecordType>()) {
53 if (ClassTemplateSpecializationDecl *Specialization =
54 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl())) {
55 ClassTemplateDecl *Template = Specialization->getSpecializedTemplate();
56 // First use the fast getName() method to avoid unnecessary calls to the
57 // slow getQualifiedNameAsString().
58 return Template->getName() == "initializer_list" &&
59 Template->getQualifiedNameAsString() == "std::initializer_list";
60 }
61 }
62 return false;
63}
64
Alexander Kornienko72f1e752014-06-18 09:33:46 +000065void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
66 const CXXConstructorDecl *Ctor =
67 Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
68 // Do not be confused: isExplicit means 'explicit' keyword is present,
69 // isImplicit means that it's a compiler-generated constructor.
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000070 if (Ctor->isOutOfLine() || Ctor->isImplicit() || Ctor->isDeleted() ||
71 Ctor->getNumParams() == 0 || Ctor->getMinRequiredArguments() > 1)
Alexander Kornienko72f1e752014-06-18 09:33:46 +000072 return;
73
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000074 bool takesInitializerList = isStdInitializerList(
75 Ctor->getParamDecl(0)->getType().getNonReferenceType());
76 if (Ctor->isExplicit() &&
77 (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) {
Alexander Kornienko72f1e752014-06-18 09:33:46 +000078 auto isKWExplicit = [](const Token &Tok) {
79 return Tok.is(tok::raw_identifier) &&
80 Tok.getRawIdentifier() == "explicit";
81 };
82 SourceRange ExplicitTokenRange =
83 FindToken(*Result.SourceManager, Result.Context->getLangOpts(),
84 Ctor->getOuterLocStart(), Ctor->getLocEnd(), isKWExplicit);
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000085 StringRef ConstructorDescription;
86 if (Ctor->isMoveConstructor())
87 ConstructorDescription = "move";
88 else if (Ctor->isCopyConstructor())
89 ConstructorDescription = "copy";
90 else
91 ConstructorDescription = "initializer-list";
92
Alexander Kornienko72f1e752014-06-18 09:33:46 +000093 DiagnosticBuilder Diag =
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000094 diag(Ctor->getLocation(),
95 "%0 constructor should not be declared explicit")
96 << ConstructorDescription;
Alexander Kornienko72f1e752014-06-18 09:33:46 +000097 if (ExplicitTokenRange.isValid()) {
98 Diag << FixItHint::CreateRemoval(
99 CharSourceRange::getCharRange(ExplicitTokenRange));
100 }
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000101 return;
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000102 }
103
104 if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000105 takesInitializerList)
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000106 return;
107
108 SourceLocation Loc = Ctor->getLocation();
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000109 diag(Loc, "single-argument constructors must be explicit")
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000110 << FixItHint::CreateInsertion(Loc, "explicit ");
111}
112
113} // namespace tidy
114} // namespace clang