-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[alpha.webkit.UncheckedCallArgsChecker] Don't emit a warning for passing a temporary object as an argument. #155033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually no, it could be lifetime extended. Moreover, since C++17 due to mandatory copy elision it can end up absolutely anywhere. Stack, heap, global, an unknown location into which the function 12 levels above you in the call stack returns, you name it. For example: https://godbolt.org/z/EnvdsWK8G - notice how there are no copy/move-constructors anywhere in that AST. The
CXXTemporaryObjectExpr
(a sub-class ofCXXConstructExpr
btw) constructs the object directly at its final destination, no matter how far apart they are in the AST.The annoying part is that you can't tell that by looking at the construct-expression itself. Normally for a method call the
this
argument is inside the call expression but for constructors it's somewhere in the parent expression and it could be one of like 30+ different rigid AST variations that you need to take into account, often with an indeterminate amount of irrelevant parent statement layers you need to peel one by one. (Eg. there could be an arbitrary amount of nested?:
operators.) And I'm afraid you can't really tell how long the object lives until you pattern-match the specific AST pattern to figure out the construction target.Frontend solves it by simply being recursive - so it always has the construction target somewhere up its visitor stack. Maybe that approach would work for you too? You could use the parent map too but that could lead you to some really weird places. For our less recursive analysis machines I've tried to learn how to pattern-match some of these cases in https://clang.llvm.org/doxygen/classclang_1_1ConstructionContext.html but I wasn't wise enough to make it available over pure AST. And it's a very incomplete list anyway.
So the remaining question is, is there a specific way to quickly notice the temporaries that aren't lifetime-extended and ignore the other cases? Basically what you're looking for is the nearest parent
MaterializeTemporaryExpr
- and then you can ask it whether itsgetStorageDuration()
isSD_FullExpression
. The annoying part is thatMaterializeTemporaryExpr
doesn't have to be there at all - considerC(14, 15)
andC(16, 17)
in the godbolt example. In this case if you keep using the parent map it may lead you to weird places. So I think your best bet may be to either find an somewhat immediateMaterializeTemporaryExpr
or give up and treat the constructor as unsafe. That'd probably handle your test case but it'd fail ifdoWorkWithObject()
accepted it by value. For that you'd need to handle the "passed directly into aCallExpr
" case too. But if you don't find either of those almost immediately (either directly above you, or additionally bypassingCXXBindTemporaryExpr
which shows up iff the class has a non-trivial destructor IIRC) then it might be better to give up and treat the constructor is unsafe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sure, but here what we need is the temporary to at least out-live the end of current statement since what we want to guarantee is that each function argument outlives the function call. It's okay if the temporary's lifetime is extended beyond that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we can revise the comment to say it "lives at least until the end of this statement"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooooooooh. Right. You already know it's an argument. Yeah, case closed, the codegen-style recursive approach wins again.
And it looks like you've correctly skipped the
MaterializeTemporaryExpr
which is probably there in your test because the argument is accepted by reference. There may also be aCXXBindTemporaryExpr
if the class has a destructor but it's probably peeled correctly for the same reason.You folks are really good at this 😅