C++: Simplify Bind::asStringLiteral

Pass the expression directly

Change-Id: I44421fad0a0251641608d266fe681c05b3631064
Reviewed-by: Przemyslaw Gorszkowski <pgorszkowski@gmail.com>
This commit is contained in:
Orgad Shaneh
2015-06-10 23:26:38 +03:00
committed by Orgad Shaneh
parent 72a34d8359
commit a968940494
2 changed files with 18 additions and 34 deletions

View File

@@ -551,7 +551,7 @@ void Bind::enumerator(EnumeratorAST *ast, Enum *symbol)
if (ExpressionAST *expr = ast->expression) { if (ExpressionAST *expr = ast->expression) {
const int firstToken = expr->firstToken(); const int firstToken = expr->firstToken();
const int lastToken = expr->lastToken(); const int lastToken = expr->lastToken();
const StringLiteral *constantValue = asStringLiteral(firstToken, lastToken); const StringLiteral *constantValue = asStringLiteral(expr);
const StringLiteral *resolvedValue = 0; const StringLiteral *resolvedValue = 0;
if (lastToken - firstToken == 1) { if (lastToken - firstToken == 1) {
if (const Identifier *constantId = identifier(firstToken)) if (const Identifier *constantId = identifier(firstToken))
@@ -1228,8 +1228,11 @@ FullySpecifiedType Bind::trailingReturnType(TrailingReturnTypeAST *ast, const Fu
return type; return type;
} }
const StringLiteral *Bind::asStringLiteral(unsigned firstToken, unsigned lastToken) const StringLiteral *Bind::asStringLiteral(const ExpressionAST *ast)
{ {
CPP_ASSERT(ast, return 0);
const unsigned firstToken = ast->firstToken();
const unsigned lastToken = ast->lastToken();
std::string buffer; std::string buffer;
for (unsigned index = firstToken; index != lastToken; ++index) { for (unsigned index = firstToken; index != lastToken; ++index) {
const Token &tk = tokenAt(index); const Token &tk = tokenAt(index);
@@ -1349,9 +1352,7 @@ bool Bind::visit(ForeachStatementAST *ast)
if (arrayType != 0) if (arrayType != 0)
type = arrayType->elementType(); type = arrayType->elementType();
else if (ast->expression != 0) { else if (ast->expression != 0) {
unsigned startOfExpression = ast->expression->firstToken(); const StringLiteral *sl = asStringLiteral(ast->expression);
unsigned endOfExpression = ast->expression->lastToken();
const StringLiteral *sl = asStringLiteral(startOfExpression, endOfExpression);
const std::string buff = std::string("*") + sl->chars() + ".begin()"; const std::string buff = std::string("*") + sl->chars() + ".begin()";
initializer = control()->stringLiteral(buff.c_str(), unsigned(buff.size())); initializer = control()->stringLiteral(buff.c_str(), unsigned(buff.size()));
} }
@@ -1399,9 +1400,7 @@ bool Bind::visit(RangeBasedForStatementAST *ast)
if (arrayType != 0) if (arrayType != 0)
type = arrayType->elementType(); type = arrayType->elementType();
else if (ast->expression != 0) { else if (ast->expression != 0) {
unsigned startOfExpression = ast->expression->firstToken(); const StringLiteral *sl = asStringLiteral(ast->expression);
unsigned endOfExpression = ast->expression->lastToken();
const StringLiteral *sl = asStringLiteral(startOfExpression, endOfExpression);
const std::string buff = std::string("*") + sl->chars() + ".begin()"; const std::string buff = std::string("*") + sl->chars() + ".begin()";
initializer = control()->stringLiteral(buff.c_str(), unsigned(buff.size())); initializer = control()->stringLiteral(buff.c_str(), unsigned(buff.size()));
} }
@@ -1664,13 +1663,8 @@ bool Bind::visit(ConditionAST *ast)
Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name); Declaration *decl = control()->newDeclaration(sourceLocation, declaratorId->name->name);
decl->setType(type); decl->setType(type);
if (type.isAuto() && translationUnit()->languageFeatures().cxx11Enabled) { if (type.isAuto() && translationUnit()->languageFeatures().cxx11Enabled)
const ExpressionAST *initializer = ast->declarator->initializer; decl->setInitializer(asStringLiteral(ast->declarator->initializer));
const unsigned startOfExpression = initializer->firstToken();
const unsigned endOfExpression = initializer->lastToken();
decl->setInitializer(asStringLiteral(startOfExpression, endOfExpression));
}
_scope->addMember(decl); _scope->addMember(decl);
} }
@@ -1923,8 +1917,7 @@ bool Bind::visit(SimpleDeclarationAST *ast)
methodKey = methodKeyForInvokableToken(tokenKind(ast->qt_invokable_token)); methodKey = methodKeyForInvokableToken(tokenKind(ast->qt_invokable_token));
// unsigned qt_invokable_token = ast->qt_invokable_token; // unsigned qt_invokable_token = ast->qt_invokable_token;
unsigned declTypeStartOfExpression = 0; const ExpressionAST *declTypeExpression = 0;
unsigned declTypeEndOfExpression = 0;
bool isTypedef = false; bool isTypedef = false;
FullySpecifiedType type; FullySpecifiedType type;
for (SpecifierListAST *it = ast->decl_specifier_list; it; it = it->next) { for (SpecifierListAST *it = ast->decl_specifier_list; it; it = it->next) {
@@ -1934,10 +1927,8 @@ bool Bind::visit(SimpleDeclarationAST *ast)
type.setTypedef(isTypedef); type.setTypedef(isTypedef);
if (type.isDecltype()) { if (type.isDecltype()) {
if (DecltypeSpecifierAST *decltypeSpec = it->value->asDecltypeSpecifier()) { if (DecltypeSpecifierAST *decltypeSpec = it->value->asDecltypeSpecifier())
declTypeStartOfExpression = decltypeSpec->expression->firstToken(); declTypeExpression = decltypeSpec->expression;
declTypeEndOfExpression = decltypeSpec->expression->lastToken();
}
} }
} }
@@ -1992,14 +1983,10 @@ bool Bind::visit(SimpleDeclarationAST *ast)
const ExpressionAST *initializer = it->value->initializer; const ExpressionAST *initializer = it->value->initializer;
if (!initializer && declaratorId) if (!initializer && declaratorId)
translationUnit()->error(location(declaratorId->name, ast->firstToken()), "auto-initialized variable must have an initializer"); translationUnit()->error(location(declaratorId->name, ast->firstToken()), "auto-initialized variable must have an initializer");
else if (initializer) { else if (initializer)
unsigned startOfExpression = initializer->firstToken(); decl->setInitializer(asStringLiteral(initializer));
unsigned endOfExpression = initializer->lastToken();
decl->setInitializer(asStringLiteral(startOfExpression, endOfExpression));
}
} else if (declTy.isDecltype()) { } else if (declTy.isDecltype()) {
decl->setInitializer(asStringLiteral(declTypeStartOfExpression, decl->setInitializer(asStringLiteral(declTypeExpression));
declTypeEndOfExpression));
} }
if (_scope->isClass()) { if (_scope->isClass()) {
@@ -2369,11 +2356,8 @@ bool Bind::visit(ParameterDeclarationAST *ast)
Argument *arg = control()->newArgument(location(declaratorId, ast->firstToken()), argName); Argument *arg = control()->newArgument(location(declaratorId, ast->firstToken()), argName);
arg->setType(type); arg->setType(type);
if (ast->expression) { if (ast->expression)
unsigned startOfExpression = ast->expression->firstToken(); arg->setInitializer(asStringLiteral(ast->expression));
unsigned endOfExpression = ast->expression->lastToken();
arg->setInitializer(asStringLiteral(startOfExpression, endOfExpression));
}
_scope->addMember(arg); _scope->addMember(arg);

View File

@@ -105,7 +105,7 @@ protected:
void capture(CaptureAST *ast); void capture(CaptureAST *ast);
Function *lambdaDeclarator(LambdaDeclaratorAST *ast); Function *lambdaDeclarator(LambdaDeclaratorAST *ast);
FullySpecifiedType trailingReturnType(TrailingReturnTypeAST *ast, const FullySpecifiedType &init); FullySpecifiedType trailingReturnType(TrailingReturnTypeAST *ast, const FullySpecifiedType &init);
const StringLiteral *asStringLiteral(unsigned firstToken, unsigned lastToken); const StringLiteral *asStringLiteral(const ExpressionAST *ast);
virtual bool preVisit(AST *); virtual bool preVisit(AST *);
virtual void postVisit(AST *); virtual void postVisit(AST *);