Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 1 | //===--- 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 | |
| 16 | using namespace clang::ast_matchers; |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace tidy { |
| 20 | |
| 21 | void ExplicitConstructorCheck::registerMatchers(MatchFinder *Finder) { |
Alexander Kornienko | ad5074d | 2014-11-30 19:41:41 +0000 | [diff] [blame] | 22 | Finder->addMatcher(constructorDecl(unless(isInstantiated())).bind("ctor"), |
| 23 | this); |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // Looks for the token matching the predicate and returns the range of the found |
| 27 | // token including trailing whitespace. |
| 28 | SourceRange 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 Kornienko | dd2dad0 | 2015-02-05 12:49:07 +0000 | [diff] [blame^] | 51 | bool 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 Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 58 | bool isStdInitializerList(QualType Type) { |
Alexander Kornienko | dd2dad0 | 2015-02-05 12:49:07 +0000 | [diff] [blame^] | 59 | 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 Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 72 | void 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 Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 77 | if (Ctor->isOutOfLine() || Ctor->isImplicit() || Ctor->isDeleted() || |
| 78 | Ctor->getNumParams() == 0 || Ctor->getMinRequiredArguments() > 1) |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 79 | return; |
| 80 | |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 81 | bool takesInitializerList = isStdInitializerList( |
| 82 | Ctor->getParamDecl(0)->getType().getNonReferenceType()); |
| 83 | if (Ctor->isExplicit() && |
| 84 | (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) { |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 85 | 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 Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 92 | 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 Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 100 | DiagnosticBuilder Diag = |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 101 | diag(Ctor->getLocation(), |
| 102 | "%0 constructor should not be declared explicit") |
| 103 | << ConstructorDescription; |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 104 | if (ExplicitTokenRange.isValid()) { |
| 105 | Diag << FixItHint::CreateRemoval( |
| 106 | CharSourceRange::getCharRange(ExplicitTokenRange)); |
| 107 | } |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 108 | return; |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() || |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 112 | takesInitializerList) |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 113 | return; |
| 114 | |
| 115 | SourceLocation Loc = Ctor->getLocation(); |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 116 | diag(Loc, "single-argument constructors must be explicit") |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 117 | << FixItHint::CreateInsertion(Loc, "explicit "); |
| 118 | } |
| 119 | |
| 120 | } // namespace tidy |
| 121 | } // namespace clang |