Skip to content

Commit 3baf3de

Browse files
Report diagnostics from scope-exit destructor calls at the destroyed variable's declaration [GH #309]
1 parent ab072d3 commit 3baf3de

6 files changed

Lines changed: 108 additions & 14 deletions

File tree

‎src/lower_init.c‎

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,14 @@ static an_expr_node_ptr make_call_node_full(
583583
an_expr_node_ptr arg_list,
584584
an_expr_node_ptr return_value,
585585
a_boolean is_virtual_call,
586-
an_insert_location *insert_location)
586+
an_insert_location *insert_location,
587+
a_source_position *call_position)
587588
/*
588589
Make an expression that calls routine "routine" with arguments "arg_list",
589590
and return a pointer to it (or to the assignment expression generated if
590591
return_value is non-NULL). arg_list is assumed to be lowered already.
592+
If call_position is non-NULL, it is recorded as the position of the call
593+
node (otherwise, the call node has no position).
591594
If return_value is non-NULL, it represents an lvalue to which the return
592595
value from the call operation will be assigned (otherwise any value returned
593596
by the call is ignored). If insert_location is not NULL, an expression
@@ -634,6 +637,7 @@ either make_call_node or make_call_statement is preferred when possible.
634637
rout_return_type = lowered_return_type_of(routine->type);
635638
call_node = make_operator_node((an_expr_operator_kind)eok_call,
636639
rout_return_type, rout_node);
640+
if (call_position != NULL) call_node->position = *call_position;
637641
/* If the caller requested that the return value from the call be
638642
assigned to an lvalue expression, create the assignment expression. */
639643
if (return_value != NULL) {
@@ -680,7 +684,8 @@ will be attempted.
680684

681685
call_node = make_call_node_full(routine, arg_list, (an_expr_node_ptr)NULL,
682686
/*is_virtual_call=*/FALSE,
683-
(an_insert_location *)NULL);
687+
(an_insert_location *)NULL,
688+
(a_source_position *)NULL);
684689
check_assertion(call_node != NULL);
685690
return call_node;
686691
} /* make_call_node */
@@ -698,7 +703,8 @@ of the call is assigned.
698703
*/
699704
{
700705
(void)make_call_node_full(routine, arg_list, return_value,
701-
/*is_virtual_call=*/FALSE, insert_location);
706+
/*is_virtual_call=*/FALSE, insert_location,
707+
(a_source_position *)NULL);
702708
} /* make_call_statement */
703709

704710

@@ -5906,15 +5912,18 @@ static void add_destructor_call(
59065912
an_init_pos_descr_ptr ipdp,
59075913
a_boolean have_complete_object,
59085914
ARG_UNUSED an_expr_node_ptr vtt_addr_node,
5909-
an_insert_location_ptr insert_location)
5915+
an_insert_location_ptr insert_location,
5916+
a_source_position *call_position)
59105917
/*
59115918
Make a call statement that invokes the destructor dtor_routine for
59125919
the entity whose position is given by ipdp. If the entity is a whole array,
59135920
destroy all the elements. have_complete_object is TRUE if the
59145921
entity is a complete object. vtt_addr_node is an expression for the virtual
59155922
table table address that should be passed to the base class destructor
59165923
(IA-64 ABI only), or NULL if one is not needed. Insert the statement
5917-
at *insert_location and update *insert_location. The call generated
5924+
at *insert_location and update *insert_location. If call_position is
5925+
non-NULL, it is recorded as the position of the call expression (the
5926+
statement gets code_pos_for_lowering regardless). The call generated
59185927
is not a virtual call even if the destructor is virtual. Note: this
59195928
routine takes separate dtor_routine and ipdp parameters instead of a
59205929
dynamic init pointer because of the make_destruction_routine case.
@@ -5953,6 +5962,7 @@ dynamic init pointer because of the make_destruction_routine case.
59535962
call_node = make_vec_delete_call(entity_node, num_elem_node,
59545963
dtor_addr_node, (a_routine *)NULL,
59555964
/*free_storage=*/FALSE);
5965+
if (call_position != NULL) call_node->position = *call_position;
59565966
/* Make a statement containing the call and insert it at the right
59575967
location. */
59585968
(void)insert_expr_statement_set_pos(call_node, insert_location);
@@ -5979,10 +5989,12 @@ dynamic init pointer because of the make_destruction_routine case.
59795989
}
59805990
#endif /* !IA64_ABI */
59815991
entity_node->next = implied_arg_list;
5982-
/* Make and insert an expression statement containing the call
5983-
expression. */
5984-
make_call_statement(dtor_routine, entity_node, (an_expr_node_ptr)NULL,
5985-
insert_location);
5992+
/* Make the call expression and (because insert_location is non-NULL)
5993+
insert an expression statement containing it. */
5994+
(void)make_call_node_full(dtor_routine, entity_node,
5995+
(an_expr_node_ptr)NULL,
5996+
/*is_virtual_call=*/FALSE, insert_location,
5997+
call_position);
59865998
} /* if */
59875999
} /* add_destructor_call */
59886000

