Skip to content

Commit 02b517f

Browse files
author
Daniel Kroening
committed
C++ front-end: use ranged for
1 parent 2e84af4 commit 02b517f

10 files changed

+119
-229
lines changed

src/cpp/cpp_constructor.cpp

+8-17
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=
@@ -234,11 +228,8 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
234228
cpp_namet(constructor_name, source_location).as_expr();
235229
function_call.arguments().reserve(operands_tc.size());
236230

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);
231+
for(auto &op : operands_tc)
232+
function_call.op1().copy_to_operands(op);
242233

243234
typecheck_side_effect_function_call(function_call);
244235
assert(function_call.get(ID_statement)==ID_temporary_object);

src/cpp/cpp_declarator_converter.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,9 @@ symbolt &cpp_declarator_convertert::convert_new_symbol(
501501
cpp_typecheck.cpp_scopes.current_scope().lookup(
502502
base_name, cpp_scopet::SCOPE_ONLY, id_set);
503503

504-
for(cpp_scopest::id_sett::const_iterator
505-
id_it=id_set.begin();
506-
id_it!=id_set.end();
507-
id_it++)
504+
for(const auto &id_ptr : id_set)
508505
{
509-
const cpp_idt &id=**id_it;
506+
const cpp_idt &id = *id_ptr;
510507
// the name is already in the scope
511508
// this is ok if they belong to different categories
512509

src/cpp/cpp_id.cpp

+6-14
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ 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
}
@@ -63,22 +61,16 @@ void cpp_idt::print_fields(std::ostream &out, unsigned indent) const
6361
for(unsigned i=0; i<indent; i++) out << ' ';
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
{
7166
for(unsigned i=0; i<indent; i++) out << ' ';
72-
out << " secondary_scope=" << (*it)->identifier << '\n';
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
{
8072
for(unsigned i=0; i<indent; i++) out << ' ';
81-
out << " using_scope=" << (*it)->identifier << '\n';
73+
out << " using_scope=" << s->identifier << '\n';
8274
}
8375

8476
for(unsigned i=0; i<indent; i++) out << ' ';

src/cpp/cpp_instantiate_template.cpp

+7-14
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

+6-15
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

+8-20
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,9 @@ void cpp_scopet::lookup(
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(
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!
@@ -130,12 +124,9 @@ void cpp_scopet::lookup(
130124
return; // done
131125

132126
// using scopes
133-
for(scope_listt::iterator
134-
it=using_scopes.begin();
135-
it!=using_scopes.end();
136-
it++)
127+
for(const auto &s_ptr : using_scopes)
137128
{
138-
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
129+
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
139130

140131
// Recursive call.
141132
// Note the different kind!
@@ -146,12 +137,9 @@ void cpp_scopet::lookup(
146137
return; // done, upwards scopes are hidden
147138

148139
// secondary scopes
149-
for(scope_listt::iterator
150-
it=secondary_scopes.begin();
151-
it!=secondary_scopes.end();
152-
it++)
140+
for(const auto &s_ptr : secondary_scopes)
153141
{
154-
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
142+
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);
155143

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

src/cpp/cpp_typecheck_code.cpp

+7-9
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

+4-6
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

@@ -40,8 +38,8 @@ void cpp_typecheck_fargst::build(
4038
operands.clear();
4139
operands.reserve(function_call.op1().operands().size());
4240

43-
for(std::size_t i=0; i<function_call.op1().operands().size(); i++)
44-
operands.push_back(function_call.op1().operands()[i]);
41+
for(const auto &op : function_call.op1().operands())
42+
operands.push_back(op);
4543
}
4644

4745
bool cpp_typecheck_fargst::match(

0 commit comments

Comments
 (0)