Skip to content

Commit ad45a4b

Browse files
authored
Merge pull request #2890 from nickrolfe/range_based_for
C++: add more extensive test for desugaring of range-based-for loops
2 parents 771cb75 + 46b226e commit ad45a4b

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
bodies
2+
| test.cpp:8:3:10:3 | for(...:...) ... | test.cpp:8:28:10:3 | { ... } |
3+
| test.cpp:28:3:30:3 | for(...:...) ... | test.cpp:28:28:30:3 | { ... } |
4+
| test.cpp:44:3:46:3 | for(...:...) ... | test.cpp:44:27:46:3 | { ... } |
5+
variables
6+
| test.cpp:8:3:10:3 | for(...:...) ... | test.cpp:8:13:8:17 | value | file://:0:0:0:0 | short |
7+
| test.cpp:28:3:30:3 | for(...:...) ... | test.cpp:28:12:28:16 | value | file://:0:0:0:0 | int |
8+
| test.cpp:44:3:46:3 | for(...:...) ... | test.cpp:44:13:44:17 | value | file://:0:0:0:0 | long |
9+
ranges
10+
| test.cpp:8:3:10:3 | for(...:...) ... | test.cpp:8:21:8:25 | array | file://:0:0:0:0 | short[100] |
11+
| test.cpp:28:3:30:3 | for(...:...) ... | test.cpp:28:20:28:25 | vector | test.cpp:16:8:16:13 | Vector<int> |
12+
| test.cpp:44:3:46:3 | for(...:...) ... | test.cpp:44:21:44:24 | list | file://:0:0:0:0 | const List<long> & |
13+
rangeVariables
14+
| test.cpp:8:3:10:3 | for(...:...) ... | test.cpp:8:3:8:3 | (__range) | file://:0:0:0:0 | short(&)[100] |
15+
| test.cpp:28:3:30:3 | for(...:...) ... | test.cpp:28:3:28:3 | (__range) | file://:0:0:0:0 | Vector<int> & |
16+
| test.cpp:44:3:46:3 | for(...:...) ... | test.cpp:44:3:44:3 | (__range) | file://:0:0:0:0 | const List<long> & |
17+
conditions
18+
| test.cpp:8:3:10:3 | for(...:...) ... | file://:0:0:0:0 | ... != ... | file://:0:0:0:0 | (__begin) | file://:0:0:0:0 | (__end) |
19+
| test.cpp:28:3:30:3 | for(...:...) ... | test.cpp:28:20:28:20 | call to operator!= | file://:0:0:0:0 | (__begin) | file://:0:0:0:0 | (__end) |
20+
| test.cpp:44:3:46:3 | for(...:...) ... | file://:0:0:0:0 | ... != ... | file://:0:0:0:0 | (__begin) | file://:0:0:0:0 | (__end) |
21+
beginVariables
22+
| test.cpp:8:3:10:3 | for(...:...) ... | test.cpp:8:3:8:3 | (__begin) | file://:0:0:0:0 | short * |
23+
| test.cpp:28:3:30:3 | for(...:...) ... | test.cpp:28:3:28:3 | (__begin) | test.cpp:17:10:17:17 | Iterator |
24+
| test.cpp:44:3:46:3 | for(...:...) ... | test.cpp:44:3:44:3 | (__begin) | file://:0:0:0:0 | long * |
25+
endVariables
26+
| test.cpp:8:3:10:3 | for(...:...) ... | test.cpp:8:3:8:3 | (__end) | file://:0:0:0:0 | short * |
27+
| test.cpp:28:3:30:3 | for(...:...) ... | test.cpp:28:3:28:3 | (__end) | test.cpp:17:10:17:17 | Iterator |
28+
| test.cpp:44:3:46:3 | for(...:...) ... | test.cpp:44:3:44:3 | (__end) | file://:0:0:0:0 | long * |
29+
updates
30+
| test.cpp:8:3:10:3 | for(...:...) ... | file://:0:0:0:0 | ++ ... | file://:0:0:0:0 | (__begin) |
31+
| test.cpp:28:3:30:3 | for(...:...) ... | test.cpp:28:20:28:20 | call to operator++ | file://:0:0:0:0 | (__begin) |
32+
| test.cpp:44:3:46:3 | for(...:...) ... | file://:0:0:0:0 | ++ ... | file://:0:0:0:0 | (__begin) |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import cpp
2+
3+
query predicate bodies(RangeBasedForStmt rbf, Stmt body) { body = rbf.getStmt() }
4+
5+
query predicate variables(RangeBasedForStmt rbf, LocalVariable v, Type t) {
6+
v = rbf.getVariable() and
7+
t = v.getType()
8+
}
9+
10+
query predicate ranges(RangeBasedForStmt rbf, Expr range, Type t) {
11+
range = rbf.getRange() and
12+
t = range.getType()
13+
}
14+
15+
query predicate rangeVariables(RangeBasedForStmt rbf, LocalVariable rangeVar, Type t) {
16+
rangeVar = rbf.getRangeVariable() and
17+
t = rangeVar.getType()
18+
}
19+
20+
query predicate conditions(RangeBasedForStmt rbf, Expr condition, Expr left, Expr right) {
21+
condition = rbf.getCondition() and
22+
(
23+
condition instanceof BinaryOperation and
24+
left = condition.(BinaryOperation).getLeftOperand() and
25+
right = condition.(BinaryOperation).getRightOperand()
26+
or
27+
condition instanceof FunctionCall and
28+
left = condition.(FunctionCall).getQualifier() and
29+
right = condition.(FunctionCall).getArgument(0)
30+
)
31+
}
32+
33+
query predicate beginVariables(RangeBasedForStmt rbf, LocalVariable beginVar, Type t) {
34+
beginVar = rbf.getBeginVariable() and
35+
t = beginVar.getType()
36+
}
37+
38+
query predicate endVariables(RangeBasedForStmt rbf, LocalVariable endVar, Type t) {
39+
endVar = rbf.getEndVariable() and
40+
t = endVar.getType()
41+
}
42+
43+
query predicate updates(RangeBasedForStmt rbf, Expr update, Expr operand) {
44+
update = rbf.getUpdate() and
45+
(
46+
update instanceof UnaryOperation and
47+
operand = update.(UnaryOperation).getOperand()
48+
or
49+
update instanceof FunctionCall and
50+
operand = update.(FunctionCall).getQualifier()
51+
)
52+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
void print_short(short);
3+
void print_int(int);
4+
void print_long(long);
5+
6+
short array[100];
7+
void loop_over_c_array() {
8+
for (auto value : array) {
9+
print_short(value);
10+
}
11+
}
12+
13+
// A class that can be used with range-based-for, because it has the member
14+
// functions `begin` and `end`.
15+
template<typename T>
16+
struct Vector {
17+
struct Iterator {
18+
const T& operator*() const;
19+
bool operator!=(const Iterator &rhs) const;
20+
Iterator operator++();
21+
};
22+
Iterator begin();
23+
Iterator end();
24+
};
25+
26+
Vector<int> vector;
27+
void loop_over_vector_object() {
28+
for (int value : vector) {
29+
print_int(value);
30+
}
31+
}
32+
33+
// A class that can be used with range-based-for, because there are `begin` and
34+
// `end` functions that take a `List` as their argument.
35+
template<typename T>
36+
struct List {};
37+
38+
template<typename T>
39+
T* begin(const List<T> &list);
40+
template<typename T>
41+
T* end(const List<T> &list);
42+
43+
void loop_over_list_object(const List<long> &list) {
44+
for (auto value : list) {
45+
print_long(value);
46+
}
47+
}

0 commit comments

Comments
 (0)