@@ -6029,9 +6041,21 @@ and not for constructor_init entries in destructors.
60296041
a_destructible_entity_descr_ptr dedp = dip->destructible_entity_descr;
60306042
an_insert_location insert_location2;
60316043
an_insert_location_ptr effective_insert_loc;
6044+
Value_saver<a_source_position> saved_error_position(&error_position);
6045+
a_source_position *call_position = NULL;
60326046

60336047
check_assertion(dedp != NULL);
60346048
check_assertion(dip->destructor != NULL);
6049+
if (dip->variable != NULL &&
6050+
dip->variable->source_corresp.decl_position.seq != 0) {
6051+
/* The generated code is attributed to the point of destruction (e.g.,
6052+
the closing brace of a block; see code_pos_for_lowering), but
6053+
diagnostics about the destruction are attributed to the declaration
6054+
of the destroyed variable, which is also where the front end reports
6055+
problems with its destructor. */
6056+
call_position = &dip->variable->source_corresp.decl_position;
6057+
error_position = *call_position;
6058+
} /* if */
60356059
/* Set the cleanup state to what it should be after the destruction,
60366060
because as soon as we start the destruction it's the destructor's
60376061
job to deal with partial destruction. */
@@ -6074,7 +6098,8 @@ and not for constructor_init entries in destructors.
60746098
&dedp->init_pos_descr,
60756099
/*have_complete_object=*/TRUE,
60766100
(an_expr_node_ptr)NULL,
6077-
effective_insert_loc);
6101+
effective_insert_loc,
6102+
call_position);
60786103
} /* gen_one_destruction */
60796104

60806105
#if VLA_DEALLOCATION_REQUIRED
@@ -7834,7 +7859,8 @@ is described by ipdp.
78347859
routine = scope->variant.routine.ptr;
78357860
/* Generate the code for the destruction. */
78367861
add_destructor_call(dip->destructor, ipdp, /*have_complete_object=*/TRUE,
7837-
(an_expr_node_ptr)NULL, &insert_location);
7862+
(an_expr_node_ptr)NULL, &insert_location,
7863+
(a_source_position *)NULL);
78387864
/* Mark the variable as referenced from another function. */
78397865
ipdp->variable->referenced_non_locally = TRUE;
78407866
pop_generated_routine_context(scope, region_number, &grcontext);
@@ -11333,7 +11359,8 @@ virtual table table pointer that should be passed to the destructor
1133311359
/*unreachable=*/FALSE);
1133411360
} /* if */
1133511361
add_destructor_call(dip->destructor, ipdp, have_complete_object,
11336-
vtt_addr_node, insert_location);
11362+
vtt_addr_node, insert_location,
11363+
(a_source_position *)NULL);
1133711364
error_position = saved_error_position;
1133811365
} /* lower_destructor_dynamic_init */
1133911366

@@ -13050,7 +13077,8 @@ tricks.
1305013077
call_node = make_call_node_full(dtor_routine, ptr_node,
1305113078
(an_expr_node_ptr)NULL,
1305213079
/*is_virtual_call=*/dtor_routine->is_virtual,
13053-
(an_insert_location *)NULL);
13080+
(an_insert_location *)NULL,
13081+
(a_source_position *)NULL);
1305413082
check_assertion(call_node != NULL);
1305513083
if (dtor_routine->is_virtual) {
1305613084
/* The destructor is virtual, so rewrite the virtual call. */
@@ -15945,7 +15973,8 @@ ABI.
1594515973
var_rvalue_expr(vtt_ptr_var), &insert_location);
1594615974
/* Generate the code to call the destructor. */
1594715975
add_destructor_call(dtor_routine, &ipd, /*have_complete_object=*/FALSE,
15948-
(an_expr_node_ptr)NULL, &insert_location);
15976+
(an_expr_node_ptr)NULL, &insert_location,
15977+
(a_source_position *)NULL);
1594915978
pop_generated_routine_context(scope, region_number, &grcontext);
1595015979
return routine;
1595115980
} /* make_subobject_destruction_routine */

