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 { |
Alexander Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 20 | namespace google { |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 21 | |
| 22 | void ExplicitConstructorCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | ec3e5d6 | 2015-09-02 16:20:42 +0000 | [diff] [blame] | 23 | // Only register the matchers for C++; the functionality currently does not |
| 24 | // provide any benefit to other languages, despite being benign. |
Alexander Kornienko | bbd8536 | 2016-12-23 15:03:12 +0000 | [diff] [blame] | 25 | if (!getLangOpts().CPlusPlus) |
| 26 | return; |
Alexander Kornienko | c7c9b75 | 2017-03-17 09:47:05 +0000 | [diff] [blame] | 27 | Finder->addMatcher( |
| 28 | cxxConstructorDecl(unless(anyOf(isImplicit(), // Compiler-generated. |
| 29 | isDeleted(), isInstantiated()))) |
| 30 | .bind("ctor"), |
| 31 | this); |
Alexander Kornienko | dd0c0ba | 2016-12-28 13:48:03 +0000 | [diff] [blame] | 32 | Finder->addMatcher( |
Alexander Kornienko | 56d0806 | 2016-12-30 13:25:03 +0000 | [diff] [blame] | 33 | cxxConversionDecl(unless(anyOf(isExplicit(), // Already marked explicit. |
| 34 | isImplicit(), // Compiler-generated. |
Alexander Kornienko | 7bcf751 | 2017-03-17 08:40:07 +0000 | [diff] [blame] | 35 | isDeleted(), isInstantiated()))) |
Alexander Kornienko | 56d0806 | 2016-12-30 13:25:03 +0000 | [diff] [blame] | 36 | |
Alexander Kornienko | dd0c0ba | 2016-12-28 13:48:03 +0000 | [diff] [blame] | 37 | .bind("conversion"), |
| 38 | this); |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Looks for the token matching the predicate and returns the range of the found |
| 42 | // token including trailing whitespace. |
Benjamin Kramer | 51a9cc9 | 2016-06-15 15:46:10 +0000 | [diff] [blame] | 43 | static SourceRange FindToken(const SourceManager &Sources, |
| 44 | const LangOptions &LangOpts, |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 45 | SourceLocation StartLoc, SourceLocation EndLoc, |
| 46 | bool (*Pred)(const Token &)) { |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 47 | if (StartLoc.isMacroID() || EndLoc.isMacroID()) |
| 48 | return SourceRange(); |
| 49 | FileID File = Sources.getFileID(Sources.getSpellingLoc(StartLoc)); |
| 50 | StringRef Buf = Sources.getBufferData(File); |
| 51 | const char *StartChar = Sources.getCharacterData(StartLoc); |
| 52 | Lexer Lex(StartLoc, LangOpts, StartChar, StartChar, Buf.end()); |
| 53 | Lex.SetCommentRetentionState(true); |
| 54 | Token Tok; |
| 55 | do { |
| 56 | Lex.LexFromRawLexer(Tok); |
| 57 | if (Pred(Tok)) { |
| 58 | Token NextTok; |
| 59 | Lex.LexFromRawLexer(NextTok); |
| 60 | return SourceRange(Tok.getLocation(), NextTok.getLocation()); |
| 61 | } |
| 62 | } while (Tok.isNot(tok::eof) && Tok.getLocation() < EndLoc); |
| 63 | |
| 64 | return SourceRange(); |
| 65 | } |
| 66 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 67 | static bool declIsStdInitializerList(const NamedDecl *D) { |
Alexander Kornienko | dd2dad0 | 2015-02-05 12:49:07 +0000 | [diff] [blame] | 68 | // First use the fast getName() method to avoid unnecessary calls to the |
| 69 | // slow getQualifiedNameAsString(). |
| 70 | return D->getName() == "initializer_list" && |
| 71 | D->getQualifiedNameAsString() == "std::initializer_list"; |
| 72 | } |
| 73 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 74 | static bool isStdInitializerList(QualType Type) { |
Alexander Kornienko | dd2dad0 | 2015-02-05 12:49:07 +0000 | [diff] [blame] | 75 | Type = Type.getCanonicalType(); |
| 76 | if (const auto *TS = Type->getAs<TemplateSpecializationType>()) { |
| 77 | if (const TemplateDecl *TD = TS->getTemplateName().getAsTemplateDecl()) |
| 78 | return declIsStdInitializerList(TD); |
| 79 | } |
| 80 | if (const auto *RT = Type->getAs<RecordType>()) { |
| 81 | if (const auto *Specialization = |
| 82 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl())) |
| 83 | return declIsStdInitializerList(Specialization->getSpecializedTemplate()); |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 88 | void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) { |
Alexander Kornienko | bbd8536 | 2016-12-23 15:03:12 +0000 | [diff] [blame] | 89 | constexpr char WarningMessage[] = |
| 90 | "%0 must be marked explicit to avoid unintentional implicit conversions"; |
| 91 | |
| 92 | if (const auto *Conversion = |
| 93 | Result.Nodes.getNodeAs<CXXConversionDecl>("conversion")) { |
Alexander Kornienko | 04e5ab3 | 2017-04-18 17:26:00 +0000 | [diff] [blame^] | 94 | if (Conversion->isOutOfLine()) |
| 95 | return; |
Alexander Kornienko | bbd8536 | 2016-12-23 15:03:12 +0000 | [diff] [blame] | 96 | SourceLocation Loc = Conversion->getLocation(); |
Alexander Kornienko | 2042f83 | 2016-12-30 15:15:14 +0000 | [diff] [blame] | 97 | // Ignore all macros until we learn to ignore specific ones (e.g. used in |
| 98 | // gmock to define matchers). |
| 99 | if (Loc.isMacroID()) |
| 100 | return; |
Alexander Kornienko | bbd8536 | 2016-12-23 15:03:12 +0000 | [diff] [blame] | 101 | diag(Loc, WarningMessage) |
| 102 | << Conversion << FixItHint::CreateInsertion(Loc, "explicit "); |
| 103 | return; |
| 104 | } |
| 105 | |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 106 | const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor"); |
Alexander Kornienko | c7c9b75 | 2017-03-17 09:47:05 +0000 | [diff] [blame] | 107 | if (Ctor->isOutOfLine() || Ctor->getNumParams() == 0 || |
| 108 | Ctor->getMinRequiredArguments() > 1) |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 109 | return; |
| 110 | |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 111 | bool takesInitializerList = isStdInitializerList( |
| 112 | Ctor->getParamDecl(0)->getType().getNonReferenceType()); |
| 113 | if (Ctor->isExplicit() && |
| 114 | (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) { |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 115 | auto isKWExplicit = [](const Token &Tok) { |
| 116 | return Tok.is(tok::raw_identifier) && |
| 117 | Tok.getRawIdentifier() == "explicit"; |
| 118 | }; |
| 119 | SourceRange ExplicitTokenRange = |
Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 120 | FindToken(*Result.SourceManager, getLangOpts(), |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 121 | Ctor->getOuterLocStart(), Ctor->getLocEnd(), isKWExplicit); |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 122 | StringRef ConstructorDescription; |
| 123 | if (Ctor->isMoveConstructor()) |
| 124 | ConstructorDescription = "move"; |
| 125 | else if (Ctor->isCopyConstructor()) |
| 126 | ConstructorDescription = "copy"; |
| 127 | else |
| 128 | ConstructorDescription = "initializer-list"; |
| 129 | |
Alexander Kornienko | bbd8536 | 2016-12-23 15:03:12 +0000 | [diff] [blame] | 130 | auto Diag = diag(Ctor->getLocation(), |
| 131 | "%0 constructor should not be declared explicit") |
| 132 | << ConstructorDescription; |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 133 | if (ExplicitTokenRange.isValid()) { |
| 134 | Diag << FixItHint::CreateRemoval( |
| 135 | CharSourceRange::getCharRange(ExplicitTokenRange)); |
| 136 | } |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 137 | return; |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() || |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 141 | takesInitializerList) |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 142 | return; |
| 143 | |
Alexander Kornienko | 0b02461 | 2015-03-31 16:24:44 +0000 | [diff] [blame] | 144 | bool SingleArgument = |
| 145 | Ctor->getNumParams() == 1 && !Ctor->getParamDecl(0)->isParameterPack(); |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 146 | SourceLocation Loc = Ctor->getLocation(); |
Alexander Kornienko | bbd8536 | 2016-12-23 15:03:12 +0000 | [diff] [blame] | 147 | diag(Loc, WarningMessage) |
Alexander Kornienko | 5eb134c | 2015-11-28 02:25:02 +0000 | [diff] [blame] | 148 | << (SingleArgument |
| 149 | ? "single-argument constructors" |
| 150 | : "constructors that are callable with a single argument") |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 151 | << FixItHint::CreateInsertion(Loc, "explicit "); |
| 152 | } |
| 153 | |
Alexander Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 154 | } // namespace google |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 155 | } // namespace tidy |
| 156 | } // namespace clang |