The attribute specifies that the call of the C++ method consumes reference to "this".
Details
- Reviewers
aaron.ballman NoQ dcoughlin - Commits
- rGda2c77f92bbb: [attributes] Add an attribute os_consumes_this, with similar semantics to…
rL348532: [attributes] Add an attribute os_consumes_this, with similar semantics to…
rC348532: [attributes] Add an attribute os_consumes_this, with similar semantics to…
Diff Detail
- Repository
- rC Clang
Event Timeline
I know that the error message is not quite right, but this has nothing to do with this patch.
I'll see on which level it could be fixed: currently the error for mismatch for CXXMethodDecl still prints "function".
clang/include/clang/Basic/AttrDocs.td | ||
---|---|---|
887 ↗ | (On Diff #176230) | Similarly -> Similar |
clang/test/Sema/attr-osobject.cpp | ||
46 ↗ | (On Diff #176230) | Missing some more test coverage:
Question: what should happen in this code? struct S { virtual void frobble() [[clang::os_consumes_this]]; }; struct T : S { void frobble() override; }; int main() { S *s = new T; T *t = static_cast<T *>(s); s->frobble(); t->frobble(); } Does the attribute get inherited by the overrider? (This question really applies to the other attributes in this area as well, but it cropped up while I was thinking about this pointers.) |
Does the attribute get inherited by the overrider?
Yes conceptually: https://reviews.llvm.org/D55154, we propagate it in the analyzer.
No in the AST: I don't think it makes sense to propagate it during AST construction
clang/include/clang/Basic/Attr.td | ||
---|---|---|
851 ↗ | (On Diff #176909) | Given how trivial the implementation is in SemaDeclAttr.cpp, I think a better approach would be to add a new SubsetSubject similar to NonStaticNonConstCXXMethod and use that as the subject list here. Then you can remove the new custom diagnostic and handler as well. Also, shouldn't this be limited to member functions, not non-member functions? |
852 ↗ | (On Diff #176909) | What do you think about splitting this out from the retain docs into consume docs (same for the two attributes below)? Or is there enough overlap between the two sections that you'd just wind up duplicating a lot of content? |
clang/include/clang/Basic/AttrDocs.td | ||
889 ↗ | (On Diff #176909) | I'd drop the second e.g., as it reads strangely. Also, it should be e.g., for the first one. |
clang/test/Sema/attr-osobject.cpp | ||
8 ↗ | (On Diff #176909) | What this pointer is being consumed here? I would have expected this to have a diagnostic. |
51 ↗ | (On Diff #176909) | But... this is a function? Also, spurious semi-colon after the function body. |
clang/include/clang/Basic/Attr.td | ||
---|---|---|
851 ↗ | (On Diff #176909) | I'm a bit confused: aren't CXXMethods already member functions by definition? |
852 ↗ | (On Diff #176909) | Yeah I think they should be grouped together. |
clang/test/Sema/attr-osobject.cpp | ||
8 ↗ | (On Diff #176909) | What do you mean? This function does not have a body, but a valid implementation could be e.g. this->release() |
Looks close, just a few minor testing concerns.
clang/include/clang/Basic/Attr.td | ||
---|---|---|
851 ↗ | (On Diff #176909) | They are, but they're not necessarily always instance methods. For instance, they can be static member functions without a this pointer. I suppose I used confusing terminology though by talking about member vs nonmember, sorry about that. |
852 ↗ | (On Diff #176909) | As you've seen, that causes some duplication in the resulting documentation. I suspect we may want to refactor the docs at some point, but that shouldn't hold up this patch. |
clang/test/Sema/attr-osobject.cpp | ||
8 ↗ | (On Diff #176909) | That's my fault -- my eyes glazed over that this was inside the structure declaration -- I thought the }; was a line earlier for some reason. :-P Can you add a second test that uses a static member function, which should be diagnosed. |
51 ↗ | (On Diff #176909) | Still has the spurious semicolon. |