-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add workaround to avoid stack overflow in debug (#3963)
- Loading branch information
1 parent
bc7917b
commit ebc7739
Showing
7 changed files
with
67 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
46 changes: 46 additions & 0 deletions
46
Source/SwiftLintFramework/Extensions/SyntaxVisitor+SwiftLint.swift
This file contains 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,46 @@ | ||
import Foundation | ||
import SwiftSyntax | ||
|
||
// workaround for https://bugs.swift.org/browse/SR-10121 so we can use `Self` in a closure | ||
protocol SwiftLintSyntaxVisitor: SyntaxVisitor {} | ||
extension SyntaxVisitor: SwiftLintSyntaxVisitor {} | ||
|
||
extension SwiftLintSyntaxVisitor { | ||
func walk<T>(tree: SourceFileSyntax, handler: (Self) -> T) -> T { | ||
#if DEBUG | ||
// workaround for stack overflow when running in debug | ||
// https://bugs.swift.org/browse/SR-11170 | ||
let lock = NSLock() | ||
let work = DispatchWorkItem { | ||
lock.lock() | ||
self.walk(tree) | ||
lock.unlock() | ||
} | ||
let thread = Thread { | ||
work.perform() | ||
} | ||
|
||
thread.stackSize = 8 << 20 // 8 MB. | ||
thread.start() | ||
work.wait() | ||
|
||
lock.lock() | ||
defer { | ||
lock.unlock() | ||
} | ||
|
||
return handler(self) | ||
#else | ||
walk(tree) | ||
return handler(self) | ||
#endif | ||
} | ||
|
||
func walk<T>(file: SwiftLintFile, handler: (Self) -> [T]) -> [T] { | ||
guard let syntaxTree = file.syntaxTree else { | ||
return [] | ||
} | ||
|
||
return walk(tree: syntaxTree, handler: handler) | ||
} | ||
} |
This file contains 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 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 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 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 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