mirror of
https://github.com/github/codeql.git
synced 2026-05-02 12:15:17 +02:00
Address PR review comments
This commit is contained in:
@@ -31,11 +31,11 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
|
||||
public override void WriteId(TextWriter trapFile, bool inContext)
|
||||
{
|
||||
elementType.GetId(trapFile, inContext);
|
||||
elementType.WriteId(trapFile, inContext);
|
||||
trapFile.Write('[');
|
||||
if (rank > 1)
|
||||
for (var i = 1; i < rank; ++i)
|
||||
{
|
||||
trapFile.Write(new string(',', rank - 1));
|
||||
trapFile.Write(',');
|
||||
}
|
||||
trapFile.Write(']');
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
{
|
||||
decoded = attrib.DecodeValue(new CustomAttributeDecoder(Cx));
|
||||
}
|
||||
catch (Exception exc)
|
||||
catch
|
||||
{
|
||||
Cx.Cx.Extractor.Logger.Log(Util.Logging.Severity.Info,
|
||||
$"Attribute decoding is partial. Decoding attribute {constructor.DeclaringType.GetQualifiedName()} failed on {@object}.");
|
||||
@@ -59,7 +59,7 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
foreach (var p in decoded.NamedArguments)
|
||||
{
|
||||
var stringValue = GetStringValue(p.Type, p.Value);
|
||||
yield return Tuples.cil_attribute_named_argument(this, p.Name, stringValue);
|
||||
yield return Tuples.cil_attribute_named_argument(this, p.Name!, stringValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,9 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
public PrimitiveTypeCode GetUnderlyingEnumType(Type type)
|
||||
{
|
||||
if (type is TypeDefinitionType tdt &&
|
||||
tdt.GetUnderlyingEnumType() is var underlying &&
|
||||
underlying.HasValue)
|
||||
tdt.GetUnderlyingEnumType() is PrimitiveTypeCode underlying)
|
||||
{
|
||||
return underlying.Value;
|
||||
return underlying;
|
||||
}
|
||||
|
||||
var name = type.GetQualifiedName();
|
||||
|
||||
@@ -178,7 +178,7 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<IExtractionProduct> Decode(byte[] ilbytes, Dictionary<int, Instruction> jump_table)
|
||||
private IEnumerable<IExtractionProduct> Decode(byte[]? ilbytes, Dictionary<int, Instruction> jump_table)
|
||||
{
|
||||
// Sequence points are stored in order of offset.
|
||||
// We use an enumerator to locate the correct sequence point for each instruction.
|
||||
@@ -203,9 +203,9 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
}
|
||||
|
||||
var child = 0;
|
||||
for (var offset = 0; offset < ilbytes.Length;)
|
||||
for (var offset = 0; offset < (ilbytes?.Length ?? 0);)
|
||||
{
|
||||
var instruction = new Instruction(Cx, this, ilbytes, offset, child++);
|
||||
var instruction = new Instruction(Cx, this, ilbytes!, offset, child++);
|
||||
yield return instruction;
|
||||
|
||||
if (nextSequencePoint != null && offset >= nextSequencePoint.Current.Offset)
|
||||
@@ -245,12 +245,12 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
var ilbytes = body.GetILBytes();
|
||||
|
||||
var child = 0;
|
||||
for (var offset = 0; offset < ilbytes.Length;)
|
||||
for (var offset = 0; offset < (ilbytes?.Length ?? 0);)
|
||||
{
|
||||
Instruction decoded;
|
||||
try
|
||||
{
|
||||
decoded = new Instruction(Cx, this, ilbytes, offset, child++);
|
||||
decoded = new Instruction(Cx, this, ilbytes!, offset, child++);
|
||||
offset += decoded.Width;
|
||||
}
|
||||
catch // lgtm[cs/catch-of-all-exceptions]
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
var newTypeParams = new TypeTypeParameter[count];
|
||||
for (var i = 0; i < newTypeParams.Length; ++i)
|
||||
{
|
||||
newTypeParams[i] = new TypeTypeParameter(container, container, i);
|
||||
newTypeParams[i] = new TypeTypeParameter(container, i);
|
||||
}
|
||||
return newTypeParams;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
var ct = type.ContainingType;
|
||||
if (ct != null)
|
||||
{
|
||||
ct.GetId(trapFile, inContext);
|
||||
ct.WriteId(trapFile, inContext);
|
||||
trapFile.Write('.');
|
||||
}
|
||||
else
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Semmle.Util;
|
||||
|
||||
namespace Semmle.Extraction.CIL.Entities
|
||||
{
|
||||
@@ -33,7 +34,7 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
{
|
||||
ExtractAssemblyName(ref name, out var lastBracketIndex);
|
||||
ExtractTypeArguments(ref name, lastBracketIndex, out var containerTypeArguments);
|
||||
ExtractContainer(ref name, containerTypeArguments);
|
||||
ContainerName = ExtractContainer(ref name, containerTypeArguments);
|
||||
|
||||
ShortName = name;
|
||||
}
|
||||
@@ -70,51 +71,43 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
|
||||
TypeArguments = thisTypeArgs;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(containerTypeArgs))
|
||||
{
|
||||
// containing type is not constructed generics
|
||||
containerTypeArguments = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
// "T3,[T4, Assembly1, Version=...],,]"
|
||||
containerTypeArguments = $"[{containerTypeArgs}]";
|
||||
}
|
||||
containerTypeArguments = string.IsNullOrWhiteSpace(containerTypeArgs)
|
||||
? "" // containing type is not constructed generics
|
||||
: $"[{containerTypeArgs}]"; // "T3,[T4, Assembly1, Version=...],,]"
|
||||
|
||||
UnboundGenericTypeName = $"{name}{AssemblySuffix}";
|
||||
}
|
||||
|
||||
private void ExtractContainer(ref string name, string containerTypeArguments)
|
||||
private string ExtractContainer(ref string name, string containerTypeArguments)
|
||||
{
|
||||
var lastPlusIndex = name.LastIndexOf('+');
|
||||
IsContainerNamespace = lastPlusIndex < 0;
|
||||
if (IsContainerNamespace)
|
||||
{
|
||||
ExtractContainerNamespace(ref name);
|
||||
}
|
||||
else
|
||||
{
|
||||
ExtractContainerType(ref name, containerTypeArguments, lastPlusIndex);
|
||||
return ExtractContainerNamespace(ref name);
|
||||
}
|
||||
|
||||
return ExtractContainerType(ref name, containerTypeArguments, lastPlusIndex);
|
||||
}
|
||||
|
||||
private void ExtractContainerNamespace(ref string name)
|
||||
private static string ExtractContainerNamespace(ref string name)
|
||||
{
|
||||
var lastDotIndex = name.LastIndexOf('.');
|
||||
if (lastDotIndex >= 0)
|
||||
{
|
||||
(ContainerName, _, name) = name.Split(lastDotIndex, lastDotIndex + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
ContainerName = ""; // global namespace name
|
||||
string containerName;
|
||||
(containerName, _, name) = name.Split(lastDotIndex, lastDotIndex + 1);
|
||||
return containerName;
|
||||
}
|
||||
|
||||
return ""; // global namespace name
|
||||
}
|
||||
|
||||
private void ExtractContainerType(ref string name, string containerTypeArguments, int lastPlusIndex)
|
||||
private string ExtractContainerType(ref string name, string containerTypeArguments, int lastPlusIndex)
|
||||
{
|
||||
(ContainerName, _, name) = name.Split(lastPlusIndex, lastPlusIndex + 1);
|
||||
ContainerName = $"{ContainerName}{containerTypeArguments}{AssemblySuffix}";
|
||||
string containerName;
|
||||
(containerName, _, name) = name.Split(lastPlusIndex, lastPlusIndex + 1);
|
||||
return $"{containerName}{containerTypeArguments}{AssemblySuffix}";
|
||||
}
|
||||
|
||||
private void ExtractAssemblyName(ref string name, out int lastBracketIndex)
|
||||
@@ -157,14 +150,10 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
thisTypeArgs.Push(typeArgs[(startCurrentType + 1)..^1]);
|
||||
}
|
||||
|
||||
if (startCurrentType != 0)
|
||||
{
|
||||
typeArgs = typeArgs.Substring(0, startCurrentType - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
typeArgs = "";
|
||||
}
|
||||
typeArgs = startCurrentType != 0
|
||||
? typeArgs.Substring(0, startCurrentType - 1)
|
||||
: "";
|
||||
|
||||
thisTypeArgCount--;
|
||||
}
|
||||
return (typeArgs, thisTypeArgs.ToList());
|
||||
|
||||
@@ -21,9 +21,9 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
{
|
||||
elementType.WriteId(trapFile, gc);
|
||||
trapFile.Write('[');
|
||||
if (shape.Rank > 1)
|
||||
for (var i = 1; i < shape.Rank; ++i)
|
||||
{
|
||||
trapFile.Write(string.Join(",", shape.Rank - 1));
|
||||
trapFile.Write(',');
|
||||
}
|
||||
trapFile.Write(']');
|
||||
}
|
||||
|
||||
@@ -48,8 +48,6 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
|
||||
public sealed override void WriteId(TextWriter trapFile) => WriteId(trapFile, false);
|
||||
|
||||
public void GetId(TextWriter trapFile, bool inContext) => WriteId(trapFile, inContext);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the friendly qualified name of types, such as
|
||||
/// ``"System.Collection.Generic.List`1"`` or
|
||||
@@ -60,7 +58,7 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
public string GetQualifiedName()
|
||||
{
|
||||
using var writer = new StringWriter();
|
||||
GetId(writer, false);
|
||||
WriteId(writer, false);
|
||||
var name = writer.ToString();
|
||||
return name.Substring(name.IndexOf(AssemblyTypeNameSeparator) + 2);
|
||||
}
|
||||
@@ -107,9 +105,8 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
/// The total number of type parameters/type arguments (including parent types).
|
||||
/// This is used for internal consistency checking only.
|
||||
/// </summary>
|
||||
public int TotalTypeParametersCount => ContainingType == null
|
||||
? ThisTypeParameterCount
|
||||
: ThisTypeParameterCount + ContainingType.TotalTypeParametersCount;
|
||||
public int TotalTypeParametersCount =>
|
||||
ThisTypeParameterCount + (ContainingType?.TotalTypeParametersCount ?? 0);
|
||||
|
||||
/// <summary>
|
||||
/// Returns all bound/unbound generic arguments of a constructed/unbound generic type.
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
|
||||
// Two-phase population because type parameters can be mutually dependent
|
||||
for (var i = 0; i < newTypeParams.Length; ++i)
|
||||
newTypeParams[i] = Cx.Populate(new TypeTypeParameter(this, this, i));
|
||||
newTypeParams[i] = Cx.Populate(new TypeTypeParameter(this, i));
|
||||
for (var i = 0; i < newTypeParams.Length; ++i)
|
||||
newTypeParams[i].PopulateHandle(genericParams[i + toSkip]);
|
||||
return newTypeParams;
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Semmle.Extraction.CIL.Entities
|
||||
private readonly Type type;
|
||||
private readonly int index;
|
||||
|
||||
public TypeTypeParameter(GenericContext cx, Type t, int i) : base(cx)
|
||||
public TypeTypeParameter(Type t, int i) : base(t)
|
||||
{
|
||||
index = i;
|
||||
type = t;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Semmle.Extraction.CIL
|
||||
namespace Semmle.Util
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
Reference in New Issue
Block a user