Skip to content

Use ranged for to iterate over components #3033

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
Sep 24, 2018
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
12 changes: 3 additions & 9 deletions jbmc/src/java_bytecode/expr2java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,9 @@ std::string expr2javat::convert_struct(
bool first=true;
size_t last_size=0;

for(struct_typet::componentst::const_iterator
c_it=components.begin();
c_it!=components.end();
c_it++)
for(const auto &c : components)
{
if(c_it->type().id()==ID_code)
{
}
else
if(c.type().id() != ID_code)
{
std::string tmp=convert(*o_it);
std::string sep;
Expand All @@ -155,7 +149,7 @@ std::string expr2javat::convert_struct(

dest+=sep;
dest+='.';
dest+=c_it->get_string(ID_pretty_name);
dest += id2string(c.get_pretty_name());
dest+='=';
dest+=tmp;
}
Expand Down
9 changes: 2 additions & 7 deletions src/ansi-c/c_typecheck_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,8 @@ bool c_typecheck_baset::is_complete_type(const typet &type) const
}
else if(type.id()==ID_struct || type.id()==ID_union)
{
const struct_union_typet::componentst &components=
to_struct_union_type(type).components();
for(struct_union_typet::componentst::const_iterator
it=components.begin();
it!=components.end();
it++)
if(!is_complete_type(it->type()))
for(const auto &c : to_struct_union_type(type).components())
if(!is_complete_type(c.type()))
return false;
}
else if(type.id()==ID_vector)
Expand Down
36 changes: 11 additions & 25 deletions src/ansi-c/c_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,28 +619,20 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
else
{
// maybe anonymous?

const struct_union_typet::componentst &components=
struct_union_type.components();

bool found2=false;

for(struct_union_typet::componentst::const_iterator
c_it=components.begin();
c_it!=components.end();
c_it++)
for(const auto &c : struct_union_type.components())
{
if(c_it->get_anonymous() &&
(follow(c_it->type()).id()==ID_struct ||
follow(c_it->type()).id()==ID_union))
if(
c.get_anonymous() && (follow(c.type()).id() == ID_struct ||
follow(c.type()).id() == ID_union))
{
if(has_component_rec(c_it->type(), component_name, *this))
if(has_component_rec(c.type(), component_name, *this))
{
if(type.id()==ID_struct)
{
exprt o=
member_offset_expr(
to_struct_type(type), c_it->get_name(), *this);
exprt o = member_offset_expr(
to_struct_type(type), c.get_name(), *this);

if(o.is_nil())
{
Expand All @@ -656,7 +648,7 @@ void c_typecheck_baset::typecheck_expr_builtin_offsetof(exprt &expr)
result=plus_exprt(result, o);
}

typet tmp=follow(c_it->type());
typet tmp = follow(c.type());
type=tmp;
assert(type.id()==ID_union || type.id()==ID_struct);
found2=true;
Expand Down Expand Up @@ -1097,21 +1089,15 @@ void c_typecheck_baset::typecheck_expr_typecast(exprt &expr)
op.make_typecast(signed_int_type());

// we need to find a member with the right type
const union_typet &union_type=to_union_type(expr_type);
const union_typet::componentst &components=union_type.components();

for(union_typet::componentst::const_iterator
it=components.begin();
it!=components.end();
it++)
for(const auto &c : to_union_type(expr_type).components())
{
if(base_type_eq(it->type(), op.type(), *this))
if(base_type_eq(c.type(), op.type(), *this))
{
// found! build union constructor
union_exprt union_expr(expr.type());
union_expr.add_source_location()=expr.source_location();
union_expr.op()=op;
union_expr.set_component_name(it->get_name());
union_expr.set_component_name(c.get_name());
expr=union_expr;
expr.set(ID_C_lvalue, true);
return;
Expand Down
34 changes: 14 additions & 20 deletions src/ansi-c/c_typecheck_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,11 @@ void c_typecheck_baset::designator_enter(
// only a top-level struct may end with a variable-length array
entry.vla_permitted=designator.empty();

for(struct_typet::componentst::const_iterator
it=struct_type.components().begin();
it!=struct_type.components().end();
++it)
for(const auto &c : struct_type.components())
{
if(it->type().id()!=ID_code &&
!it->get_is_padding())
if(c.type().id() != ID_code && !c.get_is_padding())
{
entry.subtype=it->type();
entry.subtype = c.type();
break;
}

Expand Down Expand Up @@ -758,38 +754,36 @@ designatort c_typecheck_baset::make_designator(
do
{
repeat=false;
unsigned number=0;
std::size_t number = 0;
const struct_union_typet::componentst &components=
to_struct_union_type(follow(tmp_type)).components();

for(struct_union_typet::componentst::const_iterator
c_it=components.begin();
c_it!=components.end();
c_it++, number++)
for(const auto &c : components)
{
if(c_it->get_name()==component_name)
if(c.get_name() == component_name)
{
// done!
entry.index=number;
entry.size=components.size();
entry.subtype=components[entry.index].type();
entry.subtype = c.type();
entry.type=tmp_type;
}
else if(c_it->get_anonymous() &&
(follow(c_it->type()).id()==ID_struct ||
follow(c_it->type()).id()==ID_union) &&
has_component_rec(
c_it->type(), component_name, *this))
else if(
c.get_anonymous() && (follow(c.type()).id() == ID_struct ||
follow(c.type()).id() == ID_union) &&
has_component_rec(c.type(), component_name, *this))
{
entry.index=number;
entry.size=components.size();
entry.subtype=c_it->type();
entry.subtype = c.type();
entry.type=tmp_type;
tmp_type=entry.subtype;
designator.push_entry(entry);
found=repeat=true;
break;
}

++number;
}
}
while(repeat);
Expand Down
10 changes: 2 additions & 8 deletions src/ansi-c/padding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,12 @@ mp_integer alignment(const typet &type, const namespacet &ns)
result=alignment(type.subtype(), ns);
else if(type.id()==ID_struct || type.id()==ID_union)
{
const struct_union_typet::componentst &components=
to_struct_union_type(type).components();

result=1;

// get the max
// (should really be the smallest common denominator)
for(struct_union_typet::componentst::const_iterator
it=components.begin();
it!=components.end();
it++)
result=std::max(result, alignment(it->type(), ns));
for(const auto &c : to_struct_union_type(type).components())
result = std::max(result, alignment(c.type(), ns));
}
else if(type.id()==ID_unsignedbv ||
type.id()==ID_signedbv ||
Expand Down
17 changes: 8 additions & 9 deletions src/ansi-c/type2name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,17 @@ static std::string type2name(
result+="ST";
if(type.id()==ID_union)
result+="UN";
const struct_union_typet &t=to_struct_union_type(type);
const struct_union_typet::componentst &components = t.components();
result+='[';
for(struct_union_typet::componentst::const_iterator
it=components.begin();
it!=components.end();
it++)
bool first = true;
for(const auto &c : to_struct_union_type(type).components())
{
if(it!=components.begin())
if(!first)
result+='|';
result+=type2name(it->type(), ns, symbol_number);
irep_idt component_name = it->get_name();
else
first = false;

result += type2name(c.type(), ns, symbol_number);
const irep_idt &component_name = c.get_name();
CHECK_RETURN(!component_name.empty());
result+="'"+id2string(component_name)+"'";
}
Expand Down
10 changes: 1 addition & 9 deletions src/cpp/cpp_typecheck_constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,17 +789,9 @@ void cpp_typecheckt::full_member_initialization(
/// \return return true if a copy constructor is found
bool cpp_typecheckt::find_cpctor(const symbolt &symbol) const
{
const struct_typet &struct_type=to_struct_type(symbol.type);
const struct_typet::componentst &components=struct_type.components();

for(struct_typet::componentst::const_iterator
cit=components.begin();
cit!=components.end();
cit++)
for(const auto &component : to_struct_type(symbol.type).components())
{
// Skip non-ctor
const struct_typet::componentt &component=*cit;

if(component.type().id()!=ID_code ||
to_code_type(component.type()).return_type().id() !=ID_constructor)
continue;
Expand Down
21 changes: 7 additions & 14 deletions src/goto-instrument/dump_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,8 @@ void dump_ct::convert_compound(

std::stringstream struct_body;

for(struct_union_typet::componentst::const_iterator
it=type.components().begin();
it!=type.components().end();
it++)
for(const auto &comp : type.components())
{
const struct_typet::componentt &comp=*it;
const typet &comp_type=ns.follow(comp.type());

if(comp_type.id()==ID_code ||
Expand Down Expand Up @@ -1236,18 +1232,15 @@ void dump_ct::cleanup_expr(exprt &expr)

PRECONDITION(old_components.size()==old_ops.size());
exprt::operandst::iterator o_it=old_ops.begin();
for(struct_union_typet::componentst::const_iterator
it=old_components.begin();
it!=old_components.end();
++it)
for(const auto &old_comp : old_components)
{
const bool is_zero_bit_field=
it->type().id()==ID_c_bit_field &&
to_c_bit_field_type(it->type()).get_width()==0;
const bool is_zero_bit_field =
old_comp.type().id() == ID_c_bit_field &&
to_c_bit_field_type(old_comp.type()).get_width() == 0;

if(!it->get_is_padding() && !is_zero_bit_field)
if(!old_comp.get_is_padding() && !is_zero_bit_field)
{
type.components().push_back(*it);
type.components().push_back(old_comp);
expr.move_to_operands(*o_it);
}
++o_it;
Expand Down
12 changes: 2 additions & 10 deletions src/goto-instrument/goto_program2code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1439,16 +1439,8 @@ void goto_program2codet::add_local_types(const typet &type)
!type_names_set.insert(identifier).second)
return;

const struct_union_typet &struct_union_type=
to_struct_union_type(full_type);
const struct_union_typet::componentst &components=
struct_union_type.components();

for(struct_union_typet::componentst::const_iterator
it=components.begin();
it!=components.end();
++it)
add_local_types(it->type());
for(const auto &c : to_struct_union_type(full_type).components())
add_local_types(c.type());

assert(!identifier.empty());
type_names.push_back(identifier);
Expand Down
9 changes: 2 additions & 7 deletions src/goto-programs/remove_complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,8 @@ static bool have_to_remove_complex(const typet &type)
{
if(type.id()==ID_struct || type.id()==ID_union)
{
const struct_union_typet &struct_union_type=
to_struct_union_type(type);
for(struct_union_typet::componentst::const_iterator
it=struct_union_type.components().begin();
it!=struct_union_type.components().end();
it++)
if(have_to_remove_complex(it->type()))
for(const auto &c : to_struct_union_type(type).components())
if(have_to_remove_complex(c.type()))
return true;
}
else if(type.id()==ID_pointer ||
Expand Down
10 changes: 2 additions & 8 deletions src/goto-programs/remove_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,8 @@ static bool have_to_remove_vector(const typet &type)
{
if(type.id()==ID_struct || type.id()==ID_union)
{
const struct_union_typet &struct_union_type=
to_struct_union_type(type);

for(struct_union_typet::componentst::const_iterator
it=struct_union_type.components().begin();
it!=struct_union_type.components().end();
it++)
if(have_to_remove_vector(it->type()))
for(const auto &c : to_struct_union_type(type).components())
if(have_to_remove_vector(c.type()))
return true;
}
else if(type.id()==ID_pointer ||
Expand Down
10 changes: 2 additions & 8 deletions src/goto-symex/goto_symex_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,8 @@ static bool check_renaming(const typet &type)
type.id()==ID_union ||
type.id()==ID_class)
{
const struct_union_typet &s_u_type=to_struct_union_type(type);
const struct_union_typet::componentst &components=s_u_type.components();

for(struct_union_typet::componentst::const_iterator
it=components.begin();
it!=components.end();
++it)
if(check_renaming(it->type()))
for(const auto &c : to_struct_union_type(type).components())
if(check_renaming(c.type()))
return true;
}
else if(type.has_subtype())
Expand Down
23 changes: 7 additions & 16 deletions src/linking/linking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,17 @@ std::string linkingt::type_to_string_verbose(
result+=" "+tag;
result+=" {\n";

const struct_union_typet::componentst &components=
to_struct_union_type(followed).components();

for(struct_union_typet::componentst::const_iterator
it=components.begin();
it!=components.end();
it++)
for(const auto &c : to_struct_union_type(followed).components())
{
const typet &subtype=it->type();
const typet &subtype = c.type();
result+=" ";
result+=type_to_string(ns, symbol.name, subtype);
result+=' ';

if(it->get_base_name()!="")
result+=id2string(it->get_base_name());
if(!c.get_base_name().empty())
result += id2string(c.get_base_name());
else
result+=id2string(it->get_name());
result += id2string(c.get_name());

result+=";\n";
}
Expand Down Expand Up @@ -686,11 +680,8 @@ void linkingt::duplicate_code_symbol(
const typet &src_type=t1.id()==ID_union?t2:t1;

bool found=false;
for(union_typet::componentst::const_iterator
it=union_type.components().begin();
!found && it!=union_type.components().end();
it++)
if(base_type_eq(it->type(), src_type, ns))
for(const auto &c : union_type.components())
if(base_type_eq(c.type(), src_type, ns))
{
found=true;
if(warn_msg.empty())
Expand Down
Loading