Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ internal static bool TryFindLocationFromStackFrame(string? errorStackTrace, [Not

private static bool TryGetStackFrameLocation(string stackFrame, out int line, [NotNullWhen(true)] out string? file, out string? place)
{
InitializeRegex();
Regex regex = GetOrCreateRegex();

// stack frame looks like this ' at Program.<Main>$(String[] args) in S:\t\ConsoleApp81\ConsoleApp81\Program.cs:line 9'
Match match;
try
{
match = s_regex.Match(stackFrame);
match = regex.Match(stackFrame);
}
catch (RegexMatchTimeoutException)
{
Expand Down Expand Up @@ -78,18 +78,12 @@ private static bool TryGetStackFrameLocation(string stackFrame, out int line, [N
return hasLocation;
}

[MemberNotNull(nameof(s_regex))]
private static void InitializeRegex()
{
if (s_regex is not null)
{
return;
}

// Keep this location-only pattern because MSBuild only reports frames that can provide a file and line.
s_regex = new Regex(
StackTraceRegexHelper.CreateFrameRegexPattern(matchFramesWithoutLocation: false),
RegexOptions.Compiled,
StackTraceRegexHelper.MatchTimeout);
}
private static Regex GetOrCreateRegex()
=> LazyInitializer.EnsureInitialized(
ref s_regex,
// Keep this location-only pattern because MSBuild only reports frames that can provide a file and line.
static () => new Regex(
StackTraceRegexHelper.CreateFrameRegexPattern(matchFramesWithoutLocation: false),
RegexOptions.Compiled,
StackTraceRegexHelper.MatchTimeout))!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@ internal static partial class StackTraceHelper
#else
private static Regex? s_regex;

[MemberNotNull(nameof(s_regex))]
public static Regex GetFrameRegex()
{
if (s_regex is not null)
{
return s_regex;
}

// Specifying no timeout, the regex is linear. And the timeout does not measure the regex only, but measures also any
// thread suspends, so the regex gets blamed incorrectly.
s_regex = new Regex(
StackTraceRegexHelper.CreateFrameRegexPattern(matchFramesWithoutLocation: true),
RegexOptions.Compiled | RegexOptions.ExplicitCapture);
return s_regex;
}
=> LazyInitializer.EnsureInitialized(
ref s_regex,
// Specifying no timeout, the regex is linear. And the timeout does not measure the regex only, but measures also any
// thread suspends, so the regex gets blamed incorrectly.
static () => new Regex(
StackTraceRegexHelper.CreateFrameRegexPattern(matchFramesWithoutLocation: true),
RegexOptions.Compiled | RegexOptions.ExplicitCapture))!;
#endif
}