Skip to content

C++ front-end: use ranged for #3066

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 5 commits into from
Sep 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 8 additions & 17 deletions src/cpp/cpp_constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,10 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
{
exprt::operandst operands_tc=operands;

for(exprt::operandst::iterator
it=operands_tc.begin();
it!=operands_tc.end();
it++)
for(auto &op : operands_tc)
{
typecheck_expr(*it);
add_implicit_dereference(*it);
typecheck_expr(op);
add_implicit_dereference(op);
}

if(operands_tc.empty())
Expand Down Expand Up @@ -167,13 +164,10 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
{
exprt::operandst operands_tc=operands;

for(exprt::operandst::iterator
it=operands_tc.begin();
it!=operands_tc.end();
it++)
for(auto &op : operands_tc)
{
typecheck_expr(*it);
add_implicit_dereference(*it);
typecheck_expr(op);
add_implicit_dereference(op);
}

const struct_typet &struct_type=
Expand Down Expand Up @@ -234,11 +228,8 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
cpp_namet(constructor_name, source_location).as_expr();
function_call.arguments().reserve(operands_tc.size());

for(exprt::operandst::iterator
it=operands_tc.begin();
it!=operands_tc.end();
it++)
function_call.op1().copy_to_operands(*it);
for(auto &op : operands_tc)
function_call.op1().copy_to_operands(op);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can get rid of this loop entirely if using a 3-argument constructor for function_call above.


typecheck_side_effect_function_call(function_call);
assert(function_call.get(ID_statement)==ID_temporary_object);
Expand Down
7 changes: 2 additions & 5 deletions src/cpp/cpp_declarator_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,9 @@ symbolt &cpp_declarator_convertert::convert_new_symbol(
cpp_typecheck.cpp_scopes.current_scope().lookup(
base_name, cpp_scopet::SCOPE_ONLY, id_set);

for(cpp_scopest::id_sett::const_iterator
id_it=id_set.begin();
id_it!=id_set.end();
id_it++)
for(const auto &id_ptr : id_set)
{
const cpp_idt &id=**id_it;
const cpp_idt &id = *id_ptr;
// the name is already in the scope
// this is ok if they belong to different categories

Expand Down
20 changes: 6 additions & 14 deletions src/cpp/cpp_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ void cpp_idt::print(std::ostream &out, unsigned indent) const

if(!sub.empty())
{
for(cpp_id_mapt::const_iterator it=sub.begin();
it!=sub.end();
it++)
it->second.print(out, indent+2);
for(const auto &s : sub)
s.second.print(out, indent + 2);

out << '\n';
}
Expand All @@ -63,22 +61,16 @@ void cpp_idt::print_fields(std::ostream &out, unsigned indent) const
for(unsigned i=0; i<indent; i++) out << ' ';
out << " class_identifier=" << class_identifier << '\n';

for(scope_listt::const_iterator
it=secondary_scopes.begin();
it!=secondary_scopes.end();
it++)
for(const auto &s : secondary_scopes)
{
for(unsigned i=0; i<indent; i++) out << ' ';
out << " secondary_scope=" << (*it)->identifier << '\n';
out << " secondary_scope=" << s->identifier << '\n';
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could further simplify this loop by using std::string(indent, ' ') in place of the for loop above. Applies several times across this file.

}

for(scope_listt::const_iterator
it=using_scopes.begin();
it!=using_scopes.end();
it++)
for(const auto &s : using_scopes)
{
for(unsigned i=0; i<indent; i++) out << ' ';
out << " using_scope=" << (*it)->identifier << '\n';
out << " using_scope=" << s->identifier << '\n';
}

for(unsigned i=0; i<indent; i++) out << ' ';
Expand Down
21 changes: 7 additions & 14 deletions src/cpp/cpp_instantiate_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ std::string cpp_typecheckt::template_suffix(
const cpp_template_args_tct::argumentst &arguments=
template_args.arguments();

for(cpp_template_args_tct::argumentst::const_iterator
it=arguments.begin();
it!=arguments.end();
it++)
for(const auto &expr : arguments)
{
if(first)
first=false;
else
result+=',';

const exprt expr=*it;
DATA_INVARIANT(
expr.id() != ID_ambiguous, "template argument must not be ambiguous");

Expand All @@ -69,7 +65,7 @@ std::string cpp_typecheckt::template_suffix(
i=0;
else if(to_integer(e, i))
{
error().source_location=it->find_source_location();
error().source_location = expr.find_source_location();
error() << "template argument expression expected to be "
<< "scalar constant, but got `"
<< to_string(e) << "'" << eom;
Expand All @@ -87,17 +83,14 @@ std::string cpp_typecheckt::template_suffix(

void cpp_typecheckt::show_instantiation_stack(std::ostream &out)
{
for(instantiation_stackt::const_iterator
s_it=instantiation_stack.begin();
s_it!=instantiation_stack.end();
s_it++)
for(const auto &e : instantiation_stack)
{
const symbolt &symbol=lookup(s_it->identifier);
const symbolt &symbol = lookup(e.identifier);
out << "instantiating `" << symbol.pretty_name << "' with <";

forall_expr(a_it, s_it->full_template_args.arguments())
forall_expr(a_it, e.full_template_args.arguments())
{
if(a_it!=s_it->full_template_args.arguments().begin())
if(a_it != e.full_template_args.arguments().begin())
out << ", ";

if(a_it->id()==ID_type)
Expand All @@ -106,7 +99,7 @@ void cpp_typecheckt::show_instantiation_stack(std::ostream &out)
out << to_string(*a_it);
}

out << "> at " << s_it->source_location << '\n';
out << "> at " << e.source_location << '\n';
}
}

Expand Down
21 changes: 6 additions & 15 deletions src/cpp/cpp_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,8 @@ bool cpp_languaget::generate_support_functions(

void cpp_languaget::show_parse(std::ostream &out)
{
for(cpp_parse_treet::itemst::const_iterator it=
cpp_parse_tree.items.begin();
it!=cpp_parse_tree.items.end();
it++)
show_parse(out, *it);
for(const auto &i : cpp_parse_tree.items)
show_parse(out, i);
}

void cpp_languaget::show_parse(
Expand All @@ -160,11 +157,8 @@ void cpp_languaget::show_parse(

out << "LINKAGE " << linkage_spec.linkage().get(ID_value) << ":\n";

for(cpp_linkage_spect::itemst::const_iterator
it=linkage_spec.items().begin();
it!=linkage_spec.items().end();
it++)
show_parse(out, *it);
for(const auto &i : linkage_spec.items())
show_parse(out, i);

out << '\n';
}
Expand All @@ -176,11 +170,8 @@ void cpp_languaget::show_parse(
out << "NAMESPACE " << namespace_spec.get_namespace()
<< ":\n";

for(cpp_namespace_spect::itemst::const_iterator
it=namespace_spec.items().begin();
it!=namespace_spec.items().end();
it++)
show_parse(out, *it);
for(const auto &i : namespace_spec.items())
show_parse(out, i);

out << '\n';
}
Expand Down
28 changes: 8 additions & 20 deletions src/cpp/cpp_scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,9 @@ void cpp_scopet::lookup(
return; // done

// using scopes
for(scope_listt::iterator
it=using_scopes.begin();
it!=using_scopes.end();
it++)
for(const auto &s_ptr : using_scopes)
{
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);

// Recursive call.
// Note the different kind!
Expand All @@ -67,12 +64,9 @@ void cpp_scopet::lookup(
return; // done, upwards scopes are hidden

// secondary scopes
for(scope_listt::iterator
it=secondary_scopes.begin();
it!=secondary_scopes.end();
it++)
for(const auto &s_ptr : secondary_scopes)
{
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);

// Recursive call.
// Note the different kind!
Expand Down Expand Up @@ -130,12 +124,9 @@ void cpp_scopet::lookup(
return; // done

// using scopes
for(scope_listt::iterator
it=using_scopes.begin();
it!=using_scopes.end();
it++)
for(const auto &s_ptr : using_scopes)
{
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);

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

// secondary scopes
for(scope_listt::iterator
it=secondary_scopes.begin();
it!=secondary_scopes.end();
it++)
for(const auto &s_ptr : secondary_scopes)
{
cpp_scopet &other_scope=static_cast<cpp_scopet &>(**it);
cpp_scopet &other_scope = static_cast<cpp_scopet &>(*s_ptr);

// Recursive call.
// Note the different kind!
Expand Down
16 changes: 7 additions & 9 deletions src/cpp/cpp_typecheck_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,20 @@ void cpp_typecheckt::typecheck_code(codet &code)

void cpp_typecheckt::typecheck_try_catch(codet &code)
{
codet::operandst &operands=code.operands();
bool first = true;

for(codet::operandst::iterator
it=operands.begin();
it!=operands.end();
it++)
for(auto &op : code.operands())
{
if(it==operands.begin())
if(first)
{
// this is the 'try'
typecheck_code(to_code(*it));
typecheck_code(to_code(op));
first = false;
}
else
{
// This is (one of) the catch clauses.
codet &code=to_code_block(to_code(*it));
codet &code = to_code_block(to_code(op));

// look at the catch operand
assert(!code.operands().empty());
Expand Down Expand Up @@ -105,7 +103,7 @@ void cpp_typecheckt::typecheck_try_catch(codet &code)
const typet &type=code_decl.op0().type();

// annotate exception ID
it->set(ID_exception_id, cpp_exception_id(type, *this));
op.set(ID_exception_id, cpp_exception_id(type, *this));
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/cpp/cpp_typecheck_fargs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ Author: Daniel Kroening, [email protected]

bool cpp_typecheck_fargst::has_class_type() const
{
for(exprt::operandst::const_iterator it=operands.begin();
it!=operands.end();
it++)
for(const auto &op : operands)
{
if(it->type().id()==ID_struct)
if(op.type().id() == ID_struct)
return true;
}

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

for(std::size_t i=0; i<function_call.op1().operands().size(); i++)
operands.push_back(function_call.op1().operands()[i]);
for(const auto &op : function_call.op1().operands())
operands.push_back(op);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rewrite this (and some lines above) as operands = function_call.op1().operands(); ?

}

bool cpp_typecheck_fargst::match(
Expand Down
Loading