Skip to content

Commit f02aeaf

Browse files
committed
Ruby: Move regex/non-regex split into TAstNode to convey disjointness
1 parent 4f7f924 commit f02aeaf

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

ruby/ql/lib/codeql/ruby/ast/Literal.qll

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,10 @@ class StringComponent extends AstNode, TStringComponent {
274274
* "foo#{ bar() } baz"
275275
* ```
276276
*/
277-
class StringTextComponent extends StringComponent, TStringTextComponent {
277+
class StringTextComponent extends StringComponent, TStringTextComponentNonRegexp {
278278
private Ruby::Token g;
279279

280-
StringTextComponent() {
281-
this = TStringTextComponent(g) and not g.getParent() instanceof Ruby::Regex
282-
}
280+
StringTextComponent() { this = TStringTextComponentNonRegexp(g) }
283281

284282
final override string toString() { result = g.getValue() }
285283

@@ -291,12 +289,10 @@ class StringTextComponent extends StringComponent, TStringTextComponent {
291289
/**
292290
* An escape sequence component of a string or string-like literal.
293291
*/
294-
class StringEscapeSequenceComponent extends StringComponent, TStringEscapeSequenceComponent {
292+
class StringEscapeSequenceComponent extends StringComponent, TStringEscapeSequenceComponentNonRegexp {
295293
private Ruby::EscapeSequence g;
296294

297-
StringEscapeSequenceComponent() {
298-
this = TStringEscapeSequenceComponent(g) and not g.getParent() instanceof Ruby::Regex
299-
}
295+
StringEscapeSequenceComponent() { this = TStringEscapeSequenceComponentNonRegexp(g) }
300296

301297
final override string toString() { result = g.getValue() }
302298

@@ -309,12 +305,10 @@ class StringEscapeSequenceComponent extends StringComponent, TStringEscapeSequen
309305
* An interpolation expression component of a string or string-like literal.
310306
*/
311307
class StringInterpolationComponent extends StringComponent, StmtSequence,
312-
TStringInterpolationComponent {
308+
TStringInterpolationComponentNonRegexp {
313309
private Ruby::Interpolation g;
314310

315-
StringInterpolationComponent() {
316-
this = TStringInterpolationComponent(g) and not g.getParent() instanceof Ruby::Regex
317-
}
311+
StringInterpolationComponent() { this = TStringInterpolationComponentNonRegexp(g) }
318312

319313
final override string toString() { result = "#{...}" }
320314

@@ -325,14 +319,15 @@ class StringInterpolationComponent extends StringComponent, StmtSequence,
325319
final override string getAPrimaryQlClass() { result = "StringInterpolationComponent" }
326320
}
327321

322+
private class TRegExpComponent =
323+
TStringTextComponentRegexp or TStringEscapeSequenceComponentRegexp or
324+
TStringInterpolationComponentRegexp;
325+
328326
/**
329327
* The base class for a component of a regular expression literal.
330328
*/
331-
class RegExpComponent extends AstNode, TStringComponent {
332-
private RegExpLiteral parent;
333-
334-
RegExpComponent() { toGenerated(this).getParent() = toGenerated(parent) }
335-
329+
class RegExpComponent extends AstNode, TRegExpComponent {
330+
/** Gets the source text for this regex component, if any. */
336331
string getValueText() { none() }
337332
}
338333

@@ -348,10 +343,10 @@ class RegExpComponent extends AstNode, TStringComponent {
348343
* "foo#{ bar() } baz"
349344
* ```
350345
*/
351-
class RegExpTextComponent extends RegExpComponent, TStringTextComponent {
346+
class RegExpTextComponent extends RegExpComponent, TStringTextComponentRegexp {
352347
private Ruby::Token g;
353348

354-
RegExpTextComponent() { this = TStringTextComponent(g) and g.getParent() instanceof Ruby::Regex }
349+
RegExpTextComponent() { this = TStringTextComponentRegexp(g) }
355350

356351
final override string toString() { result = g.getValue() }
357352

@@ -367,10 +362,10 @@ class RegExpTextComponent extends RegExpComponent, TStringTextComponent {
367362
/**
368363
* An escape sequence component of a regex literal.
369364
*/
370-
class RegExpEscapeSequenceComponent extends RegExpComponent, TStringEscapeSequenceComponent {
365+
class RegExpEscapeSequenceComponent extends RegExpComponent, TStringEscapeSequenceComponentRegexp {
371366
private Ruby::EscapeSequence g;
372367

373-
RegExpEscapeSequenceComponent() { this = TStringEscapeSequenceComponent(g) }
368+
RegExpEscapeSequenceComponent() { this = TStringEscapeSequenceComponentRegexp(g) }
374369

375370
final override string toString() { result = g.getValue() }
376371

@@ -387,10 +382,10 @@ class RegExpEscapeSequenceComponent extends RegExpComponent, TStringEscapeSequen
387382
* An interpolation expression component of a regex literal.
388383
*/
389384
class RegExpInterpolationComponent extends RegExpComponent, StmtSequence,
390-
TStringInterpolationComponent {
385+
TStringInterpolationComponentRegexp {
391386
private Ruby::Interpolation g;
392387

393-
RegExpInterpolationComponent() { this = TStringInterpolationComponent(g) }
388+
RegExpInterpolationComponent() { this = TStringInterpolationComponentRegexp(g) }
394389

395390
final override string toString() { result = "#{...}" }
396391

ruby/ql/lib/codeql/ruby/ast/internal/AST.qll

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,25 @@ private module Cached {
272272
TStmtSequenceSynth(AST::AstNode parent, int i) { mkSynthChild(StmtSequenceKind(), parent, i) } or
273273
TStringArrayLiteral(Ruby::StringArray g) or
274274
TStringConcatenation(Ruby::ChainedString g) or
275-
TStringEscapeSequenceComponent(Ruby::EscapeSequence g) or
276-
TStringInterpolationComponent(Ruby::Interpolation g) or
277-
TStringTextComponent(Ruby::Token g) {
278-
g instanceof Ruby::StringContent or g instanceof Ruby::HeredocContent
275+
TStringEscapeSequenceComponentNonRegexp(Ruby::EscapeSequence g) {
276+
not g.getParent() instanceof Ruby::Regex
277+
} or
278+
TStringEscapeSequenceComponentRegexp(Ruby::EscapeSequence g) {
279+
g.getParent() instanceof Ruby::Regex
280+
} or
281+
TStringInterpolationComponentNonRegexp(Ruby::Interpolation g) {
282+
not g.getParent() instanceof Ruby::Regex
283+
} or
284+
TStringInterpolationComponentRegexp(Ruby::Interpolation g) {
285+
g.getParent() instanceof Ruby::Regex
286+
} or
287+
TStringTextComponentNonRegexp(Ruby::Token g) {
288+
(g instanceof Ruby::StringContent or g instanceof Ruby::HeredocContent) and
289+
not g.getParent() instanceof Ruby::Regex
290+
} or
291+
TStringTextComponentRegexp(Ruby::Token g) {
292+
(g instanceof Ruby::StringContent or g instanceof Ruby::HeredocContent) and
293+
g.getParent() instanceof Ruby::Regex
279294
} or
280295
TSubExprReal(Ruby::Binary g) { g instanceof @ruby_binary_minus } or
281296
TSubExprSynth(AST::AstNode parent, int i) { mkSynthChild(SubExprKind(), parent, i) } or
@@ -489,9 +504,12 @@ private module Cached {
489504
n = TSplatParameter(result) or
490505
n = TStringArrayLiteral(result) or
491506
n = TStringConcatenation(result) or
492-
n = TStringEscapeSequenceComponent(result) or
493-
n = TStringInterpolationComponent(result) or
494-
n = TStringTextComponent(result) or
507+
n = TStringEscapeSequenceComponentNonRegexp(result) or
508+
n = TStringEscapeSequenceComponentRegexp(result) or
509+
n = TStringInterpolationComponentNonRegexp(result) or
510+
n = TStringInterpolationComponentRegexp(result) or
511+
n = TStringTextComponentNonRegexp(result) or
512+
n = TStringTextComponentRegexp(result) or
495513
n = TSubExprReal(result) or
496514
n = TSubshellLiteral(result) or
497515
n = TSymbolArrayLiteral(result) or
@@ -680,6 +698,14 @@ class TIntegerLiteral = TIntegerLiteralReal or TIntegerLiteralSynth;
680698

681699
class TBooleanLiteral = TTrueLiteral or TFalseLiteral;
682700

701+
class TStringTextComponent = TStringTextComponentNonRegexp or TStringTextComponentRegexp;
702+
703+
class TStringEscapeSequenceComponent =
704+
TStringEscapeSequenceComponentNonRegexp or TStringEscapeSequenceComponentRegexp;
705+
706+
class TStringInterpolationComponent =
707+
TStringInterpolationComponentNonRegexp or TStringInterpolationComponentRegexp;
708+
683709
class TStringComponent =
684710
TStringTextComponent or TStringEscapeSequenceComponent or TStringInterpolationComponent;
685711

0 commit comments

Comments
 (0)