From 647d11a6ced98a5fdd7499d2bdd2a5cda6155630 Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Thu, 8 Jan 2026 13:02:31 +0100 Subject: [PATCH] C++: Add test for C++23 multidimensional subscript operators --- .../subscript_operator/PrintAST.expected | 69 +++++++++++++++++++ .../subscript_operator/PrintAST.qlref | 1 + .../library-tests/subscript_operator/test.cpp | 12 ++++ 3 files changed, 82 insertions(+) create mode 100644 cpp/ql/test/library-tests/subscript_operator/PrintAST.expected create mode 100644 cpp/ql/test/library-tests/subscript_operator/PrintAST.qlref create mode 100644 cpp/ql/test/library-tests/subscript_operator/test.cpp diff --git a/cpp/ql/test/library-tests/subscript_operator/PrintAST.expected b/cpp/ql/test/library-tests/subscript_operator/PrintAST.expected new file mode 100644 index 00000000000..9bfa6888ec8 --- /dev/null +++ b/cpp/ql/test/library-tests/subscript_operator/PrintAST.expected @@ -0,0 +1,69 @@ +#-----| [CopyAssignmentOperator] __va_list_tag& __va_list_tag::operator=(__va_list_tag const&) +#-----| : +#-----| getParameter(0): [Parameter] (unnamed parameter 0) +#-----| Type = [LValueReferenceType] const __va_list_tag & +#-----| [MoveAssignmentOperator] __va_list_tag& __va_list_tag::operator=(__va_list_tag&&) +#-----| : +#-----| getParameter(0): [Parameter] (unnamed parameter 0) +#-----| Type = [RValueReferenceType] __va_list_tag && +test.cpp: +# 3| [CopyAssignmentOperator] S& S::operator=(S const&) +# 3| : +#-----| getParameter(0): [Parameter] (unnamed parameter 0) +#-----| Type = [LValueReferenceType] const S & +# 3| [MoveAssignmentOperator] S& S::operator=(S&&) +# 3| : +#-----| getParameter(0): [Parameter] (unnamed parameter 0) +#-----| Type = [RValueReferenceType] S && +# 5| [MemberFunction] int S::operator[](int, int) +# 5| : +# 5| getParameter(0): [Parameter] i +# 5| Type = [IntType] int +# 5| getParameter(1): [Parameter] j +# 5| Type = [IntType] int +# 5| getEntryPoint(): [BlockStmt] { ... } +# 6| getStmt(0): [ReturnStmt] return ... +# 6| getExpr(): [ArrayExpr] access to array +# 6| Type = [IntType] int +# 6| ValueCategory = prvalue(load) +# 6| getArrayBase(): [ArrayExpr] access to array +# 6| Type = [ArrayType] int[2] +# 6| ValueCategory = lvalue +# 6| getArrayBase(): [ImplicitThisFieldAccess,PointerFieldAccess] xs +# 6| Type = [ArrayType] int[2][2] +# 6| ValueCategory = lvalue +# 6| getQualifier(): [ThisExpr] this +# 6| Type = [PointerType] S * +# 6| ValueCategory = prvalue(load) +# 6| getArrayOffset(): [VariableAccess] i +# 6| Type = [IntType] int +# 6| ValueCategory = prvalue(load) +#-----| getArrayBase().getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion +#-----| Type = [PointerType] int(*)[2] +#-----| ValueCategory = prvalue +# 6| getArrayOffset(): [VariableAccess] j +# 6| Type = [IntType] int +# 6| ValueCategory = prvalue(load) +# 6| getArrayBase().getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion +# 6| Type = [IntPointerType] int * +# 6| ValueCategory = prvalue +# 10| [TopLevelFunction] int foo(S) +# 10| : +# 10| getParameter(0): [Parameter] s +# 10| Type = [Struct] S +# 10| getEntryPoint(): [BlockStmt] { ... } +# 11| getStmt(0): [ReturnStmt] return ... +# 11| getExpr(): [OverloadedArrayExpr] call to operator[] +# 11| Type = [IntType] int +# 11| ValueCategory = prvalue +# 11| getArrayBase(): [VariableAccess] s +# 11| Type = [Struct] S +# 11| ValueCategory = lvalue +# 11| getArrayOffset(): [Literal] 1 +# 11| Type = [IntType] int +# 11| Value = [Literal] 1 +# 11| ValueCategory = prvalue +# 11| getArgument(1): [Literal] 2 +# 11| Type = [IntType] int +# 11| Value = [Literal] 2 +# 11| ValueCategory = prvalue diff --git a/cpp/ql/test/library-tests/subscript_operator/PrintAST.qlref b/cpp/ql/test/library-tests/subscript_operator/PrintAST.qlref new file mode 100644 index 00000000000..6fcb30ac7a6 --- /dev/null +++ b/cpp/ql/test/library-tests/subscript_operator/PrintAST.qlref @@ -0,0 +1 @@ +semmle/code/cpp/PrintAST.ql \ No newline at end of file diff --git a/cpp/ql/test/library-tests/subscript_operator/test.cpp b/cpp/ql/test/library-tests/subscript_operator/test.cpp new file mode 100644 index 00000000000..647ad916c9c --- /dev/null +++ b/cpp/ql/test/library-tests/subscript_operator/test.cpp @@ -0,0 +1,12 @@ +// semmle-extractor-options: -std=c++23 + +struct S { + int xs[2][2]; + int operator[](int i, int j) { + return xs[i][j]; + } +}; + +int foo(S s) { + return s[1, 2]; +}