2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-04-04 14:39:27 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2011-04-04 14:39:27 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-04-04 14:39:27 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2011-04-04 14:39:27 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2011-04-04 14:39:27 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
#include "callgrindfunction.h"
|
2013-03-12 11:54:29 +01:00
|
|
|
#include "callgrindfunction_p.h"
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
#include "callgrindfunctioncall.h"
|
|
|
|
|
#include "callgrindcostitem.h"
|
|
|
|
|
#include "callgrindparsedata.h"
|
|
|
|
|
|
2011-05-18 17:05:49 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QString>
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QCoreApplication>
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
namespace Valgrind {
|
|
|
|
|
namespace Callgrind {
|
|
|
|
|
|
|
|
|
|
//BEGIN Function::Private
|
|
|
|
|
|
|
|
|
|
Function::Private::Private(const ParseData *data)
|
2011-05-18 17:05:49 +02:00
|
|
|
: m_data(data)
|
|
|
|
|
, m_fileId(-1)
|
|
|
|
|
, m_objectId(-1)
|
|
|
|
|
, m_nameId(-1)
|
|
|
|
|
, m_selfCost(data->events().size(), 0)
|
|
|
|
|
, m_inclusiveCost(data->events().size(), 0)
|
|
|
|
|
, m_called(0)
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Function::Private::~Private()
|
|
|
|
|
{
|
|
|
|
|
// we don't own m_callers
|
|
|
|
|
// we own the costitem which in turn owns the callees,
|
|
|
|
|
// so only delete the former
|
|
|
|
|
qDeleteAll(m_costItems);
|
|
|
|
|
|
|
|
|
|
qDeleteAll(m_outgoingCalls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Function::Private::accumulateCost(QVector<quint64> &base, const QVector<quint64> &add)
|
|
|
|
|
{
|
|
|
|
|
if (base.isEmpty()) {
|
|
|
|
|
base = add;
|
|
|
|
|
} else {
|
|
|
|
|
///TODO: see whether .data() is noticably faster (less detaching)
|
|
|
|
|
int i = 0;
|
2011-05-11 16:26:34 +02:00
|
|
|
foreach (quint64 cost, add)
|
2011-04-04 14:39:27 +02:00
|
|
|
base[i++] += cost;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FunctionCall *Function::Private::accumulateCall(const FunctionCall *call, CallType type)
|
|
|
|
|
{
|
|
|
|
|
const Function *key = (type == Incoming) ? call->caller() : call->callee();
|
|
|
|
|
QHash<const Function *, FunctionCall *> &callMap = (type == Incoming) ? m_incomingCallMap : m_outgoingCallMap;
|
|
|
|
|
|
|
|
|
|
FunctionCall *accumulatedCall = callMap.value(key, 0);
|
|
|
|
|
if (!accumulatedCall) {
|
|
|
|
|
accumulatedCall = new FunctionCall;
|
|
|
|
|
if (type == Incoming)
|
|
|
|
|
m_incomingCalls << accumulatedCall;
|
|
|
|
|
else
|
|
|
|
|
m_outgoingCalls << accumulatedCall;
|
|
|
|
|
|
|
|
|
|
accumulatedCall->setCallee(call->callee());
|
|
|
|
|
accumulatedCall->setCaller(call->caller());
|
|
|
|
|
///TODO: could the destinations differ from call to call? they should not, or?
|
|
|
|
|
accumulatedCall->setDestinations(call->destinations());
|
|
|
|
|
callMap.insert(key, accumulatedCall);
|
|
|
|
|
|
|
|
|
|
accumulatedCall->setCosts(call->costs());
|
|
|
|
|
} else {
|
|
|
|
|
QVector<quint64> costs = accumulatedCall->costs();
|
|
|
|
|
accumulateCost(costs, call->costs());
|
|
|
|
|
accumulatedCall->setCosts(costs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accumulatedCall->setCalls(accumulatedCall->calls() + call->calls());
|
|
|
|
|
return accumulatedCall;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//BEGIN Function
|
|
|
|
|
Function::Function(const ParseData *data)
|
2011-05-18 17:05:49 +02:00
|
|
|
: d(new Private(data))
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Function::Function(Function::Private *d)
|
2011-05-18 17:05:49 +02:00
|
|
|
: d(d)
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Function::~Function()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 Function::nameId() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_nameId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Function::name() const
|
|
|
|
|
{
|
|
|
|
|
if (d->m_nameId != -1)
|
|
|
|
|
return d->m_data->stringForFunctionCompression(d->m_nameId);
|
|
|
|
|
else
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Function::setName(qint64 id)
|
|
|
|
|
{
|
|
|
|
|
d->m_nameId = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 Function::fileId() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_fileId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Function::file() const
|
|
|
|
|
{
|
|
|
|
|
if (d->m_fileId != -1)
|
|
|
|
|
return d->m_data->stringForFileCompression(d->m_fileId);
|
|
|
|
|
else
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Function::setFile(qint64 id)
|
|
|
|
|
{
|
|
|
|
|
d->m_fileId = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 Function::objectId() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_objectId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Function::object() const
|
|
|
|
|
{
|
|
|
|
|
if (d->m_objectId != -1)
|
|
|
|
|
return d->m_data->stringForObjectCompression(d->m_objectId);
|
|
|
|
|
else
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Function::setObject(qint64 id)
|
|
|
|
|
{
|
|
|
|
|
d->m_objectId = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Function::location() const
|
|
|
|
|
{
|
|
|
|
|
QString pos;
|
2011-05-11 16:26:34 +02:00
|
|
|
foreach (const CostItem *costItem, d->m_costItems) {
|
2011-04-04 14:39:27 +02:00
|
|
|
if (costItem->differingFileId() != -1) {
|
|
|
|
|
QTextStream stream(&pos);
|
|
|
|
|
stream << '(';
|
2011-05-11 16:26:34 +02:00
|
|
|
for (int i = 0, c = costItem->positions().count(); i < c; ++i) {
|
2011-04-04 14:39:27 +02:00
|
|
|
///TODO: remember what was hex formatted
|
|
|
|
|
stream << costItem->position(i);
|
|
|
|
|
if (i != c - 1)
|
|
|
|
|
stream << ", ";
|
|
|
|
|
}
|
|
|
|
|
stream << ')';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
QString f = file();
|
|
|
|
|
|
|
|
|
|
if (!f.isEmpty()) {
|
|
|
|
|
QFileInfo info(f);
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (info.exists())
|
2011-04-04 14:39:27 +02:00
|
|
|
f = info.canonicalFilePath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString o = object();
|
|
|
|
|
if (o.isEmpty())
|
|
|
|
|
return QString();
|
2012-11-26 15:02:17 +02:00
|
|
|
if (f.isEmpty() || f == QLatin1String("???"))
|
2011-04-04 14:39:27 +02:00
|
|
|
return o;
|
|
|
|
|
if (pos.isEmpty())
|
2011-06-15 17:20:33 +02:00
|
|
|
return QCoreApplication::translate("Valgrind::Callgrind::Function", "%1 in %2").arg(f, o);
|
2011-04-04 14:39:27 +02:00
|
|
|
|
2011-06-15 17:20:33 +02:00
|
|
|
return QCoreApplication::translate("Valgrind::Callgrind::Function", "%1:%2 in %3").arg(f, pos, o);
|
2011-04-04 14:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Function::lineNumber() const
|
|
|
|
|
{
|
|
|
|
|
const int lineIdx = d->m_data->lineNumberPositionIndex();
|
|
|
|
|
if (lineIdx == -1)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2011-05-11 16:26:34 +02:00
|
|
|
foreach (const CostItem *costItem, d->m_costItems) {
|
2011-04-04 14:39:27 +02:00
|
|
|
if (costItem->differingFileId() == -1)
|
|
|
|
|
return costItem->position(lineIdx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quint64 Function::selfCost(int event) const
|
|
|
|
|
{
|
|
|
|
|
return d->m_selfCost.at(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVector< quint64 > Function::selfCosts() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_selfCost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quint64 Function::inclusiveCost(int event) const
|
|
|
|
|
{
|
|
|
|
|
return d->m_inclusiveCost.at(event) + d->m_selfCost.at(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVector<const FunctionCall *> Function::outgoingCalls() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_outgoingCalls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Function::addOutgoingCall(const FunctionCall *call)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(call->caller() == this, return);
|
|
|
|
|
|
|
|
|
|
d->accumulateCall(call, Private::Outgoing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVector<const FunctionCall *> Function::incomingCalls() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_incomingCalls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Function::addIncomingCall(const FunctionCall *call)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(call->callee() == this, return);
|
|
|
|
|
d->m_called += call->calls();
|
|
|
|
|
d->accumulateCall(call, Private::Incoming);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quint64 Function::called() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_called;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVector<const CostItem *> Function::costItems() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_costItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Function::addCostItem(const CostItem *item)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!d->m_costItems.contains(item), return);
|
|
|
|
|
|
|
|
|
|
d->m_costItems.append(item);
|
|
|
|
|
|
|
|
|
|
// accumulate costs
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (item->call())
|
2011-04-04 14:39:27 +02:00
|
|
|
d->accumulateCost(d->m_inclusiveCost, item->costs());
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
else
|
2011-04-04 14:39:27 +02:00
|
|
|
d->accumulateCost(d->m_selfCost, item->costs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Function::finalize()
|
|
|
|
|
{
|
|
|
|
|
bool recursive = false;
|
2011-05-11 16:26:34 +02:00
|
|
|
foreach (const FunctionCall *call, d->m_incomingCalls) {
|
2011-04-04 14:39:27 +02:00
|
|
|
if (call->caller() == this) {
|
|
|
|
|
recursive = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (recursive) {
|
|
|
|
|
// now handle recursive calls by setting the incl cost to the sum of all (external) calls
|
|
|
|
|
// to this function
|
|
|
|
|
// 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 + ...
|
|
|
|
|
d->m_inclusiveCost.fill(0);
|
2011-05-11 16:26:34 +02:00
|
|
|
foreach (const FunctionCall *call, d->m_incomingCalls) {
|
2011-04-04 14:39:27 +02:00
|
|
|
if (call->caller() != this) {
|
2011-05-11 16:26:34 +02:00
|
|
|
foreach (const CostItem *costItem, call->caller()->costItems()) {
|
2011-04-04 14:39:27 +02:00
|
|
|
if (costItem->call() && costItem->call()->callee() == this)
|
|
|
|
|
d->accumulateCost(d->m_inclusiveCost, costItem->costs());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-10 15:19:38 +02:00
|
|
|
// now subtract self cost (see @c inclusiveCost() implementation)
|
2011-05-11 16:26:34 +02:00
|
|
|
for (int i = 0, c = d->m_inclusiveCost.size(); i < c; ++i) {
|
2011-04-04 14:39:27 +02:00
|
|
|
if (d->m_inclusiveCost.at(i) < d->m_selfCost.at(i))
|
|
|
|
|
d->m_inclusiveCost[i] = 0;
|
|
|
|
|
else
|
|
|
|
|
d->m_inclusiveCost[i] -= d->m_selfCost.at(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:05:49 +02:00
|
|
|
} // namespace Callgrind
|
|
|
|
|
} // namespace Valgrind
|