Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/performance/NoAutomaticMoveCheck.cpp

raw
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::tidy::performance {
18
19 NoAutomaticMoveCheck::NoAutomaticMoveCheck(StringRef Name,
20 ClangTidyContext *Context)
21 : ClangTidyCheck(Name, Context),
22 AllowedTypes(
23 utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
24
25 void NoAutomaticMoveCheck::registerMatchers(MatchFinder *Finder) {
26 const auto ConstLocalVariable =
27 varDecl(hasLocalStorage(), unless(hasType(lValueReferenceType())),
28 hasType(qualType(
29 isConstQualified(),
30 hasCanonicalType(matchers::isExpensiveToCopy()),
31 unless(hasDeclaration(namedDecl(
32 matchers::matchesAnyListedName(AllowedTypes)))))))
33 .bind("vardecl");
34
35 // A matcher for a `DstT::DstT(const Src&)` where DstT also has a
36 // `DstT::DstT(Src&&)`.
37 const auto LValueRefCtor = cxxConstructorDecl(
38 hasParameter(0,
39 hasType(lValueReferenceType(pointee(type().bind("SrcT"))))),
40 ofClass(cxxRecordDecl(hasMethod(cxxConstructorDecl(
41 hasParameter(0, hasType(rValueReferenceType(
42 pointee(type(equalsBoundNode("SrcT")))))))))));
43
44 Finder->addMatcher(
45 traverse(TK_AsIs,
46 returnStmt(hasReturnValue(
47 ignoringElidableConstructorCall(ignoringParenImpCasts(
48 cxxConstructExpr(
49 hasDeclaration(LValueRefCtor),
50 hasArgument(0, ignoringParenImpCasts(declRefExpr(
51 to(ConstLocalVariable)))))
52 .bind("ctor_call")))))),
53 this);
54 }
55
56 void NoAutomaticMoveCheck::check(const MatchFinder::MatchResult &Result) {
57 const auto *Var = Result.Nodes.getNodeAs<VarDecl>("vardecl");
58 const auto *CtorCall = Result.Nodes.getNodeAs<Expr>("ctor_call");
59 diag(CtorCall->getExprLoc(), "constness of '%0' prevents automatic move")
60 << Var->getName();
61 }
62
63 void NoAutomaticMoveCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
64 Options.store(Opts, "AllowedTypes",
65 utils::options::serializeStringList(AllowedTypes));
66 }
67
68 } // namespace clang::tidy::performance