mirror of
https://github.com/github/codeql.git
synced 2026-04-27 01:35:13 +02:00
C#: Address review comments
- Undo split of `localvars` relation. - Properly extract tuple declarations in `is` expressions.
This commit is contained in:
@@ -48,7 +48,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
var type = Type.Create(cx, symbol.GetAnnotatedType());
|
||||
|
||||
return VariableDeclaration.Create(cx, symbol, type, null, cx.Create(syntax.GetLocation()), false, parent, child);
|
||||
return VariableDeclaration.Create(cx, symbol, type, null, cx.Create(syntax.GetLocation()), true, parent, child);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -138,38 +138,10 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
}
|
||||
|
||||
private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, SyntaxToken varKeyword, VariableDesignationSyntax designation)
|
||||
{
|
||||
var isVar = optionalType is null;
|
||||
if (!(designation is null) && cx.GetModel(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
|
||||
{
|
||||
var type = Entities.Type.Create(cx, symbol.GetAnnotatedType());
|
||||
VariableDeclaration.Create(cx, symbol, type, optionalType, cx.Create(pattern.GetLocation()), isVar, this, 1);
|
||||
}
|
||||
else if (!isVar)
|
||||
Expressions.TypeAccess.Create(cx, optionalType, this, 1);
|
||||
}
|
||||
|
||||
protected override void PopulateExpression(TextWriter trapFile)
|
||||
{
|
||||
Create(cx, Syntax.Expression, this, 0);
|
||||
switch (Syntax.Pattern)
|
||||
{
|
||||
case ConstantPatternSyntax constantPattern:
|
||||
Create(cx, constantPattern.Expression, this, 1);
|
||||
return;
|
||||
case VarPatternSyntax varPattern:
|
||||
PopulatePattern(varPattern, null, varPattern.VarKeyword, varPattern.Designation);
|
||||
return;
|
||||
case DeclarationPatternSyntax declPattern:
|
||||
PopulatePattern(declPattern, declPattern.Type, default(SyntaxToken), declPattern.Designation);
|
||||
return;
|
||||
case RecursivePatternSyntax recPattern:
|
||||
new RecursivePattern(cx, recPattern, this, 1);
|
||||
return;
|
||||
default:
|
||||
throw new InternalError(Syntax, "Is pattern not handled");
|
||||
}
|
||||
cx.CreatePattern(Syntax.Pattern, this, 1);
|
||||
}
|
||||
|
||||
public static Expression Create(ExpressionNodeInfo info) => new IsPattern(info).TryPopulate();
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
cx.Try(null, null, () =>
|
||||
{
|
||||
var l = LocalVariable.Create(cx, symbol);
|
||||
l.PopulateInfo(ret, isVar);
|
||||
l.PopulateManual(ret, isVar);
|
||||
if (optionalSyntax != null)
|
||||
TypeMention.Create(cx, optionalSyntax, parent, type);
|
||||
});
|
||||
@@ -38,7 +38,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
cx.Try(null, null, () =>
|
||||
{
|
||||
var l = LocalVariable.Create(cx, variableSymbol);
|
||||
l.PopulateInfo(ret, node.Type.IsVar);
|
||||
l.PopulateManual(ret, node.Type.IsVar);
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
@@ -81,7 +81,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
var decl = Create(cx, variable, Entities.Type.Create(cx, local.GetAnnotatedType()), tuple, child0++);
|
||||
var l = LocalVariable.Create(cx, local);
|
||||
l.PopulateInfo(decl, true);
|
||||
l.PopulateManual(decl, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -133,7 +133,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
var declSymbol = cx.GetModel(d).GetDeclaredSymbol(d);
|
||||
var l = LocalVariable.Create(cx, declSymbol);
|
||||
l.PopulateInfo(ret, isVar);
|
||||
l.PopulateManual(ret, isVar);
|
||||
TypeMention.Create(cx, d.Type, ret, type);
|
||||
});
|
||||
return ret;
|
||||
@@ -146,7 +146,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
|
||||
{
|
||||
var declSymbol = cx.GetModel(d).GetDeclaredSymbol(d);
|
||||
var localVar = LocalVariable.Create(cx, declSymbol);
|
||||
localVar.PopulateInfo(ret, isVar);
|
||||
localVar.PopulateManual(ret, isVar);
|
||||
|
||||
if (d.Initializer != null)
|
||||
{
|
||||
|
||||
@@ -15,8 +15,17 @@ namespace Semmle.Extraction.CSharp.Entities
|
||||
trapFile.Write(";localvar");
|
||||
}
|
||||
|
||||
public override void Populate(TextWriter trapFile)
|
||||
public override void Populate(TextWriter trapFile) { }
|
||||
|
||||
public void PopulateManual(Expression parent, bool isVar)
|
||||
{
|
||||
var trapFile = Context.TrapWriter.Writer;
|
||||
var (kind, type) =
|
||||
symbol is ILocalSymbol l ?
|
||||
(l.IsRef ? 3 : l.IsConst ? 2 : 1, Type.Create(Context, l.GetAnnotatedType())) :
|
||||
(1, parent.Type);
|
||||
trapFile.localvars(this, kind, symbol.Name, isVar ? 1 : 0, type.Type.TypeRef, parent);
|
||||
|
||||
if (symbol is ILocalSymbol local)
|
||||
{
|
||||
PopulateNullability(trapFile, local.GetAnnotatedType());
|
||||
@@ -24,20 +33,11 @@ namespace Semmle.Extraction.CSharp.Entities
|
||||
trapFile.type_annotation(this, Kinds.TypeAnnotation.Ref);
|
||||
}
|
||||
|
||||
var kind = symbol is ILocalSymbol l ? (l.IsRef ? 3 : l.IsConst ? 2 : 1) : 1;
|
||||
trapFile.localvars(this, kind, symbol.Name);
|
||||
|
||||
trapFile.localvar_location(this, Location);
|
||||
|
||||
DefineConstantValue(trapFile);
|
||||
}
|
||||
|
||||
public void PopulateInfo(Expression parent, bool isVar)
|
||||
{
|
||||
var type = symbol is ILocalSymbol l ? Type.Create(Context, l.GetAnnotatedType()) : parent.Type;
|
||||
Context.TrapWriter.Writer.localvar_info(this, isVar ? 1 : 0, type.Type.TypeRef, parent);
|
||||
}
|
||||
|
||||
public static LocalVariable Create(Context cx, ISymbol local)
|
||||
{
|
||||
return LocalVariableFactory.Instance.CreateEntity(cx, local);
|
||||
|
||||
@@ -326,14 +326,9 @@ namespace Semmle.Extraction.CSharp
|
||||
trapFile.WriteTuple("localvar_location", var, location);
|
||||
}
|
||||
|
||||
internal static void localvars(this TextWriter trapFile, LocalVariable key, int @const, string name)
|
||||
internal static void localvars(this TextWriter trapFile, LocalVariable key, int @const, string name, int @var, Type type, Expression expr)
|
||||
{
|
||||
trapFile.WriteTuple("localvars", key, @const, name);
|
||||
}
|
||||
|
||||
internal static void localvar_info(this TextWriter trapFile, LocalVariable key, int @var, Type type, Expression expr)
|
||||
{
|
||||
trapFile.WriteTuple("localvar_info", key, @var, type, expr);
|
||||
trapFile.WriteTuple("localvars", key, @const, name, @var, type, expr);
|
||||
}
|
||||
|
||||
public static void metadata_handle(this TextWriter trapFile, IEntity entity, Location assembly, int handleValue)
|
||||
|
||||
Reference in New Issue
Block a user