juliehockett created this revision.
juliehockett added reviewers: aaron.ballman, alexfh, hokein.
juliehockett added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, mgorny.

Adds a check to convert fbl::move to std::move.

This check is part of a set of migration checks as we prepare to move Zircon 
user code to use the C++ standard library, and should prevent regressions after 
the migration is complete.


https://reviews.llvm.org/D54168

Files:
  clang-tools-extra/clang-tidy/zircon/CMakeLists.txt
  clang-tools-extra/clang-tidy/zircon/FblMoveCheck.cpp
  clang-tools-extra/clang-tidy/zircon/FblMoveCheck.h
  clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/zircon-fbl-move.rst
  clang-tools-extra/test/clang-tidy/zircon-fbl-move.cpp

Index: clang-tools-extra/test/clang-tidy/zircon-fbl-move.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/zircon-fbl-move.cpp
@@ -0,0 +1,23 @@
+// RUN: %check_clang_tidy %s zircon-fbl-move %t
+
+namespace fbl {
+
+void move(int i) {}
+
+} // namespace fbl
+
+namespace std {
+
+void move(int i) {}
+
+} // namespace std
+
+int main() {
+  int i;
+  fbl::move(i);
+  // CHECK-NOTES: [[@LINE-1]]:3: warning: fbl::move is deprecated, use std::move instead
+  // CHECK-FIXES: #include <utility>
+  // CHECK-FIXES: std::move
+
+  std::move(i);
+}
Index: clang-tools-extra/docs/clang-tidy/checks/zircon-fbl-move.rst
===================================================================
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/zircon-fbl-move.rst
@@ -0,0 +1,11 @@
+.. title:: clang-tidy - zircon-fbl-move
+
+zircon-fbl-move
+===============
+
+This check is part of the migration checks for moving Zircon user code to use
+the C++ standard library.
+
+It suggests converting uses of ``fbl::move`` to ``std::move``, and suggests
+inserting the ``<utility>`` header.
+
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -254,4 +254,5 @@
    readability-string-compare
    readability-uniqueptr-delete-release
    readability-uppercase-literal-suffix
+   zircon-fbl-move
    zircon-temporary-objects
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -182,6 +182,13 @@
   <clang-tidy/checks/readability-redundant-smartptr-get>` check does not warn
   about calls inside macros anymore by default.
 
+- New :doc:`zircon-fbl-move
+  <clang-tidy/checks/zircon-fbl-include-new>` check.
+
+  This check is part of the migration checks for moving Zircon user code to use
+  the C++ standard library. It suggests converting uses of ``fbl::move`` to
+  ``std::move``, and suggests inserting the ``<utility>`` header.
+
 Improvements to include-fixer
 -----------------------------
 
Index: clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp
===================================================================
--- clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp
+++ clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp
@@ -10,6 +10,7 @@
 #include "../ClangTidy.h"
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
+#include "FblMoveCheck.h"
 #include "TemporaryObjectsCheck.h"
 
 using namespace clang::ast_matchers;
@@ -22,6 +23,8 @@
 class ZirconModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+    CheckFactories.registerCheck<FblMoveCheck>(
+        "zircon-fbl-move");
     CheckFactories.registerCheck<TemporaryObjectsCheck>(
         "zircon-temporary-objects");
   }
Index: clang-tools-extra/clang-tidy/zircon/FblMoveCheck.h
===================================================================
--- /dev/null
+++ clang-tools-extra/clang-tidy/zircon/FblMoveCheck.h
@@ -0,0 +1,45 @@
+//===--- FblMoveCheck.h - clang-tidy ----------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ZIRCON_FBLINCLUDENEWCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ZIRCON_FBLINCLUDENEWCHECK_H
+
+#include "../ClangTidy.h"
+#include "../utils/IncludeInserter.h"
+
+namespace clang {
+namespace tidy {
+namespace zircon {
+
+/// Replace instances of fbl::move with std::move, and adds the <utility> header
+/// if a replacement occurs.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/zircon-fbl-move.html
+class FblMoveCheck : public ClangTidyCheck {
+public:
+  FblMoveCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context),
+        IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
+            Options.getLocalOrGlobal("IncludeStyle", "google"))) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void registerPPCallbacks(clang::CompilerInstance &Compiler) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
+
+private:
+  std::unique_ptr<utils::IncludeInserter> Inserter;
+  const utils::IncludeSorter::IncludeStyle IncludeStyle;
+};
+
+} // namespace zircon
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ZIRCON_FBLINCLUDENEWCHECK_H
Index: clang-tools-extra/clang-tidy/zircon/FblMoveCheck.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clang-tidy/zircon/FblMoveCheck.cpp
@@ -0,0 +1,75 @@
+//===--- FblMoveCheck.cpp - clang-tidy ------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "FblMoveCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace zircon {
+
+void FblMoveCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(callExpr(callee(functionDecl(allOf(
+                                  hasDeclContext(namespaceDecl(hasName("fbl"))),
+                                  hasName("move")))))
+                         .bind("move"),
+                     this);
+}
+
+void FblMoveCheck::registerPPCallbacks(CompilerInstance &Compiler) {
+  Inserter = llvm::make_unique<utils::IncludeInserter>(
+      Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle);
+  Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
+}
+
+void FblMoveCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *E = Result.Nodes.getNodeAs<CallExpr>("move");
+  if (const FunctionDecl *F = E->getDirectCallee()) {
+    // Find the end of the name of the call.
+    SourceManager &SM = *Result.SourceManager;
+    bool Invalid = false;
+    StringRef Statement =
+        Lexer::getSourceText(CharSourceRange::getCharRange(E->getSourceRange()),
+                             SM, getLangOpts(), &Invalid);
+    if (Invalid)
+      return;
+    size_t LParen = Statement.find('(');
+    if (LParen == std::string::npos || LParen == 0)
+      // There isn't an lparen or there isn't a function, so something's gone
+      // wrong.
+      return;
+    SourceLocation Start = E->getExprLoc();
+    // Remove the lparen from the size range, since we just want the function
+    // call.
+    SourceLocation End = Start.getLocWithOffset(LParen - 1);
+    auto Diag =
+        diag(E->getExprLoc(), "fbl::move is deprecated, use std::move instead")
+        << E->getExprLoc()
+        << FixItHint::CreateReplacement(SourceRange(Start, End), "std::move");
+    // Add in the <utility> header.
+    if (auto IncludeFixit =
+            Inserter->CreateIncludeInsertion(SM.getFileID(Start), "utility",
+                                             /*IsAngled=*/true)) {
+      Diag << *IncludeFixit;
+    }
+  }
+}
+
+void FblMoveCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IncludeStyle", IncludeStyle);
+}
+
+} // namespace zircon
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/clang-tidy/zircon/CMakeLists.txt
===================================================================
--- clang-tools-extra/clang-tidy/zircon/CMakeLists.txt
+++ clang-tools-extra/clang-tidy/zircon/CMakeLists.txt
@@ -1,6 +1,7 @@
 set(LLVM_LINK_COMPONENTS support)
 
 add_clang_library(clangTidyZirconModule
+  FblMoveCheck.cpp
   TemporaryObjectsCheck.cpp
   ZirconTidyModule.cpp
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to