C++: Support pretty printing of template enclosing scope

Change-Id: Ib228184e1259585eeac61b9196195c39a9550cb9
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Orgad Shaneh
2016-07-31 23:54:05 +03:00
committed by Orgad Shaneh
parent e919746808
commit e7eac98c7e
4 changed files with 48 additions and 2 deletions

View File

@@ -84,6 +84,7 @@ Overview::Overview()
showFunctionSignatures(true),
showDefaultArguments(true),
showTemplateParameters(false),
showEnclosingTemplate(false),
includeWhiteSpaceInOperatorName(true),
markedArgument(0),
markedArgumentBegin(0),

View File

@@ -67,6 +67,7 @@ public:
bool showFunctionSignatures: 1;
bool showDefaultArguments: 1;
bool showTemplateParameters: 1;
bool showEnclosingTemplate: 1;
bool includeWhiteSpaceInOperatorName: 1; /// "operator =()" vs "operator=()"
unsigned markedArgument;

View File

@@ -166,12 +166,13 @@ void TypePrettyPrinter::visit(Namespace *type)
void TypePrettyPrinter::visit(Template *type)
{
if (Symbol *d = type->declaration()) {
if (overview()->showTemplateParameters && ! _name.isEmpty()) {
const Overview &oo = *overview();
if (oo.showTemplateParameters && ! _name.isEmpty()) {
_name += QLatin1Char('<');
for (unsigned index = 0; index < type->templateParameterCount(); ++index) {
if (index)
_name += QLatin1String(", ");
QString arg = overview()->prettyName(type->templateParameterAt(index)->name());
QString arg = oo.prettyName(type->templateParameterAt(index)->name());
if (arg.isEmpty()) {
arg += QLatin1Char('T');
arg += QString::number(index + 1);
@@ -181,6 +182,23 @@ void TypePrettyPrinter::visit(Template *type)
_name += QLatin1Char('>');
}
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);
}

View File

@@ -59,6 +59,20 @@ static Argument *arg(const QString &name, const FullySpecifiedType &ty)
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()
{ return FullySpecifiedType(new VoidType); }
@@ -93,6 +107,14 @@ static FullySpecifiedType fnTy(const QString &name, const FullySpecifiedType &re
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)
{
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())), 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()
@@ -436,6 +461,7 @@ void tst_TypePrettyPrinter::basic()
Overview o;
o.showReturnTypes = true;
o.showEnclosingTemplate = true;
o.starBindFlags = starBindFlags;
TypePrettyPrinter pp(&o);
QCOMPARE(pp(type, name), result);