================
@@ -0,0 +1,124 @@
+//===--- UseStdMoveCheck.cpp - clang-tidy 
---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "UseStdMoveCheck.h"
+
+#include "../utils/DeclRefExprUtils.h"
+
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
+#include "clang/Lex/Lexer.h"
+#include "llvm/ADT/STLExtras.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::performance {
+
+namespace {
+AST_MATCHER(CXXRecordDecl, hasTrivialMoveAssignment) {
+  return Node.hasTrivialMoveAssignment();
+}
+
+AST_MATCHER(QualType, isScalarType) { return Node->isScalarType(); }
+
+AST_MATCHER(QualType, isLValueReferenceType) {
+  return Node->isLValueReferenceType();
+}
+
+// Ignore nodes inside macros.
+AST_POLYMORPHIC_MATCHER(isInMacro,
+                        AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl)) {
+  return Node.getBeginLoc().isMacroID() || Node.getEndLoc().isMacroID();
+}
+} // namespace
+
+using utils::decl_ref_expr::allDeclRefExprs;
+
+void UseStdMoveCheck::registerMatchers(MatchFinder *Finder) {
+  auto AssignOperatorExpr =
+      cxxOperatorCallExpr(
+          hasOperatorName("="),
+          hasArgument(
+              0, hasType(cxxRecordDecl(hasMethod(isMoveAssignmentOperator()),
+                                       unless(hasTrivialMoveAssignment())))),
+          hasArgument(
+              1, declRefExpr(
+                     to(varDecl(hasLocalStorage(),
+                                hasType(qualType(unless(anyOf(
+                                    isScalarType(), isLValueReferenceType(),
+                                    isConstQualified() // Not valid.
+                                    )))))))
+                     .bind("assign-value")),
+          forCallable(functionDecl().bind("within-func")), unless(isInMacro()))
+          .bind("assign");
+  Finder->addMatcher(AssignOperatorExpr, this);
+}
+
+const CFG *UseStdMoveCheck::getCFG(const FunctionDecl *FD,
+                                   ASTContext *Context) {
+  std::unique_ptr<CFG> &TheCFG = CFGCache[FD];
+  if (!TheCFG) {
+    const CFG::BuildOptions Options;
+    std::unique_ptr<CFG> FCFG =
+        CFG::buildCFG(nullptr, FD->getBody(), Context, Options);
+    if (!FCFG)
+      return nullptr;
+    TheCFG.swap(FCFG);
+  }
+  return TheCFG.get();
+}
+
+void UseStdMoveCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *AssignExpr = Result.Nodes.getNodeAs<Expr>("assign");
+  const auto *AssignValue = 
Result.Nodes.getNodeAs<DeclRefExpr>("assign-value");
+  const auto *WithinFunctionDecl =
+      Result.Nodes.getNodeAs<FunctionDecl>("within-func");
+
+  if (AssignValue->refersToEnclosingVariableOrCapture())
+    return;
+
+  const CXXRecordDecl *AssignValueRD =
+      AssignValue->getDecl()->getType().getTypePtr()->getAsCXXRecordDecl();
----------------
flovent wrote:

```suggestion
      AssignValue->getDecl()->getType()->getAsCXXRecordDecl();
```

https://github.com/llvm/llvm-project/pull/179467
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to