[clang-tools-extra] r344871 - [clang-tidy] add IgnoreMacros option to readability-redundant-smartptr-get

Miklos Vajna via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 21 12:16:26 PDT 2018


Author: vmiklos
Date: Sun Oct 21 12:16:25 2018
New Revision: 344871

URL: http://llvm.org/viewvc/llvm-project?rev=344871&view=rev
Log:
[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

Added:
    clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get-macros.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
    clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.h
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-smartptr-get.rst
    clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp?rev=344871&r1=344870&r2=344871&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.cpp Sun Oct 21 12:16:25 2018
@@ -91,6 +91,11 @@ void registerMatchersForGetEquals(MatchF
 
 } // 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(co
   bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
   bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
   const auto *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
+  if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros)
+    return;
+
   const auto *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
 
   if (IsPtrToPtr && IsMemberExpr) {

Modified: clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.h?rev=344871&r1=344870&r2=344871&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/RedundantSmartptrGetCheck.h Sun Oct 21 12:16:25 2018
@@ -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

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=344871&r1=344870&r2=344871&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Sun Oct 21 12:16:25 2018
@@ -67,6 +67,10 @@ The improvements are...
 Improvements to clang-tidy
 --------------------------
 
+- The :doc:`readability-redundant-smartptr-get
+  <clang-tidy/checks/readability-redundant-smartptr-get>` check does not warn
+  about calls inside macros anymore by default.
+
 - New :doc:`abseil-duration-division
   <clang-tidy/checks/abseil-duration-division>` check.
 

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-smartptr-get.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-smartptr-get.rst?rev=344871&r1=344870&r2=344871&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-smartptr-get.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-smartptr-get.rst Sun Oct 21 12:16:25 2018
@@ -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.

Added: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get-macros.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get-macros.cpp?rev=344871&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get-macros.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get-macros.cpp Sun Oct 21 12:16:25 2018
@@ -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 <typename T>
+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<int> x;
+  if (MACRO(x) == nullptr)
+    ;
+  // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: redundant get() call on smart pointer
+};

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp?rev=344871&r1=344870&r2=344871&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-redundant-smartptr-get.cpp Sun Oct 21 12:16:25 2018
@@ -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<int> x;
+  if (MACRO(x) == nullptr)
+    ;
 }




More information about the cfe-commits mailing list