blob: 764f18031c84d7a12451ebd0f186c894cfe6fef5 [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
50void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
51 const CXXConstructorDecl *Ctor =
52 Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
53 // Do not be confused: isExplicit means 'explicit' keyword is present,
54 // isImplicit means that it's a compiler-generated constructor.
55 if (Ctor->isOutOfLine() || Ctor->isImplicit() || Ctor->isDeleted())
56 return;
57
58 if (Ctor->isExplicit() && Ctor->isCopyOrMoveConstructor()) {
59 auto isKWExplicit = [](const Token &Tok) {
60 return Tok.is(tok::raw_identifier) &&
61 Tok.getRawIdentifier() == "explicit";
62 };
63 SourceRange ExplicitTokenRange =
64 FindToken(*Result.SourceManager, Result.Context->getLangOpts(),
65 Ctor->getOuterLocStart(), Ctor->getLocEnd(), isKWExplicit);
66 DiagnosticBuilder Diag =
67 diag(Ctor->getLocation(), "%0 constructor declared explicit.")
68 << (Ctor->isMoveConstructor() ? "Move" : "Copy");
69 if (ExplicitTokenRange.isValid()) {
70 Diag << FixItHint::CreateRemoval(
71 CharSourceRange::getCharRange(ExplicitTokenRange));
72 }
73 }
74
75 if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() ||
76 Ctor->getNumParams() == 0 || Ctor->getMinRequiredArguments() > 1)
77 return;
78
79 SourceLocation Loc = Ctor->getLocation();
80 diag(Loc, "Single-argument constructors must be explicit")
81 << FixItHint::CreateInsertion(Loc, "explicit ");
82}
83
84} // namespace tidy
85} // namespace clang