mirror of
https://github.com/github/codeql.git
synced 2026-04-28 02:05:14 +02:00
Merge pull request #4759 from tamasvajk/feature/cil-attribute-array
C#: Improve array argument CIL extraction for attributes
This commit is contained in:
@@ -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)
|
||||
|
||||
7
csharp/ql/test/library-tests/cil/attributes/Test.cs
Normal file
7
csharp/ql/test/library-tests/cil/attributes/Test.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
// semmle-extractor-options: --cil
|
||||
|
||||
using System;
|
||||
|
||||
class Test
|
||||
{
|
||||
}
|
||||
2614
csharp/ql/test/library-tests/cil/attributes/attribute.expected
Normal file
2614
csharp/ql/test/library-tests/cil/attributes/attribute.expected
Normal file
File diff suppressed because it is too large
Load Diff
32
csharp/ql/test/library-tests/cil/attributes/attribute.ql
Normal file
32
csharp/ql/test/library-tests/cil/attributes/attribute.ql
Normal 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()
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user