From: Miklos Vajna Date: Sun, 21 Oct 2018 19:16:25 +0000 (+0000) Subject: [clang-tidy] add IgnoreMacros option to readability-redundant-smartptr-get X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e967a12733565fff0beb16865bd21e381b75b250;p=platform%2Fupstream%2Fllvm.git [clang-tidy] add IgnoreMacros option to readability-redundant-smartptr-get And also enable it by default to be consistent with e.g. modernize-use-using. This helps e.g. when running this check on client code where the macro is provided by the system, so there is no easy way to modify it. Reviewed By: JonasToth Differential Revision: https://reviews.llvm.org/D53454 llvm-svn: 344871 --- diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp index b03b546..189130e 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp @@ -91,6 +91,11 @@ void registerMatchersForGetEquals(MatchFinder *Finder, } // namespace +void RedundantSmartptrGetCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IgnoreMacros", IgnoreMacros); +} + void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) { // Only register the matchers for C++; the functionality currently does not // provide any benefit to other languages, despite being benign. @@ -126,6 +131,9 @@ void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) { bool IsPtrToPtr = Result.Nodes.getNodeAs("ptr_to_ptr") != nullptr; bool IsMemberExpr = Result.Nodes.getNodeAs("memberExpr") != nullptr; const auto *GetCall = Result.Nodes.getNodeAs("redundant_get"); + if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros) + return; + const auto *Smartptr = Result.Nodes.getNodeAs("smart_pointer"); if (IsPtrToPtr && IsMemberExpr) { diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.h b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.h index 1da6195..a6f5706 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.h +++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.h @@ -28,9 +28,14 @@ namespace readability { class RedundantSmartptrGetCheck : public ClangTidyCheck { public: RedundantSmartptrGetCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), + IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", 1) != 0) {} + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + const bool IgnoreMacros; }; } // namespace readability diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 1af6d05..5c1fde5 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -67,6 +67,10 @@ The improvements are... Improvements to clang-tidy -------------------------- +- The :doc:`readability-redundant-smartptr-get + ` check does not warn + about calls inside macros anymore by default. + - New :doc:`abseil-duration-division ` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability-redundant-smartptr-get.rst b/clang-tools-extra/docs/clang-tidy/checks/readability-redundant-smartptr-get.rst index 3fc77c5..5489419 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability-redundant-smartptr-get.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability-redundant-smartptr-get.rst @@ -14,3 +14,8 @@ Examples: *ptr->get() ==> **ptr if (ptr.get() == nullptr) ... => if (ptr == nullptr) ... + +.. option:: IgnoreMacros + + If this option is set to non-zero (default is `1`), the check will not warn + about calls inside macros. diff --git a/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get-macros.cpp b/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get-macros.cpp new file mode 100644 index 0000000..79e4d92 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get-macros.cpp @@ -0,0 +1,24 @@ +// RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t -- \ +// RUN: -config="{CheckOptions: [{key: readability-redundant-smartptr-get.IgnoreMacros, value: 0}]}" \ +// RUN: -- -std=c++11 + +namespace std { + +template +struct shared_ptr { + T &operator*() const; + T *operator->() const; + T *get() const; + explicit operator bool() const noexcept; +}; + +} // namespace std + +#define MACRO(p) p.get() + +void Positive() { + std::shared_ptr x; + if (MACRO(x) == nullptr) + ; + // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: redundant get() call on smart pointer +}; diff --git a/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get.cpp b/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get.cpp index 685a57c..51d9879 100644 --- a/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get.cpp +++ b/clang-tools-extra/test/clang-tidy/readability-redundant-smartptr-get.cpp @@ -168,6 +168,8 @@ void Positive() { // CHECK-FIXES: if (NULL == x); } +#define MACRO(p) p.get() + void Negative() { struct NegPtr { int* get(); @@ -193,4 +195,7 @@ void Negative() { bool bb = ip.get() == nullptr; bb = !ip.get(); bb = ip.get() ? true : false; + std::unique_ptr x; + if (MACRO(x) == nullptr) + ; }