Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d07d86b

Browse files
committedFeb 9, 2024
switch-case conversion: ensure case selection has source location
The expression operand was at times lacking a source location when it had been subject to expression simplification. Fixes the root cause in the C front-end, and makes sure the invariant is maintained during goto conversion.
1 parent e16e48e commit d07d86b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed
 

‎src/ansi-c/c_typecheck_expr.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -4656,6 +4656,8 @@ class is_compile_time_constantt
46564656

46574657
void c_typecheck_baset::make_constant(exprt &expr)
46584658
{
4659+
source_locationt location = expr.find_source_location();
4660+
46594661
// Floating-point expressions may require a rounding mode.
46604662
// ISO 9899:1999 F.7.2 says that the default is "round to nearest".
46614663
// Some compilers have command-line options to override.
@@ -4664,10 +4666,11 @@ void c_typecheck_baset::make_constant(exprt &expr)
46644666
adjust_float_expressions(expr, rounding_mode);
46654667

46664668
simplify(expr, *this);
4669+
expr.add_source_location() = location;
46674670

46684671
if(!is_compile_time_constantt(*this)(expr))
46694672
{
4670-
error().source_location=expr.find_source_location();
4673+
error().source_location = location;
46714674
error() << "expected constant expression, but got '" << to_string(expr)
46724675
<< "'" << eom;
46734676
throw 0;
@@ -4676,13 +4679,15 @@ void c_typecheck_baset::make_constant(exprt &expr)
46764679

46774680
void c_typecheck_baset::make_constant_index(exprt &expr)
46784681
{
4682+
source_locationt location = expr.find_source_location();
46794683
make_constant(expr);
46804684
make_index_type(expr);
46814685
simplify(expr, *this);
4686+
expr.add_source_location() = location;
46824687

46834688
if(!is_compile_time_constantt(*this)(expr))
46844689
{
4685-
error().source_location=expr.find_source_location();
4690+
error().source_location = location;
46864691
error() << "conversion to integer constant failed" << eom;
46874692
throw 0;
46884693
}

‎src/goto-programs/goto_convert.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,13 @@ void goto_convertt::convert_switch_case(
526526

527527
exprt::operandst &case_op_dest=cases_entry->second->second;
528528
case_op_dest.push_back(code.case_op());
529+
// make sure we have a source location in the case operand as otherwise we
530+
// end up with a GOTO instruction without a source location
531+
DATA_INVARIANT_WITH_DIAGNOSTICS(
532+
case_op_dest.back().source_location().is_not_nil(),
533+
"case operand should have a source location",
534+
case_op_dest.back().pretty(),
535+
code.pretty());
529536
}
530537
}
531538

0 commit comments

Comments
 (0)