[PATCH] D49910: [clang-tidy] Recognize [[clang::reinitializes]] attribute in bugprone-use-after-move

Martin Böhme via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 13 07:25:44 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL339571: [clang-tidy] Recognize [[clang::reinitializes]] attribute in bugprone-use-after… (authored by mboehme, committed by ).
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

https://reviews.llvm.org/D49910

Files:
  clang-tools-extra/trunk/clang-tidy/bugprone/UseAfterMoveCheck.cpp
  clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-use-after-move.rst
  clang-tools-extra/trunk/test/clang-tidy/bugprone-use-after-move.cpp


Index: clang-tools-extra/trunk/clang-tidy/bugprone/UseAfterMoveCheck.cpp
===================================================================
--- clang-tools-extra/trunk/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -308,6 +308,10 @@
                cxxMemberCallExpr(
                    on(allOf(DeclRefMatcher, StandardSmartPointerTypeMatcher)),
                    callee(cxxMethodDecl(hasName("reset")))),
+               // Methods that have the [[clang::reinitializes]] attribute.
+               cxxMemberCallExpr(
+                   on(DeclRefMatcher),
+                   callee(cxxMethodDecl(hasAttr(clang::attr::Reinitializes)))),
                // Passing variable to a function as a non-const pointer.
                callExpr(forEachArgumentWithParam(
                    unaryOperator(hasOperatorName("&"),
Index: clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-use-after-move.rst
===================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-use-after-move.rst
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-use-after-move.rst
@@ -178,6 +178,9 @@
   - ``reset()`` is called on the variable and the variable is of type
     ``std::unique_ptr``, ``std::shared_ptr`` or ``std::weak_ptr``.
 
+  - A member function marked with the ``[[clang::reinitializes]]`` attribute is
+    called on the variable.
+
 If the variable in question is a struct and an individual member variable of
 that struct is written to, the check does not consider this to be a
 reinitialization -- even if, eventually, all member variables of the struct are
Index: clang-tools-extra/trunk/test/clang-tidy/bugprone-use-after-move.cpp
===================================================================
--- clang-tools-extra/trunk/test/clang-tidy/bugprone-use-after-move.cpp
+++ clang-tools-extra/trunk/test/clang-tidy/bugprone-use-after-move.cpp
@@ -107,6 +107,15 @@
   int i;
 };
 
+template <class T>
+class AnnotatedContainer {
+public:
+  AnnotatedContainer();
+
+  void foo() const;
+  [[clang::reinitializes]] void clear();
+};
+
 ////////////////////////////////////////////////////////////////////////////////
 // General tests.
 
@@ -898,6 +907,32 @@
   }
 }
 
+void reinitAnnotation() {
+  {
+    AnnotatedContainer<int> obj;
+    std::move(obj);
+    obj.foo();
+    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: 'obj' used after it was
+    // CHECK-MESSAGES: [[@LINE-3]]:5: note: move occurred here
+  }
+  {
+    AnnotatedContainer<int> obj;
+    std::move(obj);
+    obj.clear();
+    obj.foo();
+  }
+  {
+    // Calling clear() on a different object to the one that was moved is not
+    // considered a reinitialization.
+    AnnotatedContainer<int> obj1, obj2;
+    std::move(obj1);
+    obj2.clear();
+    obj1.foo();
+    // CHECK-MESSAGES: [[@LINE-1]]:5: warning: 'obj1' used after it was
+    // CHECK-MESSAGES: [[@LINE-4]]:5: note: move occurred here
+  }
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // Tests related to order of evaluation within expressions
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49910.160353.patch
Type: text/x-patch
Size: 3169 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180813/54f348c7/attachment.bin>


More information about the cfe-commits mailing list