Skip to content

Commit 5c25c58

Browse files
committed
Update is_it_pointer_declaration to take unqualified_id_node
1 parent 020e781 commit 5c25c58

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

source/cppfront.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,28 +1558,27 @@ class cppfront
15581558

15591559
//-----------------------------------------------------------------------
15601560
//
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)
15631563
return false;
1564-
if (addr_cnt>deref_cnt)
1564+
if (!unqual->pointer_declarators.empty())
15651565
return true;
1566+
auto decl = sema.get_declaration_of(*unqual->identifier, true);
1567+
1568+
if (!decl || !decl->declaration)
1569+
return addr_cnt > deref_cnt;
15661570

15671571
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);
15701575
} 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);
15801579
}
15811580

1582-
return (pointer_declarators_cnt + addr_cnt - deref_cnt) > 0;
1581+
return (std::ssize(unqual->pointer_declarators) + addr_cnt - deref_cnt) > 0;
15831582
};
15841583

15851584
//-----------------------------------------------------------------------
@@ -1616,26 +1615,32 @@ class cppfront
16161615

16171616
// if initialized by something suspicious (that we have no information about) we need to add cpp1 safety checks
16181617
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) {
16201619
if (n.ops.empty()) {
16211620
last_postfix_expr_was_pointer = true;
16221621
}
16231622
else
16241623
{
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) {
16291634
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"
16321637
);
16331638
violates_bounds_safety = true;
16341639
}
1635-
else if (n.ops.front().op->type() == lexeme::Tilde) {
1640+
else if (op->type() == lexeme::Tilde) {
16361641
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"
16391644
);
16401645
}
16411646
}
@@ -1646,7 +1651,6 @@ class cppfront
16461651
std::shared_ptr<void> _on_return;
16471652

16481653
if (add_safetycheck) {
1649-
needs_safetycheck = false;
16501654
printer.print_cpp2("cpp2::safety_check(", n.position());
16511655
_on_return = [](auto l) { return std::shared_ptr<void>(nullptr, l); }([&](auto){
16521656
printer.print_cpp2(")", n.position());

0 commit comments

Comments
 (0)