[clang-tools-extra] [clang-tidy] Add bugprone-move-shared-pointer-contents check. (PR #67467)

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 1 13:22:38 PDT 2024


================
@@ -0,0 +1,157 @@
+//===--- MoveSharedPointerContentsCheck.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 "MoveSharedPointerContentsCheck.h"
+#include "../ClangTidyCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+MoveSharedPointerContentsCheck::MoveSharedPointerContentsCheck(
+    StringRef Name, ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      SharedPointerClasses(utils::options::parseStringList(
+          Options.get("SharedPointerClasses", "::std::shared_ptr"))) {}
+
+void MoveSharedPointerContentsCheck::registerMatchers(MatchFinder *Finder) {
+  auto isStdMove = callee(functionDecl(hasName("::std::move")));
+
+  // Resolved type, direct move.
+  Finder->addMatcher(
+      callExpr(isStdMove, hasArgument(0, cxxOperatorCallExpr(
+                                             hasOverloadedOperatorName("*"),
+                                             callee(cxxMethodDecl(ofClass(
+                                                 matchers::matchesAnyListedName(
+                                                     SharedPointerClasses)))))))
+          .bind("call"),
+      this);
+
+  // Resolved type, move out of get().
+  Finder->addMatcher(
+      callExpr(
+          isStdMove,
+          hasArgument(
+              0, unaryOperator(
+                     hasOperatorName("*"),
+                     hasUnaryOperand(cxxMemberCallExpr(callee(cxxMethodDecl(
+                         hasName("get"), ofClass(matchers::matchesAnyListedName(
+                                             SharedPointerClasses)))))))))
+          .bind("get_call"),
+      this);
+
+  auto isStdMoveUnresolved = callee(unresolvedLookupExpr(
+      hasAnyDeclaration(namedDecl(hasUnderlyingDecl(hasName("::std::move"))))));
+
+  // Unresolved type, direct move.
+  Finder->addMatcher(
+      callExpr(
+          isStdMoveUnresolved,
+          hasArgument(0, unaryOperator(hasOperatorName("*"),
+                                       hasUnaryOperand(declRefExpr(hasType(
+                                           qualType().bind("unresolved_p")))))))
+          .bind("unresolved_call"),
+      this);
+  // Annoyingly, the declRefExpr in the unresolved-move-of-get() case
+  // is of <dependent type> rather than shared_ptr<T>, so we have to
+  // just fetch the variable. This does leave a gap where a temporary
+  // shared_ptr wouldn't be caught, but moving out of a temporary
+  // shared pointer is a truly wild thing to do so it should be okay.
+
+  // Unresolved type, move out of get().
+  Finder->addMatcher(
+      callExpr(isStdMoveUnresolved,
+               hasArgument(
+                   0, unaryOperator(hasOperatorName("*"),
+                                    hasDescendant(cxxDependentScopeMemberExpr(
+                                        hasMemberName("get"))),
+                                    hasDescendant(declRefExpr(to(
----------------
pizzud wrote:

Couldn't actually get this to fire a false-positive.

https://github.com/llvm/llvm-project/pull/67467


More information about the cfe-commits mailing list