Skip to content

Commit 8d711a7

Browse files
author
Daniel Kroening
committed
ansi-c: use ranged for
For better readability!
1 parent 5419894 commit 8d711a7

File tree

5 files changed

+51
-98
lines changed

5 files changed

+51
-98
lines changed

src/ansi-c/ansi_c_typecheck.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ Author: Daniel Kroening, [email protected]
1414
void ansi_c_typecheckt::typecheck()
1515
{
1616
start_typecheck_code();
17-
for(ansi_c_parse_treet::itemst::iterator
18-
it=parse_tree.items.begin();
19-
it!=parse_tree.items.end();
20-
it++)
21-
{
22-
typecheck_declaration(*it);
23-
}
17+
18+
for(auto &item : parse_tree.items)
19+
typecheck_declaration(item);
2420
}
2521

2622
bool ansi_c_typecheck(

src/ansi-c/c_typecheck_base.cpp

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,8 @@ void c_typecheck_baset::typecheck_new_symbol(symbolt &symbol)
148148
{
149149
// we don't need the identifiers
150150
code_typet &code_type=to_code_type(symbol.type);
151-
for(code_typet::parameterst::iterator
152-
it=code_type.parameters().begin();
153-
it!=code_type.parameters().end();
154-
it++)
155-
it->set_identifier(irep_idt());
151+
for(auto &parameter : code_type.parameters())
152+
parameter.set_identifier(irep_idt());
156153
}
157154
}
158155
else if(!symbol.is_macro)
@@ -361,13 +358,9 @@ void c_typecheck_baset::typecheck_redefinition_non_type(
361358
old_symbol.is_file_local=new_symbol.is_file_local;
362359

363360
// remove parameter declarations to avoid conflicts
364-
const code_typet::parameterst &old_p=old_ct.parameters();
365-
for(code_typet::parameterst::const_iterator
366-
p_it=old_p.begin();
367-
p_it!=old_p.end();
368-
p_it++)
361+
for(const auto &old_parameter : old_ct.parameters())
369362
{
370-
const irep_idt &identifier=p_it->get_identifier();
363+
const irep_idt &identifier = old_parameter.get_identifier();
371364

372365
symbol_tablet::symbolst::const_iterator p_s_it=
373366
symbol_table.symbols.find(identifier);
@@ -530,31 +523,27 @@ void c_typecheck_baset::typecheck_function_body(symbolt &symbol)
530523
unsigned anon_counter=0;
531524

532525
// Add the parameter declarations into the symbol table.
533-
code_typet::parameterst &parameters=code_type.parameters();
534-
for(code_typet::parameterst::iterator
535-
p_it=parameters.begin();
536-
p_it!=parameters.end();
537-
p_it++)
526+
for(auto &p : code_type.parameters())
538527
{
539528
// may be anonymous
540-
if(p_it->get_base_name().empty())
529+
if(p.get_base_name().empty())
541530
{
542531
irep_idt base_name="#anon"+std::to_string(anon_counter++);
543-
p_it->set_base_name(base_name);
532+
p.set_base_name(base_name);
544533
}
545534

546535
// produce identifier
547-
irep_idt base_name=p_it->get_base_name();
536+
irep_idt base_name = p.get_base_name();
548537
irep_idt identifier=id2string(symbol.name)+"::"+id2string(base_name);
549538

550-
p_it->set_identifier(identifier);
539+
p.set_identifier(identifier);
551540

552541
parameter_symbolt p_symbol;
553542

554-
p_symbol.type=p_it->type();
543+
p_symbol.type = p.type();
555544
p_symbol.name=identifier;
556545
p_symbol.base_name=base_name;
557-
p_symbol.location=p_it->source_location();
546+
p_symbol.location = p.source_location();
558547

559548
symbolt *new_p_symbol;
560549
move_symbol(p_symbol, new_p_symbol);
@@ -564,13 +553,12 @@ void c_typecheck_baset::typecheck_function_body(symbolt &symbol)
564553
typecheck_code(to_code(symbol.value));
565554

566555
// check the labels
567-
for(std::map<irep_idt, source_locationt>::const_iterator
568-
it=labels_used.begin(); it!=labels_used.end(); it++)
556+
for(const auto &label : labels_used)
569557
{
570-
if(labels_defined.find(it->first)==labels_defined.end())
558+
if(labels_defined.find(label.first) == labels_defined.end())
571559
{
572-
error().source_location=it->second;
573-
error() << "branching label `" << it->first
560+
error().source_location = label.second;
561+
error() << "branching label `" << label.first
574562
<< "' is not defined in function" << eom;
575563
throw 0;
576564
}
@@ -626,12 +614,9 @@ void c_typecheck_baset::apply_asm_label(
626614
{
627615
const code_typet &code_type=to_code_type(symbol.type);
628616

629-
for(code_typet::parameterst::const_iterator
630-
p_it=code_type.parameters().begin();
631-
p_it!=code_type.parameters().end();
632-
++p_it)
617+
for(const auto &p : code_type.parameters())
633618
{
634-
const irep_idt &p_bn=p_it->get_base_name();
619+
const irep_idt &p_bn = p.get_base_name();
635620
if(p_bn.empty())
636621
continue;
637622

@@ -678,12 +663,9 @@ void c_typecheck_baset::typecheck_declaration(
678663
}
679664

680665
// Now do declarators, if any.
681-
for(ansi_c_declarationt::declaratorst::iterator
682-
d_it=declaration.declarators().begin();
683-
d_it!=declaration.declarators().end();
684-
d_it++)
666+
for(auto &declarator : declaration.declarators())
685667
{
686-
c_storage_spect full_spec(declaration.full_type(*d_it));
668+
c_storage_spect full_spec(declaration.full_type(declarator));
687669
full_spec|=c_storage_spec;
688670

689671
declaration.set_is_inline(full_spec.is_inline);
@@ -696,7 +678,7 @@ void c_typecheck_baset::typecheck_declaration(
696678
declaration.set_is_used(full_spec.is_used);
697679

698680
symbolt symbol;
699-
declaration.to_symbol(*d_it, symbol);
681+
declaration.to_symbol(declarator, symbol);
700682
current_symbol=symbol;
701683

702684
// now check other half of type
@@ -749,7 +731,7 @@ void c_typecheck_baset::typecheck_declaration(
749731
}
750732

751733
irep_idt identifier=symbol.name;
752-
d_it->set_name(identifier);
734+
declarator.set_name(identifier);
753735

754736
typecheck_symbol(symbol);
755737

src/ansi-c/c_typecheck_code.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,9 @@ void c_typecheck_baset::typecheck_decl(codet &code)
259259

260260
// iterate over declarators
261261

262-
for(ansi_c_declarationt::declaratorst::const_iterator
263-
d_it=declaration.declarators().begin();
264-
d_it!=declaration.declarators().end();
265-
d_it++)
262+
for(const auto &d : declaration.declarators())
266263
{
267-
irep_idt identifier=d_it->get_name();
264+
irep_idt identifier = d.get_name();
268265

269266
// look it up
270267
symbol_tablet::symbolst::const_iterator s_it=

src/ansi-c/c_typecheck_typecast.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,22 @@ void c_typecheck_baset::implicit_typecast(
2020

2121
c_typecast.implicit_typecast(expr, dest_type);
2222

23-
for(std::list<std::string>::const_iterator
24-
it=c_typecast.errors.begin();
25-
it!=c_typecast.errors.end();
26-
it++)
23+
for(const auto &tc_error : c_typecast.errors)
2724
{
2825
error().source_location=expr.find_source_location();
2926
error() << "in expression `" << to_string(expr) << "':\n"
30-
<< "conversion from `"
31-
<< to_string(src_type) << "' to `"
32-
<< to_string(dest_type) << "': "
33-
<< *it << eom;
27+
<< "conversion from `" << to_string(src_type) << "' to `"
28+
<< to_string(dest_type) << "': " << tc_error << eom;
3429
}
3530

3631
if(!c_typecast.errors.empty())
3732
throw 0; // give up
3833

39-
for(std::list<std::string>::const_iterator
40-
it=c_typecast.warnings.begin();
41-
it!=c_typecast.warnings.end();
42-
it++)
34+
for(const auto &tc_warning : c_typecast.warnings)
4335
{
4436
warning().source_location=expr.find_source_location();
45-
warning() << "warning: conversion from `"
46-
<< to_string(src_type)
47-
<< "' to `"
48-
<< to_string(dest_type)
49-
<< "': " << *it << eom;
37+
warning() << "warning: conversion from `" << to_string(src_type) << "' to `"
38+
<< to_string(dest_type) << "': " << tc_warning << eom;
5039
}
5140
}
5241

src/ansi-c/expr2c.cpp

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,11 @@ static std::string clean_identifier(const irep_idt &id)
9292
dest.erase(0, c_pos+2);
9393
else if(c_pos!=std::string::npos)
9494
{
95-
for(std::string::iterator it2=dest.begin();
96-
it2!=dest.end();
97-
++it2)
98-
if(*it2==':')
99-
*it2='$';
100-
else if(*it2=='-')
101-
*it2='_';
95+
for(char &ch : dest)
96+
if(ch == ':')
97+
ch = '$';
98+
else if(ch == '-')
99+
ch = '_';
102100
}
103101

104102
// rewrite . as used in ELF section names
@@ -113,20 +111,17 @@ void expr2ct::get_shorthands(const exprt &expr)
113111
find_symbols(expr, symbols);
114112

115113
// avoid renaming parameters, if possible
116-
for(find_symbols_sett::const_iterator
117-
it=symbols.begin();
118-
it!=symbols.end();
119-
it++)
114+
for(const auto &symbol_id : symbols)
120115
{
121116
const symbolt *symbol;
122-
bool is_param=!ns.lookup(*it, symbol) && symbol->is_parameter;
117+
bool is_param = !ns.lookup(symbol_id, symbol) && symbol->is_parameter;
123118

124119
if(!is_param)
125120
continue;
126121

127-
irep_idt sh=id_shorthand(*it);
122+
irep_idt sh = id_shorthand(symbol_id);
128123

129-
std::string func = id2string(*it);
124+
std::string func = id2string(symbol_id);
130125
func = func.substr(0, func.rfind("::"));
131126

132127
// if there is a global symbol of the same name as the shorthand (even if
@@ -137,19 +132,16 @@ void expr2ct::get_shorthands(const exprt &expr)
137132

138133
ns_collision[func].insert(sh);
139134

140-
if(!shorthands.insert(std::make_pair(*it, sh)).second)
135+
if(!shorthands.insert(std::make_pair(symbol_id, sh)).second)
141136
UNREACHABLE;
142137
}
143138

144-
for(find_symbols_sett::const_iterator
145-
it=symbols.begin();
146-
it!=symbols.end();
147-
it++)
139+
for(const auto &symbol_id : symbols)
148140
{
149-
if(shorthands.find(*it)!=shorthands.end())
141+
if(shorthands.find(symbol_id) != shorthands.end())
150142
continue;
151143

152-
irep_idt sh=id_shorthand(*it);
144+
irep_idt sh = id_shorthand(symbol_id);
153145

154146
bool has_collision=
155147
ns_collision[irep_idt()].find(sh)!=
@@ -168,16 +160,16 @@ void expr2ct::get_shorthands(const exprt &expr)
168160
irep_idt func;
169161

170162
const symbolt *symbol;
171-
if(!ns.lookup(*it, symbol))
163+
if(!ns.lookup(symbol_id, symbol))
172164
func=symbol->location.get_function();
173165

174166
has_collision=!ns_collision[func].insert(sh).second;
175167
}
176168

177169
if(has_collision)
178-
sh=clean_identifier(*it);
170+
sh = clean_identifier(symbol_id);
179171

180-
shorthands.insert(std::make_pair(*it, sh));
172+
shorthands.insert(std::make_pair(symbol_id, sh));
181173
}
182174
}
183175

@@ -1786,13 +1778,10 @@ std::string expr2ct::convert_constant(
17861778
const c_enum_typet::memberst &members=
17871779
to_c_enum_type(c_enum_type).members();
17881780

1789-
for(c_enum_typet::memberst::const_iterator
1790-
it=members.begin();
1791-
it!=members.end();
1792-
it++)
1781+
for(const auto &member : members)
17931782
{
1794-
if(it->get_value()==int_value_string)
1795-
return "/*enum*/"+id2string(it->get_base_name());
1783+
if(member.get_value() == int_value_string)
1784+
return "/*enum*/" + id2string(member.get_base_name());
17961785
}
17971786

17981787
// failed...

0 commit comments

Comments
 (0)