Skip to content

Commit 57f5d03

Browse files
committed
C++: Implement POSIX collating symbols
1 parent 32e3a11 commit 57f5d03

4 files changed

Lines changed: 50 additions & 80 deletions

File tree

‎cpp/ql/lib/semmle/code/cpp/regex/RegexTreeView.qll‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private newtype TRegExpParent =
5050
TRegExpBackRef(RegExp re, int start, int end) { re.backreference(start, end) } or
5151
/** A named character property */
5252
TRegExpNamedCharacterProperty(RegExp re, int start, int end) {
53-
re.namedCharacterProperty(start, end, _)
53+
re.namedCharacterProperty(start, end, [":", "."], _)
5454
}
5555

5656
/** An implementation that satisfies the RegexTreeView signature. */
@@ -1098,7 +1098,7 @@ private module Impl implements RegexTreeViewSig {
10981098

10991099
/**
11001100
* A named character property. For example, the POSIX bracket expression
1101-
* `[[:digit:]]`.
1101+
* `[:digit:]`.
11021102
*/
11031103
additional class RegExpNamedCharacterProperty extends RegExpTerm, TRegExpNamedCharacterProperty {
11041104
RegExpNamedCharacterProperty() { this = TRegExpNamedCharacterProperty(re, start, end) }
@@ -1108,10 +1108,16 @@ private module Impl implements RegexTreeViewSig {
11081108
override string getAPrimaryQlClass() { result = "RegExpNamedCharacterProperty" }
11091109

11101110
/**
1111-
* Gets the property name. For example, in `[[:digit:]]`, the result is
1111+
* Gets the property name. For example, in `[:digit:]`, the result is
11121112
* `"digit"`.
11131113
*/
11141114
string getName() { result = re.getCharacterPropertyName(start, end) }
1115+
1116+
/**
1117+
* Gets the property symbol. For example, in `[:digit:]`, the result is
1118+
* `":"`.
1119+
*/
1120+
string getSymbol() { result = re.getCharacterPropertySymbol(start, end) }
11151121
}
11161122

11171123
class Top = RegExpParent;

‎cpp/ql/lib/semmle/code/cpp/regex/internal/ParseRegExp.qll‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ abstract class RegExp extends StringLiteral {
100100
(this.nonEscapedCharAt(p) = "[" or this.nonEscapedCharAt(p) = "]") and
101101
// Brackets that are part of POSIX expressions should not count as
102102
// char-set delimiters.
103-
not exists(int x, int y |
104-
this.posixStyleNamedCharacterProperty(x, y, _) and pos >= x and pos < y
105-
)
103+
not exists(int x, int y | this.posixStyleProperty(x, y, _, _) and pos >= x and pos < y)
106104
) and
107105
(
108106
this.nonEscapedCharAt(pos) = "[" and result = true
@@ -132,9 +130,7 @@ abstract class RegExp extends StringLiteral {
132130
min(int e |
133131
e > innerStart and
134132
this.nonEscapedCharAt(e) = "]" and
135-
not exists(int x, int y |
136-
this.posixStyleNamedCharacterProperty(x, y, _) and e >= x and e < y
137-
)
133+
not exists(int x, int y | this.posixStyleProperty(x, y, _, _) and e >= x and e < y)
138134
|
139135
e
140136
)
@@ -160,7 +156,7 @@ abstract class RegExp extends StringLiteral {
160156
(
161157
this.escapedCharacter(start, end)
162158
or
163-
this.namedCharacterProperty(start, end, _)
159+
this.namedCharacterProperty(start, end, _, _)
164160
or
165161
exists(this.nonEscapedCharAt(start)) and end = start + 1
166162
)
@@ -169,7 +165,7 @@ abstract class RegExp extends StringLiteral {
169165
(
170166
this.escapedCharacter(start, end)
171167
or
172-
this.namedCharacterProperty(start, end, _)
168+
this.namedCharacterProperty(start, end, _, _)
173169
or
174170
exists(this.nonEscapedCharAt(start)) and
175171
end = start + 1 and
@@ -282,28 +278,34 @@ abstract class RegExp extends StringLiteral {
282278
)
283279
}
284280

285-
/** Matches named character properties such as `[[:digit:]]`. */
286-
predicate namedCharacterProperty(int start, int end, string name) {
287-
this.posixStyleNamedCharacterProperty(start, end, name)
281+
/** Matches named character properties such as `[:digit:]`. */
282+
predicate namedCharacterProperty(int start, int end, string symbol, string name) {
283+
this.posixStyleProperty(start, end, symbol, name)
288284
}
289285

290286
/** Gets the name of the character property in start,end */
291287
string getCharacterPropertyName(int start, int end) {
292-
this.namedCharacterProperty(start, end, result)
288+
this.namedCharacterProperty(start, end, _, result)
289+
}
290+
291+
/** Gets the symbol of the character property in start,end */
292+
string getCharacterPropertySymbol(int start, int end) {
293+
this.namedCharacterProperty(start, end, result, _)
293294
}
294295

295296
/** Matches a POSIX bracket expression such as `[:alnum:]` within a character class. */
296-
private predicate posixStyleNamedCharacterProperty(int start, int end, string name) {
297+
private predicate posixStyleProperty(int start, int end, string symbol, string name) {
297298
this.getChar(start) = "[" and
298-
this.getChar(start + 1) = ":" and
299+
this.getChar(start + 1) = symbol and
299300
end =
300301
min(int e |
301302
e > start and
302-
this.getChar(e - 2) = ":" and
303+
this.getChar(e - 2) = symbol and
303304
this.getChar(e - 1) = "]"
304305
|
305306
e
306307
) and
308+
symbol in [":", ".", "="] and
307309
name = this.getText().substring(start + 2, end - 2)
308310
}
309311

@@ -348,9 +350,7 @@ abstract class RegExp extends StringLiteral {
348350
* Holds if the character at `index` is inside a posix bracket.
349351
*/
350352
predicate inPosixBracket(int index) {
351-
exists(int x, int y |
352-
this.posixStyleNamedCharacterProperty(x, y, _) and index in [x + 1 .. y - 2]
353-
)
353+
exists(int x, int y | this.posixStyleProperty(x, y, _, _) and index in [x + 1 .. y - 2])
354354
}
355355

356356
/**
@@ -361,7 +361,7 @@ abstract class RegExp extends StringLiteral {
361361
not this.charSet(start, _) and
362362
not this.charSet(_, start + 1) and
363363
not exists(int x, int y |
364-
this.posixStyleNamedCharacterProperty(x, y, _) and
364+
this.posixStyleProperty(x, y, _, _) and
365365
start >= x and
366366
end <= y
367367
) and
@@ -731,7 +731,7 @@ abstract class RegExp extends StringLiteral {
731731
this.isGroupStart(start) or
732732
this.charSet(start, _) or
733733
this.backreference(start, _) or
734-
this.namedCharacterProperty(start, _, _)
734+
this.namedCharacterProperty(start, _, _, _)
735735
}
736736

737737
private predicate itemEnd(int end) {

‎cpp/ql/test/library-tests/regex/parse.expected‎

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -397,36 +397,15 @@ regexp.cpp:
397397

398398
# 75| [RegExpConstant, RegExpNormalChar] f
399399

400-
# 78| [RegExpCharacterClass] [[.a.]
401-
#-----| 0 -> [RegExpConstant, RegExpNormalChar] [
402-
#-----| 1 -> [RegExpConstant, RegExpNormalChar] .
403-
#-----| 2 -> [RegExpConstant, RegExpNormalChar] a
404-
#-----| 3 -> [RegExpConstant, RegExpNormalChar] .
405-
406-
# 78| [RegExpSequence] [[.a.]]
407-
#-----| 0 -> [RegExpCharacterClass] [[.a.]
408-
#-----| 1 -> [RegExpConstant, RegExpNormalChar] ]
409-
410-
# 78| [RegExpConstant, RegExpNormalChar] [
411-
412-
# 78| [RegExpConstant, RegExpNormalChar] .
413-
414-
# 78| [RegExpConstant, RegExpNormalChar] a
415-
416-
# 78| [RegExpConstant, RegExpNormalChar] .
400+
# 78| [RegExpCharacterClass] [[.a.]]
401+
#-----| 0 -> [RegExpNamedCharacterProperty] [.a.]
417402

418-
# 78| [RegExpConstant, RegExpNormalChar] ]
403+
# 78| [RegExpNamedCharacterProperty] [.a.]
419404

420-
# 79| [RegExpCharacterClass] [A-F[.b.]
405+
# 79| [RegExpCharacterClass] [A-F[.b.]a-f]
421406
#-----| 0 -> [RegExpCharacterRange] A-F
422-
#-----| 1 -> [RegExpConstant, RegExpNormalChar] [
423-
#-----| 2 -> [RegExpConstant, RegExpNormalChar] .
424-
#-----| 3 -> [RegExpConstant, RegExpNormalChar] b
425-
#-----| 4 -> [RegExpConstant, RegExpNormalChar] .
426-
427-
# 79| [RegExpSequence] [A-F[.b.]a-f]
428-
#-----| 0 -> [RegExpCharacterClass] [A-F[.b.]
429-
#-----| 1 -> [RegExpConstant, RegExpNormalChar] a-f]
407+
#-----| 1 -> [RegExpNamedCharacterProperty] [.b.]
408+
#-----| 2 -> [RegExpCharacterRange] a-f
430409

431410
# 79| [RegExpConstant, RegExpNormalChar] A
432411

@@ -436,15 +415,15 @@ regexp.cpp:
436415

437416
# 79| [RegExpConstant, RegExpNormalChar] F
438417

439-
# 79| [RegExpConstant, RegExpNormalChar] [
440-
441-
# 79| [RegExpConstant, RegExpNormalChar] .
418+
# 79| [RegExpNamedCharacterProperty] [.b.]
442419

443-
# 79| [RegExpConstant, RegExpNormalChar] b
420+
# 79| [RegExpConstant, RegExpNormalChar] a
444421

445-
# 79| [RegExpConstant, RegExpNormalChar] .
422+
# 79| [RegExpCharacterRange] a-f
423+
#-----| 0 -> [RegExpConstant, RegExpNormalChar] a
424+
#-----| 1 -> [RegExpConstant, RegExpNormalChar] f
446425

447-
# 79| [RegExpConstant, RegExpNormalChar] a-f]
426+
# 79| [RegExpConstant, RegExpNormalChar] f
448427

449428
# 82| [RegExpNamedCharacterProperty] [:digit:]
450429

‎cpp/ql/test/library-tests/regex/regexp.expected‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,16 @@ term
148148
| regexp.cpp:75:35:75:35 | a | RegExpConstant,RegExpNormalChar |
149149
| regexp.cpp:75:35:75:37 | a-f | RegExpCharacterRange |
150150
| regexp.cpp:75:37:75:37 | f | RegExpConstant,RegExpNormalChar |
151-
| regexp.cpp:78:27:78:32 | [[.a.] | RegExpCharacterClass |
152-
| regexp.cpp:78:27:78:33 | [[.a.]] | RegExpSequence |
153-
| regexp.cpp:78:28:78:28 | [ | RegExpConstant,RegExpNormalChar |
154-
| regexp.cpp:78:29:78:29 | . | RegExpConstant,RegExpNormalChar |
155-
| regexp.cpp:78:30:78:30 | a | RegExpConstant,RegExpNormalChar |
156-
| regexp.cpp:78:31:78:31 | . | RegExpConstant,RegExpNormalChar |
157-
| regexp.cpp:78:33:78:33 | ] | RegExpConstant,RegExpNormalChar |
158-
| regexp.cpp:79:27:79:35 | [A-F[.b.] | RegExpCharacterClass |
159-
| regexp.cpp:79:27:79:39 | [A-F[.b.]a-f] | RegExpSequence |
151+
| regexp.cpp:78:27:78:33 | [[.a.]] | RegExpCharacterClass |
152+
| regexp.cpp:78:28:78:32 | [.a.] | RegExpNamedCharacterProperty |
153+
| regexp.cpp:79:27:79:39 | [A-F[.b.]a-f] | RegExpCharacterClass |
160154
| regexp.cpp:79:28:79:28 | A | RegExpConstant,RegExpNormalChar |
161155
| regexp.cpp:79:28:79:30 | A-F | RegExpCharacterRange |
162156
| regexp.cpp:79:30:79:30 | F | RegExpConstant,RegExpNormalChar |
163-
| regexp.cpp:79:31:79:31 | [ | RegExpConstant,RegExpNormalChar |
164-
| regexp.cpp:79:32:79:32 | . | RegExpConstant,RegExpNormalChar |
165-
| regexp.cpp:79:33:79:33 | b | RegExpConstant,RegExpNormalChar |
166-
| regexp.cpp:79:34:79:34 | . | RegExpConstant,RegExpNormalChar |
167-
| regexp.cpp:79:36:79:39 | a-f] | RegExpConstant,RegExpNormalChar |
157+
| regexp.cpp:79:31:79:35 | [.b.] | RegExpNamedCharacterProperty |
158+
| regexp.cpp:79:36:79:36 | a | RegExpConstant,RegExpNormalChar |
159+
| regexp.cpp:79:36:79:38 | a-f | RegExpCharacterRange |
160+
| regexp.cpp:79:38:79:38 | f | RegExpConstant,RegExpNormalChar |
168161
| regexp.cpp:82:22:82:30 | [:digit:] | RegExpNamedCharacterProperty |
169162
| regexp.cpp:85:19:85:24 | \\u{987 | RegExpConstant,RegExpEscape |
170163
| regexp.cpp:88:21:88:23 | \\cA | RegExpConstant,RegExpEscape |
@@ -288,18 +281,10 @@ regExpNormalCharValue
288281
| regexp.cpp:75:25:75:25 | F | F |
289282
| regexp.cpp:75:35:75:35 | a | a |
290283
| regexp.cpp:75:37:75:37 | f | f |
291-
| regexp.cpp:78:28:78:28 | [ | [ |
292-
| regexp.cpp:78:29:78:29 | . | . |
293-
| regexp.cpp:78:30:78:30 | a | a |
294-
| regexp.cpp:78:31:78:31 | . | . |
295-
| regexp.cpp:78:33:78:33 | ] | ] |
296284
| regexp.cpp:79:28:79:28 | A | A |
297285
| regexp.cpp:79:30:79:30 | F | F |
298-
| regexp.cpp:79:31:79:31 | [ | [ |
299-
| regexp.cpp:79:32:79:32 | . | . |
300-
| regexp.cpp:79:33:79:33 | b | b |
301-
| regexp.cpp:79:34:79:34 | . | . |
302-
| regexp.cpp:79:36:79:39 | a-f] | a-f] |
286+
| regexp.cpp:79:36:79:36 | a | a |
287+
| regexp.cpp:79:38:79:38 | f | f |
303288
| regexp.cpp:85:19:85:24 | \\u{987 | \u0987 |
304289
| regexp.cpp:88:21:88:23 | \\cA | cA |
305290
| regexp.cpp:89:22:89:24 | \\cZ | cZ |

0 commit comments

Comments
 (0)