Skip to content

Commit 505a088

Browse files
committed
Add analysis of cpp2 declarations to check if deal with pointer type
Thanks to relocating pointer_declarators this change introduce deduction of pointer types also from functions - if function or variable is defined in cpp2 then cppfront has all information to deduce the proper type during compilation.
1 parent 5c25c58 commit 505a088

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

a.out

58.4 KB
Binary file not shown.

regression-tests/pure2-deduced-pointers.cpp2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
fun: (inout i:int) -> *int = {
2+
return i&;
3+
}
4+
15
main: (argc : int, argv : **char) -> int = {
26
a: int = 2;
37
pa: *int = a&;
@@ -20,5 +24,8 @@ main: (argc : int, argv : **char) -> int = {
2024
pa5 := pppa**;
2125
pa5 = 0; // caught
2226

27+
ff := fun(a);
28+
ff = 0; // caught?
29+
2330
return a*pa**ppa**; // 8
2431
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
pure2-deduced-pointers.cpp2...
2-
pure2-deduced-pointers.cpp2(6,8): error: = - pointer assignment from null or integer is illegal
3-
pure2-deduced-pointers.cpp2(9,9): error: = - pointer assignment from null or integer is illegal
4-
pure2-deduced-pointers.cpp2(12,9): error: = - pointer assignment from null or integer is illegal
5-
pure2-deduced-pointers.cpp2(13,9): error: += - pointer assignment from null or integer is illegal
6-
pure2-deduced-pointers.cpp2(17,9): error: = - pointer assignment from null or integer is illegal
2+
pure2-deduced-pointers.cpp2(10,8): error: = - pointer assignment from null or integer is illegal
3+
pure2-deduced-pointers.cpp2(13,9): error: = - pointer assignment from null or integer is illegal
4+
pure2-deduced-pointers.cpp2(16,9): error: = - pointer assignment from null or integer is illegal
5+
pure2-deduced-pointers.cpp2(17,9): error: += - pointer assignment from null or integer is illegal
76
pure2-deduced-pointers.cpp2(21,9): error: = - pointer assignment from null or integer is illegal
7+
pure2-deduced-pointers.cpp2(25,9): error: = - pointer assignment from null or integer is illegal
8+
pure2-deduced-pointers.cpp2(28,8): error: = - pointer assignment from null or integer is illegal
89
==> program violates lifetime safety guarantee - see previous errors
910

source/cppfront.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,37 @@ class cppfront
15681568
if (!decl || !decl->declaration)
15691569
return addr_cnt > deref_cnt;
15701570

1571+
// if it is a function check if it returns pointer type
1572+
if (auto* fun = std::get_if<declaration_node::function>(&decl->declaration->type)) {
1573+
if (auto* ret_id_expr = std::get_if<function_type_node::id>(&(*fun)->returns)) {
1574+
if (auto* unqual = std::get_if<id_expression_node::unqualified>(&(*ret_id_expr)->id)){
1575+
return is_it_pointer_declaration(*unqual, deref_cnt, addr_cnt);
1576+
}
1577+
}
1578+
}
1579+
1580+
// if it is an object declaration check if we declare an pointer type
1581+
if (auto* obj_id_expr = std::get_if<declaration_node::object>(&decl->declaration->type)) {
1582+
if (auto* unqual = std::get_if<id_expression_node::unqualified>(&(*obj_id_expr)->id)){
1583+
return is_it_pointer_declaration(*unqual, deref_cnt, addr_cnt);
1584+
}
1585+
}
1586+
1587+
// if we initialize an object with a function check if it is initialized with a pointer type
1588+
if (decl->initializer && decl->initializer->suspicious_initialization) {
1589+
auto init_decl = sema.get_declaration_of(*decl->initializer->suspicious_initialization, true);
1590+
if (init_decl && init_decl->declaration) {
1591+
if (auto* fun = std::get_if<declaration_node::function>(&init_decl->declaration->type)) {
1592+
if (auto* ret_id_expr = std::get_if<function_type_node::id>(&(*fun)->returns)) {
1593+
if (auto* unqual = std::get_if<id_expression_node::unqualified>(&(*ret_id_expr)->id)){
1594+
return is_it_pointer_declaration(*unqual, deref_cnt, addr_cnt);
1595+
}
1596+
}
1597+
}
1598+
}
1599+
}
1600+
1601+
// deduce if it is a pointer type based on number of dereferences and address of operations
15711602
if (decl->declaration->dereference) {
15721603
auto deref = sema.get_declaration_of(*decl->declaration->dereference, true);
15731604
assert(deref && deref->declaration);
@@ -1604,18 +1635,9 @@ class cppfront
16041635
assert(unqual);
16051636
auto decl = sema.get_declaration_of(*unqual->identifier, true);
16061637

1607-
bool is_pointer = false;
1608-
if (decl && decl->declaration) {
1609-
if (auto* obj_id_expr = std::get_if<declaration_node::object>(&decl->declaration->type)) {
1610-
if (auto* unqual = std::get_if<id_expression_node::unqualified>(&(*obj_id_expr)->id)){
1611-
is_pointer = !(*unqual)->pointer_declarators.empty();
1612-
}
1613-
}
1614-
}
1615-
16161638
// if initialized by something suspicious (that we have no information about) we need to add cpp1 safety checks
16171639
add_safetycheck = !decl && needs_safetycheck;
1618-
if (is_it_pointer_declaration(unqual) || !unqual->pointer_declarators.empty() || is_pointer) {
1640+
if (is_it_pointer_declaration(unqual)) {
16191641
if (n.ops.empty()) {
16201642
last_postfix_expr_was_pointer = true;
16211643
}

0 commit comments

Comments
 (0)