C#: Fix named attribute argument extraction

This commit is contained in:
Tamas Vajk
2021-01-04 14:30:49 +01:00
parent 44372f4db7
commit 04074c425b
4 changed files with 28 additions and 15 deletions

View File

@@ -69,11 +69,18 @@ namespace Semmle.Extraction.CSharp.Entities
var ctorArguments = attributeSyntax?.ArgumentList?.Arguments.Where(a => a.NameEquals == null).ToList();
var childIndex = 0;
foreach (var constructorArgument in symbol.ConstructorArguments)
for (var i = 0; i < symbol.ConstructorArguments.Length; i++)
{
var argSyntax = ctorArguments?.Count > childIndex
? ctorArguments[childIndex]
: null;
var constructorArgument = symbol.ConstructorArguments[i];
var paramName = symbol.AttributeConstructor?.Parameters[i].Name;
var argSyntax = ctorArguments?.SingleOrDefault(a => a.NameColon != null && a.NameColon.Name.Identifier.Text == paramName);
if (argSyntax == null && // couldn't find named argument
ctorArguments?.Count > childIndex && // there're more arguments
ctorArguments[childIndex].NameColon == null) // the argument is positional
{
argSyntax = ctorArguments[childIndex];
}
CreateExpressionFromArgument(
constructorArgument,

View File

@@ -47,10 +47,11 @@ arguments
| attributes.cs:57:6:57:16 | [My(...)] | 0 | attributes.cs:57:18:57:21 | true |
| attributes.cs:57:6:57:16 | [My(...)] | 1 | attributes.cs:57:28:57:29 | "" |
| attributes.cs:57:6:57:16 | [My(...)] | 2 | attributes.cs:57:36:57:36 | 0 |
| attributes.cs:58:6:58:8 | [My2(...)] | 0 | attributes.cs:58:10:58:13 | true |
| attributes.cs:58:6:58:8 | [My2(...)] | 1 | attributes.cs:58:16:58:20 | false |
| attributes.cs:58:6:58:8 | [My2(...)] | 0 | attributes.cs:58:28:58:32 | false |
| attributes.cs:58:6:58:8 | [My2(...)] | 1 | attributes.cs:58:13:58:16 | true |
| attributes.cs:58:6:58:8 | [My2(...)] | 2 | attributes.cs:58:6:58:8 | 12 |
| attributes.cs:58:6:58:8 | [My2(...)] | 3 | attributes.cs:58:27:58:28 | 42 |
| attributes.cs:58:6:58:8 | [My2(...)] | 3 | attributes.cs:58:22:58:22 | 1 |
| attributes.cs:58:6:58:8 | [My2(...)] | 4 | attributes.cs:58:39:58:40 | 42 |
| attributes.cs:77:2:77:5 | [Args(...)] | 0 | attributes.cs:77:7:77:8 | 42 |
| attributes.cs:77:2:77:5 | [Args(...)] | 1 | attributes.cs:77:11:77:14 | null |
| attributes.cs:77:2:77:5 | [Args(...)] | 2 | attributes.cs:77:17:77:25 | typeof(...) |
@@ -105,9 +106,10 @@ constructorArguments
| attributes.cs:46:6:46:16 | [Conditional(...)] | 0 | attributes.cs:46:18:46:25 | "DEBUG2" |
| attributes.cs:54:6:54:16 | [My(...)] | 0 | attributes.cs:54:18:54:22 | false |
| attributes.cs:57:6:57:16 | [My(...)] | 0 | attributes.cs:57:18:57:21 | true |
| attributes.cs:58:6:58:8 | [My2(...)] | 0 | attributes.cs:58:10:58:13 | true |
| attributes.cs:58:6:58:8 | [My2(...)] | 1 | attributes.cs:58:16:58:20 | false |
| attributes.cs:58:6:58:8 | [My2(...)] | 0 | attributes.cs:58:28:58:32 | false |
| attributes.cs:58:6:58:8 | [My2(...)] | 1 | attributes.cs:58:13:58:16 | true |
| attributes.cs:58:6:58:8 | [My2(...)] | 2 | attributes.cs:58:6:58:8 | 12 |
| attributes.cs:58:6:58:8 | [My2(...)] | 3 | attributes.cs:58:22:58:22 | 1 |
| attributes.cs:77:2:77:5 | [Args(...)] | 0 | attributes.cs:77:7:77:8 | 42 |
| attributes.cs:77:2:77:5 | [Args(...)] | 1 | attributes.cs:77:11:77:14 | null |
| attributes.cs:77:2:77:5 | [Args(...)] | 2 | attributes.cs:77:17:77:25 | typeof(...) |
@@ -126,6 +128,6 @@ namedArguments
| attributes.cs:41:10:41:13 | [Args(...)] | Prop | attributes.cs:41:90:41:120 | array creation of type Object[] |
| attributes.cs:57:6:57:16 | [My(...)] | x | attributes.cs:57:36:57:36 | 0 |
| attributes.cs:57:6:57:16 | [My(...)] | y | attributes.cs:57:28:57:29 | "" |
| attributes.cs:58:6:58:8 | [My2(...)] | X | attributes.cs:58:27:58:28 | 42 |
| attributes.cs:58:6:58:8 | [My2(...)] | X | attributes.cs:58:39:58:40 | 42 |
| attributes.cs:77:2:77:5 | [Args(...)] | Prop | attributes.cs:77:63:77:93 | array creation of type Object[] |
| attributes.cs:80:6:80:9 | [Args(...)] | Prop | attributes.cs:80:68:80:98 | array creation of type Object[] |

View File

@@ -140,9 +140,10 @@ attributes.cs:
# 57| 2: [IntLiteral] 0
# 58| 2: [Attribute] [My2(...)]
# 58| -1: [TypeMention] My2Attribute
# 58| 0: [BoolLiteral] true
# 58| 1: [BoolLiteral] false
# 58| 3: [IntLiteral] 42
# 58| 0: [BoolLiteral] false
# 58| 1: [BoolLiteral] true
# 58| 3: [IntLiteral] 1
# 58| 4: [IntLiteral] 42
# 59| 4: [BlockStmt] {...}
# 62| [Class] MyAttribute
#-----| 3: (Base types)
@@ -268,4 +269,7 @@ attributes.cs:
# 88| 2: [Parameter] i
# 88| -1: [TypeMention] int
# 88| 1: [IntLiteral] 12
# 88| 3: [Parameter] j
# 88| -1: [TypeMention] int
# 88| 1: [IntLiteral] 13
# 88| 4: [BlockStmt] {...}

View File

@@ -55,7 +55,7 @@ class Bar
void M1() { }
[MyAttribute(true, y = "", x = 0)]
[My2(true, false, X = 42)]
[My2(b: true, j: 1, a: false, X = 42)]
void M2() { }
}
@@ -85,5 +85,5 @@ public class X
class My2Attribute : Attribute
{
public int X { get; set; }
public My2Attribute(bool a, bool b, int i = 12) { }
public My2Attribute(bool a, bool b, int i = 12, int j = 13) { }
}