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

Commit 282b8cb

Browse files
authored
Merge pull request #175 from owen-mc/array-slice-literal
Add classes for array and slice literals
2 parents 47804d6 + e6217d9 commit 282b8cb

12 files changed

Lines changed: 192 additions & 119 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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ class MapLit extends CompositeLit {
415415

416416
/** Gets the value type of this literal. */
417417
Type getValueType() { result = mt.getValueType() }
418+
419+
override string toString() { result = "map literal" }
418420
}
419421

420422
/**
@@ -435,6 +437,73 @@ class StructLit extends CompositeLit {
435437

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

440509
/**

0 commit comments

Comments
 (0)