Skip to content

Commit 45cfae3

Browse files
Add shared LocOption module for optional types with locations
1 parent 88059d9 commit 45cfae3

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

shared/util/codeql/util/Option.qll

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ private signature class TypeWithToString {
88
string toString();
99
}
1010

11+
/** A type with `toString` and `hasLocationInfo` */
12+
private signature class TypeWithToStringAndLocation {
13+
bindingset[this]
14+
string toString();
15+
16+
predicate hasLocationInfo(
17+
string filePath, int startLine, int startColumn, int endLine, int endColumn
18+
);
19+
}
20+
1121
/**
1222
* Constructs an `Option` type that is a disjoint union of the given type and an
1323
* additional singleton element.
@@ -45,3 +55,52 @@ module Option<TypeWithToString T> {
4555
/** Gets the given element wrapped as an `Option`. */
4656
Some some(T c) { result = TSome(c) }
4757
}
58+
59+
/**
60+
* Constructs an `Option` type that is a disjoint union of the given type and an
61+
* additional singleton element, and has a `hasLocationInfo` predicate.
62+
*/
63+
module LocOption<TypeWithToStringAndLocation T> {
64+
private module O = Option<T>;
65+
66+
final private class BOption = O::Option;
67+
68+
final private class BNone = O::None;
69+
70+
final private class BSome = O::Some;
71+
72+
/**
73+
* An option type. This is either a singleton `None` or a `Some` wrapping the
74+
* given type.
75+
*/
76+
class Option extends BOption {
77+
/**
78+
* Holds if this element is at the specified location.
79+
* The location spans column `startColumn` of line `startLine` to
80+
* column `endColumn` of line `endLine` in file `filepath`.
81+
* For more information, see
82+
* [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
83+
*/
84+
predicate hasLocationInfo(
85+
string filePath, int startLine, int startColumn, int endLine, int endColumn
86+
) {
87+
this.isNone() and
88+
filePath = "" and
89+
startLine = 0 and
90+
startColumn = 0 and
91+
endLine = 0 and
92+
endColumn = 0
93+
or
94+
this.asSome().hasLocationInfo(filePath, startLine, startColumn, endLine, endColumn)
95+
}
96+
}
97+
98+
/** The singleton `None` element. */
99+
class None extends BNone, Option { }
100+
101+
/** A wrapper for the given type. */
102+
class Some extends BSome, Option { }
103+
104+
/** Gets the given element wrapped as an `Option`. */
105+
Some some(T c) { result = O::some(c) }
106+
}

0 commit comments

Comments
 (0)