Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit e9c2958

Browse files
committed
Add classes for array and slice literals
1 parent f11b956 commit e9c2958

2 files changed

Lines changed: 88 additions & 19 deletions

File tree

docs/language/learn-ql/go/ast-class-reference.rst

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -290,25 +290,29 @@ Literals
290290
All classes in this subsection are subclasses of
291291
`Literal <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$Literal.html>`__.
292292

293-
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
294-
| Expression syntax example | CodeQL class | Superclass |
295-
+=========================================+==============================================================================================+====================================================================================================+
296-
| ``23`` | `IntLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$IntLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
297-
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
298-
| ``4.2`` | `FloatLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$FloatLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
299-
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
300-
| ``4.2 + 2.7i`` | `ImagLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$ImagLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
301-
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
302-
| ``'a'`` | `CharLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CharLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
303-
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
304-
| ``"Hello"`` | `StringLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$StringLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
305-
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
306-
| ``func(x, y int) int { return x + y }`` | `FuncLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$FuncLit.html>`__ | `FuncDef <https://help.semmle.com/qldoc/go/semmle/go/Decls.qll/type.Decls$FuncDef.html>`__ |
307-
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
308-
| ``map[string]int{"A": 1, "B": 2}`` | `MapLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$MapLit.html>`__ | `CompositeLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CompositeLit.html>`__ |
309-
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
310-
| ``Point3D{0.5, -0.5, 0.5}`` | `StructLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$StructLit.html>`__ | `CompositeLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CompositeLit.html>`__ |
311-
+-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
293+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
294+
| Expression syntax example | CodeQL class | Superclass |
295+
+=========================================+==============================================================================================+==============================================================================================================================================================================================================+
296+
| ``23`` | `IntLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$IntLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
297+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
298+
| ``4.2`` | `FloatLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$FloatLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
299+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
300+
| ``4.2 + 2.7i`` | `ImagLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$ImagLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
301+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
302+
| ``'a'`` | `CharLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CharLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
303+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
304+
| ``"Hello"`` | `StringLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$StringLit.html>`__ | `BasicLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BasicLit.html>`__ |
305+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
306+
| ``func(x, y int) int { return x + y }`` | `FuncLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$FuncLit.html>`__ | `FuncDef <https://help.semmle.com/qldoc/go/semmle/go/Decls.qll/type.Decls$FuncDef.html>`__ |
307+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
308+
| ``[6]int{1, 2, 3, 5}`` | `ArrayLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$ArrayLit.html>`__ | `ArrayOrSliceLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$ArrayOrSliceLit.html>`__, `CompositeLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CompositeLit.html>`__ |
309+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
310+
| ``[]int{1, 2, 3, 5}`` | `SliceLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$SliceLit.html>`__ | `ArrayOrSliceLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$ArrayOrSliceLit.html>`__, `CompositeLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CompositeLit.html>`__ |
311+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
312+
| ``map[string]int{"A": 1, "B": 2}`` | `MapLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$MapLit.html>`__ | `CompositeLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CompositeLit.html>`__ |
313+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
314+
| ``Point3D{0.5, -0.5, 0.5}`` | `StructLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$StructLit.html>`__ | `CompositeLit <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CompositeLit.html>`__ |
315+
+-----------------------------------------+----------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
312316

313317
Unary expressions
314318
~~~~~~~~~~~~~~~~~

ql/src/semmle/go/Expr.qll

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,71 @@ class StructLit extends CompositeLit {
437437
StructType getStructType() { result = st }
438438
}
439439

440+
/**
441+
* An array or slice literal.
442+
*
443+
* Examples:
444+
*
445+
* ```go
446+
* [10]string{}
447+
* [6]int{1, 2, 3, 5}
448+
* [...]string{"Sat", "Sun"}
449+
* []int{1, 2, 3, 5}
450+
* []string{"Sat", "Sun"}
451+
* ```
452+
*/
453+
class ArrayOrSliceLit extends CompositeLit {
454+
CompositeType type;
455+
456+
ArrayOrSliceLit() {
457+
type = getType().getUnderlyingType() and
458+
(
459+
type instanceof ArrayType
460+
or
461+
type instanceof SliceType
462+
)
463+
}
464+
}
465+
466+
/**
467+
* An array literal.
468+
*
469+
* Examples:
470+
*
471+
* ```go
472+
* [10]string{}
473+
* [6]int{1, 2, 3, 5}
474+
* [...]string{"Sat", "Sun"}
475+
* ```
476+
*/
477+
class ArrayLit extends ArrayOrSliceLit {
478+
override ArrayType type;
479+
480+
/** Gets the array type underlying this literal. */
481+
ArrayType getArrayType() { result = type }
482+
483+
override string toString() { result = "array literal" }
484+
}
485+
486+
/**
487+
* A slice literal.
488+
*
489+
* Examples:
490+
*
491+
* ```go
492+
* []int{1, 2, 3, 5}
493+
* []string{"Sat", "Sun"}
494+
* ```
495+
*/
496+
class SliceLit extends ArrayOrSliceLit {
497+
override SliceType type;
498+
499+
/** Gets the slice type underlying this literal. */
500+
SliceType getSliceType() { result = type }
501+
502+
override string toString() { result = "slice literal" }
503+
}
504+
440505
/**
441506
* A parenthesized expression.
442507
*

0 commit comments

Comments
 (0)