mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-01 13:25:45 +02:00
Bug 244054 - [formatter] macros after controlflow statements without braces: new line missing
This commit is contained in:
parent
955f724c97
commit
e4f3dd764f
2 changed files with 122 additions and 29 deletions
|
@ -1097,8 +1097,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
scribe.alignFragment(alignment, i);
|
scribe.alignFragment(alignment, i);
|
||||||
exceptionSpecification[i].accept(this);
|
exceptionSpecification[i].accept(this);
|
||||||
}
|
}
|
||||||
// preferences.insert_space_before_closing_paren_in_exception_specification_throw
|
if (peekNextToken() == Token.tRPAREN) {
|
||||||
scribe.printNextToken(Token.tRPAREN, scribe.printComment());
|
// preferences.insert_space_before_closing_paren_in_exception_specification_throw
|
||||||
|
scribe.printNextToken(Token.tRPAREN, scribe.printComment());
|
||||||
|
}
|
||||||
ok = true;
|
ok = true;
|
||||||
} catch (AlignmentException e) {
|
} catch (AlignmentException e) {
|
||||||
scribe.redoAlignment(e);
|
scribe.redoAlignment(e);
|
||||||
|
@ -1477,20 +1479,22 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
if (preferences.indent_body_declarations_compare_to_access_specifier) {
|
if (preferences.indent_body_declarations_compare_to_access_specifier) {
|
||||||
scribe.unIndent();
|
scribe.unIndent();
|
||||||
}
|
}
|
||||||
startNode(declaration);
|
if (startNode(declaration)) {
|
||||||
try {
|
try {
|
||||||
scribe.startNewLine();
|
scribe.startNewLine();
|
||||||
visit((ICPPASTVisibilityLabel)declaration);
|
visit((ICPPASTVisibilityLabel)declaration);
|
||||||
} finally {
|
} finally {
|
||||||
endOfNode(declaration);
|
endOfNode(declaration);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
startNode(declaration);
|
if (startNode(declaration)) {
|
||||||
try {
|
try {
|
||||||
scribe.startNewLine();
|
scribe.startNewLine();
|
||||||
formatDeclaration(declaration);
|
formatDeclaration(declaration);
|
||||||
} finally {
|
} finally {
|
||||||
endOfNode(declaration);
|
endOfNode(declaration);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (preferences.indent_body_declarations_compare_to_access_specifier) {
|
if (preferences.indent_body_declarations_compare_to_access_specifier) {
|
||||||
scribe.unIndent();
|
scribe.unIndent();
|
||||||
|
@ -2400,7 +2404,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
} finally {
|
} finally {
|
||||||
fInsideFor= false;
|
fInsideFor= false;
|
||||||
}
|
}
|
||||||
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_for);
|
if (peekNextToken() == Token.tRPAREN) {
|
||||||
|
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_for);
|
||||||
|
}
|
||||||
|
|
||||||
formatAction(line, node.getBody(), preferences.brace_position_for_block);
|
formatAction(line, node.getBody(), preferences.brace_position_for_block);
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
|
@ -2419,14 +2425,15 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
} else {
|
} else {
|
||||||
condExpr.accept(this);
|
condExpr.accept(this);
|
||||||
}
|
}
|
||||||
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_if);
|
if (peekNextToken() == Token.tRPAREN) {
|
||||||
|
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_if);
|
||||||
|
}
|
||||||
final IASTStatement thenStatement = node.getThenClause();
|
final IASTStatement thenStatement = node.getThenClause();
|
||||||
final IASTStatement elseStatement = node.getElseClause();
|
final IASTStatement elseStatement = node.getElseClause();
|
||||||
|
|
||||||
boolean thenStatementIsBlock = false;
|
boolean thenStatementIsBlock = false;
|
||||||
if (thenStatement != null) {
|
if (thenStatement != null) {
|
||||||
if (thenStatement instanceof IASTCompoundStatement) {
|
if (thenStatement instanceof IASTCompoundStatement && !startsWithMacroExpansion(thenStatement)) {
|
||||||
final IASTCompoundStatement block = (IASTCompoundStatement) thenStatement;
|
final IASTCompoundStatement block = (IASTCompoundStatement) thenStatement;
|
||||||
thenStatementIsBlock = true;
|
thenStatementIsBlock = true;
|
||||||
final List<IASTStatement> statements = Arrays.asList(block.getStatements());
|
final List<IASTStatement> statements = Arrays.asList(block.getStatements());
|
||||||
|
@ -2444,7 +2451,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
} else {
|
} else {
|
||||||
formatLeftCurlyBrace(line, preferences.brace_position_for_block);
|
formatLeftCurlyBrace(line, preferences.brace_position_for_block);
|
||||||
thenStatement.accept(this);
|
thenStatement.accept(this);
|
||||||
if (elseStatement != null && (preferences.insert_new_line_before_else_in_if_statement)) {
|
if (elseStatement != null && preferences.insert_new_line_before_else_in_if_statement) {
|
||||||
scribe.startNewLine();
|
scribe.startNewLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2692,7 +2699,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
}
|
}
|
||||||
wasACase = false;
|
wasACase = false;
|
||||||
wasAStatement = true;
|
wasAStatement = true;
|
||||||
} else if (statement instanceof IASTCompoundStatement) {
|
} else if (statement instanceof IASTCompoundStatement && !startsWithMacroExpansion(statement)) {
|
||||||
String bracePosition;
|
String bracePosition;
|
||||||
if (wasACase) {
|
if (wasACase) {
|
||||||
if (preferences.indent_switchstatements_compare_to_cases) {
|
if (preferences.indent_switchstatements_compare_to_cases) {
|
||||||
|
@ -2788,8 +2795,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
conditionDecl.accept(this);
|
conditionDecl.accept(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_while);
|
if (peekNextToken() == Token.tRPAREN) {
|
||||||
|
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_while);
|
||||||
|
}
|
||||||
formatAction(line, node.getBody(), preferences.brace_position_for_block);
|
formatAction(line, node.getBody(), preferences.brace_position_for_block);
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
}
|
}
|
||||||
|
@ -2906,10 +2914,10 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int nextTokenOffset= getNextTokenOffset();
|
// int nextTokenOffset= getNextTokenOffset();
|
||||||
if (nextTokenOffset < nodeEndOffset && nextTokenOffset >= startOffset && nextTokenOffset <= endOffset) {
|
// if (nextTokenOffset < nodeEndOffset && nextTokenOffset >= startOffset && nextTokenOffset <= endOffset) {
|
||||||
scribe.skipRange(startOffset, endOffset);
|
// scribe.skipRange(startOffset, endOffset);
|
||||||
}
|
// }
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2976,9 +2984,13 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
operand.accept(this);
|
operand.accept(this);
|
||||||
}
|
}
|
||||||
if (peekNextToken() != Token.tRPAREN) {
|
if (peekNextToken() != Token.tRPAREN) {
|
||||||
scribe.skipToToken(Token.tRPAREN);
|
if (!enclosedInMacroExpansion(operand)) {
|
||||||
|
scribe.skipToToken(Token.tRPAREN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (peekNextToken() == Token.tRPAREN) {
|
||||||
|
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_parenthesized_expression);
|
||||||
}
|
}
|
||||||
scribe.printNextToken(Token.tRPAREN, preferences.insert_space_before_closing_paren_in_parenthesized_expression);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void formatAction(final int line, final IASTStatement stmt, String brace_position) {
|
private void formatAction(final int line, final IASTStatement stmt, String brace_position) {
|
||||||
|
@ -3001,6 +3013,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
stmt.accept(this);
|
stmt.accept(this);
|
||||||
scribe.unIndent();
|
scribe.unIndent();
|
||||||
} else {
|
} else {
|
||||||
|
scribe.printTrailingComment();
|
||||||
scribe.startNewLine();
|
scribe.startNewLine();
|
||||||
scribe.indent();
|
scribe.indent();
|
||||||
stmt.accept(this);
|
stmt.accept(this);
|
||||||
|
@ -3009,7 +3022,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean startsWithMacroExpansion(IASTNode node) {
|
private static boolean startsWithMacroExpansion(IASTNode node) {
|
||||||
IASTNodeLocation[] locations= node.getNodeLocations();
|
IASTNodeLocation[] locations= node.getNodeLocations();
|
||||||
if (locations.length == 0) {
|
if (locations.length == 0) {
|
||||||
} else if (node instanceof IASTProblemHolder) {
|
} else if (node instanceof IASTProblemHolder) {
|
||||||
|
@ -3021,6 +3034,11 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean enclosedInMacroExpansion(IASTNode node) {
|
||||||
|
IASTNodeLocation[] locations= node.getNodeLocations();
|
||||||
|
return locations.length == 1 && locations[0] instanceof IASTMacroExpansionLocation;
|
||||||
|
}
|
||||||
|
|
||||||
private void formatBlock(IASTCompoundStatement block, String block_brace_position, boolean insertSpaceBeforeOpeningBrace, boolean indentStatements) {
|
private void formatBlock(IASTCompoundStatement block, String block_brace_position, boolean insertSpaceBeforeOpeningBrace, boolean indentStatements) {
|
||||||
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace);
|
formatOpeningBrace(block_brace_position, insertSpaceBeforeOpeningBrace);
|
||||||
IASTStatement[] statements = block.getStatements();
|
IASTStatement[] statements = block.getStatements();
|
||||||
|
|
|
@ -872,4 +872,79 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
public void testPreserveNecessarySpace_Bug250969() throws Exception {
|
public void testPreserveNecessarySpace_Bug250969() throws Exception {
|
||||||
assertFormatterResult();
|
assertFormatterResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#define FOREVER1 for(;;)
|
||||||
|
//#define FOREVER2 while(1)
|
||||||
|
//
|
||||||
|
//int main(int argc, char **argv) {
|
||||||
|
// FOREVER1 {
|
||||||
|
// doSomething();
|
||||||
|
// }
|
||||||
|
// FOREVER2 {
|
||||||
|
// doSomething();
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
//#define FOREVER1 for(;;)
|
||||||
|
//#define FOREVER2 while(1)
|
||||||
|
//
|
||||||
|
//int main(int argc, char **argv) {
|
||||||
|
// FOREVER1 {
|
||||||
|
// doSomething();
|
||||||
|
// }
|
||||||
|
// FOREVER2 {
|
||||||
|
// doSomething();
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
public void testFormatterProblemsWithForeverMacro() throws Exception {
|
||||||
|
assertFormatterResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
//#define BLOCK { }
|
||||||
|
//#define DOIT1() { }
|
||||||
|
//#define DOIT2() do { } while(false)
|
||||||
|
//#define ALWAYS if(true)
|
||||||
|
//#define NEVER if(false)
|
||||||
|
//#define FOREVER for(;;)
|
||||||
|
//
|
||||||
|
//void foo() {
|
||||||
|
// int i=0;
|
||||||
|
// if (true) DOIT1();
|
||||||
|
// if (true) DOIT2();
|
||||||
|
// for (;;) BLOCK
|
||||||
|
// ALWAYS BLOCK
|
||||||
|
// NEVER FOREVER BLOCK
|
||||||
|
// switch(i) {
|
||||||
|
// case 0: BLOCK
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
//#define BLOCK { }
|
||||||
|
//#define DOIT1() { }
|
||||||
|
//#define DOIT2() do { } while(false)
|
||||||
|
//#define ALWAYS if(true)
|
||||||
|
//#define NEVER if(false)
|
||||||
|
//#define FOREVER for(;;)
|
||||||
|
//
|
||||||
|
//void foo() {
|
||||||
|
// int i = 0;
|
||||||
|
// if (true)
|
||||||
|
// DOIT1();
|
||||||
|
// if (true)
|
||||||
|
// DOIT2();
|
||||||
|
// for (;;)
|
||||||
|
// BLOCK
|
||||||
|
// ALWAYS
|
||||||
|
// BLOCK
|
||||||
|
// NEVER
|
||||||
|
// FOREVER
|
||||||
|
// BLOCK
|
||||||
|
// switch (i) {
|
||||||
|
// case 0:
|
||||||
|
// BLOCK
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
public void testCompoundStatementAsMacro_Bug244928() throws Exception {
|
||||||
|
assertFormatterResult();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue