mirror of
https://github.com/github/codeql.git
synced 2026-04-26 09:15:12 +02:00
Swift: Add a simple Regex library.
This commit is contained in:
76
swift/ql/lib/codeql/swift/regex/Regex.qll
Normal file
76
swift/ql/lib/codeql/swift/regex/Regex.qll
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Provides classes and predicates for reasoning about use of regular expressions.
|
||||
*/
|
||||
|
||||
import swift
|
||||
|
||||
/**
|
||||
* A call that evaluates a regular expression. For example:
|
||||
* ```
|
||||
* Regex("(a|b).*").firstMatch(in: myString)
|
||||
* ```
|
||||
*/
|
||||
abstract class RegexEval extends CallExpr {
|
||||
Expr regex;
|
||||
Expr input;
|
||||
|
||||
/**
|
||||
* Gets the regular expression that is evaluated.
|
||||
*/
|
||||
Expr getRegex() { result = regex }
|
||||
|
||||
/**
|
||||
* Gets the input string the regular expression is evaluated on.
|
||||
*/
|
||||
Expr getInput() { result = input }
|
||||
}
|
||||
|
||||
/**
|
||||
* A call to a function that always evaluates a regular expression.
|
||||
*/
|
||||
private class AlwaysRegexEval extends RegexEval {
|
||||
AlwaysRegexEval() {
|
||||
this.getStaticTarget()
|
||||
.(Method)
|
||||
.hasQualifiedName("Regex", ["firstMatch(in:)", "prefixMatch(in:)", "wholeMatch(in:)"]) and
|
||||
regex = this.getQualifier() and
|
||||
input = this.getArgument(0).getExpr()
|
||||
or
|
||||
this.getStaticTarget()
|
||||
.(Method)
|
||||
.hasQualifiedName("NSRegularExpression",
|
||||
[
|
||||
"numberOfMatches(in:options:range:)", "enumerateMatches(in:options:range:using:)",
|
||||
"matches(in:options:range:)", "firstMatch(in:options:range:)",
|
||||
"rangeOfFirstMatch(in:options:range:)",
|
||||
"replaceMatches(in:options:range:withTemplate:)",
|
||||
"stringByReplacingMatches(in:options:range:withTemplate:)"
|
||||
]) and
|
||||
regex = this.getQualifier() and
|
||||
input = this.getArgument(0).getExpr()
|
||||
or
|
||||
this.getStaticTarget()
|
||||
.(Method)
|
||||
.hasQualifiedName("BidirectionalCollection",
|
||||
[
|
||||
"contains(_:)", "firstMatch(of:)", "firstRange(of:)", "matches(of:)",
|
||||
"prefixMatch(of:)", "ranges(of:)",
|
||||
"split(separator:maxSplits:omittingEmptySubsequences:)", "starts(with:)",
|
||||
"trimmingPrefix(_:)", "wholeMatch(of:)"
|
||||
]) and
|
||||
regex = this.getArgument(0).getExpr() and
|
||||
input = this.getQualifier()
|
||||
or
|
||||
this.getStaticTarget()
|
||||
.(Method)
|
||||
.hasQualifiedName("RangeReplaceableCollection",
|
||||
[
|
||||
"replace(_:maxReplacements:with:)", "replace(_:with:maxReplacements:)",
|
||||
"replacing(_:maxReplacements:with:)", "replacing(_:subrange:maxReplacements:with:)",
|
||||
"replacing(_:with:maxReplacements:)", "replacing(_:with:subrange:maxReplacements:)",
|
||||
"trimPrefix(_:)"
|
||||
]) and
|
||||
regex = this.getArgument(0).getExpr() and
|
||||
input = this.getQualifier()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user