Skip to content

ansi-c: use ranged for #3721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/ansi-c/ansi_c_typecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ Author: Daniel Kroening, [email protected]
void ansi_c_typecheckt::typecheck()
{
start_typecheck_code();
for(ansi_c_parse_treet::itemst::iterator
it=parse_tree.items.begin();
it!=parse_tree.items.end();
it++)
{
typecheck_declaration(*it);
}

for(auto &item : parse_tree.items)
typecheck_declaration(item);
}

bool ansi_c_typecheck(
Expand Down
63 changes: 23 additions & 40 deletions src/ansi-c/c_typecheck_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,14 @@ void c_typecheck_baset::typecheck_new_symbol(symbolt &symbol)
{
if(symbol.value.is_not_nil() &&
!symbol.is_macro)
{
typecheck_function_body(symbol);
}
else
{
// we don't need the identifiers
code_typet &code_type=to_code_type(symbol.type);
for(code_typet::parameterst::iterator
it=code_type.parameters().begin();
it!=code_type.parameters().end();
it++)
it->set_identifier(irep_idt());
for(auto &parameter : to_code_type(symbol.type).parameters())
parameter.set_identifier(irep_idt());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a second commit in this PR maybe get rid of the code_type named temporary? And the block around these lines?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}
else if(!symbol.is_macro)
Expand Down Expand Up @@ -361,13 +359,9 @@ void c_typecheck_baset::typecheck_redefinition_non_type(
old_symbol.is_file_local=new_symbol.is_file_local;

// remove parameter declarations to avoid conflicts
const code_typet::parameterst &old_p=old_ct.parameters();
for(code_typet::parameterst::const_iterator
p_it=old_p.begin();
p_it!=old_p.end();
p_it++)
for(const auto &old_parameter : old_ct.parameters())
{
const irep_idt &identifier=p_it->get_identifier();
const irep_idt &identifier = old_parameter.get_identifier();

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

// Add the parameter declarations into the symbol table.
code_typet::parameterst &parameters=code_type.parameters();
for(code_typet::parameterst::iterator
p_it=parameters.begin();
p_it!=parameters.end();
p_it++)
for(auto &p : code_type.parameters())
{
// may be anonymous
if(p_it->get_base_name().empty())
if(p.get_base_name().empty())
{
irep_idt base_name="#anon"+std::to_string(anon_counter++);
p_it->set_base_name(base_name);
p.set_base_name(base_name);
}

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

p_it->set_identifier(identifier);
p.set_identifier(identifier);

parameter_symbolt p_symbol;

p_symbol.type=p_it->type();
p_symbol.type = p.type();
p_symbol.name=identifier;
p_symbol.base_name=base_name;
p_symbol.location=p_it->source_location();
p_symbol.location = p.source_location();

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

// check the labels
for(std::map<irep_idt, source_locationt>::const_iterator
it=labels_used.begin(); it!=labels_used.end(); it++)
for(const auto &label : labels_used)
{
if(labels_defined.find(it->first)==labels_defined.end())
if(labels_defined.find(label.first) == labels_defined.end())
{
error().source_location=it->second;
error() << "branching label `" << it->first
error().source_location = label.second;
error() << "branching label `" << label.first
<< "' is not defined in function" << eom;
throw 0;
}
Expand Down Expand Up @@ -626,12 +615,9 @@ void c_typecheck_baset::apply_asm_label(
{
const code_typet &code_type=to_code_type(symbol.type);

for(code_typet::parameterst::const_iterator
p_it=code_type.parameters().begin();
p_it!=code_type.parameters().end();
++p_it)
for(const auto &p : code_type.parameters())
{
const irep_idt &p_bn=p_it->get_base_name();
const irep_idt &p_bn = p.get_base_name();
if(p_bn.empty())
continue;

Expand Down Expand Up @@ -678,12 +664,9 @@ void c_typecheck_baset::typecheck_declaration(
}

// Now do declarators, if any.
for(ansi_c_declarationt::declaratorst::iterator
d_it=declaration.declarators().begin();
d_it!=declaration.declarators().end();
d_it++)
for(auto &declarator : declaration.declarators())
{
c_storage_spect full_spec(declaration.full_type(*d_it));
c_storage_spect full_spec(declaration.full_type(declarator));
full_spec|=c_storage_spec;

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

symbolt symbol;
declaration.to_symbol(*d_it, symbol);
declaration.to_symbol(declarator, symbol);
current_symbol=symbol;

// now check other half of type
Expand Down Expand Up @@ -749,7 +732,7 @@ void c_typecheck_baset::typecheck_declaration(
}

irep_idt identifier=symbol.name;
d_it->set_name(identifier);
declarator.set_name(identifier);

typecheck_symbol(symbol);

Expand Down
7 changes: 2 additions & 5 deletions src/ansi-c/c_typecheck_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,9 @@ void c_typecheck_baset::typecheck_decl(codet &code)

// iterate over declarators

for(ansi_c_declarationt::declaratorst::const_iterator
d_it=declaration.declarators().begin();
d_it!=declaration.declarators().end();
d_it++)
for(const auto &d : declaration.declarators())
{
irep_idt identifier=d_it->get_name();
irep_idt identifier = d.get_name();

// look it up
symbol_tablet::symbolst::const_iterator s_it=
Expand Down
23 changes: 6 additions & 17 deletions src/ansi-c/c_typecheck_typecast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,22 @@ void c_typecheck_baset::implicit_typecast(

c_typecast.implicit_typecast(expr, dest_type);

for(std::list<std::string>::const_iterator
it=c_typecast.errors.begin();
it!=c_typecast.errors.end();
it++)
for(const auto &tc_error : c_typecast.errors)
{
error().source_location=expr.find_source_location();
error() << "in expression `" << to_string(expr) << "':\n"
<< "conversion from `"
<< to_string(src_type) << "' to `"
<< to_string(dest_type) << "': "
<< *it << eom;
<< "conversion from `" << to_string(src_type) << "' to `"
<< to_string(dest_type) << "': " << tc_error << eom;
}

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

for(std::list<std::string>::const_iterator
it=c_typecast.warnings.begin();
it!=c_typecast.warnings.end();
it++)
for(const auto &tc_warning : c_typecast.warnings)
{
warning().source_location=expr.find_source_location();
warning() << "warning: conversion from `"
<< to_string(src_type)
<< "' to `"
<< to_string(dest_type)
<< "': " << *it << eom;
warning() << "warning: conversion from `" << to_string(src_type) << "' to `"
<< to_string(dest_type) << "': " << tc_warning << eom;
}
}

Expand Down
49 changes: 19 additions & 30 deletions src/ansi-c/expr2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,11 @@ static std::string clean_identifier(const irep_idt &id)
dest.erase(0, c_pos+2);
else if(c_pos!=std::string::npos)
{
for(std::string::iterator it2=dest.begin();
it2!=dest.end();
++it2)
if(*it2==':')
*it2='$';
else if(*it2=='-')
*it2='_';
for(char &ch : dest)
if(ch == ':')
ch = '$';
else if(ch == '-')
ch = '_';
}

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

// avoid renaming parameters, if possible
for(find_symbols_sett::const_iterator
it=symbols.begin();
it!=symbols.end();
it++)
for(const auto &symbol_id : symbols)
{
const symbolt *symbol;
bool is_param=!ns.lookup(*it, symbol) && symbol->is_parameter;
bool is_param = !ns.lookup(symbol_id, symbol) && symbol->is_parameter;

if(!is_param)
continue;

irep_idt sh=id_shorthand(*it);
irep_idt sh = id_shorthand(symbol_id);

std::string func = id2string(*it);
std::string func = id2string(symbol_id);
func = func.substr(0, func.rfind("::"));

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

ns_collision[func].insert(sh);

if(!shorthands.insert(std::make_pair(*it, sh)).second)
if(!shorthands.insert(std::make_pair(symbol_id, sh)).second)
UNREACHABLE;
}

for(find_symbols_sett::const_iterator
it=symbols.begin();
it!=symbols.end();
it++)
for(const auto &symbol_id : symbols)
{
if(shorthands.find(*it)!=shorthands.end())
if(shorthands.find(symbol_id) != shorthands.end())
continue;

irep_idt sh=id_shorthand(*it);
irep_idt sh = id_shorthand(symbol_id);

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

const symbolt *symbol;
if(!ns.lookup(*it, symbol))
if(!ns.lookup(symbol_id, symbol))
func=symbol->location.get_function();

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

if(has_collision)
sh=clean_identifier(*it);
sh = clean_identifier(symbol_id);

shorthands.insert(std::make_pair(*it, sh));
shorthands.insert(std::make_pair(symbol_id, sh));
}
}

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

for(c_enum_typet::memberst::const_iterator
it=members.begin();
it!=members.end();
it++)
for(const auto &member : members)
{
if(it->get_value()==int_value_string)
return "/*enum*/"+id2string(it->get_base_name());
if(member.get_value() == int_value_string)
return "/*enum*/" + id2string(member.get_base_name());
}

// failed...
Expand Down