From f11b95658364dfd5531357e7531cac09615dfd89 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan <62447351+owen-mc@users.noreply.github.com> Date: Thu, 11 Jun 2020 19:53:40 +0100 Subject: [PATCH] Add a superclass for literals (#172) --- .../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 0c0f841509c..b1cb1034d23 100644 --- a/docs/language/learn-ql/go/ast-class-reference.rst +++ b/docs/language/learn-ql/go/ast-class-reference.rst @@ -287,6 +287,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 105d5dea4af..0bdd0c092b1 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 { + 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) }