qlalr: eradicate all Q_FOREACH loops

... by replacing them with C++11 range-for, or, for loops
over .values(), with explicit iterator loops over the result
of equal_range().

Some fixes here and there to get to mark containers const for
iteration, without having to resort to qAsConst(). Didn't work
everywhere.

Change-Id: Ibc0e71d3b208d118f06e16741af47261ef4b9e15
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
This commit is contained in:
Marc Mutz 2016-01-25 12:20:01 +01:00
parent cf9d112d7a
commit 956021dbfc
5 changed files with 18 additions and 15 deletions

View File

@ -166,7 +166,7 @@ void Compress::operator () (int *table, int row_count, int column_count)
#ifndef QLALR_NO_CHECK_SORTED_TABLE
int previous_zeros = INT_MAX;
foreach (UncompressedRow row, sortedTable)
for (const UncompressedRow &row : qAsConst(sortedTable))
{
int zeros = row.count (0);
@ -178,7 +178,7 @@ void Compress::operator () (int *table, int row_count, int column_count)
index.fill (-999999, row_count);
foreach (const UncompressedRow &row, sortedTable)
for (const UncompressedRow &row : qAsConst(sortedTable))
{
int first_token = std::distance (row.begin (), row.beginNonZeros ());
QVector<int>::iterator pos = info.begin ();
@ -252,7 +252,7 @@ void Compress::operator () (int *table, int row_count, int column_count)
}
#if 0
foreach (UncompressedRow row, sortedTable)
for (const UncompressedRow &row : qAsConst(sortedTable))
{
int i = row.index ();
Q_ASSERT (i < sortedTable.size ());

View File

@ -159,12 +159,12 @@ void CppGenerator::operator () ()
int r = aut.id (item->rule);
NameSet lookaheads = aut.lookaheads.value (item);
const NameSet lookaheads = aut.lookaheads.value (item);
if (item->rule == grammar.goal)
accept_state = q;
foreach (const Name &s, lookaheads)
for (const Name &s : lookaheads)
{
int &u = ACTION (q, aut.id (s));
@ -448,7 +448,7 @@ void CppGenerator::generateDecl (QTextStream &out)
<< "public:" << endl
<< " enum VariousConstants {" << endl;
foreach (Name t, grammar.terminals)
for (Name t : qAsConst(grammar.terminals))
{
QString name = *t;
int value = std::distance (grammar.names.begin (), t);
@ -606,7 +606,7 @@ void CppGenerator::generateImpl (QTextStream &out)
out << endl << "#ifndef " << prot << endl;
out << "const int " << grammar.table_name << "::rule_info [] = {";
idx = 0;
for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx)
for (auto rule = grammar.rules.cbegin (); rule != grammar.rules.cend (); ++rule, ++idx)
{
out << endl << " ";
@ -617,7 +617,7 @@ void CppGenerator::generateImpl (QTextStream &out)
out << name_ids.value(rule->lhs);
foreach (const Name &n, rule->rhs)
for (const Name &n : rule->rhs)
out << ", " << name_ids.value (n);
}
out << "};" << endl << endl;

View File

@ -410,8 +410,10 @@ void Automaton::buildLookbackSets ()
if (! _M_grammar->isNonTerminal (A))
continue;
foreach (const RulePointer &rule, _M_grammar->rule_map.values (A))
const auto range = qAsConst(_M_grammar->rule_map).equal_range(A);
for (auto it = range.first; it != range.second; ++it)
{
const RulePointer &rule = *it;
StatePointer q = p;
for (NameList::iterator dot = rule->rhs.begin (); dot != rule->rhs.end (); ++dot)
@ -603,8 +605,10 @@ void Automaton::buildIncludesDigraph ()
if (! _M_grammar->isNonTerminal (name))
continue;
foreach (const RulePointer &rule, _M_grammar->rule_map.values (name))
const auto range = qAsConst(_M_grammar->rule_map).equal_range(name);
for (auto it = range.first; it != range.second; ++it)
{
const RulePointer &rule = *it;
StatePointer p = pp;
for (NameList::iterator A = rule->rhs.begin (); A != rule->rhs.end (); ++A)

View File

@ -67,10 +67,8 @@ int main (int argc, char *argv[])
bool qt_copyright = false;
QString file_name = 0;
QStringList args = app.arguments ();
args.removeFirst ();
foreach (const QString &arg, args) {
const QStringList args = app.arguments().mid(1);
for (const QString &arg : args) {
if (arg == QLatin1String ("-h") || arg == QLatin1String ("--help"))
help_me ();

View File

@ -86,7 +86,8 @@ void ParseTable::operator () (Automaton *aut)
first = false;
foreach (const Name &la, aut->lookaheads.value (item))
const auto lookaheads = aut->lookaheads.value(item);
for (const Name &la : lookaheads)
out << " " << *la << " reduce using rule " << aut->id (item->rule) << " (" << *item->rule->lhs << ")" << endl;
}