Merge pull request #4759 from tamasvajk/feature/cil-attribute-array

C#: Improve array argument CIL extraction for attributes
This commit is contained in:
Tamás Vajk
2020-12-08 16:38:36 +01:00
committed by GitHub
4 changed files with 2668 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
namespace Semmle.Extraction.CIL.Entities
@@ -51,19 +52,29 @@ namespace Semmle.Extraction.CIL.Entities
for (var index = 0; index < decoded.FixedArguments.Length; ++index)
{
var value = decoded.FixedArguments[index].Value;
var stringValue = value?.ToString();
yield return Tuples.cil_attribute_positional_argument(this, index, stringValue ?? "null");
var stringValue = GetStringValue(value);
yield return Tuples.cil_attribute_positional_argument(this, index, stringValue);
}
foreach (var p in decoded.NamedArguments)
{
var value = p.Value;
var stringValue = value?.ToString();
yield return Tuples.cil_attribute_named_argument(this, p.Name, stringValue ?? "null");
var stringValue = GetStringValue(value);
yield return Tuples.cil_attribute_named_argument(this, p.Name, stringValue);
}
}
}
private static string GetStringValue(object? value)
{
if (value is System.Collections.Immutable.ImmutableArray<CustomAttributeTypedArgument<Type>> values)
{
return "[" + string.Join(",", values.Select(v => GetStringValue(v.Value))) + "]";
}
return value?.ToString() ?? "null";
}
public static IEnumerable<IExtractionProduct> Populate(Context cx, IEntity @object, CustomAttributeHandleCollection attributes)
{
foreach (var attrib in attributes)

View File

@@ -0,0 +1,7 @@
// semmle-extractor-options: --cil
using System;
class Test
{
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,32 @@
import semmle.code.cil.Attribute
import semmle.code.cil.Declaration
query predicate attrNoArg(string dec, string attr) {
exists(Declaration d, Attribute a |
a.getDeclaration() = d and
not exists(a.getAnArgument())
|
dec = d.toStringWithTypes() and
attr = a.toStringWithTypes()
)
}
query predicate attrArgNamed(string dec, string attr, string name, string value) {
exists(Declaration d, Attribute a |
a.getDeclaration() = d and
a.getNamedArgument(name) = value
|
dec = d.toStringWithTypes() and
attr = a.toStringWithTypes()
)
}
query predicate attrArgPositional(string dec, string attr, int index, string value) {
exists(Declaration d, Attribute a |
a.getDeclaration() = d and
a.getArgument(index) = value
|
dec = d.toStringWithTypes() and
attr = a.toStringWithTypes()
)
}