forked from qt-creator/qt-creator
C++: Support pretty printing of template enclosing scope
Change-Id: Ib228184e1259585eeac61b9196195c39a9550cb9 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
committed by
Orgad Shaneh
parent
e919746808
commit
e7eac98c7e
@@ -84,6 +84,7 @@ Overview::Overview()
|
|||||||
showFunctionSignatures(true),
|
showFunctionSignatures(true),
|
||||||
showDefaultArguments(true),
|
showDefaultArguments(true),
|
||||||
showTemplateParameters(false),
|
showTemplateParameters(false),
|
||||||
|
showEnclosingTemplate(false),
|
||||||
includeWhiteSpaceInOperatorName(true),
|
includeWhiteSpaceInOperatorName(true),
|
||||||
markedArgument(0),
|
markedArgument(0),
|
||||||
markedArgumentBegin(0),
|
markedArgumentBegin(0),
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ public:
|
|||||||
bool showFunctionSignatures: 1;
|
bool showFunctionSignatures: 1;
|
||||||
bool showDefaultArguments: 1;
|
bool showDefaultArguments: 1;
|
||||||
bool showTemplateParameters: 1;
|
bool showTemplateParameters: 1;
|
||||||
|
bool showEnclosingTemplate: 1;
|
||||||
bool includeWhiteSpaceInOperatorName: 1; /// "operator =()" vs "operator=()"
|
bool includeWhiteSpaceInOperatorName: 1; /// "operator =()" vs "operator=()"
|
||||||
|
|
||||||
unsigned markedArgument;
|
unsigned markedArgument;
|
||||||
|
|||||||
@@ -166,12 +166,13 @@ void TypePrettyPrinter::visit(Namespace *type)
|
|||||||
void TypePrettyPrinter::visit(Template *type)
|
void TypePrettyPrinter::visit(Template *type)
|
||||||
{
|
{
|
||||||
if (Symbol *d = type->declaration()) {
|
if (Symbol *d = type->declaration()) {
|
||||||
if (overview()->showTemplateParameters && ! _name.isEmpty()) {
|
const Overview &oo = *overview();
|
||||||
|
if (oo.showTemplateParameters && ! _name.isEmpty()) {
|
||||||
_name += QLatin1Char('<');
|
_name += QLatin1Char('<');
|
||||||
for (unsigned index = 0; index < type->templateParameterCount(); ++index) {
|
for (unsigned index = 0; index < type->templateParameterCount(); ++index) {
|
||||||
if (index)
|
if (index)
|
||||||
_name += QLatin1String(", ");
|
_name += QLatin1String(", ");
|
||||||
QString arg = overview()->prettyName(type->templateParameterAt(index)->name());
|
QString arg = oo.prettyName(type->templateParameterAt(index)->name());
|
||||||
if (arg.isEmpty()) {
|
if (arg.isEmpty()) {
|
||||||
arg += QLatin1Char('T');
|
arg += QLatin1Char('T');
|
||||||
arg += QString::number(index + 1);
|
arg += QString::number(index + 1);
|
||||||
@@ -181,6 +182,23 @@ void TypePrettyPrinter::visit(Template *type)
|
|||||||
_name += QLatin1Char('>');
|
_name += QLatin1Char('>');
|
||||||
}
|
}
|
||||||
acceptType(d->type());
|
acceptType(d->type());
|
||||||
|
if (oo.showEnclosingTemplate) {
|
||||||
|
QString templateScope = "template<";
|
||||||
|
for (unsigned i = 0, total = type->templateParameterCount(); i < total; ++i) {
|
||||||
|
if (Symbol *param = type->templateParameterAt(i)) {
|
||||||
|
if (i > 0)
|
||||||
|
templateScope.append(", ");
|
||||||
|
if (TypenameArgument *typenameArg = param->asTypenameArgument()) {
|
||||||
|
templateScope.append(QLatin1String(typenameArg->isClassDeclarator()
|
||||||
|
? "class " : "typename "));
|
||||||
|
templateScope.append(oo(typenameArg->name()));
|
||||||
|
} else if (Argument *arg = param->asArgument()) {
|
||||||
|
templateScope.append(operator()(arg->type(), oo(arg->name())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_text.prepend(templateScope + ">\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
prependCv(_fullySpecifiedType);
|
prependCv(_fullySpecifiedType);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,20 @@ static Argument *arg(const QString &name, const FullySpecifiedType &ty)
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static TypenameArgument *typenameArg(const QString &name, bool isClassDeclarator)
|
||||||
|
{
|
||||||
|
TypenameArgument *arg = new TypenameArgument(0, 0, nameId(name));
|
||||||
|
arg->setClassDeclarator(isClassDeclarator);
|
||||||
|
return arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Declaration *decl(const FullySpecifiedType &ty)
|
||||||
|
{
|
||||||
|
Declaration *d = new Declaration(0, 0, nameId(""));
|
||||||
|
d->setType(ty);
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
static FullySpecifiedType voidTy()
|
static FullySpecifiedType voidTy()
|
||||||
{ return FullySpecifiedType(new VoidType); }
|
{ return FullySpecifiedType(new VoidType); }
|
||||||
|
|
||||||
@@ -93,6 +107,14 @@ static FullySpecifiedType fnTy(const QString &name, const FullySpecifiedType &re
|
|||||||
return FullySpecifiedType(fn);
|
return FullySpecifiedType(fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static FullySpecifiedType templTy(const FullySpecifiedType &declTy, bool isClassDeclarator)
|
||||||
|
{
|
||||||
|
Template *templ = new Template(0, 0, nameId(""));
|
||||||
|
templ->addMember(typenameArg("T", isClassDeclarator));
|
||||||
|
templ->addMember(decl(declTy));
|
||||||
|
return FullySpecifiedType(templ);
|
||||||
|
}
|
||||||
|
|
||||||
static FullySpecifiedType refThis(const FullySpecifiedType &type)
|
static FullySpecifiedType refThis(const FullySpecifiedType &type)
|
||||||
{
|
{
|
||||||
FullySpecifiedType result(type);
|
FullySpecifiedType result(type);
|
||||||
@@ -425,6 +447,9 @@ void tst_TypePrettyPrinter::basic_data()
|
|||||||
|
|
||||||
addRow(fnTy("foo", ref(voidTy()), ptr(voidTy())), bindToNothing, "void &foo(void *)", "foo");
|
addRow(fnTy("foo", ref(voidTy()), ptr(voidTy())), bindToNothing, "void &foo(void *)", "foo");
|
||||||
addRow(fnTy("foo", ref(voidTy()), ptr(voidTy())), bindToAll, "void&foo(void*)", "foo");
|
addRow(fnTy("foo", ref(voidTy()), ptr(voidTy())), bindToAll, "void&foo(void*)", "foo");
|
||||||
|
|
||||||
|
addRow(templTy(fnTy("foo", voidTy(), voidTy()), true), bindToNothing, "template<class T>\nvoid foo()", "foo");
|
||||||
|
addRow(templTy(fnTy("foo", voidTy(), voidTy()), false), bindToNothing, "template<typename T>\nvoid foo()", "foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_TypePrettyPrinter::basic()
|
void tst_TypePrettyPrinter::basic()
|
||||||
@@ -436,6 +461,7 @@ void tst_TypePrettyPrinter::basic()
|
|||||||
|
|
||||||
Overview o;
|
Overview o;
|
||||||
o.showReturnTypes = true;
|
o.showReturnTypes = true;
|
||||||
|
o.showEnclosingTemplate = true;
|
||||||
o.starBindFlags = starBindFlags;
|
o.starBindFlags = starBindFlags;
|
||||||
TypePrettyPrinter pp(&o);
|
TypePrettyPrinter pp(&o);
|
||||||
QCOMPARE(pp(type, name), result);
|
QCOMPARE(pp(type, name), result);
|
||||||
|
|||||||
Reference in New Issue
Block a user