clang-tools 10.0.0

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
67 void NoAutomaticMoveCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
68 Options.store(Opts, "AllowedTypes",
69 utils::options::serializeStringList(AllowedTypes));
70 }
71
72 } // namespace performance
73 } // namespace tidy
74 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::performance::NoAutomaticMoveCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: NoAutomaticMoveCheck.cpp:60
clang::tidy::utils::options::serializeStringList
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
Definition: OptionsUtils.cpp:30
NoAutomaticMoveCheck.h
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:18
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::utils::type_traits::isExpensiveToCopy
llvm::Optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
Definition: TypeTraits.cpp:41
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:58
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::performance::NoAutomaticMoveCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: NoAutomaticMoveCheck.cpp:67
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::performance::NoAutomaticMoveCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: NoAutomaticMoveCheck.cpp:27
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21