lifetime_capture_by conflicts with user-defined identifiers
Consider:
struct S {
void foo(int global) [[clang::lifetime_capture_by(global)]] {
}
};
This code is rejected with parameter cannot be named 'global' while using 'lifetime_capture_by(global)': https://godbolt.org/z/YWhW1dnoe
The usual approach of adding underscores doesn't save you: https://godbolt.org/z/9oeMoPWfK
I think 1) the above code should be accepted with the global parameter being used to denote the capture, though a warning would be reasonable, 2) we should allow optional leading & trailing underscores so library authors have a way to protect against things like:
#define global 12
#include "some_header.h" // has a declaration using [[clang::lifetime_capture_by(global)]]
@llvm/issue-subscribers-clang-temporal-safety
Author: Aaron Ballman (AaronBallman)
The usual approach of adding underscores doesn't save you: https://godbolt.org/z/9oeMoPWfK
I think 1) the above code should be accepted with the global parameter being used to denote the capture, though a warning would be reasonable, 2) we should allow optional leading & trailing underscores so library authors have a way to protect against things like:
#define global 12
#include "some_header.h" // has a declaration using [[clang::lifetime_capture_by(global)]]
@llvm/issue-subscribers-clang-frontend
Author: Aaron Ballman (AaronBallman)
The usual approach of adding underscores doesn't save you: https://godbolt.org/z/9oeMoPWfK
I think 1) the above code should be accepted with the global parameter being used to denote the capture, though a warning would be reasonable, 2) we should allow optional leading & trailing underscores so library authors have a way to protect against things like:
#define global 12
#include "some_header.h" // has a declaration using [[clang::lifetime_capture_by(global)]]
Is it too late to change the design here? We shouldn't allow a context-sensitive "keyword" in the same grammatical position where we also accept an identifier.
We could replace the existing [[clang::lifetime_capture_by(global)]] and [[clang::lifetime_capture_by(unknown)]] with [[clang::lifetime_capture_by_global]] and [[clang::lifetime_capture_by_unknown]], for example, and could even do that with a deprecation period if needed. But maybe there's a better option.
Is it too late to change the design here? We shouldn't allow a context-sensitive "keyword" in the same grammatical position where we also accept an identifier.
I hope not, because I think there are other design issues with these attributes too.
We could replace the existing
[[clang::lifetime_capture_by(global)]]and[[clang::lifetime_capture_by(unknown)]]with[[clang::lifetime_capture_by_global]]and[[clang::lifetime_capture_by_unknown]], for example, and could even do that with a deprecation period if needed. But maybe there's a better option.
+1 to this, and while we're at it, [[clang::lifetime_capture_by_this]]; all of these should be declaration attributes, not type attributes.
CC @usx95
Ping @usx95, this is still concerning.
Ack. I can prioritise this with a deprecation period.
all of these should be declaration attributes, not type attributes.
We still need to be able to annotate function type (similar to lifetimebound).
struct S { void foo() [[clang::lifetimebound]]; };
cc: @NeKon69
OK, after taking a deeper look at this, it looks kinda complex, I can try to work on this, but this looks more like a Sema change rather than a LifetimeSafety one, so this still might take a bit for me.
Ack. I can prioritise this with a deprecation period.
Thank you!
all of these should be declaration attributes, not type attributes.
We still need to be able to annotate function type (similar to lifetimebound).
struct S { void foo() [[clang::lifetimebound]]; };
I do not see the argument why that's necessary. I think lifetime_capture_by_this is a significant improvement (the attribute has no type semantics, it seemingly only exists as such because of the desire to associate the annotation with the implicit this, but that doesn't require a type attribute to support).
all of these should be declaration attributes, not type attributes.
We still need to be able to annotate function type (similar to lifetimebound).
struct S { void foo() [[clang::lifetimebound]]; };I do not see the argument why that's necessary. I think
lifetime_capture_by_thisis a significant improvement (the attribute has no type semantics, it seemingly only exists as such because of the desire to associate the annotation with the implicitthis, but that doesn't require a type attribute to support).
I see. I was not aware that we can support annotating ImplicitObjectParameter without being a TypeAttr. IIUC this is what you suggest: https://github.com/llvm/llvm-project/pull/196549.