Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b61de6

Browse files
Implement style/doc suggestions from code review
1 parent 28649da commit 5b61de6

5 files changed

Lines changed: 73 additions & 76 deletions

File tree

java/ql/lib/semmle/code/java/PrintAst.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ final class AnnotationPartNode extends ExprStmtNode {
286286

287287
/**
288288
* A node representing a `StringLiteral`.
289-
* It has a child if it is used as a regular expression, which is the root of the regular expression.
289+
* If it is used as a regular expression, then it has a single child, the root of the parsed regular expression.
290290
*/
291291
final class StringLiteralNode extends ExprStmtNode {
292292
StringLiteralNode() { element instanceof StringLiteral }

java/ql/lib/semmle/code/java/regex/RegexFlowConfigs.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private class RegexCompileFlowConf extends DataFlow2::Configuration {
2020
* Holds if `s` is used as a regex, with the mode `mode` (if known).
2121
* If regex mode is not known, `mode` will be `"None"`.
2222
*/
23-
predicate used_as_regex(Expr s, string mode) {
23+
predicate usedAsRegex(StringLiteral s, string mode) {
2424
any(RegexCompileFlowConf c).hasFlow(DataFlow2::exprNode(s), _) and
2525
mode = "None" // TODO: proper mode detection
2626
}
@@ -43,10 +43,10 @@ abstract class RegexMatchMethodAccess extends MethodAccess {
4343
stringArg in [-1 .. m.getNumberOfParameters() - 1]
4444
}
4545

46-
/** Gets the argument of this call that the regex to be matched against flows into */
46+
/** Gets the argument of this call that the regex to be matched against flows into. */
4747
Expr getRegexArg() { result = argOf(this, regexArg) }
4848

49-
/** Gets the argument of this call that the */
49+
/** Gets the argument of this call that the string being matched flows into. */
5050
Expr getStringArg() { result = argOf(this, stringArg) }
5151
}
5252

@@ -178,9 +178,9 @@ private class RegexMatchFlowConf extends DataFlow2::Configuration {
178178
}
179179

180180
/**
181-
* Holds if the string literal `regex` is matched against the expression `str`.
181+
* Holds if the string literal `regex` is a regular expression that is matched against the expression `str`.
182182
*/
183-
predicate regex_match(StringLiteral regex, Expr str) {
183+
predicate regexMatchedAgainst(StringLiteral regex, Expr str) {
184184
exists(
185185
DataFlow::Node src, DataFlow::Node sink, RegexMatchMethodAccess ma, RegexMatchFlowConf conf
186186
|

java/ql/lib/semmle/code/java/regex/RegexTreeView.qll

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ private import semmle.code.java.regex.regex
55

66
/**
77
* An element containing a regular expression term, that is, either
8-
* a string literal (parsed as a regular expression)
9-
* or another regular expression term.
8+
* a string literal (parsed as a regular expression; the root of the parse tree)
9+
* or another regular expression term (a decendent of the root).
1010
*
11-
* For sequences and alternations, we require at least one child.
11+
* For sequences and alternations, we require at least two children.
1212
* Otherwise, we wish to represent the term differently.
1313
* This avoids multiple representations of the same term.
1414
*/
15-
newtype TRegExpParent =
15+
private newtype TRegExpParent =
1616
/** A string literal used as a regular expression */
1717
TRegExpLiteral(Regex re) or
1818
/** A quantified term */
19-
TRegExpQuantifier(Regex re, int start, int end) { re.qualifiedItem(start, end, _, _) } or
19+
TRegExpQuantifier(Regex re, int start, int end) { re.quantifiedItem(start, end, _, _) } or
2020
/** A sequence term */
2121
TRegExpSequence(Regex re, int start, int end) {
2222
re.sequence(start, end) and
@@ -47,8 +47,8 @@ newtype TRegExpParent =
4747

4848
/**
4949
* An element containing a regular expression term, that is, either
50-
* a string literal (parsed as a regular expression)
51-
* or another regular expression term.
50+
* a string literal (parsed as a regular expression; the root of the parse tree)
51+
* or another regular expression term (a decendent of the root).
5252
*/
5353
class RegExpParent extends TRegExpParent {
5454
/** Gets a textual representation of this element. */
@@ -92,6 +92,7 @@ class RegExpLiteral extends TRegExpLiteral, RegExpParent {
9292

9393
/**
9494
* A regular expression term, that is, a syntactic part of a regular expression.
95+
* These are the tree nodes that form the parse tree of a regular expression literal.
9596
*/
9697
class RegExpTerm extends RegExpParent {
9798
Regex re;
@@ -187,6 +188,8 @@ class RegExpTerm extends RegExpParent {
187188
predicate hasLocationInfo(
188189
string filepath, int startline, int startcolumn, int endline, int endcolumn
189190
) {
191+
// This currently gives incorrect results for string literals including backslashes. TODO: fix that.
192+
// There are also more complex cases where it fails. Handling all of them would be difficult for not much gain.
190193
exists(int re_start, int re_end |
191194
re.getLocation().hasLocationInfo(filepath, startline, re_start, endline, re_end) and
192195
startcolumn = re_start + start + 1 and
@@ -245,7 +248,7 @@ class RegExpQuantifier extends RegExpTerm, TRegExpQuantifier {
245248

246249
RegExpQuantifier() {
247250
this = TRegExpQuantifier(re, start, end) and
248-
re.qualifiedPart(start, part_end, end, maybe_empty, may_repeat_forever)
251+
re.quantifiedPart(start, part_end, end, maybe_empty, may_repeat_forever)
249252
}
250253

251254
override RegExpTerm getChild(int i) {
@@ -255,11 +258,11 @@ class RegExpQuantifier extends RegExpTerm, TRegExpQuantifier {
255258
result.getEnd() = part_end
256259
}
257260

258-
/** Hols if this term may match an unlimited number of times. */
261+
/** Holds if this term may match an unlimited number of times. */
259262
predicate mayRepeatForever() { may_repeat_forever = true }
260263

261-
/** Gets the qualifier for this term. That is e.g "?" for "a?". */
262-
string getQualifier() { result = re.getText().substring(part_end, end) }
264+
/** Gets the quantifier for this term. That is e.g "?" for "a?". */
265+
string getquantifier() { result = re.getText().substring(part_end, end) }
263266

264267
override string getPrimaryQLClass() { result = "RegExpQuantifier" }
265268
}
@@ -281,7 +284,7 @@ class InfiniteRepetitionQuantifier extends RegExpQuantifier {
281284
* ```
282285
*/
283286
class RegExpStar extends InfiniteRepetitionQuantifier {
284-
RegExpStar() { this.getQualifier().charAt(0) = "*" }
287+
RegExpStar() { this.getquantifier().charAt(0) = "*" }
285288

286289
override string getPrimaryQLClass() { result = "RegExpStar" }
287290
}
@@ -296,7 +299,7 @@ class RegExpStar extends InfiniteRepetitionQuantifier {
296299
* ```
297300
*/
298301
class RegExpPlus extends InfiniteRepetitionQuantifier {
299-
RegExpPlus() { this.getQualifier().charAt(0) = "+" }
302+
RegExpPlus() { this.getquantifier().charAt(0) = "+" }
300303

301304
override string getPrimaryQLClass() { result = "RegExpPlus" }
302305
}
@@ -311,7 +314,7 @@ class RegExpPlus extends InfiniteRepetitionQuantifier {
311314
* ```
312315
*/
313316
class RegExpOpt extends RegExpQuantifier {
314-
RegExpOpt() { this.getQualifier().charAt(0) = "?" }
317+
RegExpOpt() { this.getquantifier().charAt(0) = "?" }
315318

316319
override string getPrimaryQLClass() { result = "RegExpOpt" }
317320
}
@@ -333,10 +336,10 @@ class RegExpRange extends RegExpQuantifier {
333336

334337
RegExpRange() { re.multiples(part_end, end, lower, upper) }
335338

336-
/** Gets the string defining the upper bound of this range, if any. */
339+
/** Gets the string defining the upper bound of this range, which is empty when no such bound exists. */
337340
string getUpper() { result = upper }
338341

339-
/** Gets the string defining the lower bound of this range, if any. */
342+
/** Gets the string defining the lower bound of this range, which is empty when no such bound exists. */
340343
string getLower() { result = lower }
341344

342345
/**
@@ -578,9 +581,6 @@ class RegExpCharacterClass extends RegExpTerm, TRegExpCharacterClass {
578581
/** Holds if this character class is inverted, matching the opposite of its content. */
579582
predicate isInverted() { re.getChar(start + 1) = "^" }
580583

581-
/** Gets the `i`th char inside this charater class. */
582-
string getCharThing(int i) { result = re.getChar(i + start) }
583-
584584
/** Holds if this character class can match anything. */
585585
predicate isUniversalClass() {
586586
// [^]
@@ -724,9 +724,9 @@ class RegExpConstant extends RegExpTerm {
724724
RegExpConstant() {
725725
(this = TRegExpNormalChar(re, start, end) or this = TRegExpQuote(re, start, end)) and
726726
not this instanceof RegExpCharacterClassEscape and
727-
// exclude chars in qualifiers
727+
// exclude chars in quantifiers
728728
// TODO: push this into regex library
729-
not exists(int qstart, int qend | re.qualifiedPart(_, qstart, qend, _, _) |
729+
not exists(int qstart, int qend | re.quantifiedPart(_, qstart, qend, _, _) |
730730
qstart <= start and end <= qend
731731
) and
732732
(value = this.(RegExpNormalChar).getValue() or value = this.(RegExpQuote).getValue())

0 commit comments

Comments
 (0)