官方文档
https://codeql.github.com/docs/ql-language-reference/predicates/
QL中的谓词用来声明逻辑关系。感觉谓词跟”函数”概念非常相似。
谓词isCountry相当于一元数组{("Belgium"),("Germany"),("France")}
谓词hasCapital相当于两元数组{("Belgium","Brussels"),("Germany","Berlin"),("France","Paris")}
内置谓词
https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#built-ins
不同的数据类型拥有不同的内置谓词
定义谓词
1\ 对于没有返回值的谓词使用predicate关键字标记,拥有返回值的谓词使用返回值对应的数据类型标记。
2\ 谓词的名称,使用小写字母开头的标识符。
3\ 谓词的参数,使用逗号分隔。需要指定参数类型。
4\ 谓词体本身,用大括号包裹起来。
定义没有result的谓词
predicate isCountry(string country) {
country = "Germany"
or
country = "Belgium"
or
country = "France"
}
定义有result的谓词
int getSuccessor(int i) {
result = i + 1
}
谓词的嵌套
谓词的种类
非成员谓词、成员谓词、特征谓词
非成员变量定义在类外
// 1. 非成员谓词
int getSuccessor(int i) {
result = i + 1 and
i in [1 .. 9]
}
class FavoriteNumbers extends int {
// 2. 特征谓词
FavoriteNumbers() {
this = 1 or
this = 4 or
this = 9
}
// 3. FavoriteNumbers类的成员谓词
string getName() {
this = 1 and result = "one"
or
this = 4 and result = "four"
or
this = 9 and result = "nine"
}
}
绑定行为
无限谓词,指不受限制的任意数量的值。
int multiplyBy4(int i) {
result = i * 4
}
无限谓词会引发错误
可以在谓词上面使用bindingset注解表示限定了某个参数的集合
bindingset[i]
int multiplyBy4(int i) {
result = i * 4
}
from int i
where i in [1 .. 10]
select multiplyBy4(i)