Commit 2960c735 authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

Debugger/CPP: Move helpers to simplify types to CPlusPlus-lib.



Make available for log simplification, etc.
Add autotest, fix broken std::set and MSVC.
Rubber-stamped-by: default avatarChristian Kamm <christian.d.kamm@nokia.com>
parent 68ddcab8
......@@ -37,11 +37,12 @@
#include <Overview.h>
#include <QtCore/QVarLengthArray>
#include <QtCore/QRegExp>
#include <QtCore/QDebug>
using namespace CPlusPlus;
namespace CPlusPlus {
class CPlusPlus::Rewrite
class Rewrite
{
public:
Rewrite(Control *control, SubstitutionEnvironment *env)
......@@ -414,18 +415,187 @@ FullySpecifiedType UseQualifiedNames::apply(const Name *name, Rewrite *rewrite)
}
FullySpecifiedType CPlusPlus::rewriteType(const FullySpecifiedType &type,
SubstitutionEnvironment *env,
Control *control)
FullySpecifiedType rewriteType(const FullySpecifiedType &type,
SubstitutionEnvironment *env,
Control *control)
{
Rewrite rewrite(control, env);
return rewrite.rewriteType(type);
}
const Name *CPlusPlus::rewriteName(const Name *name,
SubstitutionEnvironment *env,
Control *control)
const Name *rewriteName(const Name *name,
SubstitutionEnvironment *env,
Control *control)
{
Rewrite rewrite(control, env);
return rewrite.rewriteName(name);
}
// Simplify complicated STL template types,
// such as 'std::basic_string<char,std::char_traits<char>,std::allocator<char> >'
// -> 'std::string' and helpers.
static QString chopConst(QString type)
{
while (1) {
if (type.startsWith(QLatin1String("const")))
type = type.mid(5);
else if (type.startsWith(QLatin1Char(' ')))
type = type.mid(1);
else if (type.endsWith(QLatin1String("const")))
type.chop(5);
else if (type.endsWith(QLatin1Char(' ')))
type.chop(1);
else
break;
}
return type;
}
static inline QRegExp stdStringRegExp(const QString &charType)
{
QString rc = QLatin1String("basic_string<");
rc += charType;
rc += QLatin1String(",[ ]?std::char_traits<");
rc += charType;
rc += QLatin1String(">,[ ]?std::allocator<");
rc += charType;
rc += QLatin1String("> >");
const QRegExp re(rc);
Q_ASSERT(re.isValid());
return re;
}
// Simplify string types in a type
// 'std::set<std::basic_string<char... > >' -> std::set<std::string>'
static inline void simplifyStdString(const QString &charType, const QString &replacement,
QString *type)
{
QRegExp stringRegexp = stdStringRegExp(charType);
const int replacementSize = replacement.size();
for (int pos = 0; pos < type->size(); ) {
// Check next match
const int matchPos = stringRegexp.indexIn(*type, pos);
if (matchPos == -1)
break;
const int matchedLength = stringRegexp.matchedLength();
type->replace(matchPos, matchedLength, replacement);
pos = matchPos + replacementSize;
// If we were inside an 'allocator<std::basic_string..char > >'
// kill the following blank -> 'allocator<std::string>'
if (pos + 1 < type->size() && type->at(pos) == QLatin1Char(' ')
&& type->at(pos + 1) == QLatin1Char('>'))
type->remove(pos, 1);
}
}
// Fix 'std::allocator<std::string >' -> 'std::allocator<std::string>',
// which can happen when replacing/simplifying
static inline QString fixNestedTemplates(QString s)
{
const int size = s.size();
if (size > 3
&& s.at(size - 1) == QLatin1Char('>')
&& s.at(size - 2) == QLatin1Char(' ')
&& s.at(size - 3) != QLatin1Char('>'))
s.remove(size - 2, 1);
return s;
}
CPLUSPLUS_EXPORT QString simplifySTLType(const QString &typeIn)
{
QString type = typeIn;
if (type.startsWith("class ")) // MSVC prepends class,struct
type.remove(0, 6);
if (type.startsWith("struct "))
type.remove(0, 7);
type.replace(QLatin1Char('*'), QLatin1Char('@'));
for (int i = 0; i < 10; ++i) {
int start = type.indexOf("std::allocator<");
if (start == -1)
break;
// search for matching '>'
int pos;
int level = 0;
for (pos = start + 12; pos < type.size(); ++pos) {
int c = type.at(pos).unicode();
if (c == '<') {
++level;
} else if (c == '>') {
--level;
if (level == 0)
break;
}
}
const QString alloc = fixNestedTemplates(type.mid(start, pos + 1 - start).trimmed());
const QString inner = fixNestedTemplates(alloc.mid(15, alloc.size() - 16).trimmed());
if (inner == QLatin1String("char")) { // std::string
simplifyStdString(QLatin1String("char"), QLatin1String("string"), &type);
} else if (inner == QLatin1String("wchar_t")) { // std::wstring
simplifyStdString(QLatin1String("wchar_t"), QLatin1String("wstring"), &type);
} else if (inner == QLatin1String("unsigned short")) { // std::wstring/MSVC
simplifyStdString(QLatin1String("unsigned short"), QLatin1String("wstring"), &type);
}
// std::vector, std::deque, std::list
const QRegExp re1(QString::fromLatin1("(vector|list|deque)<%1, ?%2\\s*>").arg(inner, alloc));
Q_ASSERT(re1.isValid());
if (re1.indexIn(type) != -1)
type.replace(re1.cap(0), QString::fromLatin1("%1<%2>").arg(re1.cap(1), inner));
// std::stack
QRegExp stackRE(QString::fromLatin1("stack<%1, ?std::deque<%2> >").arg(inner, inner));
stackRE.setMinimal(true);
Q_ASSERT(stackRE.isValid());
if (stackRE.indexIn(type) != -1)
type.replace(stackRE.cap(0), QString::fromLatin1("stack<%1>").arg(inner));
// std::set
QRegExp setRE(QString::fromLatin1("set<%1, ?std::less<%2>, ?%3\\s*>").arg(inner, inner, alloc));
setRE.setMinimal(true);
Q_ASSERT(setRE.isValid());
if (setRE.indexIn(type) != -1)
type.replace(setRE.cap(0), QString::fromLatin1("set<%1>").arg(inner));
// std::map
if (inner.startsWith("std::pair<")) {
// search for outermost ',', split key and value
int pos;
int level = 0;
for (pos = 10; pos < inner.size(); ++pos) {
int c = inner.at(pos).unicode();
if (c == '<')
++level;
else if (c == '>')
--level;
else if (c == ',' && level == 0)
break;
}
const QString key = chopConst(inner.mid(10, pos - 10));
// Get value: MSVC: 'pair<a const ,b>', gcc: 'pair<const a, b>'
if (inner.at(++pos) == QLatin1Char(' '))
pos++;
QString value = inner.mid(pos, inner.size() - pos - 1).trimmed();
QRegExp mapRE1(QString("map<%1, ?%2, ?std::less<%3 ?>, ?%4\\s*>")
.arg(key, value, key, alloc));
mapRE1.setMinimal(true);
Q_ASSERT(mapRE1.isValid());
if (mapRE1.indexIn(type) != -1) {
type.replace(mapRE1.cap(0), QString("map<%1, %2>").arg(key, value));
} else {
QRegExp mapRE2(QString("map<const %1, ?%2, ?std::less<const %3>, ?%4\\s*>")
.arg(key, value, key, alloc));
mapRE2.setMinimal(true);
if (mapRE2.indexIn(type) != -1) {
type.replace(mapRE2.cap(0), QString("map<const %1, %2>").arg(key, value));
}
}
}
}
type.replace(QLatin1Char('@'), QLatin1Char('*'));
type.replace(QLatin1String(" >"), QLatin1String(">"));
return type;
}
} // namespace CPlusPlus
......@@ -104,6 +104,11 @@ CPLUSPLUS_EXPORT const Name *rewriteName(const Name *name,
SubstitutionEnvironment *env,
Control *control);
// Simplify complicated STL template types, such as
// 'std::basic_string<char,std::char_traits<char>,std::allocator<char> > '->
// 'std::string'.
CPLUSPLUS_EXPORT QString simplifySTLType(const QString &typeIn);
} // end of namespace CPlusPlus
#endif
......@@ -43,6 +43,8 @@
#include <utils/qtcassert.h>
#include <utils/savedaction.h>
#include <cplusplus/CppRewriter.h>
#include <QtCore/QDebug>
#include <QtCore/QEvent>
#include <QtCore/QFile>
......@@ -243,38 +245,6 @@ static QByteArray parentName(const QByteArray &iname)
return iname.left(pos);
}
static QString chopConst(QString type)
{
while (1) {
if (type.startsWith(QLatin1String("const")))
type = type.mid(5);
else if (type.startsWith(QLatin1Char(' ')))
type = type.mid(1);
else if (type.endsWith(QLatin1String("const")))
type.chop(5);
else if (type.endsWith(QLatin1Char(' ')))
type.chop(1);
else
break;
}
return type;
}
static inline QRegExp stdStringRegExp(const QString &charType)
{
QString rc = QLatin1String("basic_string<");
rc += charType;
rc += QLatin1String(",[ ]?std::char_traits<");
rc += charType;
rc += QLatin1String(">,[ ]?std::allocator<");
rc += charType;
rc += QLatin1String("> >");
const QRegExp re(rc);
Q_ASSERT(re.isValid());
return re;
}
static QString niceTypeHelper(const QByteArray &typeIn)
{
typedef QMap<QByteArray, QString> Cache;
......@@ -282,100 +252,9 @@ static QString niceTypeHelper(const QByteArray &typeIn)
const Cache::const_iterator it = cache.constFind(typeIn);
if (it != cache.constEnd())
return it.value();
QString type = QString::fromUtf8(typeIn);
type.replace(QLatin1Char('*'), QLatin1Char('@'));
for (int i = 0; i < 10; ++i) {
int start = type.indexOf("std::allocator<");
if (start == -1)
break;
// search for matching '>'
int pos;
int level = 0;
for (pos = start + 12; pos < type.size(); ++pos) {
int c = type.at(pos).unicode();
if (c == '<') {
++level;
} else if (c == '>') {
--level;
if (level == 0)
break;
}
}
QString alloc = type.mid(start, pos + 1 - start).trimmed();
QString inner = alloc.mid(15, alloc.size() - 16).trimmed();
if (inner == QLatin1String("char")) { // std::string
const QRegExp stringRegexp = stdStringRegExp(inner);
type.replace(stringRegexp, QLatin1String("string"));
} else if (inner == QLatin1String("wchar_t")) { // std::wstring
const QRegExp wchartStringRegexp = stdStringRegExp(inner);
type.replace(wchartStringRegexp, QLatin1String("wstring"));
} else if (inner == QLatin1String("unsigned short")) { // std::wstring/MSVC
const QRegExp usStringRegexp = stdStringRegExp(inner);
type.replace(usStringRegexp, QLatin1String("wstring"));
}
// std::vector, std::deque, std::list
const QRegExp re1(QString::fromLatin1("(vector|list|deque)<%1, ?%2\\s*>").arg(inner, alloc));
Q_ASSERT(re1.isValid());
if (re1.indexIn(type) != -1)
type.replace(re1.cap(0), QString::fromLatin1("%1<%2>").arg(re1.cap(1), inner));
// std::stack
QRegExp re6(QString::fromLatin1("stack<%1, ?std::deque<%2> >").arg(inner, inner));
if (!re6.isMinimal())
re6.setMinimal(true);
Q_ASSERT(re6.isValid());
if (re6.indexIn(type) != -1)
type.replace(re6.cap(0), QString::fromLatin1("stack<%1>").arg(inner));
// std::set
QRegExp re4(QString::fromLatin1("set<%1, ?std::less<%2>, ?%3\\s*>").arg(inner, inner, alloc));
if (!re4.isMinimal())
re4.setMinimal(true);
Q_ASSERT(re4.isValid());
if (re4.indexIn(type) != -1)
type.replace(re4.cap(0), QString::fromLatin1("set<%1>").arg(inner));
// std::map
if (inner.startsWith("std::pair<")) {
// search for outermost ','
int pos;
int level = 0;
for (pos = 10; pos < inner.size(); ++pos) {
int c = inner.at(pos).unicode();
if (c == '<')
++level;
else if (c == '>')
--level;
else if (c == ',' && level == 0)
break;
}
QString ckey = inner.mid(10, pos - 10);
QString key = chopConst(ckey);
QString value = inner.mid(pos + 2, inner.size() - 3 - pos).trimmed();
QRegExp re5(QString("map<%1, ?%2, ?std::less<%3 ?>, ?%4\\s*>")
.arg(key, value, key, alloc));
if (!re5.isMinimal())
re5.setMinimal(true);
Q_ASSERT(re5.isValid());
if (re5.indexIn(type) != -1) {
type.replace(re5.cap(0), QString("map<%1, %2>").arg(key, value));
} else {
QRegExp re7(QString("map<const %1, ?%2, ?std::less<const %3>, ?%4\\s*>")
.arg(key, value, key, alloc));
if (!re7.isMinimal())
re7.setMinimal(true);
if (re7.indexIn(type) != -1)
type.replace(re7.cap(0), QString("map<const %1, %2>").arg(key, value));
}
}
}
type.replace(QLatin1Char('@'), QLatin1Char('*'));
type.replace(QLatin1String(" >"), QLatin1String(">"));
cache.insert(typeIn, type); // For simplicity, also cache unmodified types
return type;
const QString simplified = CPlusPlus::simplifySTLType(typeIn);
cache.insert(typeIn, simplified); // For simplicity, also cache unmodified types
return simplified;
}
QString WatchModel::displayType(const WatchData &data) const
......
......@@ -228,6 +228,30 @@ void WatchWindow::mouseDoubleClickEvent(QMouseEvent *ev)
QTreeView::mouseDoubleClickEvent(ev);
}
// Text for add watch action with truncated expression
static inline QString addWatchActionText(QString exp)
{
if (exp.isEmpty())
return WatchWindow::tr("Watch Expression");
if (exp.size() > 30) {
exp.truncate(30);
exp.append(QLatin1String("..."));
}
return WatchWindow::tr("Watch Expression \"%1\"").arg(exp);
}
// Text for add watch action with truncated expression
static inline QString removeWatchActionText(QString exp)
{
if (exp.isEmpty())
return WatchWindow::tr("Remove Watch Expression");
if (exp.size() > 30) {
exp.truncate(30);
exp.append(QLatin1String("..."));
}
return WatchWindow::tr("Remove Watch Expression \"%1\"").arg(exp);
}
void WatchWindow::contextMenuEvent(QContextMenuEvent *ev)
{
DebuggerEngine *engine = currentEngine();
......@@ -369,15 +393,11 @@ void WatchWindow::contextMenuEvent(QContextMenuEvent *ev)
tr("Setting a watchpoint on an address will cause the program "
"to stop when the data at the address it modified."));
QString actionName = exp.isEmpty() ? tr("Watch Expression")
: tr("Watch Expression \"%1\"").arg(exp);
QAction *actWatchExpression = new QAction(actionName, &menu);
QAction *actWatchExpression = new QAction(addWatchActionText(exp), &menu);
actWatchExpression->setEnabled(canHandleWatches && !exp.isEmpty());
// Can remove watch if engine can handle it or session engine.
actionName = exp.isEmpty() ? tr("Remove Watch Expression")
: tr("Remove Watch Expression \"%1\"").arg(exp);
QAction *actRemoveWatchExpression = new QAction(actionName, &menu);
QAction *actRemoveWatchExpression = new QAction(removeWatchActionText(exp), &menu);
actRemoveWatchExpression->setEnabled(
(canHandleWatches || state == DebuggerNotReady) && !exp.isEmpty());
......
......@@ -9,4 +9,6 @@ SUBDIRS = \
lookup \
preprocessor \
semantic \
typeprettyprinter
typeprettyprinter \
simplifytypes
include(../../qttest.pri)
include(../shared/shared.pri)
SOURCES += tst_simplifytypestest.cpp
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include <cplusplus/CppRewriter.h>
#include <QtCore/QString>
#include <QtTest/QtTest>
const char *description[] =
{
"g++_stdstring",
"g++_stdwstring",
"g++_stringmap",
"g++_wstringmap",
"g++_stringlist",
"g++_stringset",
"g++_stringvector",
"g++_wstringvector",
"msvc_stdstring",
"msvc_stdwstring",
"msvc_stringmap",
"msvc_wstringmap",
"msvc_stringlist",
"msvc_stringset",
"msvc_stringvector",
"msvc_wstringvector",
};
const char *input[] =
{
// g++
"std::string",
"std::wstring",
"std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >",
"std::map<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, std::less<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, std::allocator<std::pair<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > > >",
"std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >",
"std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >",
"std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >",
"std::vector<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, std::allocator<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > >",
// MSVC
"class std::basic_string<char,std::char_traits<char>,std::allocator<char> >",
"class std::basic_string<unsigned short,std::char_traits<unsigned short>,std::allocator<unsigned short> >",
"class std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > >",
"class std::map<std::basic_string<unsigned short,std::char_traits<unsigned short>,std::allocator<unsigned short> >,std::basic_string<unsigned short,std::char_traits<unsigned short>,std::allocator<unsigned short> >,std::less<std::basic_string<unsigned short,std::char_traits<unsigned short>,std::allocator<unsigned short> > >,std::allocator<std::pair<std::basic_string<unsigned short,std::char_traits<unsigned short>,std::allocator<unsigned short> > const ,std::basic_string<unsigned short,std::char_traits<unsigned short>,std::allocator<unsigned short> > > > >",
"class std::list<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >",
"class std::set<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >",
"class std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >",
"class std::vector<std::basic_string<unsigned short,std::char_traits<unsigned short>,std::allocator<unsigned short> >,std::allocator<std::basic_string<unsigned short,std::char_traits<unsigned short>,std::allocator<unsigned short> > > >"
};
const char *output[] =
{
"std::string",
"std::wstring",
"std::map<std::string, std::string>",
"std::map<std::wstring, std::wstring>",
"std::list<std::string>",
"std::set<std::string>",
"std::vector<std::string>",
"std::vector<std::wstring>",
"std::string",
"std::wstring",
"std::map<std::string, std::string>",
"std::map<std::wstring, std::wstring>",
"std::list<std::string>",
"std::set<std::string>",
"std::vector<std::string>",
"std::vector<std::wstring>",
};
class SimplifyTypesTest : public QObject
{
Q_OBJECT
public:
SimplifyTypesTest();
private Q_SLOTS:
void testCase1();
void testCase1_data();
};
SimplifyTypesTest::SimplifyTypesTest()
{
}
void SimplifyTypesTest::testCase1()
{
QFETCH(QString, input);
QFETCH(QString, expected);
const QString output = CPlusPlus::simplifySTLType(input);
const bool ok = output == expected;
if (!ok) {
const QString msg = QString::fromAscii("Failure: got '%1' where '%2' was expected for '%3'")
.arg(output, expected, input);
QWARN(qPrintable(msg));
}
QVERIFY2(ok, "Failure");
}
void SimplifyTypesTest::testCase1_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("expected");
const size_t count = sizeof(input)/sizeof(const char *);
for (size_t i = 0; i < count; i++ )
QTest::newRow(description[i]) << QString::fromAscii(input[i])
<< QString::fromAscii(output[i]);
}
QTEST_APPLESS_MAIN(SimplifyTypesTest);
#include "tst_simplifytypestest.moc"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment