[PATCH] D49911: Summary:Add clang::reinitializes attribute
Martin Böhme via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 27 05:30:13 PDT 2018
mboehme created this revision.
Herald added a subscriber: cfe-commits.
This is for use by clang-tidy's bugprone-use-after-move check -- see
corresponding clang-tidy patch at https://reviews.llvm.org/D49910.
Repository:
rC Clang
https://reviews.llvm.org/D49911
Files:
include/clang/Basic/Attr.td
include/clang/Basic/AttrDocs.td
lib/Sema/SemaDeclAttr.cpp
test/SemaCXX/attr-reinitializes.cpp
Index: test/SemaCXX/attr-reinitializes.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/attr-reinitializes.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+
+[[clang::reinitializes]] int a; // expected-error {{only applies to}}
+
+[[clang::reinitializes]] void f(); // expected-error {{only applies to}}
+
+struct A {
+ [[clang::reinitializes]] void foo();
+ [[clang::reinitializes]] void bar() const; // expected-error {{only applies to non-static non-const}}
+ [[clang::reinitializes]] static void baz(); // expected-error {{only applies to}}
+ [[clang::reinitializes]] int a; // expected-error {{only applies to}}
+};
Index: lib/Sema/SemaDeclAttr.cpp
===================================================================
--- lib/Sema/SemaDeclAttr.cpp
+++ lib/Sema/SemaDeclAttr.cpp
@@ -6539,6 +6539,11 @@
case ParsedAttr::AT_XRayLogArgs:
handleXRayLogArgsAttr(S, D, AL);
break;
+
+ // Move semantics attribute.
+ case ParsedAttr::AT_Reinitializes:
+ handleSimpleAttribute<ReinitializesAttr>(S, D, AL);
+ break;
}
}
Index: include/clang/Basic/AttrDocs.td
===================================================================
--- include/clang/Basic/AttrDocs.td
+++ include/clang/Basic/AttrDocs.td
@@ -3419,3 +3419,31 @@
corresponding line within the inlined callee.
}];
}
+
+def ReinitializesDocs : Documentation {
+ let Category = DocCatFunction;
+ let Content = [{
+The ``reinitializes`` attribute can be applied to a non-static, non-const C++
+member function to indicate that this member function reinitializes the entire
+object to a known state, independent of the previous state of the object.
+
+This attribute can be interpreted by static analyzers that warn about uses of an
+object that has been left in an indeterminate state by a move operation. If a
+member function marked with the ``reinitializes`` attribute is called on a
+moved-from object, the analyzer can conclude that the object is no longer in an
+indeterminate state.
+
+A typical example where this attribute would be used is on functions that clear
+a container class:
+
+.. code-block:: c++
+
+ template <class T>
+ class Container {
+ public:
+ ...
+ [[clang::reinitializes]] void Clear();
+ ...
+ };
+ }];
+}
Index: include/clang/Basic/Attr.td
===================================================================
--- include/clang/Basic/Attr.td
+++ include/clang/Basic/Attr.td
@@ -90,6 +90,11 @@
[{!S->isBitField()}],
"non-bit-field non-static data members">;
+def NonStaticNonConstCXXMethod
+ : SubsetSubject<CXXMethod,
+ [{!S->isStatic() && !S->isConst()}],
+ "non-static non-const member functions">;
+
def ObjCInstanceMethod : SubsetSubject<ObjCMethod,
[{S->isInstanceMethod()}],
"Objective-C instance methods">;
@@ -2935,3 +2940,9 @@
let Subjects = SubjectList<[Var, Function, CXXRecord]>;
let Documentation = [InternalLinkageDocs];
}
+
+def Reinitializes : InheritableAttr {
+ let Spellings = [CXX11<"clang", "reinitializes">];
+ let Subjects = SubjectList<[NonStaticNonConstCXXMethod], ErrorDiag>;
+ let Documentation = [ReinitializesDocs];
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49911.157670.patch
Type: text/x-patch
Size: 3362 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180727/59fce4c2/attachment.bin>
More information about the cfe-commits
mailing list