0

Converge on NS_VALID_UNTIL_END_OF_SCOPE

NS_VALID_UNTIL_END_OF_SCOPE is a macro for the objc_precise_lifetime
attribute. It was used more than the attribute, so standardize on
using it.

Bug: none
Change-Id: Ic7e52c30db87ab655dc31f429c56ecf10a314741
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5891478
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: Leonard Grey <lgrey@chromium.org>
Reviewed-by: Mark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1360512}
This commit is contained in:
Avi Drissman
2024-09-26 14:14:59 +00:00
committed by Chromium LUCI CQ
parent d9f3095aea
commit e4fd3f0d80
8 changed files with 16 additions and 16 deletions
chrome/browser/ui/cocoa
components/remote_cocoa/app_shim
content
docs/mac
ios
chrome
browser
web
ui/accessibility/platform

@ -197,8 +197,8 @@ ConfirmQuitPanelController* __strong g_confirmQuitPanelController = nil;
}
- (BOOL)runModalLoopForApplication:(NSApplication*)app {
[[maybe_unused]] ConfirmQuitPanelController* __attribute__((
objc_precise_lifetime)) keepAlive = self;
[[maybe_unused]] NS_VALID_UNTIL_END_OF_SCOPE ConfirmQuitPanelController*
keepAlive = self;
// If this is the second of two such attempts to quit within a certain time
// interval, then just quit.
@ -305,8 +305,8 @@ ConfirmQuitPanelController* __strong g_confirmQuitPanelController = nil;
- (void)showWindow:(id)sender {
// If a panel that is fading out is going to be reused here, make sure it
// does not get released when the animation finishes.
[[maybe_unused]] ConfirmQuitPanelController* __attribute__((
objc_precise_lifetime)) keepAlive = self;
[[maybe_unused]] NS_VALID_UNTIL_END_OF_SCOPE ConfirmQuitPanelController*
keepAlive = self;
self.window.animations = @{};
[self.window center];

@ -727,7 +727,7 @@ void NativeWidgetNSWindowBridge::CloseWindow() {
void NativeWidgetNSWindowBridge::CloseWindowNow() {
// NSWindows must be retained until -[NSWindow close] returns.
NSWindow* __attribute__((objc_precise_lifetime)) window_retain = window_;
NS_VALID_UNTIL_END_OF_SCOPE NSWindow* window_retain = window_;
// If there's a bridge at this point, it means there must be a window as well.
DCHECK(window_);
@ -1738,7 +1738,7 @@ void NativeWidgetNSWindowBridge::RemoveOrDestroyChildren() {
// The NSWindow can only be destroyed after -[NSWindow close] is complete.
// Retain the window, otherwise the reference count can reach zero when the
// child calls back into RemoveChildWindow() via its OnWindowWillClose().
NSWindow* __attribute__((objc_precise_lifetime)) child =
NS_VALID_UNTIL_END_OF_SCOPE NSWindow* child =
child_windows_.back()->ns_window();
[child close];
}

@ -1199,8 +1199,7 @@ void ExtractUnderlines(NSAttributedString* string,
_unmatchedKeyDownCodes.insert(keyCode);
RenderWidgetHostViewCocoa* __attribute__((objc_precise_lifetime))
keepSelfAlive = self;
NS_VALID_UNTIL_END_OF_SCOPE RenderWidgetHostViewCocoa* keepSelfAlive = self;
// Records the current marked text state, so that we can know if the marked
// text was deleted or not after handling the key down event.

@ -171,8 +171,8 @@ TEST_F(RenderWidgetHostViewMacEditCommandHelperWithTaskEnvTest,
// ARC conversion note: the previous version of this code held this view
// strongly throughout with a scoped_nsobject. The precise lifetime
// attribute replicates that but it's not clear if it's necessary.
[[maybe_unused]] RenderWidgetHostViewCocoa* __attribute__((
objc_precise_lifetime)) rwhv_cocoa = rwhv_mac->GetInProcessNSView();
[[maybe_unused]] NS_VALID_UNTIL_END_OF_SCOPE RenderWidgetHostViewCocoa*
rwhv_cocoa = rwhv_mac->GetInProcessNSView();
NSArray* edit_command_strings = RenderWidgetHostViewMacEditCommandHelper::
GetEditSelectorNamesForTesting();

@ -261,8 +261,10 @@ remain, however they should not (or sometimes cannot) be used from ARC:
Counting](https://en.wikipedia.org/wiki/Automatic_Reference_Counting)
- Clang documentation (very technical): [Objective-C Automatic Reference
Counting (ARC)](https://clang.llvm.org/docs/AutomaticReferenceCounting.html)
- Theres a specialized tool named
[`objc_precise_lifetime`](https://clang.llvm.org/docs/AutomaticReferenceCounting.html#precise-lifetime-semantics)
- Theres a specialized tool named [`NS_VALID_UNTIL_END_OF_SCOPE` (the
preferred spelling and available in `.mm` files that use Foundation) a.k.a.
`objc_precise_lifetime` (for use
otherwise)](https://clang.llvm.org/docs/AutomaticReferenceCounting.html#precise-lifetime-semantics)
that might be useful in specific situations where the compiler cannot fully
deduce what lifetime is needed for a local variable. Its not usually
needed, but if you have gotten to this point in this document, you should

@ -47,7 +47,7 @@ TEST_F(ChromeActivityItemThumbnailGeneratorTest, DeallocOnBackgroundSequence) {
// a background sequence.
base::OnceClosure closure;
@autoreleasepool {
__attribute__((objc_precise_lifetime))
NS_VALID_UNTIL_END_OF_SCOPE
ChromeActivityItemThumbnailGenerator* generator =
[[ChromeActivityItemThumbnailGenerator alloc]
initWithWebState:&fake_web_state_];

@ -1178,7 +1178,7 @@ void LogPresentingErrorPageFailedWithError(NSError* error) {
// if the download cannot be started.
- (BOOL)onDownloadNativeTaskBridgeReadyForDownload:
(DownloadNativeTaskBridge*)bridge {
__attribute__((objc_precise_lifetime))
NS_VALID_UNTIL_END_OF_SCOPE
DownloadNativeTaskBridge* nativeTaskBridge = bridge;
[_nativeTaskBridges removeObject:bridge];
if (!self.webStateImpl)

@ -213,8 +213,7 @@ TEST_F(BrowserAccessibilityMacTest, BasicAttributeTest) {
TEST_F(BrowserAccessibilityMacTest, RetainedDetachedObjectsReturnNil) {
// Get the first child. Hold it in a precise lifetime variable. This simulates
// what the system might do with an accessibility object.
BrowserAccessibilityCocoa* __attribute__((objc_precise_lifetime))
retainedFirstChild =
NS_VALID_UNTIL_END_OF_SCOPE BrowserAccessibilityCocoa* retainedFirstChild =
[accessibility_ accessibilityHitTest:NSMakePoint(50, 50)];
EXPECT_NSEQ(@"Child1", retainedFirstChild.accessibilityLabel);