clang-tools  10.0.0git
NoAutomaticMoveCheck.cpp
Go to the documentation of this file.
1 //===--- NoAutomaticMoveCheck.cpp - clang-tidy ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "NoAutomaticMoveCheck.h"
10 #include "../utils/Matchers.h"
11 #include "../utils/OptionsUtils.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace performance {
20 
21 NoAutomaticMoveCheck::NoAutomaticMoveCheck(StringRef Name,
22  ClangTidyContext *Context)
23  : ClangTidyCheck(Name, Context),
24  AllowedTypes(
25  utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
26 
27 void NoAutomaticMoveCheck::registerMatchers(MatchFinder *Finder) {
28  // Automatic move exists only for c++11 onwards.
29  if (!getLangOpts().CPlusPlus11)
30  return;
31 
32  const auto ConstLocalVariable =
33  varDecl(hasLocalStorage(), unless(hasType(lValueReferenceType())),
34  hasType(qualType(
35  isConstQualified(),
36  hasCanonicalType(matchers::isExpensiveToCopy()),
37  unless(hasDeclaration(namedDecl(
38  matchers::matchesAnyListedName(AllowedTypes)))))))
39  .bind("vardecl");
40 
41  // A matcher for a `DstT::DstT(const Src&)` where DstT also has a
42  // `DstT::DstT(Src&&)`.
43  const auto LValueRefCtor = cxxConstructorDecl(
44  hasParameter(0,
45  hasType(lValueReferenceType(pointee(type().bind("SrcT"))))),
46  ofClass(cxxRecordDecl(hasMethod(cxxConstructorDecl(
47  hasParameter(0, hasType(rValueReferenceType(
48  pointee(type(equalsBoundNode("SrcT")))))))))));
49 
50  Finder->addMatcher(
51  returnStmt(
52  hasReturnValue(ignoringElidableConstructorCall(ignoringParenImpCasts(
53  cxxConstructExpr(hasDeclaration(LValueRefCtor),
54  hasArgument(0, ignoringParenImpCasts(declRefExpr(
55  to(ConstLocalVariable)))))
56  .bind("ctor_call"))))),
57  this);
58 }
59 
60 void NoAutomaticMoveCheck::check(const MatchFinder::MatchResult &Result) {
61  const auto *Var = Result.Nodes.getNodeAs<VarDecl>("vardecl");
62  const auto *CtorCall = Result.Nodes.getNodeAs<Expr>("ctor_call");
63  diag(CtorCall->getExprLoc(), "constness of '%0' prevents automatic move")
64  << Var->getName();
65 }
66 
68  Options.store(Opts, "AllowedTypes",
70 }
71 
72 } // namespace performance
73 } // namespace tidy
74 } // namespace clang
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
llvm::Optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
Definition: TypeTraits.cpp:41
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.