Valgrind: Remove foreach / Q_FOREACH usage

Task-number: QTCREATORBUG-27464
Change-Id: Ia507fb7aab405226a954b6059ef326e999a8171a
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Artem Sokolovskii
2022-05-17 11:53:31 +02:00
parent c31285d317
commit 7450bcb2b9
14 changed files with 64 additions and 45 deletions

View File

@@ -45,14 +45,14 @@ CycleDetection::CycleDetection(ParseData *data)
QVector<const Function *> CycleDetection::run(const QVector<const Function *> &input) QVector<const Function *> CycleDetection::run(const QVector<const Function *> &input)
{ {
foreach (const Function *function, input) { for (const Function *function : input) {
Node *node = new Node; Node *node = new Node;
node->function = function; node->function = function;
node->dfs = -1; node->dfs = -1;
node->lowlink = -1; node->lowlink = -1;
m_nodes.insert(function, node); m_nodes.insert(function, node);
} }
foreach (Node *node, m_nodes) { for (Node *node : qAsConst(m_nodes)) {
if (node->dfs == -1) if (node->dfs == -1)
tarjan(node); tarjan(node);
} }
@@ -69,7 +69,8 @@ void CycleDetection::tarjan(Node *node)
m_depth++; m_depth++;
m_stack.push(node); m_stack.push(node);
foreach (const FunctionCall *call, node->function->outgoingCalls()) const QVector<const FunctionCall *> calls = node->function->outgoingCalls();
for (const FunctionCall *call : calls)
tarjanForChildNode(node, m_nodes.value(call->callee())); tarjanForChildNode(node, m_nodes.value(call->callee()));
if (node->dfs == node->lowlink) { if (node->dfs == node->lowlink) {

View File

@@ -67,7 +67,7 @@ void Function::Private::accumulateCost(QVector<quint64> &base, const QVector<qui
} else { } else {
///TODO: see whether .data() is noticably faster (less detaching) ///TODO: see whether .data() is noticably faster (less detaching)
int i = 0; int i = 0;
foreach (quint64 cost, add) for (quint64 cost : add)
base[i++] += cost; base[i++] += cost;
} }
} }
@@ -175,7 +175,7 @@ void Function::setObject(qint64 id)
QString Function::location() const QString Function::location() const
{ {
QString pos; QString pos;
foreach (const CostItem *costItem, d->m_costItems) { for (const CostItem *costItem : qAsConst(d->m_costItems)) {
if (costItem->differingFileId() != -1) { if (costItem->differingFileId() != -1) {
QTextStream stream(&pos); QTextStream stream(&pos);
stream << '('; stream << '(';
@@ -214,7 +214,7 @@ int Function::lineNumber() const
if (lineIdx == -1) if (lineIdx == -1)
return -1; return -1;
foreach (const CostItem *costItem, d->m_costItems) { for (const CostItem *costItem : qAsConst(d->m_costItems)) {
if (costItem->differingFileId() == -1) if (costItem->differingFileId() == -1)
return costItem->position(lineIdx); return costItem->position(lineIdx);
} }
@@ -287,7 +287,7 @@ void Function::addCostItem(const CostItem *item)
void Function::finalize() void Function::finalize()
{ {
bool recursive = false; bool recursive = false;
foreach (const FunctionCall *call, d->m_incomingCalls) { for (const FunctionCall *call : qAsConst(d->m_incomingCalls)) {
if (call->caller() == this) { if (call->caller() == this) {
recursive = true; recursive = true;
break; break;
@@ -300,9 +300,10 @@ void Function::finalize()
// e.g.: A -> B -> B ..., C -> B -> B ... // e.g.: A -> B -> B ..., C -> B -> B ...
// cost of B = cost of call to B in A + cost of call to B in C + ... // cost of B = cost of call to B in A + cost of call to B in C + ...
d->m_inclusiveCost.fill(0); d->m_inclusiveCost.fill(0);
foreach (const FunctionCall *call, d->m_incomingCalls) { for (const FunctionCall *call : qAsConst(d->m_incomingCalls)) {
if (call->caller() != this) { if (call->caller() != this) {
foreach (const CostItem *costItem, call->caller()->costItems()) { const QVector<const CostItem *> costItems = call->caller()->costItems();
for (const CostItem *costItem : costItems) {
if (costItem->call() && costItem->call()->callee() == this) if (costItem->call() && costItem->call()->callee() == this)
d->accumulateCost(d->m_inclusiveCost, costItem->costs()); d->accumulateCost(d->m_inclusiveCost, costItem->costs());
} }

View File

@@ -73,16 +73,17 @@ void FunctionCycle::setFunctions(const QVector<const Function *> &functions)
d->m_selfCost.fill(0, d->m_data->events().size()); d->m_selfCost.fill(0, d->m_data->events().size());
d->m_inclusiveCost.fill(0, d->m_data->events().size()); d->m_inclusiveCost.fill(0, d->m_data->events().size());
foreach (const Function *func, functions) { for (const Function *func : functions) {
// just add up self cost // just add up self cost
Private::accumulateCost(d->m_selfCost, func->selfCosts()); Private::accumulateCost(d->m_selfCost, func->selfCosts());
// add outgoing calls to functions that are not part of the cycle // add outgoing calls to functions that are not part of the cycle
foreach (const FunctionCall *call, func->outgoingCalls()) { const QVector<const FunctionCall *> calls = func->outgoingCalls();
for (const FunctionCall *call : calls) {
if (!functions.contains(call->callee())) if (!functions.contains(call->callee()))
d->accumulateCall(call, Function::Private::Outgoing); d->accumulateCall(call, Function::Private::Outgoing);
} }
// add incoming calls from functions that are not part of the cycle // add incoming calls from functions that are not part of the cycle
foreach (const FunctionCall *call, func->incomingCalls()) { for (const FunctionCall *call : calls) {
if (!functions.contains(call->caller())) { if (!functions.contains(call->caller())) {
d->accumulateCall(call, Function::Private::Incoming); d->accumulateCall(call, Function::Private::Incoming);
d->m_called += call->calls(); d->m_called += call->calls();

View File

@@ -92,7 +92,7 @@ ParseData::Private::~Private()
void ParseData::Private::cleanupFunctionCycles() void ParseData::Private::cleanupFunctionCycles()
{ {
m_cycleCacheValid = false; m_cycleCacheValid = false;
foreach (const Function *func, m_cycleCache) { for (const Function *func : qAsConst(m_cycleCache)) {
if (dynamic_cast<const FunctionCycle *>(func)) if (dynamic_cast<const FunctionCycle *>(func))
delete func; delete func;
} }

View File

@@ -219,17 +219,19 @@ void Parser::Private::parse(const FilePath &filePath)
// build fast lookup of functions by their nameId // build fast lookup of functions by their nameId
QHash<qint64, QList<const Function *> > functionLookup; QHash<qint64, QList<const Function *> > functionLookup;
foreach (const Function *function, data->functions()) { const QVector<const Function *> functions = data->functions();
for (const Function *function : functions) {
functionLookup[function->nameId()].append(function); functionLookup[function->nameId()].append(function);
} }
// functions that need to accumulate their calees // functions that need to accumulate their calees
QSet<Function *> pendingFunctions; QSet<Function *> pendingFunctions;
foreach (const CallData &callData, pendingCallees) { for (const CallData &callData : qAsConst(pendingCallees)) {
Function *calledFunction = nullptr; Function *calledFunction = nullptr;
QTC_ASSERT(callData.call, continue); QTC_ASSERT(callData.call, continue);
QTC_ASSERT(callData.call->caller(), continue); QTC_ASSERT(callData.call->caller(), continue);
foreach (const Function *function, functionLookup.value(callData.calledFunction)) { const QList<const Function *> functions = functionLookup.value(callData.calledFunction);
for (const Function *function : functions) {
QTC_ASSERT(function->nameId() == callData.calledFunction, continue); QTC_ASSERT(function->nameId() == callData.calledFunction, continue);
if (function->objectId() == callData.calledObject if (function->objectId() == callData.calledObject
&& function->fileId() == callData.calledFile) && function->fileId() == callData.calledFile)
@@ -245,7 +247,7 @@ void Parser::Private::parse(const FilePath &filePath)
qDebug() << "caller is:" << callData.call->caller()->name() << callData.call->caller()->nameId(); qDebug() << "caller is:" << callData.call->caller()->name() << callData.call->caller()->nameId();
qDebug() << "called file:" << callData.calledFile << "object:" << callData.calledObject; qDebug() << "called file:" << callData.calledFile << "object:" << callData.calledObject;
qDebug() << data->stringForFileCompression(callData.calledFile) << data->stringForObjectCompression(callData.calledObject); qDebug() << data->stringForFileCompression(callData.calledFile) << data->stringForObjectCompression(callData.calledObject);
foreach (const Function *function, functionLookup.value(callData.calledFunction)) { for (const Function *function, functionLookup.value(callData.calledFunction)) {
qDebug() << "available function file:" << function->fileId() << function->file() << "object:" << function->objectId() << function->object(); qDebug() << "available function file:" << function->fileId() << function->file() << "object:" << function->objectId() << function->object();
} }
} }
@@ -263,7 +265,7 @@ void Parser::Private::parse(const FilePath &filePath)
// lookup done // lookup done
// now accumulate callees // now accumulate callees
foreach (Function *func, pendingFunctions) for (Function *func : qAsConst(pendingFunctions))
func->finalize(); func->finalize();
emit q->parserDataReady(); emit q->parserDataReady();
@@ -322,7 +324,8 @@ void Parser::Private::parseHeader(QIODevice *device)
} else if (line.startsWith("summary: ")) { } else if (line.startsWith("summary: ")) {
QString values = getValue(line, 9); QString values = getValue(line, 9);
uint i = 0; uint i = 0;
foreach (const QString &value, values.split(' ', Qt::SkipEmptyParts)) const QStringList valueList = values.split(' ', Qt::SkipEmptyParts);
for (const QString &value : valueList)
data->setTotalCost(i++, value.toULongLong()); data->setTotalCost(i++, value.toULongLong());
} else if (!line.trimmed().isEmpty()) { } else if (!line.trimmed().isEmpty()) {
// handle line and exit parseHeader // handle line and exit parseHeader

View File

@@ -130,7 +130,8 @@ bool DataProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_
// check if the function from this index is a child of (called by) the filter function // check if the function from this index is a child of (called by) the filter function
if (m_function) { if (m_function) {
bool isValid = false; bool isValid = false;
foreach (const FunctionCall *call, func->incomingCalls()) { const QVector<const FunctionCall *> calls = func->incomingCalls();
for (const FunctionCall *call : calls) {
if (call->caller() == m_function) { if (call->caller() == m_function) {
isValid = true; isValid = true;
break; break;

View File

@@ -735,7 +735,8 @@ void CallgrindToolPrivate::updateEventCombo()
} }
m_eventCombo->show(); m_eventCombo->show();
foreach (const QString &event, data->events()) const QStringList events = data->events();
for (const QString &event : events)
m_eventCombo->addItem(ParseData::prettyStringForEvent(event)); m_eventCombo->addItem(ParseData::prettyStringForEvent(event));
} }
@@ -848,7 +849,7 @@ void CallgrindToolPrivate::editorOpened(IEditor *editor)
void CallgrindToolPrivate::requestContextMenu(TextEditorWidget *widget, int line, QMenu *menu) void CallgrindToolPrivate::requestContextMenu(TextEditorWidget *widget, int line, QMenu *menu)
{ {
// Find callgrind text mark that corresponds to this editor's file and line number // Find callgrind text mark that corresponds to this editor's file and line number
foreach (CallgrindTextMark *textMark, m_textMarks) { for (CallgrindTextMark *textMark : qAsConst(m_textMarks)) {
if (textMark->fileName() == widget->textDocument()->filePath() && textMark->lineNumber() == line) { if (textMark->fileName() == widget->textDocument()->filePath() && textMark->lineNumber() == line) {
const Function *func = textMark->function(); const Function *func = textMark->function();
QAction *action = menu->addAction(CallgrindTool::tr("Select This Function in the Analyzer Output")); QAction *action = menu->addAction(CallgrindTool::tr("Select This Function in the Analyzer Output"));

View File

@@ -236,7 +236,8 @@ void Visualization::Private::handleMousePressEvent(QMouseEvent *event,
{ {
// find the first item that accepts mouse presses under the cursor position // find the first item that accepts mouse presses under the cursor position
QGraphicsItem *itemAtPos = nullptr; QGraphicsItem *itemAtPos = nullptr;
foreach (QGraphicsItem *item, q->items(event->pos())) { const QList<QGraphicsItem *>items = q->items(event->pos());
for (QGraphicsItem *item : items) {
if (!(item->acceptedMouseButtons() & event->button())) if (!(item->acceptedMouseButtons() & event->button()))
continue; continue;
@@ -291,7 +292,8 @@ const Function *Visualization::functionForItem(QGraphicsItem *item) const
QGraphicsItem *Visualization::itemForFunction(const Function *function) const QGraphicsItem *Visualization::itemForFunction(const Function *function) const
{ {
foreach (QGraphicsItem *item, items()) { const QList<QGraphicsItem *> itemList = items();
for (QGraphicsItem *item : itemList) {
if (functionForItem(item) == function) if (functionForItem(item) == function)
return item; return item;
} }

View File

@@ -100,7 +100,7 @@ QList<QAction *> MemcheckErrorView::customActions() const
QTC_ASSERT(!indizes.isEmpty(), return actions); QTC_ASSERT(!indizes.isEmpty(), return actions);
bool hasErrors = false; bool hasErrors = false;
foreach (const QModelIndex &index, indizes) { for (const QModelIndex &index : indizes) {
Error error = model()->data(index, ErrorListModel::ErrorRole).value<Error>(); Error error = model()->data(index, ErrorListModel::ErrorRole).value<Error>();
if (!error.suppression().isNull()) { if (!error.suppression().isNull()) {
hasErrors = true; hasErrors = true;

View File

@@ -284,7 +284,7 @@ static ErrorListModel::RelevantFrameFinder makeFrameFinder(const QStringList &pr
//find the first frame belonging to the project //find the first frame belonging to the project
if (!projectFiles.isEmpty()) { if (!projectFiles.isEmpty()) {
foreach (const Frame &frame, frames) { for (const Frame &frame : frames) {
if (frame.directory().isEmpty() || frame.fileName().isEmpty()) if (frame.directory().isEmpty() || frame.fileName().isEmpty())
continue; continue;
@@ -296,7 +296,7 @@ static ErrorListModel::RelevantFrameFinder makeFrameFinder(const QStringList &pr
} }
//if no frame belonging to the project was found, return the first one that is not malloc/new //if no frame belonging to the project was found, return the first one that is not malloc/new
foreach (const Frame &frame, frames) { for (const Frame &frame : frames) {
if (!frame.functionName().isEmpty() && frame.functionName() != "malloc" if (!frame.functionName().isEmpty() && frame.functionName() != "malloc"
&& !frame.functionName().startsWith("operator new(")) && !frame.functionName().startsWith("operator new("))
{ {
@@ -365,9 +365,10 @@ bool MemcheckErrorFilterProxyModel::filterAcceptsRow(int sourceRow, const QModel
QSet<QString> validFolders; QSet<QString> validFolders;
for (Project *project : SessionManager::projects()) { for (Project *project : SessionManager::projects()) {
validFolders << project->projectDirectory().toString(); validFolders << project->projectDirectory().toString();
foreach (Target *target, project->targets()) { const QList<Target *> targets = project->targets();
foreach (const DeployableFile &file, for (const Target *target : targets) {
target->deploymentData().allFiles()) { const QList<DeployableFile> files = target->deploymentData().allFiles();
for (const DeployableFile &file : files) {
if (file.isExecutable()) if (file.isExecutable())
validFolders << file.remoteDirectory(); validFolders << file.remoteDirectory();
} }
@@ -383,7 +384,7 @@ bool MemcheckErrorFilterProxyModel::filterAcceptsRow(int sourceRow, const QModel
bool inProject = false; bool inProject = false;
for (int i = 0; i < framesToLookAt; ++i) { for (int i = 0; i < framesToLookAt; ++i) {
const Frame &frame = frames.at(i); const Frame &frame = frames.at(i);
foreach (const QString &folder, validFolders) { for (const QString &folder : qAsConst(validFolders)) {
if (frame.directory().startsWith(folder)) { if (frame.directory().startsWith(folder)) {
inProject = true; inProject = true;
break; break;
@@ -633,7 +634,7 @@ MemcheckToolPrivate::MemcheckToolPrivate()
filterButton->setProperty("noArrow", true); filterButton->setProperty("noArrow", true);
m_filterMenu = new QMenu(filterButton); m_filterMenu = new QMenu(filterButton);
foreach (QAction *filterAction, m_errorFilterActions) for (QAction *filterAction : qAsConst(m_errorFilterActions))
m_filterMenu->addAction(filterAction); m_filterMenu->addAction(filterAction);
m_filterMenu->addSeparator(); m_filterMenu->addSeparator();
m_filterMenu->addAction(m_filterProjectAction); m_filterMenu->addAction(m_filterProjectAction);
@@ -934,9 +935,10 @@ void MemcheckToolPrivate::settingsDestroyed(QObject *settings)
void MemcheckToolPrivate::updateFromSettings() void MemcheckToolPrivate::updateFromSettings()
{ {
foreach (QAction *action, m_errorFilterActions) { for (QAction *action : qAsConst(m_errorFilterActions)) {
bool contained = true; bool contained = true;
foreach (const QVariant &v, action->data().toList()) { const QList<QVariant> actions = action->data().toList();
for (const QVariant &v : actions) {
bool ok; bool ok;
int kind = v.toInt(&ok); int kind = v.toInt(&ok);
if (ok && !m_settings->visibleErrorKinds.value().contains(kind)) if (ok && !m_settings->visibleErrorKinds.value().contains(kind))
@@ -1119,10 +1121,11 @@ void MemcheckToolPrivate::updateErrorFilter()
m_settings->filterExternalIssues.setValue(!m_filterProjectAction->isChecked()); m_settings->filterExternalIssues.setValue(!m_filterProjectAction->isChecked());
QList<int> errorKinds; QList<int> errorKinds;
foreach (QAction *a, m_errorFilterActions) { for (QAction *a : qAsConst(m_errorFilterActions)) {
if (!a->isChecked()) if (!a->isChecked())
continue; continue;
foreach (const QVariant &v, a->data().toList()) { const QList<QVariant> actions = a->data().toList();
for (const QVariant &v : actions) {
bool ok; bool ok;
int kind = v.toInt(&ok); int kind = v.toInt(&ok);
if (ok) if (ok)

View File

@@ -205,12 +205,13 @@ QString Error::toXml() const
stream << " <what>" << d->what << "</what>\n"; stream << " <what>" << d->what << "</what>\n";
} }
foreach (const Stack &stack, d->stacks) { for (const Stack &stack : qAsConst(d->stacks)) {
if (!stack.auxWhat().isEmpty()) if (!stack.auxWhat().isEmpty())
stream << " <auxwhat>" << stack.auxWhat() << "</auxwhat>\n"; stream << " <auxwhat>" << stack.auxWhat() << "</auxwhat>\n";
stream << " <stack>\n"; stream << " <stack>\n";
foreach (const Frame &frame, stack.frames()) { const QVector<Frame> frames = stack.frames();
for (const Frame &frame : frames) {
stream << " <frame>\n"; stream << " <frame>\n";
stream << " <ip>0x" << QString::number(frame.instructionPointer(), 16) << "</ip>\n"; stream << " <ip>0x" << QString::number(frame.instructionPointer(), 16) << "</ip>\n";
if (!frame.object().isEmpty()) if (!frame.object().isEmpty())

View File

@@ -165,10 +165,12 @@ ErrorItem::ErrorItem(const ErrorListModel *model, const Error &error)
// just annoy the user. // just annoy the user.
// The same goes for the frame level. // The same goes for the frame level.
if (m_error.stacks().count() > 1) { if (m_error.stacks().count() > 1) {
foreach (const Stack &s, m_error.stacks()) const QVector<Stack> stacks = m_error.stacks();
for (const Stack &s : stacks)
appendChild(new StackItem(s)); appendChild(new StackItem(s));
} else if (m_error.stacks().constFirst().frames().count() > 1) { } else if (m_error.stacks().constFirst().frames().count() > 1) {
foreach (const Frame &f, m_error.stacks().constFirst().frames()) const QVector<Frame> frames = m_error.stacks().constFirst().frames();
for (const Frame &f : frames)
appendChild(new FrameItem(f)); appendChild(new FrameItem(f));
} }
} }
@@ -197,11 +199,13 @@ QVariant ErrorItem::data(int column, int role) const
<< m_model->errorLocation(m_error) << m_model->errorLocation(m_error)
<< "\n"; << "\n";
foreach (const Stack &stack, m_error.stacks()) { const QVector<Stack> stacks = m_error.stacks();
for (const Stack &stack : stacks) {
if (!stack.auxWhat().isEmpty()) if (!stack.auxWhat().isEmpty())
stream << stack.auxWhat(); stream << stack.auxWhat();
int i = 1; int i = 1;
foreach (const Frame &frame, stack.frames()) const QVector<Frame> frames = stack.frames();
for (const Frame &frame : frames)
stream << " " << i++ << ": " << makeFrameName(frame, true) << "\n"; stream << " " << i++ << ": " << makeFrameName(frame, true) << "\n";
} }
@@ -229,7 +233,8 @@ QVariant ErrorItem::data(int column, int role) const
StackItem::StackItem(const Stack &stack) : m_stack(stack) StackItem::StackItem(const Stack &stack) : m_stack(stack)
{ {
foreach (const Frame &f, m_stack.frames()) const QVector<Frame> frames = m_stack.frames();
for (const Frame &f : frames)
appendChild(new FrameItem(f)); appendChild(new FrameItem(f));
} }

View File

@@ -64,7 +64,7 @@ QString toolTipForFrame(const Frame &frame)
"<style>dt { font-weight:bold; } dd { font-family: monospace; }</style>\n" "<style>dt { font-weight:bold; } dd { font-family: monospace; }</style>\n"
"</head><body><dl>"; "</head><body><dl>";
foreach (const StringPair &pair, lines) { for (const StringPair &pair : qAsConst(lines)) {
html += "<dt>"; html += "<dt>";
html += pair.first; html += pair.first;
html += "</dt><dd>"; html += "</dt><dd>";

View File

@@ -208,7 +208,7 @@ QString Suppression::toString() const
stream << "{\n"; stream << "{\n";
stream << indent << d->name << '\n'; stream << indent << d->name << '\n';
stream << indent << d->kind << '\n'; stream << indent << d->kind << '\n';
foreach (const SuppressionFrame &frame, d->frames) for (const SuppressionFrame &frame : qAsConst(d->frames))
stream << indent << frame.toString() << '\n'; stream << indent << frame.toString() << '\n';
stream << "}\n"; stream << "}\n";
return ret; return ret;