From e9c295809587b710fb9ed0ac5953c8e5dd2255ad Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Thu, 11 Jun 2020 17:07:46 +0100 Subject: [PATCH] Add classes for array and slice literals --- .../learn-ql/go/ast-class-reference.rst | 42 ++++++------ ql/src/semmle/go/Expr.qll | 65 +++++++++++++++++++ 2 files changed, 88 insertions(+), 19 deletions(-) diff --git a/docs/language/learn-ql/go/ast-class-reference.rst b/docs/language/learn-ql/go/ast-class-reference.rst index b1cb1034d23..d874652a894 100644 --- a/docs/language/learn-ql/go/ast-class-reference.rst +++ b/docs/language/learn-ql/go/ast-class-reference.rst @@ -290,25 +290,29 @@ Literals All classes in this subsection are subclasses of `Literal `__. -+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ -| Expression syntax example | CodeQL class | Superclass | -+=========================================+==============================================================================================+====================================================================================================+ -| ``23`` | `IntLit `__ | `BasicLit `__ | -+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ -| ``4.2`` | `FloatLit `__ | `BasicLit `__ | -+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ -| ``4.2 + 2.7i`` | `ImagLit `__ | `BasicLit `__ | -+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ -| ``'a'`` | `CharLit `__ | `BasicLit `__ | -+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ -| ``"Hello"`` | `StringLit `__ | `BasicLit `__ | -+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ -| ``func(x, y int) int { return x + y }`` | `FuncLit `__ | `FuncDef `__ | -+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ -| ``map[string]int{"A": 1, "B": 2}`` | `MapLit `__ | `CompositeLit `__ | -+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ -| ``Point3D{0.5, -0.5, 0.5}`` | `StructLit `__ | `CompositeLit `__ | -+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Expression syntax example | CodeQL class | Superclass | ++=========================================+==============================================================================================+==============================================================================================================================================================================================================+ +| ``23`` | `IntLit `__ | `BasicLit `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ``4.2`` | `FloatLit `__ | `BasicLit `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ``4.2 + 2.7i`` | `ImagLit `__ | `BasicLit `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ``'a'`` | `CharLit `__ | `BasicLit `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ``"Hello"`` | `StringLit `__ | `BasicLit `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ``func(x, y int) int { return x + y }`` | `FuncLit `__ | `FuncDef `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ``[6]int{1, 2, 3, 5}`` | `ArrayLit `__ | `ArrayOrSliceLit `__, `CompositeLit `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ``[]int{1, 2, 3, 5}`` | `SliceLit `__ | `ArrayOrSliceLit `__, `CompositeLit `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ``map[string]int{"A": 1, "B": 2}`` | `MapLit `__ | `CompositeLit `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| ``Point3D{0.5, -0.5, 0.5}`` | `StructLit `__ | `CompositeLit `__ | ++-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ Unary expressions ~~~~~~~~~~~~~~~~~ diff --git a/ql/src/semmle/go/Expr.qll b/ql/src/semmle/go/Expr.qll index 0bdd0c092b1..3f2fc2897cd 100644 --- a/ql/src/semmle/go/Expr.qll +++ b/ql/src/semmle/go/Expr.qll @@ -437,6 +437,71 @@ class StructLit extends CompositeLit { StructType getStructType() { result = st } } +/** + * An array or slice literal. + * + * Examples: + * + * ```go + * [10]string{} + * [6]int{1, 2, 3, 5} + * [...]string{"Sat", "Sun"} + * []int{1, 2, 3, 5} + * []string{"Sat", "Sun"} + * ``` + */ +class ArrayOrSliceLit extends CompositeLit { + CompositeType type; + + ArrayOrSliceLit() { + type = getType().getUnderlyingType() and + ( + type instanceof ArrayType + or + type instanceof SliceType + ) + } +} + +/** + * An array literal. + * + * Examples: + * + * ```go + * [10]string{} + * [6]int{1, 2, 3, 5} + * [...]string{"Sat", "Sun"} + * ``` + */ +class ArrayLit extends ArrayOrSliceLit { + override ArrayType type; + + /** Gets the array type underlying this literal. */ + ArrayType getArrayType() { result = type } + + override string toString() { result = "array literal" } +} + +/** + * A slice literal. + * + * Examples: + * + * ```go + * []int{1, 2, 3, 5} + * []string{"Sat", "Sun"} + * ``` + */ +class SliceLit extends ArrayOrSliceLit { + override SliceType type; + + /** Gets the slice type underlying this literal. */ + SliceType getSliceType() { result = type } + + override string toString() { result = "slice literal" } +} + /** * A parenthesized expression. *