Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c28fa7e

Browse files
committed
C#: Handle VarPatternSyntax class introduced by Roslyn 3.0.0
1 parent 8a78c8f commit c28fa7e

2 files changed

Lines changed: 48 additions & 29 deletions

File tree

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/IsPattern.cs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,43 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
88
{
99
class IsPattern : Expression<IsPatternExpressionSyntax>
1010
{
11-
IsPattern(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.IS))
11+
private IsPattern(ExpressionNodeInfo info) : base(info.SetKind(ExprKind.IS))
1212
{
1313
}
1414

15-
protected override void Populate()
15+
private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, SyntaxToken varKeyword, VariableDesignationSyntax designation)
1616
{
17-
var constantPattern = Syntax.Pattern as ConstantPatternSyntax;
18-
if (constantPattern != null)
17+
bool isVar = optionalType is null;
18+
if (!isVar)
19+
Expressions.TypeAccess.Create(cx, optionalType, this, 1);
20+
21+
if (cx.Model(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
1922
{
20-
Create(cx, Syntax.Expression, this, 0);
21-
Create(cx, constantPattern.Expression, this, 3);
22-
return;
23-
}
23+
var type = Type.Create(cx, symbol.Type);
2424

25-
var pattern = Syntax.Pattern as DeclarationPatternSyntax;
25+
if (isVar)
26+
new Expression(new ExpressionInfo(cx, type, cx.Create(varKeyword.GetLocation()), ExprKind.TYPE_ACCESS, this, 1, false, null));
2627

27-
if (pattern == null)
28-
{
29-
throw new InternalError(Syntax, "Is-pattern not handled");
28+
VariableDeclaration.Create(cx, symbol, type, cx.Create(pattern.GetLocation()), cx.Create(designation.GetLocation()), isVar, this, 2);
3029
}
30+
}
3131

32+
protected override void Populate()
33+
{
3234
Create(cx, Syntax.Expression, this, 0);
33-
TypeAccess.Create(cx, pattern.Type, this, 1);
34-
35-
var symbol = cx.Model(Syntax).GetDeclaredSymbol(pattern.Designation) as ILocalSymbol;
36-
if (symbol != null)
35+
switch (Syntax.Pattern)
3736
{
38-
var type = Type.Create(cx, symbol.Type);
39-
var isVar = pattern.Type.IsVar;
40-
VariableDeclaration.Create(cx, symbol, type, cx.Create(pattern.GetLocation()), cx.Create(pattern.Designation.GetLocation()), isVar, this, 2);
37+
case ConstantPatternSyntax constantPattern:
38+
Create(cx, constantPattern.Expression, this, 3);
39+
return;
40+
case VarPatternSyntax varPattern:
41+
PopulatePattern(varPattern, null, varPattern.VarKeyword, varPattern.Designation);
42+
return;
43+
case DeclarationPatternSyntax declPattern:
44+
PopulatePattern(declPattern, declPattern.Type, default(SyntaxToken), declPattern.Designation);
45+
return;
46+
default:
47+
throw new InternalError(Syntax, "Is pattern not handled");
4148
}
4249
}
4350

csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Case.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,35 @@ public static CaseDefault Create(Context cx, DefaultSwitchLabelSyntax node, Swit
6464

6565
class CasePattern : Case<CasePatternSwitchLabelSyntax>
6666
{
67-
CasePattern(Context cx, CasePatternSwitchLabelSyntax node, Switch parent, int child)
67+
private CasePattern(Context cx, CasePatternSwitchLabelSyntax node, Switch parent, int child)
6868
: base(cx, node, parent, child) { }
6969

70+
private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, SyntaxToken varKeyword, VariableDesignationSyntax designation)
71+
{
72+
var isVar = optionalType is null;
73+
if(!isVar)
74+
Expressions.TypeAccess.Create(cx, optionalType, this, 1);
75+
76+
if (cx.Model(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
77+
{
78+
var type = Type.Create(cx, symbol.Type);
79+
80+
if (isVar)
81+
new Expression(new ExpressionInfo(cx, type, cx.Create(varKeyword.GetLocation()), ExprKind.TYPE_ACCESS, this, 1, false, null));
82+
83+
Expressions.VariableDeclaration.Create(cx, symbol, type, cx.Create(pattern.GetLocation()), cx.Create(designation.GetLocation()), isVar, this, 0);
84+
}
85+
}
86+
7087
protected override void Populate()
7188
{
7289
switch(Stmt.Pattern)
7390
{
91+
case VarPatternSyntax varPattern:
92+
PopulatePattern(varPattern, null, varPattern.VarKeyword, varPattern.Designation);
93+
break;
7494
case DeclarationPatternSyntax declarationPattern:
75-
var symbol = cx.Model(Stmt).GetDeclaredSymbol(declarationPattern.Designation) as ILocalSymbol;
76-
if (symbol != null)
77-
{
78-
var type = Type.Create(cx, symbol.Type);
79-
var isVar = declarationPattern.Type.IsVar;
80-
Expressions.VariableDeclaration.Create(cx, symbol, type, cx.Create(declarationPattern.GetLocation()), cx.Create(declarationPattern.Designation.GetLocation()), isVar, this, 0);
81-
}
82-
83-
Expressions.TypeAccess.Create(cx, declarationPattern.Type, this, 1);
95+
PopulatePattern(declarationPattern, declarationPattern.Type, default(SyntaxToken), declarationPattern.Designation);
8496
break;
8597
case ConstantPatternSyntax pattern:
8698
Expression.Create(cx, pattern.Expression, this, 0);

0 commit comments

Comments
 (0)