Skip to content

Commit 791a642

Browse files
committed
C#: Fix params attribute argument extraction
1 parent e730815 commit 791a642

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Attribute.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,16 @@ private void ExtractArguments(TextWriter trapFile)
8282
var paramName = Symbol.AttributeConstructor?.Parameters[i].Name;
8383
var argSyntax = ctorArguments?.SingleOrDefault(a => a.NameColon is not null && a.NameColon.Name.Identifier.Text == paramName);
8484

85+
var isParamsParameter = false;
86+
8587
if (argSyntax is null && // couldn't find named argument
8688
ctorArguments?.Count > childIndex && // there're more arguments
8789
ctorArguments[childIndex].NameColon is null) // the argument is positional
8890
{
91+
// The current argument is not named
92+
// so the previous ones were also not named
93+
// so the child index matches the parameter index.
94+
isParamsParameter = Symbol?.AttributeConstructor?.Parameters[childIndex].IsParams == true;
8995
argSyntax = ctorArguments[childIndex];
9096
}
9197

@@ -94,6 +100,28 @@ private void ExtractArguments(TextWriter trapFile)
94100
argSyntax?.Expression,
95101
this,
96102
childIndex++);
103+
104+
if (isParamsParameter &&
105+
ctorArguments is not null)
106+
{
107+
// The current argument is a params argument, so we're processing all the remaining arguments:
108+
while (childIndex < ctorArguments.Count)
109+
{
110+
if (ctorArguments[childIndex].Expression is null)
111+
{
112+
// This shouldn't happen
113+
continue;
114+
}
115+
116+
CreateExpressionFromArgument(
117+
constructorArgument,
118+
ctorArguments[childIndex].Expression,
119+
this,
120+
childIndex);
121+
122+
childIndex++;
123+
}
124+
}
97125
}
98126

99127
foreach (var namedArgument in Symbol.NamedArguments)

csharp/ql/test/library-tests/attributes/AttributeArguments.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ arguments
9393
| attributes.cs:141:6:141:11 | [Params(...)] | 0 | attributes.cs:141:13:141:15 | "a" |
9494
| attributes.cs:141:6:141:11 | [Params(...)] | 1 | attributes.cs:141:18:141:20 | "b" |
9595
| attributes.cs:141:6:141:11 | [Params(...)] | 2 | attributes.cs:141:23:141:23 | 1 |
96+
| attributes.cs:141:6:141:11 | [Params(...)] | 3 | attributes.cs:141:26:141:26 | 2 |
97+
| attributes.cs:141:6:141:11 | [Params(...)] | 4 | attributes.cs:141:29:141:29 | 3 |
9698
| attributes.cs:144:6:144:11 | [Params(...)] | 0 | attributes.cs:144:17:144:19 | "a" |
9799
| attributes.cs:144:6:144:11 | [Params(...)] | 1 | attributes.cs:144:26:144:28 | "b" |
98100
| attributes.cs:144:6:144:11 | [Params(...)] | 2 | attributes.cs:144:31:144:31 | 1 |
101+
| attributes.cs:144:6:144:11 | [Params(...)] | 3 | attributes.cs:144:34:144:34 | 2 |
102+
| attributes.cs:144:6:144:11 | [Params(...)] | 4 | attributes.cs:144:37:144:37 | 3 |
99103
| attributes.cs:147:6:147:11 | [Params(...)] | 0 | attributes.cs:147:35:147:37 | "a" |
100104
| attributes.cs:147:6:147:11 | [Params(...)] | 1 | attributes.cs:147:26:147:28 | "b" |
101105
| attributes.cs:147:6:147:11 | [Params(...)] | 2 | attributes.cs:147:19:147:19 | 1 |
@@ -185,9 +189,13 @@ constructorArguments
185189
| attributes.cs:141:6:141:11 | [Params(...)] | 0 | attributes.cs:141:13:141:15 | "a" |
186190
| attributes.cs:141:6:141:11 | [Params(...)] | 1 | attributes.cs:141:18:141:20 | "b" |
187191
| attributes.cs:141:6:141:11 | [Params(...)] | 2 | attributes.cs:141:23:141:23 | 1 |
192+
| attributes.cs:141:6:141:11 | [Params(...)] | 3 | attributes.cs:141:26:141:26 | 2 |
193+
| attributes.cs:141:6:141:11 | [Params(...)] | 4 | attributes.cs:141:29:141:29 | 3 |
188194
| attributes.cs:144:6:144:11 | [Params(...)] | 0 | attributes.cs:144:17:144:19 | "a" |
189195
| attributes.cs:144:6:144:11 | [Params(...)] | 1 | attributes.cs:144:26:144:28 | "b" |
190196
| attributes.cs:144:6:144:11 | [Params(...)] | 2 | attributes.cs:144:31:144:31 | 1 |
197+
| attributes.cs:144:6:144:11 | [Params(...)] | 3 | attributes.cs:144:34:144:34 | 2 |
198+
| attributes.cs:144:6:144:11 | [Params(...)] | 4 | attributes.cs:144:37:144:37 | 3 |
191199
| attributes.cs:147:6:147:11 | [Params(...)] | 0 | attributes.cs:147:35:147:37 | "a" |
192200
| attributes.cs:147:6:147:11 | [Params(...)] | 1 | attributes.cs:147:26:147:28 | "b" |
193201
| attributes.cs:147:6:147:11 | [Params(...)] | 2 | attributes.cs:147:19:147:19 | 1 |

csharp/ql/test/library-tests/attributes/PrintAst.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ attributes.cs:
427427
# 141| 0: [StringLiteralUtf16] "a"
428428
# 141| 1: [StringLiteralUtf16] "b"
429429
# 141| 2: [IntLiteral] 1
430+
# 141| 3: [IntLiteral] 2
431+
# 141| 4: [IntLiteral] 3
430432
# 142| 4: [BlockStmt] {...}
431433
# 145| 7: [Method] M2
432434
# 145| -1: [TypeMention] Void
@@ -436,6 +438,8 @@ attributes.cs:
436438
# 144| 0: [StringLiteralUtf16] "a"
437439
# 144| 1: [StringLiteralUtf16] "b"
438440
# 144| 2: [IntLiteral] 1
441+
# 144| 3: [IntLiteral] 2
442+
# 144| 4: [IntLiteral] 3
439443
# 145| 4: [BlockStmt] {...}
440444
# 148| 8: [Method] M3
441445
# 148| -1: [TypeMention] Void

0 commit comments

Comments
 (0)