Skip to content

Commit e67c662

Browse files
authored
Merge pull request #3033 from tautschnig/more-ranged-for
Use ranged for to iterate over components
2 parents 2e3e235 + cde5ac3 commit e67c662

17 files changed

+87
-235
lines changed

jbmc/src/java_bytecode/expr2java.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,9 @@ std::string expr2javat::convert_struct(
127127
bool first=true;
128128
size_t last_size=0;
129129

130-
for(struct_typet::componentst::const_iterator
131-
c_it=components.begin();
132-
c_it!=components.end();
133-
c_it++)
130+
for(const auto &c : components)
134131
{
135-
if(c_it->type().id()==ID_code)
136-
{
137-
}
138-
else
132+
if(c.type().id() != ID_code)
139133
{
140134
std::string tmp=convert(*o_it);
141135
std::string sep;
@@ -155,7 +149,7 @@ std::string expr2javat::convert_struct(
155149

156150
dest+=sep;
157151
dest+='.';
158-
dest+=c_it->get_string(ID_pretty_name);
152+
dest += id2string(c.get_pretty_name());
159153
dest+='=';
160154
dest+=tmp;
161155
}

src/ansi-c/c_typecheck_code.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,8 @@ bool c_typecheck_baset::is_complete_type(const typet &type) const
372372
}
373373
else if(type.id()==ID_struct || type.id()==ID_union)
374374
{
375-
const struct_union_typet::componentst &components=
376-
to_struct_union_type(type).components();
377-
for(struct_union_typet::componentst::const_iterator
378-
it=components.begin();
379-
it!=components.end();
380-
it++)
381-
if(!is_complete_type(it->type()))
375+
for(const auto &c : to_struct_union_type(type).components())
376+
if(!is_complete_type(c.type()))
382377
return false;
383378
}
384379
else if(type.id()==ID_vector)

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -621,28 +621,20 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
621621
else
622622
{
623623
// maybe anonymous?
624-
625-
const struct_union_typet::componentst &components=
626-
struct_union_type.components();
627-
628624
bool found2=false;
629625

630-
for(struct_union_typet::componentst::const_iterator
631-
c_it=components.begin();
632-
c_it!=components.end();
633-
c_it++)
626+
for(const auto &c : struct_union_type.components())
634627
{
635-
if(c_it->get_anonymous() &&
636-
(follow(c_it->type()).id()==ID_struct ||
637-
follow(c_it->type()).id()==ID_union))
628+
if(
629+
c.get_anonymous() && (follow(c.type()).id() == ID_struct ||
630+
follow(c.type()).id() == ID_union))
638631
{
639-
if(has_component_rec(c_it->type(), component_name, *this))
632+
if(has_component_rec(c.type(), component_name, *this))
640633
{
641634
if(type.id()==ID_struct)
642635
{
643-
exprt o=
644-
member_offset_expr(
645-
to_struct_type(type), c_it->get_name(), *this);
636+
exprt o = member_offset_expr(
637+
to_struct_type(type), c.get_name(), *this);
646638

647639
if(o.is_nil())
648640
{
@@ -658,7 +650,7 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
658650
result=plus_exprt(result, o);
659651
}
660652

661-
typet tmp=follow(c_it->type());
653+
typet tmp = follow(c.type());
662654
type=tmp;
663655
assert(type.id()==ID_union || type.id()==ID_struct);
664656
found2=true;
@@ -1099,21 +1091,15 @@ void c_typecheck_baset::typecheck_expr_typecast(exprt &expr)
10991091
op.make_typecast(signed_int_type());
11001092

11011093
// we need to find a member with the right type
1102-
const union_typet &union_type=to_union_type(expr_type);
1103-
const union_typet::componentst &components=union_type.components();
1104-
1105-
for(union_typet::componentst::const_iterator
1106-
it=components.begin();
1107-
it!=components.end();
1108-
it++)
1094+
for(const auto &c : to_union_type(expr_type).components())
11091095
{
1110-
if(base_type_eq(it->type(), op.type(), *this))
1096+
if(base_type_eq(c.type(), op.type(), *this))
11111097
{
11121098
// found! build union constructor
11131099
union_exprt union_expr(expr.type());
11141100
union_expr.add_source_location()=expr.source_location();
11151101
union_expr.op()=op;
1116-
union_expr.set_component_name(it->get_name());
1102+
union_expr.set_component_name(c.get_name());
11171103
expr=union_expr;
11181104
expr.set(ID_C_lvalue, true);
11191105
return;

src/ansi-c/c_typecheck_initializer.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,11 @@ void c_typecheck_baset::designator_enter(
268268
// only a top-level struct may end with a variable-length array
269269
entry.vla_permitted=designator.empty();
270270

271-
for(struct_typet::componentst::const_iterator
272-
it=struct_type.components().begin();
273-
it!=struct_type.components().end();
274-
++it)
271+
for(const auto &c : struct_type.components())
275272
{
276-
if(it->type().id()!=ID_code &&
277-
!it->get_is_padding())
273+
if(c.type().id() != ID_code && !c.get_is_padding())
278274
{
279-
entry.subtype=it->type();
275+
entry.subtype = c.type();
280276
break;
281277
}
282278

@@ -758,38 +754,36 @@ designatort c_typecheck_baset::make_designator(
758754
do
759755
{
760756
repeat=false;
761-
unsigned number=0;
757+
std::size_t number = 0;
762758
const struct_union_typet::componentst &components=
763759
to_struct_union_type(follow(tmp_type)).components();
764760

765-
for(struct_union_typet::componentst::const_iterator
766-
c_it=components.begin();
767-
c_it!=components.end();
768-
c_it++, number++)
761+
for(const auto &c : components)
769762
{
770-
if(c_it->get_name()==component_name)
763+
if(c.get_name() == component_name)
771764
{
772765
// done!
773766
entry.index=number;
774767
entry.size=components.size();
775-
entry.subtype=components[entry.index].type();
768+
entry.subtype = c.type();
776769
entry.type=tmp_type;
777770
}
778-
else if(c_it->get_anonymous() &&
779-
(follow(c_it->type()).id()==ID_struct ||
780-
follow(c_it->type()).id()==ID_union) &&
781-
has_component_rec(
782-
c_it->type(), component_name, *this))
771+
else if(
772+
c.get_anonymous() && (follow(c.type()).id() == ID_struct ||
773+
follow(c.type()).id() == ID_union) &&
774+
has_component_rec(c.type(), component_name, *this))
783775
{
784776
entry.index=number;
785777
entry.size=components.size();
786-
entry.subtype=c_it->type();
778+
entry.subtype = c.type();
787779
entry.type=tmp_type;
788780
tmp_type=entry.subtype;
789781
designator.push_entry(entry);
790782
found=repeat=true;
791783
break;
792784
}
785+
786+
++number;
793787
}
794788
}
795789
while(repeat);

src/ansi-c/padding.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,12 @@ mp_integer alignment(const typet &type, const namespacet &ns)
5656
result=alignment(type.subtype(), ns);
5757
else if(type.id()==ID_struct || type.id()==ID_union)
5858
{
59-
const struct_union_typet::componentst &components=
60-
to_struct_union_type(type).components();
61-
6259
result=1;
6360

6461
// get the max
6562
// (should really be the smallest common denominator)
66-
for(struct_union_typet::componentst::const_iterator
67-
it=components.begin();
68-
it!=components.end();
69-
it++)
70-
result=std::max(result, alignment(it->type(), ns));
63+
for(const auto &c : to_struct_union_type(type).components())
64+
result = std::max(result, alignment(c.type(), ns));
7165
}
7266
else if(type.id()==ID_unsignedbv ||
7367
type.id()==ID_signedbv ||

src/ansi-c/type2name.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,17 @@ static std::string type2name(
203203
result+="ST";
204204
if(type.id()==ID_union)
205205
result+="UN";
206-
const struct_union_typet &t=to_struct_union_type(type);
207-
const struct_union_typet::componentst &components = t.components();
208206
result+='[';
209-
for(struct_union_typet::componentst::const_iterator
210-
it=components.begin();
211-
it!=components.end();
212-
it++)
207+
bool first = true;
208+
for(const auto &c : to_struct_union_type(type).components())
213209
{
214-
if(it!=components.begin())
210+
if(!first)
215211
result+='|';
216-
result+=type2name(it->type(), ns, symbol_number);
217-
irep_idt component_name = it->get_name();
212+
else
213+
first = false;
214+
215+
result += type2name(c.type(), ns, symbol_number);
216+
const irep_idt &component_name = c.get_name();
218217
CHECK_RETURN(!component_name.empty());
219218
result+="'"+id2string(component_name)+"'";
220219
}

src/cpp/cpp_typecheck_constructor.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -789,17 +789,9 @@ void cpp_typecheckt::full_member_initialization(
789789
/// \return return true if a copy constructor is found
790790
bool cpp_typecheckt::find_cpctor(const symbolt &symbol) const
791791
{
792-
const struct_typet &struct_type=to_struct_type(symbol.type);
793-
const struct_typet::componentst &components=struct_type.components();
794-
795-
for(struct_typet::componentst::const_iterator
796-
cit=components.begin();
797-
cit!=components.end();
798-
cit++)
792+
for(const auto &component : to_struct_type(symbol.type).components())
799793
{
800794
// Skip non-ctor
801-
const struct_typet::componentt &component=*cit;
802-
803795
if(component.type().id()!=ID_code ||
804796
to_code_type(component.type()).return_type().id() !=ID_constructor)
805797
continue;

src/goto-instrument/dump_c.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,8 @@ void dump_ct::convert_compound(
416416

417417
std::stringstream struct_body;
418418

419-
for(struct_union_typet::componentst::const_iterator
420-
it=type.components().begin();
421-
it!=type.components().end();
422-
it++)
419+
for(const auto &comp : type.components())
423420
{
424-
const struct_typet::componentt &comp=*it;
425421
const typet &comp_type=ns.follow(comp.type());
426422

427423
if(comp_type.id()==ID_code ||
@@ -1236,18 +1232,15 @@ void dump_ct::cleanup_expr(exprt &expr)
12361232

12371233
PRECONDITION(old_components.size()==old_ops.size());
12381234
exprt::operandst::iterator o_it=old_ops.begin();
1239-
for(struct_union_typet::componentst::const_iterator
1240-
it=old_components.begin();
1241-
it!=old_components.end();
1242-
++it)
1235+
for(const auto &old_comp : old_components)
12431236
{
1244-
const bool is_zero_bit_field=
1245-
it->type().id()==ID_c_bit_field &&
1246-
to_c_bit_field_type(it->type()).get_width()==0;
1237+
const bool is_zero_bit_field =
1238+
old_comp.type().id() == ID_c_bit_field &&
1239+
to_c_bit_field_type(old_comp.type()).get_width() == 0;
12471240

1248-
if(!it->get_is_padding() && !is_zero_bit_field)
1241+
if(!old_comp.get_is_padding() && !is_zero_bit_field)
12491242
{
1250-
type.components().push_back(*it);
1243+
type.components().push_back(old_comp);
12511244
expr.move_to_operands(*o_it);
12521245
}
12531246
++o_it;

src/goto-instrument/goto_program2code.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,16 +1439,8 @@ void goto_program2codet::add_local_types(const typet &type)
14391439
!type_names_set.insert(identifier).second)
14401440
return;
14411441

1442-
const struct_union_typet &struct_union_type=
1443-
to_struct_union_type(full_type);
1444-
const struct_union_typet::componentst &components=
1445-
struct_union_type.components();
1446-
1447-
for(struct_union_typet::componentst::const_iterator
1448-
it=components.begin();
1449-
it!=components.end();
1450-
++it)
1451-
add_local_types(it->type());
1442+
for(const auto &c : to_struct_union_type(full_type).components())
1443+
add_local_types(c.type());
14521444

14531445
assert(!identifier.empty());
14541446
type_names.push_back(identifier);

src/goto-programs/remove_complex.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,8 @@ static bool have_to_remove_complex(const typet &type)
8181
{
8282
if(type.id()==ID_struct || type.id()==ID_union)
8383
{
84-
const struct_union_typet &struct_union_type=
85-
to_struct_union_type(type);
86-
for(struct_union_typet::componentst::const_iterator
87-
it=struct_union_type.components().begin();
88-
it!=struct_union_type.components().end();
89-
it++)
90-
if(have_to_remove_complex(it->type()))
84+
for(const auto &c : to_struct_union_type(type).components())
85+
if(have_to_remove_complex(c.type()))
9186
return true;
9287
}
9388
else if(type.id()==ID_pointer ||

src/goto-programs/remove_vector.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,8 @@ static bool have_to_remove_vector(const typet &type)
5050
{
5151
if(type.id()==ID_struct || type.id()==ID_union)
5252
{
53-
const struct_union_typet &struct_union_type=
54-
to_struct_union_type(type);
55-
56-
for(struct_union_typet::componentst::const_iterator
57-
it=struct_union_type.components().begin();
58-
it!=struct_union_type.components().end();
59-
it++)
60-
if(have_to_remove_vector(it->type()))
53+
for(const auto &c : to_struct_union_type(type).components())
54+
if(have_to_remove_vector(c.type()))
6155
return true;
6256
}
6357
else if(type.id()==ID_pointer ||

src/goto-symex/goto_symex_state.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,8 @@ static bool check_renaming(const typet &type)
206206
type.id()==ID_union ||
207207
type.id()==ID_class)
208208
{
209-
const struct_union_typet &s_u_type=to_struct_union_type(type);
210-
const struct_union_typet::componentst &components=s_u_type.components();
211-
212-
for(struct_union_typet::componentst::const_iterator
213-
it=components.begin();
214-
it!=components.end();
215-
++it)
216-
if(check_renaming(it->type()))
209+
for(const auto &c : to_struct_union_type(type).components())
210+
if(check_renaming(c.type()))
217211
return true;
218212
}
219213
else if(type.has_subtype())

src/linking/linking.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,17 @@ std::string linkingt::type_to_string_verbose(
7272
result+=" "+tag;
7373
result+=" {\n";
7474

75-
const struct_union_typet::componentst &components=
76-
to_struct_union_type(followed).components();
77-
78-
for(struct_union_typet::componentst::const_iterator
79-
it=components.begin();
80-
it!=components.end();
81-
it++)
75+
for(const auto &c : to_struct_union_type(followed).components())
8276
{
83-
const typet &subtype=it->type();
77+
const typet &subtype = c.type();
8478
result+=" ";
8579
result+=type_to_string(ns, symbol.name, subtype);
8680
result+=' ';
8781

88-
if(it->get_base_name()!="")
89-
result+=id2string(it->get_base_name());
82+
if(!c.get_base_name().empty())
83+
result += id2string(c.get_base_name());
9084
else
91-
result+=id2string(it->get_name());
85+
result += id2string(c.get_name());
9286

9387
result+=";\n";
9488
}
@@ -686,11 +680,8 @@ void linkingt::duplicate_code_symbol(
686680
const typet &src_type=t1.id()==ID_union?t2:t1;
687681

688682
bool found=false;
689-
for(union_typet::componentst::const_iterator
690-
it=union_type.components().begin();
691-
!found && it!=union_type.components().end();
692-
it++)
693-
if(base_type_eq(it->type(), src_type, ns))
683+
for(const auto &c : union_type.components())
684+
if(base_type_eq(c.type(), src_type, ns))
694685
{
695686
found=true;
696687
if(warn_msg.empty())

0 commit comments

Comments
 (0)