-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C#: Make synthetic ToString calls in binary add expressions.
- Loading branch information
1 parent
dad9420
commit a306bb7
Showing
3 changed files
with
80 additions
and
3 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
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
53 changes: 53 additions & 0 deletions
53
csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitToString.cs
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,53 @@ | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Semmle.Extraction.CSharp.Util; | ||
using Semmle.Extraction.Kinds; | ||
|
||
|
||
namespace Semmle.Extraction.CSharp.Entities.Expressions | ||
{ | ||
internal sealed class ImplicitToString : Expression | ||
{ | ||
/// <summary> | ||
/// Gets the `ToString` method for the given type. | ||
/// </summary> | ||
private static IMethodSymbol? GetToStringMethod(ITypeSymbol? type) | ||
{ | ||
return type? | ||
.GetMembers() | ||
.OfType<IMethodSymbol>() | ||
.Where(method => | ||
method.GetName() == "ToString" && | ||
method.Parameters.Length == 0 | ||
) | ||
.FirstOrDefault(); | ||
} | ||
|
||
private ImplicitToString(ExpressionNodeInfo info, IMethodSymbol toString) : base(new ExpressionInfo(info.Context, info.ConvertedType, info.Location, ExprKind.METHOD_INVOCATION, info.Parent, info.Child, isCompilerGenerated: true, info.ExprValue)) | ||
{ | ||
Factory.Create(info.SetParent(this, -1)); | ||
|
||
var target = Method.Create(Context, toString); | ||
Context.TrapWriter.Writer.expr_call(this, target); | ||
} | ||
|
||
private static bool IsStringType(AnnotatedTypeSymbol? type) => | ||
type.HasValue && type.Value.Symbol?.SpecialType == SpecialType.System_String; | ||
|
||
/// <summary> | ||
/// Creates a new expression, adding implicit `ToString` as required. | ||
/// </summary> | ||
public static Expression Create(Context cx, ExpressionSyntax node, Expression parent, int child) | ||
{ | ||
var info = new ExpressionNodeInfo(cx, node, parent, child); | ||
if (IsStringType(parent.Type) && | ||
!IsStringType(info.Type) && | ||
GetToStringMethod(info.ConvertedType.Symbol) is IMethodSymbol toString) | ||
{ | ||
return new ImplicitToString(info, toString); | ||
} | ||
return CreateFromNode(info); | ||
} | ||
} | ||
} |