Skip to content

Commit b03abfc

Browse files
author
Daniel Kroening
authored
Merge pull request #3721 from diffblue/ansi-c-ranged-for
ansi-c: use ranged for
2 parents abaf5a0 + 149dc1f commit b03abfc

File tree

5 files changed

+53
-99
lines changed

5 files changed

+53
-99
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: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,14 @@ void c_typecheck_baset::typecheck_new_symbol(symbolt &symbol)
143143
{
144144
if(symbol.value.is_not_nil() &&
145145
!symbol.is_macro)
146+
{
146147
typecheck_function_body(symbol);
148+
}
147149
else
148150
{
149151
// we don't need the identifiers
150-
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());
152+
for(auto &parameter : to_code_type(symbol.type).parameters())
153+
parameter.set_identifier(irep_idt());
156154
}
157155
}
158156
else if(!symbol.is_macro)
@@ -361,13 +359,9 @@ void c_typecheck_baset::typecheck_redefinition_non_type(
361359
old_symbol.is_file_local=new_symbol.is_file_local;
362360

363361
// 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++)
362+
for(const auto &old_parameter : old_ct.parameters())
369363
{
370-
const irep_idt &identifier=p_it->get_identifier();
364+
const irep_idt &identifier = old_parameter.get_identifier();
371365

372366
symbol_tablet::symbolst::const_iterator p_s_it=
373367
symbol_table.symbols.find(identifier);
@@ -530,31 +524,27 @@ void c_typecheck_baset::typecheck_function_body(symbolt &symbol)
530524
unsigned anon_counter=0;
531525

532526
// 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++)
527+
for(auto &p : code_type.parameters())
538528
{
539529
// may be anonymous
540-
if(p_it->get_base_name().empty())
530+
if(p.get_base_name().empty())
541531
{
542532
irep_idt base_name="#anon"+std::to_string(anon_counter++);
543-
p_it->set_base_name(base_name);
533+
p.set_base_name(base_name);
544534
}
545535

546536
// produce identifier
547-
irep_idt base_name=p_it->get_base_name();
537+
irep_idt base_name = p.get_base_name();
548538
irep_idt identifier=id2string(symbol.name)+"::"+id2string(base_name);
549539

550-
p_it->set_identifier(identifier);
540+
p.set_identifier(identifier);
551541

552542
parameter_symbolt p_symbol;
553543

554-
p_symbol.type=p_it->type();
544+
p_symbol.type = p.type();
555545
p_symbol.name=identifier;
556546
p_symbol.base_name=base_name;
557-
p_symbol.location=p_it->source_location();
547+
p_symbol.location = p.source_location();
558548

559549
symbolt *new_p_symbol;
560550
move_symbol(p_symbol, new_p_symbol);
@@ -564,13 +554,12 @@ void c_typecheck_baset::typecheck_function_body(symbolt &symbol)
564554
typecheck_code(to_code(symbol.value));
565555

566556
// check the labels
567-
for(std::map<irep_idt, source_locationt>::const_iterator
568-
it=labels_used.begin(); it!=labels_used.end(); it++)
557+
for(const auto &label : labels_used)
569558
{
570-
if(labels_defined.find(it->first)==labels_defined.end())
559+
if(labels_defined.find(label.first) == labels_defined.end())
571560
{
572-
error().source_location=it->second;
573-
error() << "branching label `" << it->first
561+
error().source_location = label.second;
562+
error() << "branching label `" << label.first
574563
<< "' is not defined in function" << eom;
575564
throw 0;
576565
}
@@ -626,12 +615,9 @@ void c_typecheck_baset::apply_asm_label(
626615
{
627616
const code_typet &code_type=to_code_type(symbol.type);
628617

629-
for(code_typet::parameterst::const_iterator
630-
p_it=code_type.parameters().begin();
631-
p_it!=code_type.parameters().end();
632-
++p_it)
618+
for(const auto &p : code_type.parameters())
633619
{
634-
const irep_idt &p_bn=p_it->get_base_name();
620+
const irep_idt &p_bn = p.get_base_name();
635621
if(p_bn.empty())
636622
continue;
637623

@@ -678,12 +664,9 @@ void c_typecheck_baset::typecheck_declaration(
678664
}
679665

680666
// 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++)
667+
for(auto &declarator : declaration.declarators())
685668
{
686-
c_storage_spect full_spec(declaration.full_type(*d_it));
669+
c_storage_spect full_spec(declaration.full_type(declarator));
687670
full_spec|=c_storage_spec;
688671

689672
declaration.set_is_inline(full_spec.is_inline);
@@ -696,7 +679,7 @@ void c_typecheck_baset::typecheck_declaration(
696679
declaration.set_is_used(full_spec.is_used);
697680

698681
symbolt symbol;
699-
declaration.to_symbol(*d_it, symbol);
682+
declaration.to_symbol(declarator, symbol);
700683
current_symbol=symbol;
701684

702685
// now check other half of type
@@ -749,7 +732,7 @@ void c_typecheck_baset::typecheck_declaration(
749732
}
750733

751734
irep_idt identifier=symbol.name;
752-
d_it->set_name(identifier);
735+
declarator.set_name(identifier);
753736

754737
typecheck_symbol(symbol);
755738

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)