Skip to content

Commit e7ccb1f

Browse files
authored
Commit 8: Add location tests exposing term-location off-by-one (pre-fix)
Add location test cases to test.cpp covering various C++ string literal forms: - r_plain: "a+b" — plain, offset 1 (already correct) - r_raw: R"(a+b)" — raw, offset 3 (R"( = 3); currently uses 1 (WRONG) - r_raw2: R"(\s+$)" — raw with metacharacters; currently wrong - r_raw3: R"(\(([,\w]+)+\)$)" — complex raw; currently wrong - r_raw4: R"x(a+b)x" — custom-delimiter raw, offset 4; currently uses 1 (WRONG) - r_wide: L"a+b" — L" prefix, offset 2; currently uses 1 (WRONG) - r_wide_raw: LR"(a+b)" — combined LR"( prefix, offset 4; currently uses 1 (WRONG) - r_esc1: "\\s+" — escape-containing plain; offset 1 (correct) - r_esc2: "a\\.b" — escaped dot; offset 1 (correct) Added std::wregex typedef to stubs (for L"..." and LR"(...)" wide-char literals). New locations.ql query reports per-term: litStartCol, valueStart, valueEnd, termStartCol, termEndCol — restricted to test_locations() function. Generated locations.expected via codeql test run --learn (CodeQL 2.26.1). All 3 tests pass. The expected output captures the CURRENT (pre-fix) columns: raw/prefixed rows show termStartCol = litStartCol + 1 (wrong); plain rows show litStartCol + 1 (correct). Commit 9 changes the raw/prefixed rows to their correct values.
1 parent cebddbd commit e7ccb1f

5 files changed

Lines changed: 741 additions & 466 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
| test.cpp:144:23:144:23 | a | 22 | 0 | 1 | 23 | 23 |
2+
| test.cpp:144:23:144:24 | a+ | 22 | 0 | 2 | 23 | 24 |
3+
| test.cpp:144:23:144:25 | a+b | 22 | 0 | 3 | 23 | 25 |
4+
| test.cpp:144:25:144:25 | b | 22 | 2 | 3 | 25 | 25 |
5+
| test.cpp:147:21:147:21 | a | 20 | 0 | 1 | 21 | 21 |
6+
| test.cpp:147:21:147:22 | a+ | 20 | 0 | 2 | 21 | 22 |
7+
| test.cpp:147:21:147:23 | a+b | 20 | 0 | 3 | 21 | 23 |
8+
| test.cpp:147:23:147:23 | b | 20 | 2 | 3 | 23 | 23 |
9+
| test.cpp:150:22:150:23 | \\s | 21 | 0 | 2 | 22 | 23 |
10+
| test.cpp:150:22:150:24 | \\s+ | 21 | 0 | 3 | 22 | 24 |
11+
| test.cpp:150:22:150:25 | \\s+$ | 21 | 0 | 4 | 22 | 25 |
12+
| test.cpp:150:25:150:25 | $ | 21 | 3 | 4 | 25 | 25 |
13+
| test.cpp:153:22:153:23 | \\( | 21 | 0 | 2 | 22 | 23 |
14+
| test.cpp:153:22:153:35 | \\(([,\\w]+)+\\)$ | 21 | 0 | 14 | 22 | 35 |
15+
| test.cpp:153:24:153:31 | ([,\\w]+) | 21 | 2 | 10 | 24 | 31 |
16+
| test.cpp:153:24:153:32 | ([,\\w]+)+ | 21 | 2 | 11 | 24 | 32 |
17+
| test.cpp:153:25:153:29 | [,\\w] | 21 | 3 | 8 | 25 | 29 |
18+
| test.cpp:153:25:153:30 | [,\\w]+ | 21 | 3 | 9 | 25 | 30 |
19+
| test.cpp:153:26:153:26 | , | 21 | 4 | 5 | 26 | 26 |
20+
| test.cpp:153:27:153:28 | \\w | 21 | 5 | 7 | 27 | 28 |
21+
| test.cpp:153:33:153:34 | \\) | 21 | 11 | 13 | 33 | 34 |
22+
| test.cpp:153:35:153:35 | $ | 21 | 13 | 14 | 35 | 35 |
23+
| test.cpp:156:22:156:22 | a | 21 | 0 | 1 | 22 | 22 |
24+
| test.cpp:156:22:156:23 | a+ | 21 | 0 | 2 | 22 | 23 |
25+
| test.cpp:156:22:156:24 | a+b | 21 | 0 | 3 | 22 | 24 |
26+
| test.cpp:156:24:156:24 | b | 21 | 2 | 3 | 24 | 24 |
27+
| test.cpp:159:23:159:23 | a | 22 | 0 | 1 | 23 | 23 |
28+
| test.cpp:159:23:159:24 | a+ | 22 | 0 | 2 | 23 | 24 |
29+
| test.cpp:159:23:159:25 | a+b | 22 | 0 | 3 | 23 | 25 |
30+
| test.cpp:159:25:159:25 | b | 22 | 2 | 3 | 25 | 25 |
31+
| test.cpp:162:27:162:27 | a | 26 | 0 | 1 | 27 | 27 |
32+
| test.cpp:162:27:162:28 | a+ | 26 | 0 | 2 | 27 | 28 |
33+
| test.cpp:162:27:162:29 | a+b | 26 | 0 | 3 | 27 | 29 |
34+
| test.cpp:162:29:162:29 | b | 26 | 2 | 3 | 29 | 29 |
35+
| test.cpp:166:22:166:23 | \\s | 21 | 0 | 2 | 22 | 23 |
36+
| test.cpp:166:22:166:24 | \\s+ | 21 | 0 | 3 | 22 | 24 |
37+
| test.cpp:169:22:169:22 | a | 21 | 0 | 1 | 22 | 22 |
38+
| test.cpp:169:22:169:25 | a\\.b | 21 | 0 | 4 | 22 | 25 |
39+
| test.cpp:169:23:169:24 | \\. | 21 | 1 | 3 | 23 | 24 |
40+
| test.cpp:169:25:169:25 | b | 21 | 3 | 4 | 25 | 25 |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Reports per-term location info for each regex term, showing the literal's
3+
* start column, the term's value offsets, and the computed start/end columns.
4+
* Used to verify that `hasLocationInfo` produces correct columns for plain,
5+
* raw, and encoding-prefixed C++ string literals.
6+
*
7+
* Commit 8 captures pre-fix (wrong) columns for raw/prefixed literals.
8+
* Commit 9 fixes them; plain "..." rows must remain unchanged.
9+
*/
10+
11+
import cpp
12+
import semmle.code.cpp.regex.RegexTreeView as RE
13+
14+
query predicate locations(
15+
RE::RegExpTerm t,
16+
int litStartCol,
17+
int valueStart,
18+
int valueEnd,
19+
int termStartCol,
20+
int termEndCol
21+
) {
22+
// Only report terms from the location test function (test_locations)
23+
// to keep the output focused.
24+
t.getRegExp().getLocation().getStartLine() >= 142 and // test_locations starts here
25+
t.getRegExp().getLocation().hasLocationInfo(_, _, litStartCol, _, _) and
26+
valueStart = t.getStart() and
27+
valueEnd = t.getEnd() and
28+
t.hasLocationInfo(_, _, termStartCol, _, termEndCol)
29+
}

0 commit comments

Comments
 (0)