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>
This commit is contained in:
Orgad Shaneh
2013-01-08 03:32:53 +02:00
committed by hjk
parent 73a2717bed
commit 29a93998df
396 changed files with 1856 additions and 3135 deletions

View File

@@ -898,9 +898,8 @@ bool Parser::parseConversionFunctionId(NameAST *&node)
return false;
unsigned operator_token = consumeToken();
SpecifierListAST *type_specifier = 0;
if (! parseTypeSpecifier(type_specifier)) {
if (! parseTypeSpecifier(type_specifier))
return false;
}
PtrOperatorListAST *ptr_operators = 0, **ptr_operators_tail = &ptr_operators;
while (parsePtrOperator(*ptr_operators_tail))
ptr_operators_tail = &(*ptr_operators_tail)->next;
@@ -1550,9 +1549,8 @@ bool Parser::parseDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specif
for (SpecifierListAST *iter = decl_specifier_list; !hasAuto && iter; iter = iter->next) {
SpecifierAST *spec = iter->value;
if (SimpleSpecifierAST *simpleSpec = spec->asSimpleSpecifier()) {
if (_translationUnit->tokenKind(simpleSpec->specifier_token) == T_AUTO) {
if (_translationUnit->tokenKind(simpleSpec->specifier_token) == T_AUTO)
hasAuto = true;
}
}
}
@@ -1567,9 +1565,8 @@ bool Parser::parseDeclarator(DeclaratorAST *&node, SpecifierListAST *decl_specif
} else if (LA() == T_LBRACKET) {
ArrayDeclaratorAST *ast = new (_pool) ArrayDeclaratorAST;
ast->lbracket_token = consumeToken();
if (LA() == T_RBRACKET || parseConstantExpression(ast->expression)) {
if (LA() == T_RBRACKET || parseConstantExpression(ast->expression))
match(T_RBRACKET, &ast->rbracket_token);
}
*postfix_ptr = new (_pool) PostfixDeclaratorListAST(ast);
postfix_ptr = &(*postfix_ptr)->next;
} else
@@ -1715,9 +1712,8 @@ bool Parser::parseEnumSpecifier(SpecifierListAST *&node)
skipUntil(T_IDENTIFIER);
}
if (parseEnumerator(*enumerator_ptr)) {
if (parseEnumerator(*enumerator_ptr))
enumerator_ptr = &(*enumerator_ptr)->next;
}
if (LA() == T_COMMA && LA(2) == T_RBRACE)
ast->stray_comma_token = consumeToken();
@@ -2182,11 +2178,10 @@ bool Parser::parseQtPropertyDeclaration(DeclarationAST *&node)
SimpleNameAST *property_name = new (_pool) SimpleNameAST;
// special case: keywords are allowed for property names!
if (tok().isKeyword()) {
if (tok().isKeyword())
property_name->identifier_token = consumeToken();
} else {
else
match(T_IDENTIFIER, &property_name->identifier_token);
}
ast->property_name = property_name;
QtPropertyDeclarationItemListAST **iter = &ast->property_declaration_item_list;
@@ -3484,11 +3479,10 @@ bool Parser::parseForStatement(StatementAST *&node)
ast->colon_token = consumeToken();
blockErrors(blocked);
if (LA() == T_LBRACE) {
if (LA() == T_LBRACE)
parseBracedInitList0x(ast->expression);
} else {
else
parseExpression(ast->expression);
}
match(T_RPAREN, &ast->rparen_token);
parseStatement(ast->statement);
@@ -5093,11 +5087,10 @@ bool Parser::parseNewArrayDeclarator(NewArrayDeclaratorListAST *&node)
bool Parser::parseNewInitializer(ExpressionAST *&node)
{
DEBUG_THIS_RULE();
if (LA() == T_LPAREN) {
if (LA() == T_LPAREN)
return parseExpressionListParen(node);
} else if (_cxx0xEnabled && LA() == T_LBRACE) {
else if (_cxx0xEnabled && LA() == T_LBRACE)
return parseBracedInitList0x(node);
}
return false;
}
@@ -5787,9 +5780,8 @@ bool Parser::parseObjCMethodDefinition(DeclarationAST *&node)
// - (void) foo; { body; }
// so a method is a forward declaration when it doesn't have a _body_.
// However, we still need to read the semicolon.
if (LA() == T_SEMICOLON) {
if (LA() == T_SEMICOLON)
ast->semicolon_token = consumeToken();
}
parseFunctionBody(ast->function_body);