2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-07-16 11:03:39 +02:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-07-16 11:03:39 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-07-16 11:03:39 +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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-07-16 11:03:39 +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
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-07-16 11:03:39 +02:00
|
|
|
#include "CppRewriter.h"
|
|
|
|
|
#include <TypeVisitor.h>
|
|
|
|
|
#include <NameVisitor.h>
|
|
|
|
|
#include <CoreTypes.h>
|
|
|
|
|
#include <Symbols.h>
|
|
|
|
|
#include <Literals.h>
|
|
|
|
|
#include <Names.h>
|
|
|
|
|
#include <Scope.h>
|
2010-07-23 14:16:10 +10:00
|
|
|
#include <Overview.h>
|
2010-07-16 11:03:39 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QVarLengthArray>
|
|
|
|
|
#include <QRegExp>
|
|
|
|
|
#include <QDebug>
|
2010-07-16 11:03:39 +02:00
|
|
|
|
2012-08-23 23:41:50 +02:00
|
|
|
#define QTC_ASSERT_STRINGIFY_HELPER(x) #x
|
|
|
|
|
#define QTC_ASSERT_STRINGIFY(x) QTC_ASSERT_STRINGIFY_HELPER(x)
|
|
|
|
|
#define QTC_ASSERT_STRING(cond) qDebug("SOFT ASSERT: \"" cond"\" in file " __FILE__ ", line " QTC_ASSERT_STRINGIFY(__LINE__))
|
|
|
|
|
#define QTC_ASSERT(cond, action) if (cond) {} else { QTC_ASSERT_STRING(#cond); action; } do {} while (0)
|
|
|
|
|
|
2010-11-25 13:51:54 +01:00
|
|
|
namespace CPlusPlus {
|
2010-07-16 11:03:39 +02:00
|
|
|
|
2010-11-25 13:51:54 +01:00
|
|
|
class Rewrite
|
2010-07-16 11:03:39 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2010-07-19 19:24:31 +02:00
|
|
|
Rewrite(Control *control, SubstitutionEnvironment *env)
|
2010-07-16 11:03:39 +02:00
|
|
|
: control(control), env(env), rewriteType(this), rewriteName(this) {}
|
|
|
|
|
|
|
|
|
|
class RewriteType: public TypeVisitor
|
|
|
|
|
{
|
|
|
|
|
Rewrite *rewrite;
|
|
|
|
|
QList<FullySpecifiedType> temps;
|
|
|
|
|
|
|
|
|
|
Control *control() const
|
|
|
|
|
{ return rewrite->control; }
|
|
|
|
|
|
|
|
|
|
void accept(const FullySpecifiedType &ty)
|
|
|
|
|
{
|
|
|
|
|
TypeVisitor::accept(ty.type());
|
|
|
|
|
unsigned flags = ty.flags();
|
|
|
|
|
flags |= temps.back().flags();
|
|
|
|
|
temps.back().setFlags(flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
RewriteType(Rewrite *r): rewrite(r) {}
|
|
|
|
|
|
|
|
|
|
FullySpecifiedType operator()(const FullySpecifiedType &ty)
|
|
|
|
|
{
|
|
|
|
|
accept(ty);
|
|
|
|
|
return temps.takeLast();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(UndefinedType *)
|
|
|
|
|
{
|
|
|
|
|
temps.append(FullySpecifiedType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(VoidType *)
|
|
|
|
|
{
|
|
|
|
|
temps.append(control()->voidType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(IntegerType *type)
|
|
|
|
|
{
|
|
|
|
|
temps.append(control()->integerType(type->kind()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(FloatType *type)
|
|
|
|
|
{
|
|
|
|
|
temps.append(control()->floatType(type->kind()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(PointerToMemberType *type)
|
|
|
|
|
{
|
|
|
|
|
const Name *memberName = rewrite->rewriteName(type->memberName());
|
|
|
|
|
const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
|
|
|
|
|
temps.append(control()->pointerToMemberType(memberName, elementType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(PointerType *type)
|
|
|
|
|
{
|
|
|
|
|
const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
|
|
|
|
|
temps.append(control()->pointerType(elementType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(ReferenceType *type)
|
|
|
|
|
{
|
|
|
|
|
const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
|
|
|
|
|
temps.append(control()->referenceType(elementType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(ArrayType *type)
|
|
|
|
|
{
|
|
|
|
|
const FullySpecifiedType elementType = rewrite->rewriteType(type->elementType());
|
|
|
|
|
temps.append(control()->arrayType(elementType, type->size()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(NamedType *type)
|
|
|
|
|
{
|
2010-07-19 19:24:31 +02:00
|
|
|
FullySpecifiedType ty = rewrite->env->apply(type->name(), rewrite);
|
2010-07-16 11:03:39 +02:00
|
|
|
if (! ty->isUndefinedType())
|
2010-07-20 15:04:50 +02:00
|
|
|
temps.append(ty);
|
2010-07-16 11:03:39 +02:00
|
|
|
else {
|
|
|
|
|
const Name *name = rewrite->rewriteName(type->name());
|
|
|
|
|
temps.append(control()->namedType(name));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(Function *type)
|
|
|
|
|
{
|
|
|
|
|
Function *funTy = control()->newFunction(0, 0);
|
|
|
|
|
funTy->copy(type);
|
2010-10-06 15:49:59 +02:00
|
|
|
funTy->setConst(type->isConst());
|
|
|
|
|
funTy->setVolatile(type->isVolatile());
|
2010-07-16 11:03:39 +02:00
|
|
|
|
|
|
|
|
funTy->setName(rewrite->rewriteName(type->name()));
|
|
|
|
|
|
|
|
|
|
funTy->setReturnType(rewrite->rewriteType(type->returnType()));
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < type->argumentCount(); ++i) {
|
|
|
|
|
Symbol *arg = type->argumentAt(i);
|
|
|
|
|
|
|
|
|
|
Argument *newArg = control()->newArgument(0, 0);
|
|
|
|
|
newArg->copy(arg);
|
|
|
|
|
newArg->setName(rewrite->rewriteName(arg->name()));
|
|
|
|
|
newArg->setType(rewrite->rewriteType(arg->type()));
|
|
|
|
|
|
2011-10-25 11:28:51 +02:00
|
|
|
// the copy() call above set the scope to 'type'
|
|
|
|
|
// reset it to 0 before adding addMember to avoid assert
|
|
|
|
|
newArg->resetScope();
|
2010-08-05 17:02:25 +02:00
|
|
|
funTy->addMember(newArg);
|
2010-07-16 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
temps.append(funTy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(Namespace *type)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO;
|
|
|
|
|
temps.append(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(Class *type)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO;
|
|
|
|
|
temps.append(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(Enum *type)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO;
|
|
|
|
|
temps.append(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(ForwardClassDeclaration *type)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO;
|
|
|
|
|
temps.append(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(ObjCClass *type)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO;
|
|
|
|
|
temps.append(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(ObjCProtocol *type)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO;
|
|
|
|
|
temps.append(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(ObjCMethod *type)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO;
|
|
|
|
|
temps.append(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(ObjCForwardClassDeclaration *type)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO;
|
|
|
|
|
temps.append(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(ObjCForwardProtocolDeclaration *type)
|
|
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO;
|
|
|
|
|
temps.append(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class RewriteName: public NameVisitor
|
|
|
|
|
{
|
|
|
|
|
Rewrite *rewrite;
|
|
|
|
|
QList<const Name *> temps;
|
|
|
|
|
|
|
|
|
|
Control *control() const
|
|
|
|
|
{ return rewrite->control; }
|
|
|
|
|
|
|
|
|
|
const Identifier *identifier(const Identifier *other) const
|
|
|
|
|
{
|
|
|
|
|
if (! other)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2010-08-11 14:24:28 +02:00
|
|
|
return control()->identifier(other->chars(), other->size());
|
2010-07-16 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
RewriteName(Rewrite *r): rewrite(r) {}
|
|
|
|
|
|
|
|
|
|
const Name *operator()(const Name *name)
|
|
|
|
|
{
|
|
|
|
|
if (! name)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
accept(name);
|
|
|
|
|
return temps.takeLast();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(const QualifiedNameId *name)
|
|
|
|
|
{
|
|
|
|
|
const Name *base = rewrite->rewriteName(name->base());
|
|
|
|
|
const Name *n = rewrite->rewriteName(name->name());
|
|
|
|
|
temps.append(control()->qualifiedNameId(base, n));
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-02 11:59:01 +02:00
|
|
|
virtual void visit(const Identifier *name)
|
2010-07-16 11:03:39 +02:00
|
|
|
{
|
2010-09-02 11:59:01 +02:00
|
|
|
temps.append(control()->identifier(name->chars(), name->size()));
|
2010-07-16 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(const TemplateNameId *name)
|
|
|
|
|
{
|
|
|
|
|
QVarLengthArray<FullySpecifiedType, 8> args(name->templateArgumentCount());
|
|
|
|
|
for (unsigned i = 0; i < name->templateArgumentCount(); ++i)
|
|
|
|
|
args[i] = rewrite->rewriteType(name->templateArgumentAt(i));
|
2013-01-12 22:05:41 +01:00
|
|
|
temps.append(control()->templateNameId(identifier(name->identifier()), name->isSpecialization(),
|
|
|
|
|
args.data(), args.size()));
|
2010-07-16 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(const DestructorNameId *name)
|
|
|
|
|
{
|
|
|
|
|
temps.append(control()->destructorNameId(identifier(name->identifier())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(const OperatorNameId *name)
|
|
|
|
|
{
|
|
|
|
|
temps.append(control()->operatorNameId(name->kind()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(const ConversionNameId *name)
|
|
|
|
|
{
|
|
|
|
|
FullySpecifiedType ty = rewrite->rewriteType(name->type());
|
|
|
|
|
temps.append(control()->conversionNameId(ty));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(const SelectorNameId *name)
|
|
|
|
|
{
|
|
|
|
|
QVarLengthArray<const Name *, 8> names(name->nameCount());
|
|
|
|
|
for (unsigned i = 0; i < name->nameCount(); ++i)
|
|
|
|
|
names[i] = rewrite->rewriteName(name->nameAt(i));
|
|
|
|
|
temps.append(control()->selectorNameId(names.constData(), names.size(), name->hasArguments()));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public: // attributes
|
|
|
|
|
Control *control;
|
2010-07-19 19:24:31 +02:00
|
|
|
SubstitutionEnvironment *env;
|
2010-07-16 11:03:39 +02:00
|
|
|
RewriteType rewriteType;
|
|
|
|
|
RewriteName rewriteName;
|
|
|
|
|
};
|
|
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
SubstitutionEnvironment::SubstitutionEnvironment()
|
|
|
|
|
: _scope(0)
|
2010-07-16 11:03:39 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
FullySpecifiedType SubstitutionEnvironment::apply(const Name *name, Rewrite *rewrite) const
|
2010-07-16 11:03:39 +02:00
|
|
|
{
|
2010-07-20 15:04:50 +02:00
|
|
|
if (name) {
|
|
|
|
|
for (int index = _substs.size() - 1; index != -1; --index) {
|
|
|
|
|
const Substitution *subst = _substs.at(index);
|
|
|
|
|
|
|
|
|
|
FullySpecifiedType ty = subst->apply(name, rewrite);
|
|
|
|
|
if (! ty->isUndefinedType())
|
|
|
|
|
return ty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FullySpecifiedType();
|
2010-07-16 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
void SubstitutionEnvironment::enter(Substitution *subst)
|
2010-07-16 11:03:39 +02:00
|
|
|
{
|
2010-07-20 15:04:50 +02:00
|
|
|
_substs.append(subst);
|
|
|
|
|
}
|
2010-07-16 11:03:39 +02:00
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
void SubstitutionEnvironment::leave()
|
|
|
|
|
{
|
|
|
|
|
_substs.removeLast();
|
|
|
|
|
}
|
2010-07-16 11:03:39 +02:00
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
Scope *SubstitutionEnvironment::scope() const
|
|
|
|
|
{
|
|
|
|
|
return _scope;
|
|
|
|
|
}
|
2010-07-16 11:03:39 +02:00
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
Scope *SubstitutionEnvironment::switchScope(Scope *scope)
|
|
|
|
|
{
|
|
|
|
|
Scope *previous = _scope;
|
|
|
|
|
_scope = scope;
|
|
|
|
|
return previous;
|
|
|
|
|
}
|
2010-07-16 11:03:39 +02:00
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
const LookupContext &SubstitutionEnvironment::context() const
|
|
|
|
|
{
|
|
|
|
|
return _context;
|
2010-07-16 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
void SubstitutionEnvironment::setContext(const LookupContext &context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
2010-07-16 11:03:39 +02:00
|
|
|
|
|
|
|
|
SubstitutionMap::SubstitutionMap()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SubstitutionMap::~SubstitutionMap()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SubstitutionMap::bind(const Name *name, const FullySpecifiedType &ty)
|
|
|
|
|
{
|
|
|
|
|
_map.append(qMakePair(name, ty));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FullySpecifiedType SubstitutionMap::apply(const Name *name, Rewrite *) const
|
|
|
|
|
{
|
|
|
|
|
for (int n = _map.size() - 1; n != -1; --n) {
|
|
|
|
|
const QPair<const Name *, FullySpecifiedType> &p = _map.at(n);
|
|
|
|
|
|
|
|
|
|
if (name->isEqualTo(p.first))
|
|
|
|
|
return p.second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FullySpecifiedType();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
|
2011-07-28 21:17:28 +02:00
|
|
|
UseMinimalNames::UseMinimalNames(ClassOrNamespace *target)
|
|
|
|
|
: _target(target)
|
2010-07-20 15:04:50 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-28 21:17:28 +02:00
|
|
|
UseMinimalNames::~UseMinimalNames()
|
2010-07-20 15:04:50 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-28 21:17:28 +02:00
|
|
|
FullySpecifiedType UseMinimalNames::apply(const Name *name, Rewrite *rewrite) const
|
2010-07-20 15:04:50 +02:00
|
|
|
{
|
|
|
|
|
SubstitutionEnvironment *env = rewrite->env;
|
|
|
|
|
Scope *scope = env->scope();
|
|
|
|
|
|
2012-09-18 22:12:07 +02:00
|
|
|
if (name->isTemplateNameId() ||
|
|
|
|
|
(name->isQualifiedNameId() && name->asQualifiedNameId()->name()->isTemplateNameId()))
|
2010-07-20 15:09:44 +02:00
|
|
|
return FullySpecifiedType();
|
|
|
|
|
|
2010-07-20 15:04:50 +02:00
|
|
|
if (! scope)
|
|
|
|
|
return FullySpecifiedType();
|
|
|
|
|
|
|
|
|
|
const LookupContext &context = env->context();
|
|
|
|
|
Control *control = rewrite->control;
|
|
|
|
|
|
|
|
|
|
const QList<LookupItem> results = context.lookup(name, scope);
|
|
|
|
|
foreach (const LookupItem &r, results) {
|
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 (Symbol *d = r.declaration())
|
2011-10-31 12:09:27 +01:00
|
|
|
return control->namedType(LookupContext::minimalName(d, _target, control));
|
2010-07-20 15:04:50 +02:00
|
|
|
|
|
|
|
|
return r.type();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FullySpecifiedType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-07-28 21:17:28 +02:00
|
|
|
UseQualifiedNames::UseQualifiedNames()
|
|
|
|
|
: UseMinimalNames(0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UseQualifiedNames::~UseQualifiedNames()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-11-25 13:51:54 +01:00
|
|
|
FullySpecifiedType rewriteType(const FullySpecifiedType &type,
|
|
|
|
|
SubstitutionEnvironment *env,
|
|
|
|
|
Control *control)
|
2010-07-16 11:03:39 +02:00
|
|
|
{
|
|
|
|
|
Rewrite rewrite(control, env);
|
|
|
|
|
return rewrite.rewriteType(type);
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-25 13:51:54 +01:00
|
|
|
const Name *rewriteName(const Name *name,
|
|
|
|
|
SubstitutionEnvironment *env,
|
|
|
|
|
Control *control)
|
2010-07-16 11:03:39 +02:00
|
|
|
{
|
|
|
|
|
Rewrite rewrite(control, env);
|
|
|
|
|
return rewrite.rewriteName(name);
|
|
|
|
|
}
|
2010-11-25 13:51:54 +01:00
|
|
|
|
|
|
|
|
// 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);
|
2012-08-23 23:41:50 +02:00
|
|
|
QTC_ASSERT(re.isValid(), /**/);
|
2010-11-25 13:51:54 +01:00
|
|
|
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;
|
2012-11-27 22:07:47 +02:00
|
|
|
if (type.startsWith(QLatin1String("class "))) // MSVC prepends class,struct
|
2010-11-25 13:51:54 +01:00
|
|
|
type.remove(0, 6);
|
2012-11-27 22:07:47 +02:00
|
|
|
if (type.startsWith(QLatin1String("struct ")))
|
2010-11-25 13:51:54 +01:00
|
|
|
type.remove(0, 7);
|
|
|
|
|
|
|
|
|
|
type.replace(QLatin1Char('*'), QLatin1Char('@'));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
2013-02-04 16:22:48 +01:00
|
|
|
// std::ifstream
|
|
|
|
|
QRegExp ifstreamRE(QLatin1String("std::basic_ifstream<char,\\s*std::char_traits<char>\\s*>"));
|
|
|
|
|
ifstreamRE.setMinimal(true);
|
|
|
|
|
QTC_ASSERT(ifstreamRE.isValid(), return typeIn);
|
|
|
|
|
if (ifstreamRE.indexIn(type) != -1)
|
|
|
|
|
type.replace(ifstreamRE.cap(0), QLatin1String("std::ifstream"));
|
|
|
|
|
|
|
|
|
|
// Anything with a std::allocator
|
2012-11-27 22:07:47 +02:00
|
|
|
int start = type.indexOf(QLatin1String("std::allocator<"));
|
2010-11-25 13:51:54 +01:00
|
|
|
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());
|
2012-08-24 12:35:12 +02:00
|
|
|
const QString allocEsc = QRegExp::escape(alloc);
|
|
|
|
|
const QString innerEsc = QRegExp::escape(inner);
|
2010-11-25 13:51:54 +01:00
|
|
|
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
|
2012-08-24 12:35:12 +02:00
|
|
|
QRegExp re1(QString::fromLatin1("(vector|list|deque)<%1, ?%2\\s*>").arg(innerEsc, allocEsc));
|
2012-08-23 23:41:50 +02:00
|
|
|
QTC_ASSERT(re1.isValid(), return typeIn);
|
2010-11-25 13:51:54 +01:00
|
|
|
if (re1.indexIn(type) != -1)
|
|
|
|
|
type.replace(re1.cap(0), QString::fromLatin1("%1<%2>").arg(re1.cap(1), inner));
|
|
|
|
|
|
|
|
|
|
// std::stack
|
2012-08-24 12:35:12 +02:00
|
|
|
QRegExp stackRE(QString::fromLatin1("stack<%1, ?std::deque<%2> >").arg(innerEsc, innerEsc));
|
2010-11-25 13:51:54 +01:00
|
|
|
stackRE.setMinimal(true);
|
2012-08-23 23:41:50 +02:00
|
|
|
QTC_ASSERT(stackRE.isValid(), return typeIn);
|
2010-11-25 13:51:54 +01:00
|
|
|
if (stackRE.indexIn(type) != -1)
|
|
|
|
|
type.replace(stackRE.cap(0), QString::fromLatin1("stack<%1>").arg(inner));
|
|
|
|
|
|
|
|
|
|
// std::set
|
2012-08-24 12:35:12 +02:00
|
|
|
QRegExp setRE(QString::fromLatin1("set<%1, ?std::less<%2>, ?%3\\s*>").arg(innerEsc, innerEsc, allocEsc));
|
2010-11-25 13:51:54 +01:00
|
|
|
setRE.setMinimal(true);
|
2012-08-23 23:41:50 +02:00
|
|
|
QTC_ASSERT(setRE.isValid(), return typeIn);
|
2010-11-25 13:51:54 +01:00
|
|
|
if (setRE.indexIn(type) != -1)
|
|
|
|
|
type.replace(setRE.cap(0), QString::fromLatin1("set<%1>").arg(inner));
|
|
|
|
|
|
|
|
|
|
// std::map
|
2012-11-27 22:07:47 +02:00
|
|
|
if (inner.startsWith(QLatin1String("std::pair<"))) {
|
2010-11-25 13:51:54 +01:00
|
|
|
// 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));
|
2012-08-24 12:35:12 +02:00
|
|
|
const QString keyEsc = QRegExp::escape(key);
|
2010-11-25 13:51:54 +01:00
|
|
|
// Get value: MSVC: 'pair<a const ,b>', gcc: 'pair<const a, b>'
|
|
|
|
|
if (inner.at(++pos) == QLatin1Char(' '))
|
|
|
|
|
pos++;
|
2012-08-24 12:35:12 +02:00
|
|
|
const QString value = inner.mid(pos, inner.size() - pos - 1).trimmed();
|
|
|
|
|
const QString valueEsc = QRegExp::escape(value);
|
2012-11-27 22:07:47 +02:00
|
|
|
QRegExp mapRE1(QString::fromLatin1("map<%1, ?%2, ?std::less<%3 ?>, ?%4\\s*>")
|
2012-08-24 12:35:12 +02:00
|
|
|
.arg(keyEsc, valueEsc, keyEsc, allocEsc));
|
2010-11-25 13:51:54 +01:00
|
|
|
mapRE1.setMinimal(true);
|
2012-08-23 23:41:50 +02:00
|
|
|
QTC_ASSERT(mapRE1.isValid(), return typeIn);
|
2010-11-25 13:51:54 +01:00
|
|
|
if (mapRE1.indexIn(type) != -1) {
|
2012-11-27 22:07:47 +02:00
|
|
|
type.replace(mapRE1.cap(0), QString::fromLatin1("map<%1, %2>").arg(key, value));
|
2010-11-25 13:51:54 +01:00
|
|
|
} else {
|
2012-11-27 22:07:47 +02:00
|
|
|
QRegExp mapRE2(QString::fromLatin1("map<const %1, ?%2, ?std::less<const %3>, ?%4\\s*>")
|
2012-08-24 12:35:12 +02:00
|
|
|
.arg(keyEsc, valueEsc, keyEsc, allocEsc));
|
2010-11-25 13:51:54 +01:00
|
|
|
mapRE2.setMinimal(true);
|
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 (mapRE2.indexIn(type) != -1)
|
2012-11-27 22:07:47 +02:00
|
|
|
type.replace(mapRE2.cap(0), QString::fromLatin1("map<const %1, %2>").arg(key, value));
|
2010-11-25 13:51:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
type.replace(QLatin1Char('@'), QLatin1Char('*'));
|
|
|
|
|
type.replace(QLatin1String(" >"), QLatin1String(">"));
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace CPlusPlus
|