CodeQL documentation

Expressions

An expression evaluates to a set of values and has a type.

For example, the expression 1 + 2 evaluates to the integer 3 and the expression "QL" evaluates to the string "QL". 1 + 2 has type int and "QL" has type string.

The following sections describe the expressions that are available in QL.

Variable references

A variable reference is the name of a declared variable. This kind of expression has the same type as the variable it refers to.

For example, if you have declared the variables int i and LocalScopeVariable lsv, then the expressions i and lsv have types int and LocalScopeVariable respectively.

You can also refer to the variables this and result. These are used in predicate definitions and act in the same way as other variable references.

Literals

You can express certain values directly in QL, such as numbers, booleans, and strings.

  • Boolean literals: These are the values true and false.

  • Integer literals: These are sequences of decimal digits (0 through 9), possibly starting with a minus sign (-). For example:

    0
    42
    -2048
    
  • Float literals: These are sequences of decimal digits separated by a dot (.), possibly starting with a minus sign (-). For example:

    2.0
    123.456
    -100.5
    
  • String literals: These are finite strings of 16-bit characters. You can define a string literal by enclosing characters in quotation marks ("..."). Most characters represent themselves, but there are a few characters that you need to “escape” with a backslash. The following are examples of string literals:

    "hello"
    "They said, \"Please escape quotation marks!\""
    

    See String literals in the QL language specification for more details.

    Note: there is no “date literal” in QL. Instead, to specify a date, you should convert a string to the date that it represents using the toDate() predicate. For example, "2016-04-03".toDate() is the date April 3, 2016, and "2000-01-01 00:00:01".toDate() is the point in time one second after New Year 2000.

    The following string formats are recognized as dates:
    • ISO dates, such as "2016-04-03 17:00:24". The seconds part is optional (assumed to be "00" if it’s missing), and the entire time part can also be missing (in which case it’s assumed to be "00:00:00").

    • Short-hand ISO dates, such as "20160403".

    • UK-style dates, such as "03/04/2016".

    • Verbose dates, such as "03 April 2016".

Parenthesized expressions

A parenthesized expression is an expression surrounded by parentheses, ( and ). This expression has exactly the same type and values as the original expression. Parentheses are useful for grouping expressions together to remove ambiguity and improve readability.

Ranges

A range expression denotes a range of values ordered between two expressions. It consists of two expressions separated by .. and enclosed in brackets ([ and ]). For example, [3 .. 7] is a valid range expression. Its values are any integers between 3 and 7 (including 3 and 7 themselves).

In a valid range, the start and end expression are integers, floats, or dates. If one of them is a date, then both must be dates. If one of them is an integer and the other a float, then both are treated as floats.

Set literal expressions

A set literal expression allows the explicit listing of a choice between several values. It consists of a comma-separated collection of expressions that are enclosed in brackets ([ and ]). For example, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] is a valid set literal expression. Its values are the first ten prime numbers.

The values of the contained expressions need to be of compatible types for a valid set literal expression. Furthermore, at least one of the set elements has to be of a type that is a supertype of the types of all the other contained expressions.