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 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 +/// `` 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 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,94 @@ +//===--- 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" +#include "llvm/ADT/Optional.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); + Finder->addMatcher( + declRefExpr(hasDeclaration(functionDecl( + allOf(hasDeclContext(namespaceDecl(hasName("::fbl"))), + hasName("move"))))) + .bind("move"), + this); +} + +void FblMoveCheck::registerPPCallbacks(CompilerInstance &Compiler) { + Inserter = llvm::make_unique( + Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle); + Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); +} + +void FblMoveCheck::check(const MatchFinder::MatchResult &Result) { + const auto *E = Result.Nodes.getNodeAs("move"); + + // Find the end of the name of the call. + SourceManager &SM = *Result.SourceManager; + SourceLocation Start = E->getBeginLoc(); + SourceLocation End = E->getEndLoc(); + + // If it's a macro, we want to extract information about the macro instead of + // the expr. + if (Start.isMacroID()) { + Start = SM.getSpellingLoc(Start); + End = SM.getSpellingLoc(End); + } + + bool Invalid = false; + StringRef Statement = Lexer::getSourceText( + CharSourceRange::getCharRange(Start, End), SM, getLangOpts(), &Invalid); + if (Invalid) + return; + + size_t LParen = Statement.find('('); + if (LParen == 0) + // There isn't a function, so something's gone wrong. + return; + if (LParen == std::string::npos) + // If there isn't an lparen, we want the whole statement. + End = Start.getLocWithOffset(Statement.size()); + else + // Remove the lparen from the size range, since we just want the call. + End = Start.getLocWithOffset(LParen - 1); + + DiagnosticBuilder Diag = + diag(Start, "fbl::move is deprecated, use std::move instead") + << FixItHint::CreateReplacement(SourceRange(Start, End), "std::move"); + + // Add in the header. + if (llvm::Optional 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/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( + "zircon-fbl-move"); CheckFactories.registerCheck( "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,12 @@ ` check does not warn about calls inside macros anymore by default. +- New :doc:`zircon-fbl-move + ` check. + + Suggests converting uses of ``fbl::move`` to ``std::move``, and suggests + to include the ```` header. + Improvements to include-fixer ----------------------------- 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/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 +to include the ```` header. + 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,37 @@ +// RUN: %check_clang_tidy %s zircon-fbl-move %t + +// CHECK-FIXES: #include + +namespace fbl { + +void move(int i) {} + +} // namespace fbl + +namespace std { + +void move(int i) {} + +} // namespace std + +#define MOVE(i) fbl::move(i) +// CHECK-NOTES: [[@LINE-1]]:17: warning: fbl::move is deprecated, use std::move instead +// CHECK-FIXES: #define MOVE(i) std::move(i) + +int main() { + int i = 0; + fbl::move(i); + // CHECK-NOTES: [[@LINE-1]]:3: warning: fbl::move is deprecated, use std::move instead + // CHECK-FIXES: std::move(i) + + int j = 0; + std::move(j); + + void (*foo)(int); + foo = &fbl::move; + // CHECK-NOTES: [[@LINE-1]]:10: warning: fbl::move is deprecated, use std::move instead + // CHECK-FIXES: foo = &std::move + + int k = 0; + MOVE(k); +}