-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
all: implement scoped defer (part 1) #25639
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
+285
−89
Merged
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
bc0017c
extract logic from `defer` to a separate file (`defer.v`)
StunxFS d841de3
add scope to DeferStmt and Return
StunxFS c11400f
generate only `defer`s from the current scope
StunxFS 7f3901d
generate only `defer`s of the current and previous scopes with return…
StunxFS f6364c1
fix defer tests
StunxFS cbafe85
fix
StunxFS 528de91
LockExpr: write defers
StunxFS b63c27b
activate scoped defer by flag `-scoped-defer`
StunxFS 61e5583
move is_same_scope to Scope.is_same
StunxFS 1335c36
extract scoped defer test from defer_test.v to scoped_defer_test.v
StunxFS e759cc3
remove `scoped_defer` compile define
StunxFS 71f1539
support `defer(fn) {`
StunxFS 1331b4b
add test for defer(fn)
StunxFS f6d3351
add vfmt support for defer(fn)
StunxFS 316b467
remove unnecessary ifs
StunxFS a8fe0bf
fix fmt
StunxFS 6fe9625
fix unlocks generated when they shouldn't be
StunxFS fd28d3c
detect nil scopes, add comments
StunxFS 630a1ae
generate `defer` blocks when `or {}` or `some_fn()\!`/`some_fn()?`
StunxFS 02a509e
fix nil scopes
StunxFS b0d1a24
fix OrExpr scope
StunxFS e4749c7
fix test
StunxFS 6569b54
generate `defer`s within `select`
StunxFS ca269a9
fix test
StunxFS 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. | ||
| // Use of this source code is governed by an MIT license | ||
| // that can be found in the LICENSE file. | ||
| module c | ||
|
|
||
| import v.ast | ||
|
|
||
| fn (g &Gen) defer_flag_var(stmt &ast.DeferStmt) string { | ||
| return '${g.last_fn_c_name}_defer_${stmt.idx_in_fn}' | ||
| } | ||
|
|
||
| @[inline] | ||
| fn is_same_scope(a &ast.Scope, b &ast.Scope) bool { | ||
| return a.start_pos == b.start_pos && a.end_pos == b.end_pos | ||
| } | ||
|
|
||
| fn (mut g Gen) write_defer_stmts(scope &ast.Scope, lookup bool) { | ||
| g.indent++ | ||
| for i := g.defer_stmts.len - 1; i >= 0; i-- { | ||
| defer_stmt := g.defer_stmts[i] | ||
| if g.pref.scoped_defer { | ||
| if !((lookup && defer_stmt.scope.start_pos < scope.start_pos | ||
| && defer_stmt.scope.end_pos > scope.end_pos) | ||
| || is_same_scope(defer_stmt.scope, scope)) { | ||
| // generate only `defer`s from the current scope | ||
| continue | ||
| } | ||
| g.writeln('{ // defer begin') | ||
| if defer_stmt.ifdef.len > 0 { | ||
| g.writeln(defer_stmt.ifdef) | ||
| g.stmts(defer_stmt.stmts) | ||
| g.writeln2('', '#endif') | ||
| } else { | ||
| g.stmts(defer_stmt.stmts) | ||
| } | ||
| g.writeln('} // defer end') | ||
| } else { | ||
| g.writeln('if (${g.defer_flag_var(defer_stmt)}) { // defer begin') | ||
| if defer_stmt.ifdef.len > 0 { | ||
| g.writeln(defer_stmt.ifdef) | ||
| g.stmts(defer_stmt.stmts) | ||
| g.writeln2('', '#endif') | ||
| } else { | ||
| g.stmts(defer_stmt.stmts) | ||
| } | ||
| g.writeln('} // defer end') | ||
| } | ||
| } | ||
| g.indent-- | ||
| } | ||
|
|
||
| fn (mut g Gen) write_defer_stmts_when_needed(scope &ast.Scope, lookup bool) { | ||
| // unlock all mutexes, in case we are in a lock statement. defers are not | ||
| // allowed in lock statements. | ||
| g.unlock_locks() | ||
| if g.defer_stmts.len > 0 { | ||
| g.write_defer_stmts(scope, lookup) | ||
| } | ||
| if g.defer_profile_code.len > 0 { | ||
| g.writeln2('', '\t// defer_profile_code') | ||
| g.writeln2(g.defer_profile_code, '') | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.