@@ -1558,28 +1558,27 @@ class cppfront
1558
1558
1559
1559
// -----------------------------------------------------------------------
1560
1560
//
1561
- auto is_it_pointer_declaration (declaration_sym const * decl , int deref_cnt = 0 , int addr_cnt = 0 ) -> bool {
1562
- if (!decl || !decl-> declaration )
1561
+ auto is_it_pointer_declaration (std::unique_ptr<unqualified_id_node> const & unqual , int deref_cnt = 0 , int addr_cnt = 0 ) -> bool {
1562
+ if (!unqual )
1563
1563
return false ;
1564
- if (addr_cnt>deref_cnt )
1564
+ if (!unqual-> pointer_declarators . empty () )
1565
1565
return true ;
1566
+ auto decl = sema.get_declaration_of (*unqual->identifier , true );
1567
+
1568
+ if (!decl || !decl->declaration )
1569
+ return addr_cnt > deref_cnt;
1566
1570
1567
1571
if (decl->declaration ->dereference ) {
1568
- auto deref = sema.get_declaration_of (*decl->declaration ->dereference );
1569
- return is_it_pointer_declaration (deref, deref_cnt+decl->declaration ->dereference_cnt , addr_cnt);
1572
+ auto deref = sema.get_declaration_of (*decl->declaration ->dereference , true );
1573
+ assert (deref && deref->declaration );
1574
+ return is_it_pointer_declaration (deref->declaration ->identifier , deref_cnt+decl->declaration ->dereference_cnt , addr_cnt);
1570
1575
} else if (decl->declaration ->address_of ) {
1571
- auto addr = sema.get_declaration_of (*decl->declaration ->address_of );
1572
- return is_it_pointer_declaration (addr, deref_cnt, addr_cnt+1 );
1573
- }
1574
-
1575
- int pointer_declarators_cnt = 0 ;
1576
- if (auto * obj_id_expr = std::get_if<declaration_node::object>(&decl->declaration ->type )) {
1577
- if (auto * unqual = std::get_if<id_expression_node::unqualified>(&(*obj_id_expr)->id )){
1578
- pointer_declarators_cnt = std::ssize ((*unqual)->pointer_declarators );
1579
- }
1576
+ auto addr = sema.get_declaration_of (*decl->declaration ->address_of , true );
1577
+ assert (addr && addr->declaration );
1578
+ return is_it_pointer_declaration (addr->declaration ->identifier , deref_cnt, addr_cnt+1 );
1580
1579
}
1581
1580
1582
- return (pointer_declarators_cnt + addr_cnt - deref_cnt) > 0 ;
1581
+ return (std::ssize (unqual-> pointer_declarators ) + addr_cnt - deref_cnt) > 0 ;
1583
1582
};
1584
1583
1585
1584
// -----------------------------------------------------------------------
@@ -1616,26 +1615,32 @@ class cppfront
1616
1615
1617
1616
// if initialized by something suspicious (that we have no information about) we need to add cpp1 safety checks
1618
1617
add_safetycheck = !decl && needs_safetycheck;
1619
- if (is_it_pointer_declaration (decl ) || !unqual->pointer_declarators .empty () || is_pointer) {
1618
+ if (is_it_pointer_declaration (unqual ) || !unqual->pointer_declarators .empty () || is_pointer) {
1620
1619
if (n.ops .empty ()) {
1621
1620
last_postfix_expr_was_pointer = true ;
1622
1621
}
1623
1622
else
1624
1623
{
1625
- if (n.ops .front ().op ->type () == lexeme::PlusPlus ||
1626
- n.ops .front ().op ->type () == lexeme::MinusMinus ||
1627
- n.ops .front ().op ->type () == lexeme::LeftBracket
1628
- ) {
1624
+ auto op = [&](){
1625
+ if (n.ops .size () >= 2 && n.ops [0 ].op ->type () == lexeme::LeftParen) {
1626
+ return n.ops [1 ].op ;
1627
+ } else {
1628
+ return n.ops .front ().op ;
1629
+ }
1630
+ }();
1631
+ if (op->type () == lexeme::PlusPlus ||
1632
+ op->type () == lexeme::MinusMinus ||
1633
+ op->type () == lexeme::LeftBracket) {
1629
1634
errors.emplace_back (
1630
- n. ops . front (). op ->position (),
1631
- n. ops . front (). op ->to_string (true ) + " - pointer arithmetic is illegal - use std::span or gsl::span instead"
1635
+ op->position (),
1636
+ op->to_string (true ) + " - pointer arithmetic is illegal - use std::span or gsl::span instead"
1632
1637
);
1633
1638
violates_bounds_safety = true ;
1634
1639
}
1635
- else if (n. ops . front (). op ->type () == lexeme::Tilde) {
1640
+ else if (op->type () == lexeme::Tilde) {
1636
1641
errors.emplace_back (
1637
- n. ops . front (). op ->position (),
1638
- n. ops . front (). op ->to_string (true ) + " - pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first"
1642
+ op->position (),
1643
+ op->to_string (true ) + " - pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first"
1639
1644
);
1640
1645
}
1641
1646
}
@@ -1646,7 +1651,6 @@ class cppfront
1646
1651
std::shared_ptr<void > _on_return;
1647
1652
1648
1653
if (add_safetycheck) {
1649
- needs_safetycheck = false ;
1650
1654
printer.print_cpp2 (" cpp2::safety_check(" , n.position ());
1651
1655
_on_return = [](auto l) { return std::shared_ptr<void >(nullptr , l); }([&](auto ){
1652
1656
printer.print_cpp2 (" )" , n.position ());
0 commit comments