[Openmp-commits] [libc] [flang] [openmp] [compiler-rt] [libcxx] [clang] [llvm] [clang-tools-extra] [clang-tidy] Add bugprone-move-shared-pointer-contents check. (PR #67467)

Piotr Zegar via Openmp-commits openmp-commits at lists.llvm.org
Mon Dec 4 22:12:42 PST 2023


================
@@ -0,0 +1,49 @@
+//===--- 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) {
+  Finder->addMatcher(
+      callExpr(anyOf(callee(functionDecl(hasName("::std::move"))),
+                     callee(unresolvedLookupExpr(hasAnyDeclaration(namedDecl(
+                         hasUnderlyingDecl(hasName("::std::move"))))))),
+               hasArgument(0, anyOf(cxxOperatorCallExpr(
+                                        hasOverloadedOperatorName("*"),
+                                        callee(cxxMethodDecl(ofClass(
+                                            matchers::matchesAnyListedName(
+                                                SharedPointerClasses))))),
+                                    unaryOperator(hasOperatorName("*")))))
----------------
PiotrZSL wrote:

What you need is something like:
```
callExpr(callee(unresolvedLookupExpr()), hasArgument(0, unaryOperator(hasUnaryOperand(expr(hasType(namedDecl().bind("c")))))))
```

But additionally:
- you need to ignore some implicit code in here, mainly in hasArgument(0, ...)
- you need take into account that type may be typedef to shared_ptr (you need to get canonical type)
- you need take into account that type may be a reference to shared_ptr (you may need to drop reference)

As for get, it's actually easy to handle:
```
CallExpr 0x55e2d6219a18 <col:10, col:28> '<dependent type>'
            |-UnresolvedLookupExpr 0x55e2d6219928 <col:10, col:15> '<overloaded function type>' lvalue (no ADL) = 'move' 0x55e2d5a7c948 0x55e2d5c63b08
            `-UnaryOperator 0x55e2d6219a00 <col:20, col:27> '<dependent type>' prefix '*' cannot overflow
              `-CallExpr 0x55e2d62199e0 <col:21, col:27> '<dependent type>'
                `-CXXDependentScopeMemberExpr 0x55e2d6219998 <col:21, col:23> '<dependent type>' lvalue .get
                  `-DeclRefExpr 0x55e2d6219978 <col:21> 'std::shared_ptr<T>':'shared_ptr<T>' lvalue ParmVar 0x55e2d6219608 'p' 'std::shared_ptr<T>':'shared_ptr<T>'
```

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


More information about the Openmp-commits mailing list