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 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
32 changes: 10 additions & 22 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 @@ -228,17 +222,11 @@ optionalt<codet> cpp_typecheckt::cpp_constructor(
// there is always a constructor for non-PODs
assert(constructor_name!="");

side_effect_expr_function_callt function_call;
side_effect_expr_function_callt function_call(
cpp_namet(constructor_name, source_location).as_expr(),
operands_tc);

function_call.add_source_location()=source_location;
function_call.function() =
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);

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
40 changes: 16 additions & 24 deletions src/cpp/cpp_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,46 @@ 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';
}
}

void cpp_idt::print_fields(std::ostream &out, unsigned indent) const
{
for(unsigned i=0; i<indent; i++) out << ' ';
out << std::string(indent, ' ');
out << "**identifier=" << identifier << '\n';

for(unsigned i=0; i<indent; i++) out << ' ';
out << std::string(indent, ' ');
out << " prefix=" << prefix << '\n';

for(unsigned i=0; i<indent; i++) out << ' ';
out << std::string(indent, ' ');
out << " suffix=" << suffix << '\n';

for(unsigned i=0; i<indent; i++) out << ' ';
out << std::string(indent, ' ');
out << " base_name=" << base_name << '\n';

for(unsigned i=0; i<indent; i++) out << ' ';
out << std::string(indent, ' ');
out << " method=" << is_method << '\n';

for(unsigned i=0; i<indent; i++) out << ' ';
out << std::string(indent, ' ');
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 << std::string(indent, ' ');
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 << std::string(indent, ' ');
out << " using_scope=" << s->identifier << '\n';
}

for(unsigned i=0; i<indent; i++) out << ' ';
out << std::string(indent, ' ');
out << " flags:";
if(is_constructor)
out << " constructor";
Expand All @@ -93,7 +85,7 @@ void cpp_idt::print_fields(std::ostream &out, unsigned indent) const
out << " static_member";
out << '\n';

for(unsigned i=0; i<indent; i++) out << ' ';
out << std::string(indent, ' ');
out << " id_class=" << id_class << '\n';
}

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
13 changes: 3 additions & 10 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 @@ -36,12 +34,7 @@ void cpp_typecheck_fargst::build(
const side_effect_expr_function_callt &function_call)
{
in_use=true;

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]);
operands = function_call.op1().operands();
}

bool cpp_typecheck_fargst::match(
Expand Down
Loading