‎tests/expectations/edg_x86_32_cp/regressions.log‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
gh-183-redecl.sft.cpp:fp:-DTEST_NUMBER=1 --c++17:REQUIREMENTS NOT MET:
2+
gh-309-diag.sft.cpp:fp:-DTEST_NUMBER=1 --g++:REQUIREMENTS NOT MET:Diagnostic position of implicit destructor calls at scope exit
3+
gh-309-diag.sft.cpp:fp:-DTEST_NUMBER=2 --g++ --no_exceptions:REQUIREMENTS NOT MET:Diagnostic position of implicit destructor calls at scope exit
24
gh-309.sft.cpp:fp:-DTEST_NUMBER=1 --g++:REQUIREMENTS NOT MET:
35
gh-309.sft.cpp:fp:-DTEST_NUMBER=2 --g++ --no_exceptions:REQUIREMENTS NOT MET:
46
immediate-pragma-lost.sft.cpp:cppbe:-DTEST_NUMBER=1:PASS:

‎tests/expectations/edg_x86_64_cp/regressions.log‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
gh-183-redecl.sft.cpp:fp:-DTEST_NUMBER=1 --c++17:REQUIREMENTS NOT MET:
2+
gh-309-diag.sft.cpp:fp:-DTEST_NUMBER=1 --g++:REQUIREMENTS NOT MET:Diagnostic position of implicit destructor calls at scope exit
3+
gh-309-diag.sft.cpp:fp:-DTEST_NUMBER=2 --g++ --no_exceptions:REQUIREMENTS NOT MET:Diagnostic position of implicit destructor calls at scope exit
24
gh-309.sft.cpp:fp:-DTEST_NUMBER=1 --g++:REQUIREMENTS NOT MET:
35
gh-309.sft.cpp:fp:-DTEST_NUMBER=2 --g++ --no_exceptions:REQUIREMENTS NOT MET:
46
immediate-pragma-lost.sft.cpp:cppbe:-DTEST_NUMBER=1:PASS:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fe_only -DTEST_NUMBER=1 --il_display --g++ Test_name.c
2+
position.seq: 19
3+
position.column: 5
4+
position.seq: 24
5+
position.column: 5
6+
position.seq: 27
7+
position.column: 7
8+
position.seq: 31
9+
position.column: 7
10+
position.seq: 31
11+
position.column: 7
12+
position.seq: 24
13+
position.column: 5
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fe_only -DTEST_NUMBER=2 --il_display --g++ --no_exceptions Test_name.c
2+
position.seq: 19
3+
position.column: 5
4+
position.seq: 24
5+
position.column: 5
6+
position.seq: 27
7+
position.column: 7
8+
position.seq: 31
9+
position.column: 7
10+
position.seq: 31
11+
position.column: 7
12+
position.seq: 24
13+
position.column: 5
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//type:fp
2+
//options:--g++:--g++ --no_exceptions
3+
//options_all:--il_display
4+
//require:DO_IL_LOWERING 1
5+
//filter:awk -v RS='' -v ORS='\n\n' '/\noperation[.]kind: +eok_call\n/' | grep -E '^position[.](seq|column):'
6+
//remark:Diagnostic position of implicit destructor calls at scope exit
7+
8+
// The calls of the destructors of d1, ..., d4 are attributed (in the
9+
// generated code) to the points where the variables are destroyed, but the
10+
// call expressions are positioned at the declarations of the variables
11+
// (which is where diagnostics arising from those calls should be reported).
12+
// Before, they had no position.
13+
struct D {
14+
~D();
15+
};
16+
int sink;
17+
18+
void f() {
19+
D d1;
20+
sink = 1;
21+
}
22+
23+
void g(bool b) {
24+
D d2;
25+
if (b) return;
26+
{
27+
D d3;
28+
sink = 2;
29+
}
30+
for (;;) {
31+
D d4;
32+
if (sink) break;
33+
}
34+
sink = 3;
35+
}

0 commit comments

Comments
 (0)