CPlusPlus: Remove foreach / Q_FOREACH usage

Task-number: QTCREATORBUG-27464
Change-Id: I62e27bca141a529ac220211f8b31e78be0f7e855
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-05-30 12:08:21 +02:00
parent 67fdb23001
commit f7097cef19
8 changed files with 47 additions and 43 deletions

View File

@@ -629,7 +629,7 @@ ClassOrNamespace *ClassOrNamespace::parent() const
return _parent; return _parent;
} }
QList<ClassOrNamespace *> ClassOrNamespace::usings() const const QList<ClassOrNamespace *> ClassOrNamespace::usings() const
{ {
const_cast<ClassOrNamespace *>(this)->flush(); const_cast<ClassOrNamespace *>(this)->flush();
return _usings; return _usings;
@@ -641,7 +641,7 @@ QList<Enum *> ClassOrNamespace::unscopedEnums() const
return _enums; return _enums;
} }
QList<Symbol *> ClassOrNamespace::symbols() const const QList<Symbol *> ClassOrNamespace::symbols() const
{ {
const_cast<ClassOrNamespace *>(this)->flush(); const_cast<ClassOrNamespace *>(this)->flush();
return _symbols; return _symbols;

View File

@@ -70,9 +70,9 @@ public:
ClassOrNamespace *instantiationOrigin() const; ClassOrNamespace *instantiationOrigin() const;
ClassOrNamespace *parent() const; ClassOrNamespace *parent() const;
QList<ClassOrNamespace *> usings() const; const QList<ClassOrNamespace *> usings() const;
QList<Enum *> unscopedEnums() const; QList<Enum *> unscopedEnums() const;
QList<Symbol *> symbols() const; const QList<Symbol *> symbols() const;
ClassOrNamespace *globalNamespace() const; ClassOrNamespace *globalNamespace() const;

View File

@@ -97,7 +97,7 @@ public:
typedef QPair<QByteArray, QByteArray> Pair; typedef QPair<QByteArray, QByteArray> Pair;
foreach (const Pair &conn, _connections) for (const Pair &conn : qAsConst(_connections))
out << conn.first.constData() << " -> " << conn.second.constData() << std::endl; out << conn.first.constData() << " -> " << conn.second.constData() << std::endl;
alignTerminals(); alignTerminals();
@@ -113,7 +113,7 @@ public:
protected: protected:
void alignTerminals() { void alignTerminals() {
out<<"{ rank=same;" << std::endl; out<<"{ rank=same;" << std::endl;
foreach (const QByteArray &terminalShape, _terminalShapes) { for (const QByteArray &terminalShape : qAsConst(_terminalShapes)) {
out << " " << std::string(terminalShape.constData(), terminalShape.size()).c_str() << ";" << std::endl; out << " " << std::string(terminalShape.constData(), terminalShape.size()).c_str() << ";" << std::endl;
} }
out<<"}"<<std::endl; out<<"}"<<std::endl;
@@ -439,10 +439,10 @@ public:
/// successfully parse with one of the given parseModes (one parse mode after the other /// successfully parse with one of the given parseModes (one parse mode after the other
/// is tried), otherwise a null pointer. /// is tried), otherwise a null pointer.
static Document::Ptr parse(const QString &fileName, const QByteArray &source, static Document::Ptr parse(const QString &fileName, const QByteArray &source,
QList<Document::ParseMode> parseModes, QByteArray *errors, const QList<Document::ParseMode> parseModes, QByteArray *errors,
bool verbose = false) bool verbose = false)
{ {
foreach (const Document::ParseMode parseMode, parseModes) { for (const Document::ParseMode parseMode : parseModes) {
ErrorHandler *errorHandler = new ErrorHandler(parseMode, errors); // Deleted by ~Document. ErrorHandler *errorHandler = new ErrorHandler(parseMode, errors); // Deleted by ~Document.
if (verbose) if (verbose)
std::cout << "Parsing as " << qPrintable(parseModeToString(parseMode)) << "..."; std::cout << "Parsing as " << qPrintable(parseModeToString(parseMode)) << "...";
@@ -466,7 +466,7 @@ static Document::Ptr parse(const QString &fileName, const QByteArray &source,
/// Convenience function /// Convenience function
static Document::Ptr parse(const QString &fileName, const QByteArray &source, static Document::Ptr parse(const QString &fileName, const QByteArray &source,
Document::ParseMode parseMode, QByteArray *errors, const Document::ParseMode parseMode, QByteArray *errors,
bool verbose = false) bool verbose = false)
{ {
QList<Document::ParseMode> parseModes = QList<Document::ParseMode>() << parseMode; QList<Document::ParseMode> parseModes = QList<Document::ParseMode>() << parseMode;
@@ -576,7 +576,7 @@ int main(int argc, char *argv[])
// Process files // Process files
const QStringList files = args; const QStringList files = args;
foreach (const QString &fileName, files) { for (const QString &fileName : files) {
if (! QFile::exists(fileName)) { if (! QFile::exists(fileName)) {
std::cerr << "Error: File \"" << qPrintable(fileName) << "\" does not exist." std::cerr << "Error: File \"" << qPrintable(fileName) << "\" does not exist."
<< std::endl; << std::endl;
@@ -638,7 +638,7 @@ int main(int argc, char *argv[])
QString(fileName + QLatin1String(".ast.png")))); QString(fileName + QLatin1String(".ast.png"))));
inputOutputFiles.append(qMakePair(QString(fileName + QLatin1String(".symbols.dot")), inputOutputFiles.append(qMakePair(QString(fileName + QLatin1String(".symbols.dot")),
QString(fileName + QLatin1String(".symbols.png")))); QString(fileName + QLatin1String(".symbols.png"))));
foreach (const Pair &pair, inputOutputFiles) { for (const Pair &pair : qAsConst(inputOutputFiles)) {
createImageFromDot(pair.first, pair.second, optionVerbose); createImageFromDot(pair.first, pair.second, optionVerbose);
std::cout << qPrintable(QDir::toNativeSeparators(pair.second)) << std::endl; std::cout << qPrintable(QDir::toNativeSeparators(pair.second)) << std::endl;
} }

View File

@@ -80,7 +80,7 @@ int main(int argc, char *argv[])
// Process files // Process files
const QStringList files = args; const QStringList files = args;
foreach (const QString &fileName, files) { for (const QString &fileName : files) {
// Run preprocessor // Run preprocessor
const QString fileNamePreprocessed = fileName + QLatin1String(".preprocessed"); const QString fileNamePreprocessed = fileName + QLatin1String(".preprocessed");
CplusplusToolsUtils::SystemPreprocessor preprocessor(optionVerbose); CplusplusToolsUtils::SystemPreprocessor preprocessor(optionVerbose);

View File

@@ -65,7 +65,7 @@ class MkVisitor: protected SymbolVisitor
bool isMiscNode(ClassOrNamespace *b) const bool isMiscNode(ClassOrNamespace *b) const
{ {
foreach (ClassOrNamespace *u, b->usings()) { for (const ClassOrNamespace *u : b->usings()) {
if (oo(u->symbols().first()->name()) == QLatin1String("AST")) if (oo(u->symbols().first()->name()) == QLatin1String("AST"))
return true; return true;
} }
@@ -126,11 +126,11 @@ public:
<< " Semantic(TranslationUnit *unit): ASTVisitor(unit) { translationUnit(unit->ast()->asTranslationUnit()); }" << std::endl << " Semantic(TranslationUnit *unit): ASTVisitor(unit) { translationUnit(unit->ast()->asTranslationUnit()); }" << std::endl
<< std::endl; << std::endl;
foreach (ClassOrNamespace *b, interfaces) { for (ClassOrNamespace *b : qAsConst(interfaces)) {
Q_ASSERT(! b->symbols().isEmpty()); Q_ASSERT(! b->symbols().isEmpty());
Class *klass = 0; Class *klass = 0;
foreach (Symbol *s, b->symbols()) for (Symbol *s : b->symbols())
if ((klass = s->asClass()) != 0) if ((klass = s->asClass()) != 0)
break; break;
@@ -158,9 +158,9 @@ public:
<< std::endl; << std::endl;
QHash<ClassOrNamespace *, QList<ClassOrNamespace *> > implements; QHash<ClassOrNamespace *, QList<ClassOrNamespace *> > implements;
foreach (ClassOrNamespace *b, nodes) { for (ClassOrNamespace *b : qAsConst(nodes)) {
ClassOrNamespace *iface = 0; ClassOrNamespace *iface = 0;
foreach (ClassOrNamespace *u, b->usings()) { for (ClassOrNamespace *u : b->usings()) {
if (interfaces.contains(u)) { if (interfaces.contains(u)) {
iface = u; iface = u;
break; break;
@@ -170,13 +170,14 @@ public:
implements[iface].append(b); implements[iface].append(b);
} }
foreach (ClassOrNamespace *iface, interfaces) { for (ClassOrNamespace *iface : qAsConst(interfaces)) {
foreach (ClassOrNamespace *b, implements.value(iface)) { const QList<ClassOrNamespace *> values = implements.value(iface);
for (ClassOrNamespace *b : values) {
if (! isMiscNode(b)) if (! isMiscNode(b))
continue; continue;
Class *klass = 0; Class *klass = 0;
foreach (Symbol *s, b->symbols()) for (Symbol *s : b->symbols())
if ((klass = s->asClass()) != 0) if ((klass = s->asClass()) != 0)
break; break;
@@ -190,11 +191,12 @@ public:
std::cout << std::endl; std::cout << std::endl;
foreach (ClassOrNamespace *iface, interfaces) { for (ClassOrNamespace *iface : qAsConst(interfaces)) {
std::cout << " // " << qPrintable(oo(iface->symbols().first()->name())) << std::endl; std::cout << " // " << qPrintable(oo(iface->symbols().first()->name())) << std::endl;
foreach (ClassOrNamespace *b, implements.value(iface)) { const QList<ClassOrNamespace *> values = implements.value(iface);
for (ClassOrNamespace *b : values) {
Class *klass = 0; Class *klass = 0;
foreach (Symbol *s, b->symbols()) for (Symbol *s : b->symbols())
if ((klass = s->asClass()) != 0) if ((klass = s->asClass()) != 0)
break; break;
@@ -207,11 +209,11 @@ public:
} }
std::cout << "private:" << std::endl; std::cout << "private:" << std::endl;
foreach (ClassOrNamespace *b, interfaces) { for (ClassOrNamespace *b : qAsConst(interfaces)) {
Q_ASSERT(! b->symbols().isEmpty()); Q_ASSERT(! b->symbols().isEmpty());
Class *klass = 0; Class *klass = 0;
foreach (Symbol *s, b->symbols()) for (Symbol *s : b->symbols())
if ((klass = s->asClass()) != 0) if ((klass = s->asClass()) != 0)
break; break;
@@ -240,11 +242,11 @@ public:
// implementation // implementation
foreach (ClassOrNamespace *b, interfaces) { for (ClassOrNamespace *b : qAsConst(interfaces)) {
Q_ASSERT(! b->symbols().isEmpty()); Q_ASSERT(! b->symbols().isEmpty());
Class *klass = 0; Class *klass = 0;
foreach (Symbol *s, b->symbols()) for (Symbol *s : b->symbols())
if ((klass = s->asClass()) != 0) if ((klass = s->asClass()) != 0)
break; break;
@@ -275,11 +277,12 @@ public:
<< std::endl; << std::endl;
} }
foreach (ClassOrNamespace *iface, interfaces) { for (ClassOrNamespace *iface : qAsConst(interfaces)) {
std::cout << "// " << qPrintable(oo(iface->symbols().first()->name())) << std::endl; std::cout << "// " << qPrintable(oo(iface->symbols().first()->name())) << std::endl;
foreach (ClassOrNamespace *b, implements.value(iface)) { const QList<ClassOrNamespace *> values = implements.value(iface);
for (ClassOrNamespace *b : values) {
Class *klass = 0; Class *klass = 0;
foreach (Symbol *s, b->symbols()) for (Symbol *s : b->symbols())
if ((klass = s->asClass()) != 0) if ((klass = s->asClass()) != 0)
break; break;
@@ -383,8 +386,8 @@ protected:
QList<ClassOrNamespace *> baseClasses(ClassOrNamespace *b) { QList<ClassOrNamespace *> baseClasses(ClassOrNamespace *b) {
QList<ClassOrNamespace *> usings = b->usings(); QList<ClassOrNamespace *> usings = b->usings();
foreach (ClassOrNamespace *u, usings) for (int length = usings.size(), i = 0; i < length; ++i)
usings += baseClasses(u); usings += baseClasses(usings[i]);
return usings; return usings;
} }
@@ -400,7 +403,7 @@ protected:
if (Symbol *s = klass->find(accept0)) { if (Symbol *s = klass->find(accept0)) {
if (Function *meth = s->type()->asFunctionType()) { if (Function *meth = s->type()->asFunctionType()) {
if (! meth->isPureVirtual()) { if (! meth->isPureVirtual()) {
foreach (ClassOrNamespace *u, b->usings()) { for (const ClassOrNamespace *u : b->usings()) {
if (interfaces.contains(u)) { if (interfaces.contains(u)) {
// qDebug() << oo(klass->name()) << "implements" << oo(u->symbols().first()->name()); // qDebug() << oo(klass->name()) << "implements" << oo(u->symbols().first()->name());
} else { } else {

View File

@@ -1294,7 +1294,7 @@ void generateASTMatcher_H(const Snapshot &, const QDir &cplusplusDir,
" virtual ~ASTMatcher();\n" " virtual ~ASTMatcher();\n"
"\n"; "\n";
foreach (const QByteArray &klass, classes) { for (const QByteArray &klass : classes) {
out << " virtual bool match(" << klass << " *node, " << klass << " *pattern);\n"; out << " virtual bool match(" << klass << " *node, " << klass << " *pattern);\n";
} }
@@ -1340,7 +1340,7 @@ QStringList generateAST_H(const Snapshot &snapshot, const QDir &cplusplusDir, co
Overview oo; Overview oo;
QStringList castMethods; QStringList castMethods;
foreach (ClassSpecifierAST *classAST, astNodes.deriveds) { for (ClassSpecifierAST *classAST : qAsConst(astNodes.deriveds)) {
cursors[classAST] = removeCastMethods(classAST); cursors[classAST] = removeCastMethods(classAST);
const QString className = oo(classAST->symbol->name()); const QString className = oo(classAST->symbol->name());
const QString methodName = QLatin1String("as") + className.mid(0, className.length() - 3); const QString methodName = QLatin1String("as") + className.mid(0, className.length() - 3);

View File

@@ -70,7 +70,7 @@ class CExecList : public QVector<char *>
public: public:
CExecList(const QStringList &list) CExecList(const QStringList &list)
{ {
foreach (const QString &item, list) for (const QString &item : list)
append(qstrdup(item.toLatin1().data())); append(qstrdup(item.toLatin1().data()));
append(0); append(0);
} }
@@ -195,7 +195,7 @@ bool CrashHandler::collectRestartAppData()
return false; return false;
} }
commandLine.removeLast(); commandLine.removeLast();
foreach (const QByteArray &item, commandLine) for (const QByteArray &item : qAsConst(commandLine))
d->restartAppCommandLine.append(QString::fromLatin1(item)); d->restartAppCommandLine.append(QString::fromLatin1(item));
// Get environment. // Get environment.
@@ -209,7 +209,7 @@ bool CrashHandler::collectRestartAppData()
} }
if (environment.last().isEmpty()) if (environment.last().isEmpty())
environment.removeLast(); environment.removeLast();
foreach (const QByteArray &item, environment) for (const QByteArray &item : qAsConst(environment))
d->restartAppEnvironment.append(QString::fromLatin1(item)); d->restartAppEnvironment.append(QString::fromLatin1(item));
return true; return true;

View File

@@ -159,7 +159,7 @@ private:
void Project::setPaths(const QStringList &paths) void Project::setPaths(const QStringList &paths)
{ {
foreach (const QString &path, paths) for (const QString &path : paths)
m_items.append(path); m_items.append(path);
} }
@@ -220,7 +220,8 @@ void Project::handleBinary(const QString &item)
// "}] (gdb) // "}] (gdb)
int first = input.indexOf(QLatin1Char('{')); int first = input.indexOf(QLatin1Char('{'));
input = input.mid(first, input.lastIndexOf(QLatin1Char('}')) - first); input = input.mid(first, input.lastIndexOf(QLatin1Char('}')) - first);
foreach (QString item, input.split(QLatin1String("},{"))) { const QStringList items = input.split(QLatin1String("},{"));
for (QString item : items) {
//qDebug() << "ITEM: " << item; //qDebug() << "ITEM: " << item;
int full = item.indexOf(QLatin1String(",fullname=\"")); int full = item.indexOf(QLatin1String(",fullname=\""));
if (full != -1) if (full != -1)
@@ -299,17 +300,17 @@ void Project::writeProFile()
if (m_subdirs.isEmpty()) { if (m_subdirs.isEmpty()) {
ts << "TEMPLATE = app\n"; ts << "TEMPLATE = app\n";
ts << "TARGET = " << QFileInfo(m_outputFileName).baseName() << "\n"; ts << "TARGET = " << QFileInfo(m_outputFileName).baseName() << "\n";
foreach (const FileClass &fc, m_fileClasses) for (const FileClass &fc : qAsConst(m_fileClasses))
fc.writeProBlock(ts); fc.writeProBlock(ts);
ts << "\nPATHS *="; ts << "\nPATHS *=";
foreach (const QDir &dir, m_items) for (const QDir dir : qAsConst(m_items))
ts << " \\\n " << dir.path(); ts << " \\\n " << dir.path();
ts << "\n\nDEPENDPATH *= $$PATHS\n"; ts << "\n\nDEPENDPATH *= $$PATHS\n";
ts << "\nINCLUDEPATH *= $$PATHS\n"; ts << "\nINCLUDEPATH *= $$PATHS\n";
} else { } else {
ts << "TEMPLATE = subdirs\n"; ts << "TEMPLATE = subdirs\n";
ts << "SUBDIRS = "; ts << "SUBDIRS = ";
foreach (const QString &subdir, m_subdirs) for (const QString &subdir : qAsConst(m_subdirs))
ts << " \\\n " << subdir; ts << " \\\n " << subdir;
ts << "\n"; ts << "\n";
} }