C#: Fix params attribute argument extraction

This commit is contained in:
Tamas Vajk
2023-10-13 11:30:02 +02:00
parent e730815f41
commit 791a6422b3
3 changed files with 40 additions and 0 deletions

View File

@@ -82,10 +82,16 @@ namespace Semmle.Extraction.CSharp.Entities
var paramName = Symbol.AttributeConstructor?.Parameters[i].Name;
var argSyntax = ctorArguments?.SingleOrDefault(a => a.NameColon is not null && a.NameColon.Name.Identifier.Text == paramName);
var isParamsParameter = false;
if (argSyntax is null && // couldn't find named argument
ctorArguments?.Count > childIndex && // there're more arguments
ctorArguments[childIndex].NameColon is null) // the argument is positional
{
// The current argument is not named
// so the previous ones were also not named
// so the child index matches the parameter index.
isParamsParameter = Symbol?.AttributeConstructor?.Parameters[childIndex].IsParams == true;
argSyntax = ctorArguments[childIndex];
}
@@ -94,6 +100,28 @@ namespace Semmle.Extraction.CSharp.Entities
argSyntax?.Expression,
this,
childIndex++);
if (isParamsParameter &&
ctorArguments is not null)
{
// The current argument is a params argument, so we're processing all the remaining arguments:
while (childIndex < ctorArguments.Count)
{
if (ctorArguments[childIndex].Expression is null)
{
// This shouldn't happen
continue;
}
CreateExpressionFromArgument(
constructorArgument,
ctorArguments[childIndex].Expression,
this,
childIndex);
childIndex++;
}
}
}
foreach (var namedArgument in Symbol.NamedArguments)

View File

@@ -93,9 +93,13 @@ arguments
| attributes.cs:141:6:141:11 | [Params(...)] | 0 | attributes.cs:141:13:141:15 | "a" |
| attributes.cs:141:6:141:11 | [Params(...)] | 1 | attributes.cs:141:18:141:20 | "b" |
| attributes.cs:141:6:141:11 | [Params(...)] | 2 | attributes.cs:141:23:141:23 | 1 |
| attributes.cs:141:6:141:11 | [Params(...)] | 3 | attributes.cs:141:26:141:26 | 2 |
| attributes.cs:141:6:141:11 | [Params(...)] | 4 | attributes.cs:141:29:141:29 | 3 |
| attributes.cs:144:6:144:11 | [Params(...)] | 0 | attributes.cs:144:17:144:19 | "a" |
| attributes.cs:144:6:144:11 | [Params(...)] | 1 | attributes.cs:144:26:144:28 | "b" |
| attributes.cs:144:6:144:11 | [Params(...)] | 2 | attributes.cs:144:31:144:31 | 1 |
| attributes.cs:144:6:144:11 | [Params(...)] | 3 | attributes.cs:144:34:144:34 | 2 |
| attributes.cs:144:6:144:11 | [Params(...)] | 4 | attributes.cs:144:37:144:37 | 3 |
| attributes.cs:147:6:147:11 | [Params(...)] | 0 | attributes.cs:147:35:147:37 | "a" |
| attributes.cs:147:6:147:11 | [Params(...)] | 1 | attributes.cs:147:26:147:28 | "b" |
| attributes.cs:147:6:147:11 | [Params(...)] | 2 | attributes.cs:147:19:147:19 | 1 |
@@ -185,9 +189,13 @@ constructorArguments
| attributes.cs:141:6:141:11 | [Params(...)] | 0 | attributes.cs:141:13:141:15 | "a" |
| attributes.cs:141:6:141:11 | [Params(...)] | 1 | attributes.cs:141:18:141:20 | "b" |
| attributes.cs:141:6:141:11 | [Params(...)] | 2 | attributes.cs:141:23:141:23 | 1 |
| attributes.cs:141:6:141:11 | [Params(...)] | 3 | attributes.cs:141:26:141:26 | 2 |
| attributes.cs:141:6:141:11 | [Params(...)] | 4 | attributes.cs:141:29:141:29 | 3 |
| attributes.cs:144:6:144:11 | [Params(...)] | 0 | attributes.cs:144:17:144:19 | "a" |
| attributes.cs:144:6:144:11 | [Params(...)] | 1 | attributes.cs:144:26:144:28 | "b" |
| attributes.cs:144:6:144:11 | [Params(...)] | 2 | attributes.cs:144:31:144:31 | 1 |
| attributes.cs:144:6:144:11 | [Params(...)] | 3 | attributes.cs:144:34:144:34 | 2 |
| attributes.cs:144:6:144:11 | [Params(...)] | 4 | attributes.cs:144:37:144:37 | 3 |
| attributes.cs:147:6:147:11 | [Params(...)] | 0 | attributes.cs:147:35:147:37 | "a" |
| attributes.cs:147:6:147:11 | [Params(...)] | 1 | attributes.cs:147:26:147:28 | "b" |
| attributes.cs:147:6:147:11 | [Params(...)] | 2 | attributes.cs:147:19:147:19 | 1 |

View File

@@ -427,6 +427,8 @@ attributes.cs:
# 141| 0: [StringLiteralUtf16] "a"
# 141| 1: [StringLiteralUtf16] "b"
# 141| 2: [IntLiteral] 1
# 141| 3: [IntLiteral] 2
# 141| 4: [IntLiteral] 3
# 142| 4: [BlockStmt] {...}
# 145| 7: [Method] M2
# 145| -1: [TypeMention] Void
@@ -436,6 +438,8 @@ attributes.cs:
# 144| 0: [StringLiteralUtf16] "a"
# 144| 1: [StringLiteralUtf16] "b"
# 144| 2: [IntLiteral] 1
# 144| 3: [IntLiteral] 2
# 144| 4: [IntLiteral] 3
# 145| 4: [BlockStmt] {...}
# 148| 8: [Method] M3
# 148| -1: [TypeMention] Void