-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Avoid stack overflow due to deep recursion on long chain of calls. #67913
Changes from 3 commits
1f97f7f
09a18da
3829157
b131a4f
8fecdcf
aeb9d49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,15 +65,6 @@ protected void FindExpressionVariables( | |
_variablesBuilder = save; | ||
} | ||
|
||
public override void Visit(SyntaxNode node) | ||
{ | ||
if (node != null) | ||
{ | ||
// no stackguard | ||
((CSharpSyntaxNode)node).Accept(this); | ||
} | ||
} | ||
|
||
public override void VisitSwitchExpression(SwitchExpressionSyntax node) | ||
{ | ||
Visit(node.GoverningExpression); | ||
|
@@ -348,6 +339,50 @@ public override void VisitBinaryExpression(BinaryExpressionSyntax node) | |
operands.Free(); | ||
} | ||
|
||
public override void VisitInvocationExpression(InvocationExpressionSyntax node) | ||
{ | ||
if (receiverIsInvocation(node, out InvocationExpressionSyntax nested)) | ||
{ | ||
var invocations = ArrayBuilder<InvocationExpressionSyntax>.GetInstance(); | ||
|
||
invocations.Push(node); | ||
|
||
node = nested; | ||
if (receiverIsInvocation(node, out nested)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, the typo has been fixed already There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we'll push at most two nodes into the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, this is a typo.
AlekseyTs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
invocations.Push(node); | ||
node = nested; | ||
} | ||
|
||
Visit(node.Expression); | ||
|
||
do | ||
{ | ||
Visit(node.ArgumentList); | ||
} | ||
while (invocations.TryPop(out node)); | ||
|
||
invocations.Free(); | ||
} | ||
else | ||
{ | ||
Visit(node.Expression); | ||
Visit(node.ArgumentList); | ||
} | ||
|
||
static bool receiverIsInvocation(InvocationExpressionSyntax node, out InvocationExpressionSyntax nested) | ||
{ | ||
if (node.Expression is MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax receiver }) | ||
{ | ||
nested = receiver; | ||
return true; | ||
} | ||
|
||
nested = null; | ||
return false; | ||
} | ||
} | ||
|
||
public override void VisitDeclarationExpression(DeclarationExpressionSyntax node) | ||
{ | ||
var argumentSyntax = node.Parent as ArgumentSyntax; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using ReferenceEqualityComparer = Roslyn.Utilities.ReferenceEqualityComparer; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp | ||
|
@@ -240,9 +241,49 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node) | |
return; | ||
} | ||
|
||
base.VisitInvocationExpression(node); | ||
if (receiverIsInvocation(node, out InvocationExpressionSyntax? nested)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this is roughly the same code on several derivations of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There are only two of them and they are slightly different. I do not think it is worth adding an abstraction across them |
||
{ | ||
var invocations = ArrayBuilder<InvocationExpressionSyntax>.GetInstance(); | ||
|
||
invocations.Push(node); | ||
|
||
node = nested; | ||
while (receiverIsInvocation(node, out nested)) | ||
{ | ||
invocations.Push(node); | ||
node = nested; | ||
} | ||
|
||
Visit(node.Expression); | ||
|
||
do | ||
{ | ||
Visit(node.ArgumentList); | ||
} | ||
while (invocations.TryPop(out node!)); | ||
|
||
invocations.Free(); | ||
} | ||
else | ||
{ | ||
Visit(node.Expression); | ||
Visit(node.ArgumentList); | ||
} | ||
|
||
return; | ||
|
||
static bool receiverIsInvocation(InvocationExpressionSyntax node, [NotNullWhen(true)] out InvocationExpressionSyntax? nested) | ||
{ | ||
if (node.Expression is MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax receiver } && !receiver.MayBeNameofOperator()) | ||
{ | ||
nested = receiver; | ||
return true; | ||
} | ||
|
||
nested = null; | ||
return false; | ||
} | ||
|
||
static Symbol getAttributeTarget(Binder current) | ||
{ | ||
Debug.Assert((current.Flags & BinderFlags.InContextualAttributeBinder) != 0); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,5 +135,49 @@ protected BoundTreeWalkerWithStackGuardWithoutRecursionOnTheLeftOfBinaryOperator | |
rightOperands.Free(); | ||
return null; | ||
} | ||
|
||
public sealed override BoundNode? VisitCall(BoundCall node) | ||
{ | ||
if (node.ReceiverOpt is BoundCall receiver1) | ||
{ | ||
var calls = ArrayBuilder<BoundCall>.GetInstance(); | ||
|
||
calls.Push(node); | ||
|
||
node = receiver1; | ||
while (node.ReceiverOpt is BoundCall receiver2) | ||
{ | ||
calls.Push(node); | ||
node = receiver2; | ||
} | ||
|
||
VisitReceiver(node); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't this inside the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The intermediate |
||
|
||
do | ||
{ | ||
VisitArguments(node); | ||
} | ||
while (calls.TryPop(out node!)); | ||
|
||
calls.Free(); | ||
} | ||
else | ||
{ | ||
VisitReceiver(node); | ||
VisitArguments(node); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected virtual void VisitReceiver(BoundCall node) | ||
AlekseyTs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
this.Visit(node.ReceiverOpt); | ||
} | ||
|
||
protected virtual void VisitArguments(BoundCall node) | ||
{ | ||
this.VisitList(node.Arguments); | ||
} | ||
} | ||
} |
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.
I didn't follow where this came from. May be worth a comment #Resolved
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.
Added a comment