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) { |
| 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. |
| 27 | SourceRange 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 Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 50 | bool 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 Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 64 | void 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 Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 69 | if (Ctor->isOutOfLine() || Ctor->isImplicit() || Ctor->isDeleted() || |
| 70 | Ctor->getNumParams() == 0 || Ctor->getMinRequiredArguments() > 1) |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 71 | return; |
| 72 | |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 73 | bool takesInitializerList = isStdInitializerList( |
| 74 | Ctor->getParamDecl(0)->getType().getNonReferenceType()); |
| 75 | if (Ctor->isExplicit() && |
| 76 | (Ctor->isCopyOrMoveConstructor() || takesInitializerList)) { |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 77 | 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 Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 84 | 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 Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 92 | DiagnosticBuilder Diag = |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 93 | diag(Ctor->getLocation(), |
| 94 | "%0 constructor should not be declared explicit") |
| 95 | << ConstructorDescription; |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 96 | if (ExplicitTokenRange.isValid()) { |
| 97 | Diag << FixItHint::CreateRemoval( |
| 98 | CharSourceRange::getCharRange(ExplicitTokenRange)); |
| 99 | } |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 100 | return; |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() || |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 104 | takesInitializerList) |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 105 | return; |
| 106 | |
| 107 | SourceLocation Loc = Ctor->getLocation(); |
Alexander Kornienko | 15c5e6a | 2014-11-27 11:11:47 +0000 | [diff] [blame] | 108 | diag(Loc, "single-argument constructors must be explicit") |
Alexander Kornienko | 72f1e75 | 2014-06-18 09:33:46 +0000 | [diff] [blame] | 109 | << FixItHint::CreateInsertion(Loc, "explicit "); |
| 110 | } |
| 111 | |
| 112 | } // namespace tidy |
| 113 | } // namespace clang |