From 9f271dd4709132a9a586be22a6dff5332751fa56 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Mon, 8 Jun 2020 16:37:03 +0100 Subject: [PATCH 1/2] Add a superclass for literals --- .../learn-ql/go/ast-class-reference.rst | 3 +++ ql/src/semmle/go/Expr.qll | 25 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/docs/language/learn-ql/go/ast-class-reference.rst b/docs/language/learn-ql/go/ast-class-reference.rst index 562c2f2f0..406d7cf10 100644 --- a/docs/language/learn-ql/go/ast-class-reference.rst +++ b/docs/language/learn-ql/go/ast-class-reference.rst @@ -283,6 +283,9 @@ All classes in this section are subclasses of Literals ~~~~~~~~ +All classes in this subsection are subclasses of +`Literal `__. + +-----------------------------------------+----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+ | Expression syntax example | CodeQL class | Superclass | +=========================================+==============================================================================================+====================================================================================================+ diff --git a/ql/src/semmle/go/Expr.qll b/ql/src/semmle/go/Expr.qll index b4928892a..d4788b9a3 100644 --- a/ql/src/semmle/go/Expr.qll +++ b/ql/src/semmle/go/Expr.qll @@ -212,6 +212,23 @@ class Ellipsis extends @ellipsis, Expr { override string toString() { result = "..." } } +/** + * A literal expression. + * + * Examples: + * + * ```go + * "hello" + * func(x, y int) int { return x + y } + * map[string]int{"A": 1, "B": 2} + * ``` + */ +class Literal extends @expr, Expr { + Literal() { + this instanceof @basiclit or this instanceof @funclit or this instanceof @compositelit + } +} + /** * A literal expression of basic type. * @@ -222,7 +239,7 @@ class Ellipsis extends @ellipsis, Expr { * "hello" * ``` */ -class BasicLit extends @basiclit, Expr { +class BasicLit extends @basiclit, Literal { /** Gets the value of this literal expressed as a string. */ string getValue() { literals(this, result, _) } @@ -319,10 +336,10 @@ class StringLit extends @stringlit, BasicLit { } * func(x, y int) int { return x + y } * ``` */ -class FuncLit extends @funclit, Expr, StmtParent, FuncDef { +class FuncLit extends @funclit, Literal, StmtParent, FuncDef { override FuncTypeExpr getTypeExpr() { result = getChildExpr(0) } - override SignatureType getType() { result = Expr.super.getType() } + override SignatureType getType() { result = Literal.super.getType() } /** Gets the body of this function literal. */ override BlockStmt getBody() { result = getChildStmt(1) } @@ -342,7 +359,7 @@ class FuncLit extends @funclit, Expr, StmtParent, FuncDef { * map[string]int{"A": 1, "B": 2} * ``` */ -class CompositeLit extends @compositelit, Expr { +class CompositeLit extends @compositelit, Literal { /** Gets the expression representing the type of this composite literal. */ Expr getTypeExpr() { result = getChildExpr(0) } From 18463a9744dd9cb97dd5cc874ecde12013e3ffb2 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan <62447351+owen-mc@users.noreply.github.com> Date: Thu, 11 Jun 2020 17:22:48 +0100 Subject: [PATCH 2/2] Make Literal extend @expr as well Co-authored-by: Max Schaefer <54907921+max-schaefer@users.noreply.github.com> --- ql/src/semmle/go/Expr.qll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ql/src/semmle/go/Expr.qll b/ql/src/semmle/go/Expr.qll index d4788b9a3..060ad9b4f 100644 --- a/ql/src/semmle/go/Expr.qll +++ b/ql/src/semmle/go/Expr.qll @@ -223,7 +223,7 @@ class Ellipsis extends @ellipsis, Expr { * map[string]int{"A": 1, "B": 2} * ``` */ -class Literal extends @expr, Expr { +class Literal extends Expr { Literal() { this instanceof @basiclit or this instanceof @funclit or this instanceof @compositelit }