Skip to content

Commit

Permalink
[wasm][debugger] Remove usage of GeneratedRegex (#86911)
Browse files Browse the repository at this point in the history
* avoid breaking change

* Addressin @radical comments

* Addressing @radical comments
  • Loading branch information
thaystg authored Jun 20, 2023
1 parent 0b26395 commit 3fc0f5f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
7 changes: 4 additions & 3 deletions src/mono/wasm/debugger/BrowserDebugProxy/DebugStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,8 +1246,9 @@ internal async Task LoadPDBFromSymbolServer(DebugStore debugStore, CancellationT

internal sealed partial class SourceFile
{
[GeneratedRegex(@"([:/])")]
private static partial Regex RegexForEscapeFileName();
#pragma warning disable SYSLIB1045
private static readonly Regex regexForEscapeFileName = new (@"([:/])");
#pragma warning restore SYSLIB1045

private readonly Dictionary<int, MethodInfo> methods;
private readonly AssemblyInfo assembly;
Expand Down Expand Up @@ -1349,7 +1350,7 @@ private static string GetHashOfString(string str)
private static string EscapePathForUri(string path)
{
var builder = new StringBuilder();
foreach (var part in RegexForEscapeFileName().Split(path))
foreach (var part in regexForEscapeFileName.Split(path))
{
if (part == ":" || part == "/")
builder.Append(part);
Expand Down
12 changes: 6 additions & 6 deletions src/mono/wasm/debugger/BrowserDebugProxy/EvaluateExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ internal static partial class ExpressionEvaluator
));
private sealed partial class ExpressionSyntaxReplacer : CSharpSyntaxWalker
{
[GeneratedRegex(@"[^A-Za-z0-9_]", RegexOptions.Singleline)]
private static partial Regex RegexForReplaceVarName();

#pragma warning disable SYSLIB1045
private static Regex regexForReplaceVarName = new (@"[^A-Za-z0-9_]", RegexOptions.Singleline);
#pragma warning restore SYSLIB1045
public List<IdentifierNameSyntax> identifiers = new List<IdentifierNameSyntax>();
public List<InvocationExpressionSyntax> methodCalls = new List<InvocationExpressionSyntax>();
public List<MemberAccessExpressionSyntax> memberAccesses = new List<MemberAccessExpressionSyntax>();
Expand Down Expand Up @@ -113,7 +113,7 @@ public SyntaxTree ReplaceVars(SyntaxTree syntaxTree, IEnumerable<JObject> ma_val
{
// Generate a random suffix
string suffix = Guid.NewGuid().ToString().Substring(0, 5);
string prefix = RegexForReplaceVarName().Replace(ma_str, "_");
string prefix = regexForReplaceVarName.Replace(ma_str, "_");
id_name = $"{prefix}_{suffix}";
memberAccessToParamName[ma_str] = id_name;
Expand All @@ -130,7 +130,7 @@ public SyntaxTree ReplaceVars(SyntaxTree syntaxTree, IEnumerable<JObject> ma_val
{
// Generate a random suffix
string suffix = Guid.NewGuid().ToString().Substring(0, 5);
string prefix = RegexForReplaceVarName().Replace(iesStr, "_");
string prefix = regexForReplaceVarName.Replace(iesStr, "_");
id_name = $"{prefix}_{suffix}";
methodCallToParamName[iesStr] = id_name;
}
Expand All @@ -146,7 +146,7 @@ public SyntaxTree ReplaceVars(SyntaxTree syntaxTree, IEnumerable<JObject> ma_val
{
// Generate a random suffix
string suffix = Guid.NewGuid().ToString().Substring(0, 5);
string prefix = RegexForReplaceVarName().Replace(eaStr, "_");
string prefix = regexForReplaceVarName.Replace(eaStr, "_");
id_name = $"{prefix}_{suffix}";
elementAccessToParamName[eaStr] = id_name;
}
Expand Down
54 changes: 24 additions & 30 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -807,23 +807,19 @@ internal sealed partial class MonoSDBHelper

internal readonly ILogger logger;

[GeneratedRegex(@"\<(?<varName>[^)]*)\>(?<varId>[^)]*)(__)(?<scopeId>\d+)", RegexOptions.Singleline)]
private static partial Regex RegexForAsyncLocals(); //<testCSharpScope>5__1 // works
#pragma warning disable SYSLIB1045
private static Regex regexForAsyncLocals = new (@"\<(?<varName>[^)]*)\>(?<varId>[^)]*)(__)(?<scopeId>\d+)", RegexOptions.Singleline);

[GeneratedRegex(@"\$VB\$ResumableLocal_(?<varName>[^\$]*)\$(?<scopeId>\d+)", RegexOptions.Singleline)]
private static partial Regex RegexForVBAsyncLocals(); //$VB$ResumableLocal_testVbScope$2
private static Regex regexForVBAsyncLocals = new (@"\$VB\$ResumableLocal_(?<varName>[^\$]*)\$(?<scopeId>\d+)", RegexOptions.Singleline); //$VB$ResumableLocal_testVbScope$2

[GeneratedRegex(@"VB\$StateMachine_(\d+)_(?<methodName>.*)", RegexOptions.Singleline)]
private static partial Regex RegexForVBAsyncMethodName(); //VB$StateMachine_2_RunVBScope
private static Regex regexForVBAsyncMethodName = new (@"VB\$StateMachine_(\d+)_(?<methodName>.*)", RegexOptions.Singleline); //VB$StateMachine_2_RunVBScope

[GeneratedRegex(@"\<([^>]*)\>([d][_][_])([0-9]*)")]
private static partial Regex RegexForAsyncMethodName();
private static Regex regexForAsyncMethodName = new (@"\<([^>]*)\>([d][_][_])([0-9]*)");

[GeneratedRegex(@"[`][0-9]+")]
private static partial Regex RegexForGenericArgs();
private static Regex regexForGenericArgs = new (@"[`][0-9]+");

[GeneratedRegex("^(((?'Open'<)[^<>]*)+((?'Close-Open'>)[^<>]*)+)*(?(Open)(?!))[^<>]*")]
private static partial Regex RegexForNestedLeftRightAngleBrackets(); // <ContinueWithStaticAsync>b__3_0
private static Regex regexForNestedLeftRightAngleBrackets = new ("^(((?'Open'<)[^<>]*)+((?'Close-Open'>)[^<>]*)+)*(?(Open)(?!))[^<>]*"); // <ContinueWithStaticAsync>b__3_0
#pragma warning restore SYSLIB1045

public JObjectValueCreator ValueCreator { get; init; }

Expand Down Expand Up @@ -890,7 +886,7 @@ public static string GetPrettierMethodName(string methodName)
{
methodName = methodName.Replace(':', '.');
methodName = methodName.Replace('/', '.');
methodName = RegexForGenericArgs().Replace(methodName, "");
methodName = regexForGenericArgs.Replace(methodName, "");
return methodName;
}

Expand Down Expand Up @@ -1272,25 +1268,25 @@ public async Task<string> GetPrettyMethodName(int methodId, bool isAnonymous, Ca
var ret = retDebuggerCmdReader.ReadString();
if (ret.IndexOf(':') is int index && index > 0)
ret = ret.Substring(0, index);
ret = RegexForAsyncMethodName().Replace(ret, "$1");
ret = regexForAsyncMethodName.Replace(ret, "$1");
var numGenericTypeArgs = retDebuggerCmdReader.ReadInt32();
var numGenericMethodArgs = retDebuggerCmdReader.ReadInt32();
int numTotalGenericArgs = numGenericTypeArgs + numGenericMethodArgs;
var genericArgs = new List<string>(capacity: numTotalGenericArgs);
for (int i = 0; i < numTotalGenericArgs; i++)
{
var typeArgC = retDebuggerCmdReader.ReadString();
typeArgC = RegexForGenericArgs().Replace(typeArgC, "");
typeArgC = regexForGenericArgs.Replace(typeArgC, "");
genericArgs.Add(typeArgC);
}
var match = RegexForGenericArgs().Match(ret);
var match = regexForGenericArgs.Match(ret);
while (match.Success)
{
var countArgs = Convert.ToInt32(match.Value.Remove(0, 1));
ret = ret.Remove(match.Index, match.Value.Length);
ret = ret.Insert(match.Index, $"<{string.Join(", ", genericArgs.Take(countArgs))}>");
genericArgs.RemoveRange(0, countArgs);
match = RegexForGenericArgs().Match(ret);
match = regexForGenericArgs.Match(ret);
}
ret = ret.Replace('/', '.');
return ret;
Expand All @@ -1310,15 +1306,15 @@ public async Task<string> GetPrettyMethodName(int methodId, bool isAnonymous, Ca
}
else if (klassName.StartsWith("VB$"))
{
var match = RegexForVBAsyncMethodName().Match(klassName);
var match = regexForVBAsyncMethodName.Match(klassName);
if (match.Success)
ret = ret.Insert(0, match.Groups["methodName"].Value);
else
ret = ret.Insert(0, klassName);
}
else
{
var matchOnClassName = RegexForNestedLeftRightAngleBrackets().Match(klassName);
var matchOnClassName = regexForNestedLeftRightAngleBrackets.Match(klassName);
if (matchOnClassName.Success && matchOnClassName.Groups["Close"].Captures.Count > 0)
klassName = matchOnClassName.Groups["Close"].Captures[0].Value;
if (ret.Length > 0)
Expand All @@ -1327,7 +1323,7 @@ public async Task<string> GetPrettyMethodName(int methodId, bool isAnonymous, Ca
}
}
var methodName = retDebuggerCmdReader.ReadString();
var matchOnMethodName = RegexForNestedLeftRightAngleBrackets().Match(methodName);
var matchOnMethodName = regexForNestedLeftRightAngleBrackets.Match(methodName);
if (matchOnMethodName.Success && matchOnMethodName.Groups["Close"].Captures.Count > 0)
{
if (isAnonymous && anonymousMethodId.Length == 0 && methodName.Contains("__"))
Expand Down Expand Up @@ -1680,19 +1676,17 @@ public async Task<string> GetValueFromDebuggerDisplayAttribute(DotnetObjectId do
}
return null;
}

[GeneratedRegex(@"`\d+")]
private static partial Regex RegexForGenericArity();

[GeneratedRegex(@"[[, ]+]")]
private static partial Regex RegexForSquareBrackets();
#pragma warning disable SYSLIB1045
private static Regex regexForGenericArity = new (@"`\d+");
private static Regex regexForSquareBrackets = new (@"[[, ]+]");
#pragma warning restore SYSLIB1045

public async Task<string> GetTypeName(int typeId, CancellationToken token)
{
string className = await GetTypeNameOriginal(typeId, token);
className = className.Replace("+", ".");
className = RegexForGenericArity().Replace(className, "");
className = RegexForSquareBrackets().Replace(className, "__SQUARED_BRACKETS__");
className = regexForGenericArity.Replace(className, "");
className = regexForSquareBrackets.Replace(className, "__SQUARED_BRACKETS__");
//className = className.Replace("[]", "__SQUARED_BRACKETS__");
className = className.Replace("[", "<");
className = className.Replace("]", ">");
Expand Down Expand Up @@ -2035,7 +2029,7 @@ public async Task<JArray> GetHoistedLocalVariables(MethodInfoWithDebugInformatio
}
else if (fieldName.StartsWith('<')) //examples: <code>5__2
{
var match = RegexForAsyncLocals().Match(fieldName);
var match = regexForAsyncLocals.Match(fieldName);
if (match.Success)
{
if (!method.Info.ContainsAsyncScope(Convert.ToInt32(match.Groups["scopeId"].Value), offset))
Expand All @@ -2050,7 +2044,7 @@ public async Task<JArray> GetHoistedLocalVariables(MethodInfoWithDebugInformatio
}
else if (fieldName.StartsWith("$VB$ResumableLocal_", StringComparison.Ordinal))
{
var match = RegexForVBAsyncLocals().Match(fieldName);
var match = regexForVBAsyncLocals.Match(fieldName);
if (match.Success)
{
if (!method.Info.ContainsAsyncScope(Convert.ToInt32(match.Groups["scopeId"].Value) + 1, offset))
Expand Down

0 comments on commit 3fc0f5f

Please sign in to comment.