diff options
| author | Martin Bohme <mboehme@google.com> | 2018-08-13 14:11:03 +0000 |
|---|---|---|
| committer | Martin Bohme <mboehme@google.com> | 2018-08-13 14:11:03 +0000 |
| commit | 4e1293b5e1b68fc2e3ca2bd85ade431350ab3c61 (patch) | |
| tree | 4319d5ed2aadcef81e79a4069c01ecd71798843b /clang | |
| parent | 0ce6360e0efdf12fbecc5a931a43449091e6f5f0 (diff) | |
| download | bcm5719-llvm-4e1293b5e1b68fc2e3ca2bd85ade431350ab3c61.tar.gz bcm5719-llvm-4e1293b5e1b68fc2e3ca2bd85ade431350ab3c61.zip | |
Summary:Add clang::reinitializes attribute
Summary:
This is for use by clang-tidy's bugprone-use-after-move check -- see
corresponding clang-tidy patch at https://reviews.llvm.org/D49910.
Reviewers: aaron.ballman, rsmith
Reviewed By: aaron.ballman
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D49911
llvm-svn: 339569
Diffstat (limited to 'clang')
| -rw-r--r-- | clang/include/clang/Basic/Attr.td | 11 | ||||
| -rw-r--r-- | clang/include/clang/Basic/AttrDocs.td | 28 | ||||
| -rw-r--r-- | clang/lib/Sema/SemaDeclAttr.cpp | 5 | ||||
| -rw-r--r-- | clang/test/SemaCXX/attr-reinitializes.cpp | 15 |
4 files changed, 59 insertions, 0 deletions
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 7f0417dffe4..75916351766 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -90,6 +90,11 @@ def NonBitField : SubsetSubject<Field, [{!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">; @@ -2974,3 +2979,9 @@ def InternalLinkage : InheritableAttr { let Subjects = SubjectList<[Var, Function, CXXRecord]>; let Documentation = [InternalLinkageDocs]; } + +def Reinitializes : InheritableAttr { + let Spellings = [Clang<"reinitializes", 0>]; + let Subjects = SubjectList<[NonStaticNonConstCXXMethod], ErrorDiag>; + let Documentation = [ReinitializesDocs]; +} diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index bb2993eab4b..27334b535ad 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -3458,3 +3458,31 @@ the resulting instructions with the call site, rather than with the 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(); + ... + }; + }]; +} diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 800bf619708..39d7019b259 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -6582,6 +6582,11 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, case ParsedAttr::AT_XRayLogArgs: handleXRayLogArgsAttr(S, D, AL); break; + + // Move semantics attribute. + case ParsedAttr::AT_Reinitializes: + handleSimpleAttribute<ReinitializesAttr>(S, D, AL); + break; } } diff --git a/clang/test/SemaCXX/attr-reinitializes.cpp b/clang/test/SemaCXX/attr-reinitializes.cpp new file mode 100644 index 00000000000..129d359b8b7 --- /dev/null +++ b/clang/test/SemaCXX/attr-reinitializes.cpp @@ -0,0 +1,15 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s + +[[clang::reinitializes]] int a; // expected-error {{'reinitializes' attribute only applies to non-static non-const member functions}} + +[[clang::reinitializes]] void f(); // expected-error {{only applies to}} + +struct A { + [[clang::reinitializes]] void foo(); + __attribute__((reinitializes)) void gnu_foo(); + [[clang::reinitializes]] void bar() const; // expected-error {{only applies to}} + [[clang::reinitializes]] static void baz(); // expected-error {{only applies to}} + [[clang::reinitializes]] int a; // expected-error {{only applies to}} + + [[clang::reinitializes("arg")]] void qux(); // expected-error {{'reinitializes' attribute takes no arguments}} +}; |

