官方文档

https://codeql.github.com/docs/ql-language-reference/expressions/

1、变量引用

特殊的变量:this、result

2、常量

Boolean、Integer、Float、String
Date类型需要使用toDate谓词转换

  1. "2000-01-01 00:00:01".toDate()

3、括号表达式

加括号可以用来增加可读性

4、范围

大于等于N,小于等于M

  1. [N..M]

5、常量表达式

给集合指定具体的数值

  1. [N1, N2, N3, N4, N5]

6、超级表达式

  1. class A extends int {
  2. A() { this = 1 }
  3. int getANumber() { result = 2 }
  4. }
  5. class B extends int {
  6. B() { this = 1 }
  7. int getANumber() { result = 3 }
  8. }
  9. class C extends A, B {
  10. // Need to define `int getANumber()`; otherwise it would be ambiguous
  11. int getANumber() {
  12. result = B.super.getANumber()
  13. }
  14. }
  15. from C c
  16. select c, c.getANumber()

7、调用含返回值的谓词

8、聚合表达式(Aggregations)

聚合在信息科学中是指对有关的数据进行内容挑选、分析、归类,最后分析得到人们想要的结果,主要是指任何能够从数组产生标量值的数据转换过程。 https://baike.baidu.com/item/聚合/20367464

通用语法格式

  1. <aggregate>(<variable declarations> | <formula> | <expression>)

区域声明的变量被称为聚合变量(aggregation variables)
可以在区域使用order by关键字和asc/desc关键字来限定不同的顺序,默认使用asc关键字

count

min
max
avg
sum
concat
部分必须是字符串

  1. concat(int i | i = [0 .. 3] | i.toString() order by i desc)

如下示例输出:0|1|2|3

  1. concat(int i | i = [0 .. 3] | i.toString(), "|")

rank
unique

Evaluation of aggregates

TODO

省略部分内容

Omitting parts of an aggregation
1、当你要写的聚合表达式形式这样时<aggregate>(<type> v | <expression> = v | v),你可以省略<variable declarations>部分和<formula> 部分

  1. count(int i | i = "hello".indexOf("l") | i)

=>

  1. count("hello".indexOf("l"))

2、当只有一个聚合变量时,可以省略部分

  1. avg(int i | i = [0 .. 3] | i)

=>

  1. avg(int i | i = [0 .. 3])

3、特例,即使有多个聚合变量时,你可以在count事件中省略部分

  1. count(int i, int j | i in [1 .. 3] and j in [1 .. 3] | 1)

=>

  1. count(int i, int j | i in [1 .. 3] and j in [1 .. 3])

4、你可以省略部分,但是|符号需要保留

  1. <aggregate>(<variable declarations> | | <expression>)
  1. max(File f | | f.getTotalNumberOfLines())

5、你可以同时省略部分和部分

  1. count(File f | any() | 1)

=>

  1. count(File f | | 1)

=>

  1. count(File f)

Monotonic aggregates

TODO

9、ANY

10、一元操作符(Unary operations)

一个一元操作由一个一元操作符+一个表达式构成

  1. -6.28
  2. +(10 - 4)

11、二元操作符(Binary operations)

一个二元操作由一个表达式+一个二元操作符+一个表达式构成

  1. 5 % 2
  2. (9 + 1) / (-2)
  3. "Q" + "L"

注意:221 + "B" 的值是 "221B"

12、类型转换(casts)

  1. import java
  2. from Type t

后缀型类型转换

  1. where t.(Class).getASupertype().hasName("List")

前缀型类型转换

  1. where ((Class)t).getASupertype().hasName("List")

13、临时表达式(Don’t-care expressions)

  1. from string s
  2. where s = "hello".charAt(_)
  3. select s

等价于

  1. from string s,int i
  2. where s = "hello".charAt(i)
  3. select s