From a2ac0ab7d559fe724895329ab7e49a046e4877e2 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 19 May 2026 09:38:37 +0200 Subject: [PATCH] C#: Add test case for indexer calls and update test expected for other files. --- .../indexers/Indexers13.expected | 3 ++ .../test/library-tests/indexers/Indexers13.ql | 8 +++ .../library-tests/indexers/PrintAst.expected | 54 +++++++++++++++++++ .../test/library-tests/indexers/indexers.cs | 27 ++++++++++ 4 files changed, 92 insertions(+) create mode 100644 csharp/ql/test/library-tests/indexers/Indexers13.expected create mode 100644 csharp/ql/test/library-tests/indexers/Indexers13.ql diff --git a/csharp/ql/test/library-tests/indexers/Indexers13.expected b/csharp/ql/test/library-tests/indexers/Indexers13.expected new file mode 100644 index 00000000000..0e7281cf645 --- /dev/null +++ b/csharp/ql/test/library-tests/indexers/Indexers13.expected @@ -0,0 +1,3 @@ +| indexers.cs:24:21:24:24 | Item | indexers.cs:62:22:62:29 | access to indexer | indexers.cs:26:13:26:15 | get_Item | +| indexers.cs:24:21:24:24 | Item | indexers.cs:65:25:65:32 | access to indexer | indexers.cs:34:13:34:15 | set_Item | +| indexers.cs:143:24:143:27 | Item | indexers.cs:157:21:157:24 | access to indexer | indexers.cs:145:13:145:15 | get_Item | diff --git a/csharp/ql/test/library-tests/indexers/Indexers13.ql b/csharp/ql/test/library-tests/indexers/Indexers13.ql new file mode 100644 index 00000000000..63680269007 --- /dev/null +++ b/csharp/ql/test/library-tests/indexers/Indexers13.ql @@ -0,0 +1,8 @@ +import csharp + +from IndexerCall ic, Indexer i, Accessor target +where + ic.getIndexer() = i and + ic.getTarget() = target and + i.fromSource() +select i, ic, target diff --git a/csharp/ql/test/library-tests/indexers/PrintAst.expected b/csharp/ql/test/library-tests/indexers/PrintAst.expected index 93160309c79..57b83223c36 100644 --- a/csharp/ql/test/library-tests/indexers/PrintAst.expected +++ b/csharp/ql/test/library-tests/indexers/PrintAst.expected @@ -360,3 +360,57 @@ indexers.cs: # 130| 4: [BlockStmt] {...} # 130| 0: [ReturnStmt] return ...; # 130| 0: [IntLiteral] 0 +# 134| 5: [RefStruct] S +# 136| 6: [Field] x +# 136| -1: [TypeMention] int +# 138| 7: [InstanceConstructor] S +#-----| 2: (Parameters) +# 138| 0: [Parameter] v +# 138| -1: [TypeMention] int +# 139| 4: [BlockStmt] {...} +# 140| 0: [ExprStmt] ...; +# 140| 0: [AssignExpr] ... = ... +# 140| 0: [FieldAccess] access to field x +# 140| 1: [RefExpr] ref ... +# 140| 0: [ParameterAccess] access to parameter v +# 143| 8: [Indexer] Item +# 143| -1: [TypeMention] int +#-----| 1: (Parameters) +# 143| 0: [Parameter] i +# 143| -1: [TypeMention] int +# 145| 3: [Getter] get_Item +#-----| 2: (Parameters) +# 143| 0: [Parameter] i +# 145| 4: [BlockStmt] {...} +# 145| 0: [ReturnStmt] return ...; +# 145| 0: [RefExpr] ref ... +# 145| 0: [FieldAccess] access to field x +# 149| 6: [Class] TestRefReturns +# 151| 6: [Method] M +# 151| -1: [TypeMention] Void +# 152| 4: [BlockStmt] {...} +# 153| 0: [LocalVariableDeclStmt] ... ...; +# 153| 0: [LocalVariableDeclAndInitExpr] Int32 a = ... +# 153| -1: [TypeMention] int +# 153| 0: [LocalVariableAccess] access to local variable a +# 153| 1: [IntLiteral] 0 +# 155| 1: [LocalVariableDeclStmt] ... ...; +# 155| 0: [LocalVariableDeclAndInitExpr] S s = ... +# 155| -1: [TypeMention] S +# 155| 0: [LocalVariableAccess] access to local variable s +# 155| 1: [ObjectCreation] object creation of type S +# 155| -1: [TypeMention] S +# 155| 0: [LocalVariableAccess] access to local variable a +# 156| 2: [ExprStmt] ...; +# 156| 0: [AssignExpr] ... = ... +# 156| 0: [IndexerCall] access to indexer +# 156| -1: [LocalVariableAccess] access to local variable s +# 156| 0: [IntLiteral] 0 +# 156| 1: [IntLiteral] 1 +# 157| 3: [LocalVariableDeclStmt] ... ...; +# 157| 0: [LocalVariableDeclAndInitExpr] Int32 x = ... +# 157| -1: [TypeMention] int +# 157| 0: [LocalVariableAccess] access to local variable x +# 157| 1: [IndexerCall] access to indexer +# 157| -1: [LocalVariableAccess] access to local variable s +# 157| 0: [IntLiteral] 0 diff --git a/csharp/ql/test/library-tests/indexers/indexers.cs b/csharp/ql/test/library-tests/indexers/indexers.cs index 6da14ae769d..55011d82755 100644 --- a/csharp/ql/test/library-tests/indexers/indexers.cs +++ b/csharp/ql/test/library-tests/indexers/indexers.cs @@ -130,4 +130,31 @@ namespace Indexers get { return 0; } } } + + public ref struct S + { + private ref int x; + + public S(ref int v) + { + x = ref v; + } + + public ref int this[int i] + { + get { return ref x; } + } + } + + public class TestRefReturns + { + public void M() + { + int a = 0; + + S s = new S(ref a); + s[0] = 1; + var x = s[0]; + } + } }