[clang] [clang-tidy] Add performance-move-smart-pointer-contents check. (PR #66139)

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 13 03:37:03 PDT 2023


================
@@ -0,0 +1,95 @@
+//===--- MoveSmartPointerContentsCheck.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 <string>
+
+<<<<<<< HEAD
+#include "MoveSmartPointerContentsCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+=======
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
+#include "MoveSmartPointerContentsCheck.h"
+>>>>>>> b0179a7f3cac ([clang-tidy] Add performance-move-smart-pointer-contents check.)
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::performance {
+
+bool MoveSmartPointerContentsCheck::isLanguageVersionSupported(
+    const LangOptions &LangOptions) const {
+  if (!LangOptions.CPlusPlus) {
+    return false;
+  }
+
+  // Anything after C++11 is fine.
+  return LangOptions.CPlusPlus11;
+}
+
+MoveSmartPointerContentsCheck::MoveSmartPointerContentsCheck(
+    StringRef Name, ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      UniquePointerClasses(utils::options::parseStringList(
+          Options.get("UniquePointerClasses", "std::unique_ptr"))),
+      SharedPointerClasses(utils::options::parseStringList(
+          Options.get("SharedPointerClasses", "std::shared_ptr"))) {}
+
+void MoveSmartPointerContentsCheck::storeOptions(
+    ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "UniquePtrClasses",
+                utils::options::serializeStringList(UniquePointerClasses));
+  Options.store(Opts, "SharedPtrClasses",
+                utils::options::serializeStringList(SharedPointerClasses));
+}
+
+void MoveSmartPointerContentsCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+      callExpr(
+          callee(functionDecl(hasName("std::move"))),
+          hasArgument(
+              0,
+              anyOf(cxxOperatorCallExpr(hasOperatorName("*"),
+                                        hasDeclaration(cxxMethodDecl(namedDecl(
+                                            matchers::matchesAnyListedName(
+                                                UniquePointerClasses)))))
----------------
martinboehme wrote:

I believe the canonical way of doing this is:

```
cxxOperatorCallExpr(hasOverloadedOperatorName("*"),
                          callee(cxxMethodDecl(ofClass(matchers::matchesAnyListedName(
                                                UniquePointerClasses)))));
```

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


More information about the cfe-commits mailing list