Dosi-Dough updated this revision to Diff 184431. Dosi-Dough marked 11 inline comments as done. Dosi-Dough added a comment.
fixed white space/ formatting issues. removed .DS_Store. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D57435/new/ https://reviews.llvm.org/D57435 Files: clang-tidy/.DS_Store clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/WrapUniqueCheck.cpp docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-wrap-unique.rst test/clang-tidy/abseil-wrap-unique.cpp
Index: test/clang-tidy/abseil-wrap-unique.cpp =================================================================== --- test/clang-tidy/abseil-wrap-unique.cpp +++ test/clang-tidy/abseil-wrap-unique.cpp @@ -1,6 +1,5 @@ // RUN: %check_clang_tidy %s abseil-wrap-unique %t - namespace std { template <typename T> @@ -29,7 +28,6 @@ }; } // namespace std - class A { public: static A* NewA() { @@ -85,7 +83,5 @@ //CHECK-FIXES: e = absl::WrapUnique(A::NewA()) //std::unique_ptr<int> e(new int[2] {1,2}); - } - Index: docs/clang-tidy/checks/abseil-wrap-unique.rst =================================================================== --- docs/clang-tidy/checks/abseil-wrap-unique.rst +++ docs/clang-tidy/checks/abseil-wrap-unique.rst @@ -2,33 +2,30 @@ abseil-wrap-unique ================== - -Checks for instances of static function within a class being called and -returning a std:unique_ptr<T> type. Also checks for instances where reset -is called on a static function which returns std::unique_ptr<T>. +Looks for instances of factory functions which uses a non-public constructor +that returns a ``std::unqiue_ptr<T>`` then recommends using +``absl::wrap_unique(new T(...))``. .. code-block:: c++ - class A { - public: - static A* NewA() { - return new A(); - } + public: + static A* NewA() { return new A(); } - private: - A() {} + private: + A() {} }; - + std::unique_ptr<A> a; - - //Original - reset called with a static function returning a std::unqiue_ptr + + // Original - reset called with a static function returning a std::unqiue_ptr a.reset(A::NewA()); - //Suggested - reset ptr with absl::WrapUnique + // Suggested - reset ptr with absl::WrapUnique a = absl::WrapUnique(A::NewA()); - //Original - std::unique_ptr initialized with static function + // Original - std::unique_ptr initialized with static function std::unique_ptr<A> b(A::NewA()); - //Suggested - initialize with absl::WrapUnique instead + // Suggested - initialize with absl::WrapUnique instead auto b = absl::WrapUnique(A::NewA()) + Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,19 +67,20 @@ Improvements to clang-tidy -------------------------- -- New :doc:`abseil-wrap-unique - <clang-tidy/checks/abseil-wrap-unique>` check. - - Looks for instances of factory functions which uses a non-public constructor - that returns a std::unqiue_ptr<T> then recommends using - absl::wrap_unique(new T(...)). - - New :doc:`abseil-duration-conversion-cast <clang-tidy/checks/abseil-duration-conversion-cast>` check. Checks for casts of ``absl::Duration`` conversion functions, and recommends the right conversion function instead. +- New :doc:`abseil-wrap-unique + <clang-tidy/checks/abseil-wrap-unique>` check. + + Looks for instances of factory functions which uses a non-public constructor + that returns a ``std::unqiue_ptr<T>`` then recommends using + ``absl::wrap_unique(new T(...))``. + + Improvements to include-fixer ----------------------------- Index: clang-tidy/abseil/WrapUniqueCheck.cpp =================================================================== --- clang-tidy/abseil/WrapUniqueCheck.cpp +++ clang-tidy/abseil/WrapUniqueCheck.cpp @@ -1,4 +1,4 @@ -//===--- WrapUniqueCheck.cpp - clang-tidy ---------------------------------===// +//===--- WrapUniqueCheck.cpp - clang-tidy ---------------------------------===// // // The LLVM Compiler Infrastructure // @@ -7,10 +7,10 @@ // //===----------------------------------------------------------------------===// -#include <string> #include "WrapUniqueCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include <string> using namespace clang::ast_matchers; @@ -21,26 +21,30 @@ std::string WrapUniqueCheck::getArgs(const SourceManager *SM, const CallExpr *MemExpr) { llvm::StringRef ArgRef = Lexer::getSourceText( - CharSourceRange::getCharRange( - MemExpr->getSourceRange()), *SM, LangOptions()); + CharSourceRange::getCharRange(MemExpr->getSourceRange()), *SM, + LangOptions()); return (ArgRef.str().length() > 0) ? ArgRef.str() + ")" : "()"; } void WrapUniqueCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher( - cxxMemberCallExpr( - callee(cxxMethodDecl(hasName("reset"), - ofClass(cxxRecordDecl(hasName("std::unique_ptr"), decl())))), - has(memberExpr(has(declRefExpr()))), - has(callExpr(has(implicitCastExpr(has(declRefExpr()))))), - hasArgument(0, callExpr().bind("callExpr"))).bind("facCons"), this); + Finder->addMatcher( + cxxMemberCallExpr( + callee(cxxMethodDecl( + hasName("reset"), + ofClass(cxxRecordDecl(hasName("std::unique_ptr"), decl())))), + has(memberExpr(has(declRefExpr()))), + has(callExpr(has(implicitCastExpr(has(declRefExpr()))))), + hasArgument(0, callExpr().bind("callExpr"))) + .bind("facCons"), + this); Finder->addMatcher( - cxxConstructExpr( - anyOf(hasParent(decl().bind("cons_decl")), anything()), - hasType(cxxRecordDecl(hasName("std::unique_ptr"))), - has(callExpr().bind("FC_call"))).bind("upfc"),this); + cxxConstructExpr(anyOf(hasParent(decl().bind("cons_decl")), anything()), + hasType(cxxRecordDecl(hasName("std::unique_ptr"))), + has(callExpr().bind("FC_call"))) + .bind("upfc"), + this); } void WrapUniqueCheck::check(const MatchFinder::MatchResult &Result) { @@ -53,49 +57,48 @@ const auto *consDecl = Result.Nodes.getNodeAs<Decl>("cons_decl"); const auto *FC_Call = Result.Nodes.getNodeAs<CallExpr>("FC_call"); - if(facExpr){ + if (facExpr) { std::string diagText = "Perfer absl::WrapUnique for resetting unique_ptr"; std::string newText; - - const Expr *ObjectArg = facExpr -> getImplicitObjectArgument(); - SourceLocation Target = ObjectArg -> getExprLoc(); - llvm::StringRef ObjName = Lexer::getSourceText(CharSourceRange::getCharRange( - ObjectArg->getBeginLoc(), facExpr->getExprLoc().getLocWithOffset(-1)), - *SM, LangOptions()); - newText = ObjName.str() + " = absl::WrapUnique(" + getArgs(SM, callExpr) + ")"; + const Expr *ObjectArg = facExpr->getImplicitObjectArgument(); + SourceLocation Target = ObjectArg->getExprLoc(); + llvm::StringRef ObjName = + Lexer::getSourceText(CharSourceRange::getCharRange( + ObjectArg->getBeginLoc(), + facExpr->getExprLoc().getLocWithOffset(-1)), + *SM, LangOptions()); - diag(Target, diagText) - << FixItHint::CreateReplacement( - CharSourceRange::getTokenRange( - Target, facExpr->getEndLoc()), newText); + newText = + ObjName.str() + " = absl::WrapUnique(" + getArgs(SM, callExpr) + ")"; + + diag(Target, diagText) << FixItHint::CreateReplacement( + CharSourceRange::getTokenRange(Target, facExpr->getEndLoc()), newText); } - - if(cons){ - - if (cons->isListInitialization()){ - return; - } + + if (cons) { + if (cons->isListInitialization()) { + return; + } std::string diagText = "Perfer absl::WrapUnique to constructing unique_ptr"; std::string newText; std::string Left; - llvm::StringRef NameRef = Lexer::getSourceText(CharSourceRange::getCharRange( - cons->getBeginLoc(), cons->getParenOrBraceRange().getBegin()),*SM, - LangOptions()); + llvm::StringRef NameRef = Lexer::getSourceText( + CharSourceRange::getCharRange(cons->getBeginLoc(), + cons->getParenOrBraceRange().getBegin()), + *SM, LangOptions()); - Left = (consDecl) ? "auto " + NameRef.str() + " = " : ""; + Left = (consDecl) ? "auto " + NameRef.str() + " = " : ""; newText = Left + "absl::WrapUnique(" + getArgs(SM, FC_Call) + ")"; - SourceLocation Target = (consDecl) ? consDecl -> getBeginLoc() : - cons -> getExprLoc(); - - diag(Target, diagText) - << FixItHint::CreateReplacement( - CharSourceRange::getTokenRange( - Target, cons->getEndLoc()), newText); + SourceLocation Target = + (consDecl) ? consDecl->getBeginLoc() : cons->getExprLoc(); + + diag(Target, diagText) << FixItHint::CreateReplacement( + CharSourceRange::getTokenRange(Target, cons->getEndLoc()), newText); } } } // namespace abseil } // namespace tidy -} // namespace clang +} // namespace clang Index: clang-tidy/abseil/AbseilTidyModule.cpp =================================================================== --- clang-tidy/abseil/AbseilTidyModule.cpp +++ clang-tidy/abseil/AbseilTidyModule.cpp @@ -58,7 +58,6 @@ "abseil-upgrade-duration-conversions"); CheckFactories.registerCheck<WrapUniqueCheck>( "abseil-wrap-unique"); - } };
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits