Skip to content

Commit 270d487

Browse files
authored
Merge pull request #3066 from diffblue/cpp-ranged-for
C++ front-end: use ranged for
2 parents 63bf323 + 4bae1df commit 270d487

10 files changed

+130
-249
lines changed

src/cpp/cpp_constructor.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,10 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
125125
{
126126
exprt::operandst operands_tc=operands;
127127

128-
for(exprt::operandst::iterator
129-
it=operands_tc.begin();
130-
it!=operands_tc.end();
131-
it++)
128+
for(auto &op : operands_tc)
132129
{
133-
typecheck_expr(*it);
134-
add_implicit_dereference(*it);
130+
typecheck_expr(op);
131+
add_implicit_dereference(op);
135132
}
136133

137134
if(operands_tc.empty())
@@ -167,13 +164,10 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
167164
{
168165
exprt::operandst operands_tc=operands;
169166

170-
for(exprt::operandst::iterator
171-
it=operands_tc.begin();
172-
it!=operands_tc.end();
173-
it++)
167+
for(auto &op : operands_tc)
174168
{
175-
typecheck_expr(*it);
176-
add_implicit_dereference(*it);
169+
typecheck_expr(op);
170+
add_implicit_dereference(op);
177171
}
178172

179173
const struct_typet &struct_type=
@@ -228,17 +222,11 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
228222
// there is always a constructor for non-PODs
229223
assert(constructor_name!="");
230224

231-
side_effect_expr_function_callt function_call;
225+
side_effect_expr_function_callt function_call(
226+
cpp_namet(constructor_name, source_location).as_expr(),
227+
operands_tc);
228+
232229
function_call.add_source_location()=source_location;
233-
function_call.function() =
234-
cpp_namet(constructor_name, source_location).as_expr();
235-
function_call.arguments().reserve(operands_tc.size());
236-
237-
for(exprt::operandst::iterator
238-
it=operands_tc.begin();
239-
it!=operands_tc.end();
240-
it++)
241-
function_call.op1().copy_to_operands(*it);
242230

243231
typecheck_side_effect_function_call(function_call);
244232
assert(function_call.get(ID_statement)==ID_temporary_object);

src/cpp/cpp_declarator_converter.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,9 @@ symbolt &cpp_declarator_convertert::convert_new_symbol(
497497
const auto id_set = cpp_typecheck.cpp_scopes.current_scope().lookup(
498498
base_name, cpp_scopet::SCOPE_ONLY);
499499

500-
for(cpp_scopest::id_sett::const_iterator
501-
id_it=id_set.begin();
502-
id_it!=id_set.end();
503-
id_it++)
500+
for(const auto &id_ptr : id_set)
504501
{
505-
const cpp_idt &id=**id_it;
502+
const cpp_idt &id = *id_ptr;
506503
// the name is already in the scope
507504
// this is ok if they belong to different categories
508505

src/cpp/cpp_id.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,54 +34,46 @@ void cpp_idt::print(std::ostream &out, unsigned indent) const
3434

3535
if(!sub.empty())
3636
{
37-
for(cpp_id_mapt::const_iterator it=sub.begin();
38-
it!=sub.end();
39-
it++)
40-
it->second.print(out, indent+2);
37+
for(const auto &s : sub)
38+
s.second.print(out, indent + 2);
4139

4240
out << '\n';
4341
}
4442
}
4543

4644
void cpp_idt::print_fields(std::ostream &out, unsigned indent) const
4745
{
48-
for(unsigned i=0; i<indent; i++) out << ' ';
46+
out << std::string(indent, ' ');
4947
out << "**identifier=" << identifier << '\n';
5048

51-
for(unsigned i=0; i<indent; i++) out << ' ';
49+
out << std::string(indent, ' ');
5250
out << " prefix=" << prefix << '\n';
5351

54-
for(unsigned i=0; i<indent; i++) out << ' ';
52+
out << std::string(indent, ' ');
5553
out << " suffix=" << suffix << '\n';
5654

57-
for(unsigned i=0; i<indent; i++) out << ' ';
55+
out << std::string(indent, ' ');
5856
out << " base_name=" << base_name << '\n';
5957

60-
for(unsigned i=0; i<indent; i++) out << ' ';
58+
out << std::string(indent, ' ');
6159
out << " method=" << is_method << '\n';
6260

63-
for(unsigned i=0; i<indent; i++) out << ' ';
61+
out << std::string(indent, ' ');
6462
out << " class_identifier=" << class_identifier << '\n';
6563

66-
for(scope_listt::const_iterator
67-
it=secondary_scopes.begin();
68-
it!=secondary_scopes.end();
69-
it++)
64+
for(const auto &s : secondary_scopes)
7065
{
71-
for(unsigned i=0; i<indent; i++) out << ' ';
72-
out << " secondary_scope=" << (*it)->identifier << '\n';
66+
out << std::string(indent, ' ');
67+
out << " secondary_scope=" << s->identifier << '\n';
7368
}
7469

75-
for(scope_listt::const_iterator
76-
it=using_scopes.begin();
77-
it!=using_scopes.end();
78-
it++)
70+
for(const auto &s : using_scopes)
7971
{
80-
for(unsigned i=0; i<indent; i++) out << ' ';
81-
out << " using_scope=" << (*it)->identifier << '\n';
72+
out << std::string(indent, ' ');
73+
out << " using_scope=" << s->identifier << '\n';
8274
}
8375

84-
for(unsigned i=0; i<indent; i++) out << ' ';
76+
out << std::string(indent, ' ');
8577
out << " flags:";
8678
if(is_constructor)
8779
out << " constructor";
@@ -93,7 +85,7 @@ void cpp_idt::print_fields(std::ostream &out, unsigned indent) const
9385
out << " static_member";
9486
out << '\n';
9587

96-
for(unsigned i=0; i<indent; i++) out << ' ';
88+
out << std::string(indent, ' ');
9789
out << " id_class=" << id_class << '\n';
9890
}
9991

src/cpp/cpp_instantiate_template.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ std::string cpp_typecheckt::template_suffix(
3333
const cpp_template_args_tct::argumentst &arguments=
3434
template_args.arguments();
3535

36-
for(cpp_template_args_tct::argumentst::const_iterator
37-
it=arguments.begin();
38-
it!=arguments.end();
39-
it++)
36+
for(const auto &expr : arguments)
4037
{
4138
if(first)
4239
first=false;
4340
else
4441
result+=',';
4542

46-
const exprt expr=*it;
4743
DATA_INVARIANT(
4844
expr.id() != ID_ambiguous, "template argument must not be ambiguous");
4945

@@ -69,7 +65,7 @@ std::string cpp_typecheckt::template_suffix(
6965
i=0;
7066
else if(to_integer(e, i))
7167
{
72-
error().source_location=it->find_source_location();
68+
error().source_location = expr.find_source_location();
7369
error() << "template argument expression expected to be "
7470
<< "scalar constant, but got `"
7571
<< to_string(e) << "'" << eom;
@@ -87,17 +83,14 @@ std::string cpp_typecheckt::template_suffix(
8783

8884
void cpp_typecheckt::show_instantiation_stack(std::ostream &out)
8985
{
90-
for(instantiation_stackt::const_iterator
91-
s_it=instantiation_stack.begin();
92-
s_it!=instantiation_stack.end();
93-
s_it++)
86+
for(const auto &e : instantiation_stack)
9487
{
95-
const symbolt &symbol=lookup(s_it->identifier);
88+
const symbolt &symbol = lookup(e.identifier);
9689
out << "instantiating `" << symbol.pretty_name << "' with <";
9790

98-
forall_expr(a_it, s_it->full_template_args.arguments())
91+
forall_expr(a_it, e.full_template_args.arguments())
9992
{
100-
if(a_it!=s_it->full_template_args.arguments().begin())
93+
if(a_it != e.full_template_args.arguments().begin())
10194
out << ", ";
10295

10396
if(a_it->id()==ID_type)
@@ -106,7 +99,7 @@ void cpp_typecheckt::show_instantiation_stack(std::ostream &out)
10699
out << to_string(*a_it);
107100
}
108101

109-
out << "> at " << s_it->source_location << '\n';
102+
out << "> at " << e.source_location << '\n';
110103
}
111104
}
112105

src/cpp/cpp_language.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,8 @@ bool cpp_languaget::generate_support_functions(
142142

143143
void cpp_languaget::show_parse(std::ostream &out)
144144
{
145-
for(cpp_parse_treet::itemst::const_iterator it=
146-
cpp_parse_tree.items.begin();
147-
it!=cpp_parse_tree.items.end();
148-
it++)
149-
show_parse(out, *it);
145+
for(const auto &i : cpp_parse_tree.items)
146+
show_parse(out, i);
150147
}
151148

152149
void cpp_languaget::show_parse(
@@ -160,11 +157,8 @@ void cpp_languaget::show_parse(
160157

161158
out << "LINKAGE " << linkage_spec.linkage().get(ID_value) << ":\n";
162159

163-
for(cpp_linkage_spect::itemst::const_iterator
164-
it=linkage_spec.items().begin();
165-
it!=linkage_spec.items().end();
166-
it++)
167-
show_parse(out, *it);
160+
for(const auto &i : linkage_spec.items())
161+
show_parse(out, i);
168162

169163
out << '\n';
170164
}
@@ -176,11 +170,8 @@ void cpp_languaget::show_parse(
176170
out << "NAMESPACE " << namespace_spec.get_namespace()
177171
<< ":\n";
178172

179-
for(cpp_namespace_spect::itemst::const_iterator
180-
it=namespace_spec.items().begin();
181-
it!=namespace_spec.items().end();
182-
it++)
183-
show_parse(out, *it);
173+
for(const auto &i : namespace_spec.items())
174+
show_parse(out, i);
184175

185176
out << '\n';
186177
}

src/cpp/cpp_scope.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,9 @@ void cpp_scopet::lookup_rec(
5151
return; // done
5252

5353
// using scopes
54-
for(scope_listt::iterator
55-
it=using_scopes.begin();
56-
it!=using_scopes.end();
57-
it++)
54+
for(const auto &s_ptr : using_scopes)
5855
{
59-
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
56+
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
6057

6158
// Recursive call.
6259
// Note the different kind!
@@ -67,12 +64,9 @@ void cpp_scopet::lookup_rec(
6764
return; // done, upwards scopes are hidden
6865

6966
// secondary scopes
70-
for(scope_listt::iterator
71-
it=secondary_scopes.begin();
72-
it!=secondary_scopes.end();
73-
it++)
67+
for(const auto &s_ptr : secondary_scopes)
7468
{
75-
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
69+
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
7670

7771
// Recursive call.
7872
// Note the different kind!
@@ -131,12 +125,9 @@ void cpp_scopet::lookup_rec(
131125
return; // done
132126

133127
// using scopes
134-
for(scope_listt::iterator
135-
it=using_scopes.begin();
136-
it!=using_scopes.end();
137-
it++)
128+
for(const auto &s_ptr : using_scopes)
138129
{
139-
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
130+
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
140131

141132
// Recursive call.
142133
// Note the different kind!
@@ -147,12 +138,9 @@ void cpp_scopet::lookup_rec(
147138
return; // done, upwards scopes are hidden
148139

149140
// secondary scopes
150-
for(scope_listt::iterator
151-
it=secondary_scopes.begin();
152-
it!=secondary_scopes.end();
153-
it++)
141+
for(const auto &s_ptr : secondary_scopes)
154142
{
155-
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
143+
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
156144

157145
// Recursive call.
158146
// Note the different kind!

src/cpp/cpp_typecheck_code.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,20 @@ void cpp_typecheckt::typecheck_code(codet &code)
4747

4848
void cpp_typecheckt::typecheck_try_catch(codet &code)
4949
{
50-
codet::operandst &operands=code.operands();
50+
bool first = true;
5151

52-
for(codet::operandst::iterator
53-
it=operands.begin();
54-
it!=operands.end();
55-
it++)
52+
for(auto &op : code.operands())
5653
{
57-
if(it==operands.begin())
54+
if(first)
5855
{
5956
// this is the 'try'
60-
typecheck_code(to_code(*it));
57+
typecheck_code(to_code(op));
58+
first = false;
6159
}
6260
else
6361
{
6462
// This is (one of) the catch clauses.
65-
codet &code=to_code_block(to_code(*it));
63+
codet &code = to_code_block(to_code(op));
6664

6765
// look at the catch operand
6866
assert(!code.operands().empty());
@@ -105,7 +103,7 @@ void cpp_typecheckt::typecheck_try_catch(codet &code)
105103
const typet &type=code_decl.op0().type();
106104

107105
// annotate exception ID
108-
it->set(ID_exception_id, cpp_exception_id(type, *this));
106+
op.set(ID_exception_id, cpp_exception_id(type, *this));
109107
}
110108
}
111109
}

src/cpp/cpp_typecheck_fargs.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ Author: Daniel Kroening, [email protected]
2121

2222
bool cpp_typecheck_fargst::has_class_type() const
2323
{
24-
for(exprt::operandst::const_iterator it=operands.begin();
25-
it!=operands.end();
26-
it++)
24+
for(const auto &op : operands)
2725
{
28-
if(it->type().id()==ID_struct)
26+
if(op.type().id() == ID_struct)
2927
return true;
3028
}
3129

@@ -36,12 +34,7 @@ void cpp_typecheck_fargst::build(
3634
const side_effect_expr_function_callt &function_call)
3735
{
3836
in_use=true;
39-
40-
operands.clear();
41-
operands.reserve(function_call.op1().operands().size());
42-
43-
for(std::size_t i=0; i<function_call.op1().operands().size(); i++)
44-
operands.push_back(function_call.op1().operands()[i]);
37+
operands = function_call.op1().operands();
4538
}
4639

4740
bool cpp_typecheck_fargst::match(

0 commit comments

Comments
 (0)