blob: a07902e5293c7cc3e9dae8b4d9140e53fe36b392 [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) {
22 Finder->addMatcher(constructorDecl().bind("ctor"), this);
23}
24
25// Looks for the token matching the predicate and returns the range of the found
26// token including trailing whitespace.
27SourceRange FindToken(const SourceManager &Sources, LangOptions LangOpts,
28 SourceLocation StartLoc, SourceLocation EndLoc,
29 bool (*Pred)(const Token &)) {
30 if (StartLoc.isMacroID() || EndLoc.isMacroID())
31 return SourceRange();
32 FileID File = Sources.getFileID(Sources.getSpellingLoc(StartLoc));
33 StringRef Buf = Sources.getBufferData(File);
34 const char *StartChar = Sources.getCharacterData(StartLoc);
35 Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end());
36 Lex.SetCommentRetentionState(true);
37 Token Tok;
38 do {
39 Lex.LexFromRawLexer(Tok);
40 if (Pred(Tok)) {
41 Token NextTok;
42 Lex.LexFromRawLexer(NextTok);
43 return SourceRange(Tok.getLocation(), NextTok.getLocation());
44 }
45 } while (Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc);
46
47 return SourceRange();
48}
49
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000050bool isStdInitializerList(QualType Type) {
51 if (const RecordType *RT = Type.getCanonicalType()->getAs<RecordType>()) {
52 if (ClassTemplateSpecializationDecl *Specialization =
53 dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl())) {
54 ClassTemplateDecl *Template = Specialization->getSpecializedTemplate();
55 // First use the fast getName() method to avoid unnecessary calls to the
56 // slow getQualifiedNameAsString().
57 return Template->getName() == "initializer_list" &&
58 Template->getQualifiedNameAsString() == "std::initializer_list";
59 }
60 }
61 return false;
62}
63
Alexander Kornienko72f1e752014-06-18 09:33:46 +000064void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
65 const CXXConstructorDecl *Ctor =
66 Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
67 // Do not be confused: isExplicit means 'explicit' keyword is present,
68 // isImplicit means that it's a compiler-generated constructor.
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000069 if (Ctor->isOutOfLine() || Ctor->isImplicit() || Ctor->isDeleted() ||
70 Ctor->getNumParams() == 0 || Ctor->getMinRequiredArguments() > 1)
Alexander Kornienko72f1e752014-06-18 09:33:46 +000071 return;
72
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000073 bool takesInitializerList = isStdInitializerList(
74 Ctor->getParamDecl(0)->getType().getNonReferenceType());
75 if (Ctor->isExplicit() &&
76 (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) {
Alexander Kornienko72f1e752014-06-18 09:33:46 +000077 auto isKWExplicit = [](const Token &Tok) {
78 return Tok.is(tok::raw_identifier) &&
79 Tok.getRawIdentifier() == "explicit";
80 };
81 SourceRange ExplicitTokenRange =
82 FindToken(*Result.SourceManager, Result.Context->getLangOpts(),
83 Ctor->getOuterLocStart(), Ctor->getLocEnd(), isKWExplicit);
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000084 StringRef ConstructorDescription;
85 if (Ctor->isMoveConstructor())
86 ConstructorDescription = "move";
87 else if (Ctor->isCopyConstructor())
88 ConstructorDescription = "copy";
89 else
90 ConstructorDescription = "initializer-list";
91
Alexander Kornienko72f1e752014-06-18 09:33:46 +000092 DiagnosticBuilder Diag =
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +000093 diag(Ctor->getLocation(),
94 "%0 constructor should not be declared explicit")
95 << ConstructorDescription;
Alexander Kornienko72f1e752014-06-18 09:33:46 +000096 if (ExplicitTokenRange.isValid()) {
97 Diag << FixItHint::CreateRemoval(
98 CharSourceRange::getCharRange(ExplicitTokenRange));
99 }
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000100 return;
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000101 }
102
103 if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000104 takesInitializerList)
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000105 return;
106
107 SourceLocation Loc = Ctor->getLocation();
Alexander Kornienko15c5e6a2014-11-27 11:11:47 +0000108 diag(Loc, "single-argument constructors must be explicit")
Alexander Kornienko72f1e752014-06-18 09:33:46 +0000109 << FixItHint::CreateInsertion(Loc, "explicit ");
110}
111
112} // namespace tidy
113} // namespace clang