mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-08 18:26:01 +02:00
Bug 242053 - [formatter] is confused by some macros
Bug 242232 - [Formatter] White space isn't inserted before the first semicolon in 'for' statement
This commit is contained in:
parent
8e70b3930f
commit
18540abf27
2 changed files with 71 additions and 21 deletions
|
@ -318,7 +318,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTDeclaration node) {
|
public int visit(IASTDeclaration node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
int indentLevel= scribe.indentationLevel;
|
int indentLevel= scribe.indentationLevel;
|
||||||
try {
|
try {
|
||||||
if (node instanceof IASTFunctionDefinition) {
|
if (node instanceof IASTFunctionDefinition) {
|
||||||
|
@ -370,7 +370,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTName node) {
|
public int visit(IASTName node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
if (node instanceof ICPPASTQualifiedName) {
|
if (node instanceof ICPPASTQualifiedName) {
|
||||||
visit((ICPPASTQualifiedName)node);
|
visit((ICPPASTQualifiedName)node);
|
||||||
|
@ -401,7 +401,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
if (node instanceof IASTInitializerExpression) {
|
if (node instanceof IASTInitializerExpression) {
|
||||||
visit((IASTInitializerExpression)node);
|
visit((IASTInitializerExpression)node);
|
||||||
|
@ -423,7 +423,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTParameterDeclaration node) {
|
public int visit(IASTParameterDeclaration node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
// decl-specifier
|
// decl-specifier
|
||||||
final IASTDeclSpecifier declSpec= node.getDeclSpecifier();
|
final IASTDeclSpecifier declSpec= node.getDeclSpecifier();
|
||||||
|
@ -450,7 +450,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTDeclarator node) {
|
public int visit(IASTDeclarator node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// common to all declarators
|
// common to all declarators
|
||||||
|
@ -527,7 +527,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTDeclSpecifier node) {
|
public int visit(IASTDeclSpecifier node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
if (node instanceof ICPPASTCompositeTypeSpecifier) {
|
if (node instanceof ICPPASTCompositeTypeSpecifier) {
|
||||||
visit((ICPPASTCompositeTypeSpecifier)node);
|
visit((ICPPASTCompositeTypeSpecifier)node);
|
||||||
|
@ -555,7 +555,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTExpression node) {
|
public int visit(IASTExpression node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
if (node instanceof IASTArraySubscriptExpression) {
|
if (node instanceof IASTArraySubscriptExpression) {
|
||||||
visit((IASTArraySubscriptExpression)node);
|
visit((IASTArraySubscriptExpression)node);
|
||||||
|
@ -603,7 +603,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTStatement node) {
|
public int visit(IASTStatement node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
int indentLevel= scribe.indentationLevel;
|
int indentLevel= scribe.indentationLevel;
|
||||||
try {
|
try {
|
||||||
if (node instanceof IASTCompoundStatement) {
|
if (node instanceof IASTCompoundStatement) {
|
||||||
|
@ -665,7 +665,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTTypeId node) {
|
public int visit(IASTTypeId node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
if (node instanceof IASTProblemHolder) {
|
if (node instanceof IASTProblemHolder) {
|
||||||
throw new ASTProblemException(((IASTProblemHolder)node).getProblem());
|
throw new ASTProblemException(((IASTProblemHolder)node).getProblem());
|
||||||
|
@ -695,7 +695,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTEnumerator enumerator) {
|
public int visit(IASTEnumerator enumerator) {
|
||||||
startNode(enumerator);
|
if (!startNode(enumerator)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
// name
|
// name
|
||||||
enumerator.getName().accept(this);
|
enumerator.getName().accept(this);
|
||||||
|
@ -720,7 +720,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(ICPPASTBaseSpecifier specifier) {
|
public int visit(ICPPASTBaseSpecifier specifier) {
|
||||||
startNode(specifier);
|
if (!startNode(specifier)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
boolean needSpace= false;
|
boolean needSpace= false;
|
||||||
loop: while (true) {
|
loop: while (true) {
|
||||||
|
@ -752,7 +752,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(ICPPASTNamespaceDefinition node) {
|
public int visit(ICPPASTNamespaceDefinition node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
final int line= scribe.line;
|
final int line= scribe.line;
|
||||||
// namespace <name>
|
// namespace <name>
|
||||||
|
@ -878,7 +878,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int visit(ICPPASTTemplateParameter node) {
|
public int visit(ICPPASTTemplateParameter node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
if (node instanceof ICPPASTSimpleTypeTemplateParameter) {
|
if (node instanceof ICPPASTSimpleTypeTemplateParameter) {
|
||||||
visit((ICPPASTSimpleTypeTemplateParameter)node);
|
visit((ICPPASTSimpleTypeTemplateParameter)node);
|
||||||
|
@ -955,7 +955,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int visit(ICPPASTConstructorInitializer node) {
|
private int visit(ICPPASTConstructorInitializer node) {
|
||||||
startNode(node);
|
if (!startNode(node)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
// format like a function call
|
// format like a function call
|
||||||
formatFunctionCallArguments(node.getExpression());
|
formatFunctionCallArguments(node.getExpression());
|
||||||
|
@ -1247,11 +1247,11 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
align.fSpaceBeforeComma= preferences.insert_space_before_comma_in_declarator_list;
|
align.fSpaceBeforeComma= preferences.insert_space_before_comma_in_declarator_list;
|
||||||
formatList(declarators, align, false, false);
|
formatList(declarators, align, false, false);
|
||||||
}
|
}
|
||||||
if (fExpectSemicolonAfterDeclaration && !fInsideFor) {
|
if (fExpectSemicolonAfterDeclaration) {
|
||||||
if (peekNextToken() != Token.tSEMI) {
|
if (peekNextToken() != Token.tSEMI) {
|
||||||
scribe.skipToToken(Token.tSEMI);
|
scribe.skipToToken(Token.tSEMI);
|
||||||
}
|
}
|
||||||
scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon);
|
scribe.printNextToken(Token.tSEMI, fInsideFor ? preferences.insert_space_before_semicolon_in_for : preferences.insert_space_before_semicolon);
|
||||||
scribe.printTrailingComment();
|
scribe.printTrailingComment();
|
||||||
}
|
}
|
||||||
return PROCESS_SKIP;
|
return PROCESS_SKIP;
|
||||||
|
@ -2618,7 +2618,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
} else {
|
} else {
|
||||||
statements= Collections.singletonList(bodyStmt);
|
statements= Collections.singletonList(bodyStmt);
|
||||||
}
|
}
|
||||||
startNode(bodyStmt);
|
if (!startNode(bodyStmt)) { return PROCESS_SKIP; }
|
||||||
try {
|
try {
|
||||||
final int statementsLength = statements.size();
|
final int statementsLength = statements.size();
|
||||||
if (statementsLength != 0) {
|
if (statementsLength != 0) {
|
||||||
|
@ -2794,10 +2794,11 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
* the expansion is reached.
|
* the expansion is reached.
|
||||||
*
|
*
|
||||||
* @param node the AST node to be tested
|
* @param node the AST node to be tested
|
||||||
|
* @return <code>false</code> if the node should be skipped
|
||||||
*/
|
*/
|
||||||
private void startNode(IASTNode node) {
|
private boolean startNode(IASTNode node) {
|
||||||
if (node instanceof IASTProblemHolder) {
|
if (node instanceof IASTProblemHolder) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
IASTNodeLocation[] locations= node.getNodeLocations();
|
IASTNodeLocation[] locations= node.getNodeLocations();
|
||||||
if (locations.length == 0) {
|
if (locations.length == 0) {
|
||||||
|
@ -2806,10 +2807,14 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
int startOffset= expansionLocation.getNodeOffset();
|
int startOffset= expansionLocation.getNodeOffset();
|
||||||
int endOffset= startOffset + expansionLocation.getNodeLength();
|
int endOffset= startOffset + expansionLocation.getNodeLength();
|
||||||
scribe.skipRange(startOffset, endOffset);
|
scribe.skipRange(startOffset, endOffset);
|
||||||
|
if (locations.length == 1 && endOffset <= scribe.scanner.getCurrentPosition()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
IASTFileLocation fileLocation= node.getFileLocation();
|
IASTFileLocation fileLocation= node.getFileLocation();
|
||||||
scribe.restartAtOffset(fileLocation.getNodeOffset());
|
scribe.restartAtOffset(fileLocation.getNodeOffset());
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2950,7 +2955,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
if (stmt != null) {
|
if (stmt != null) {
|
||||||
if (stmt instanceof IASTCompoundStatement && !startsWithMacroExpansion(stmt)) {
|
if (stmt instanceof IASTCompoundStatement && !startsWithMacroExpansion(stmt)) {
|
||||||
formatLeftCurlyBrace(line, brace_position);
|
formatLeftCurlyBrace(line, brace_position);
|
||||||
startNode(stmt);
|
if (!startNode(stmt)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
formatBlock((IASTCompoundStatement)stmt, brace_position, preferences.insert_space_before_opening_brace_in_block, preferences.indent_statements_compare_to_block);
|
formatBlock((IASTCompoundStatement)stmt, brace_position, preferences.insert_space_before_opening_brace_in_block, preferences.indent_statements_compare_to_block);
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -3057,7 +3064,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor {
|
||||||
final boolean previousStatementIsNullStmt= previousStatement instanceof IASTNullStatement;
|
final boolean previousStatementIsNullStmt= previousStatement instanceof IASTNullStatement;
|
||||||
for (int i = 1; i < statementsLength - 1; i++) {
|
for (int i = 1; i < statementsLength - 1; i++) {
|
||||||
final IASTStatement statement= statements.get(i);
|
final IASTStatement statement= statements.get(i);
|
||||||
startNode(statement);
|
if (!startNode(statement)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
final boolean statementIsNullStmt= statement instanceof IASTNullStatement;
|
final boolean statementIsNullStmt= statement instanceof IASTNullStatement;
|
||||||
if ((previousStatementIsNullStmt && !statementIsNullStmt)
|
if ((previousStatementIsNullStmt && !statementIsNullStmt)
|
||||||
|| (!previousStatementIsNullStmt && !statementIsNullStmt)) {
|
|| (!previousStatementIsNullStmt && !statementIsNullStmt)) {
|
||||||
|
|
|
@ -805,4 +805,45 @@ public class CodeFormatterTest extends BaseUITestCase {
|
||||||
public void testPreserveSpaceInParameterDecl_Bug241967() throws Exception {
|
public void testPreserveSpaceInParameterDecl_Bug241967() throws Exception {
|
||||||
assertFormatterResult();
|
assertFormatterResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#define MY_MACRO int a; \
|
||||||
|
// int b; \
|
||||||
|
// int c();
|
||||||
|
//
|
||||||
|
//class asdf {
|
||||||
|
// MY_MACRO
|
||||||
|
//
|
||||||
|
//public:
|
||||||
|
// asdf();
|
||||||
|
//~asdf();
|
||||||
|
//};
|
||||||
|
|
||||||
|
//#define MY_MACRO int a; \
|
||||||
|
// int b; \
|
||||||
|
// int c();
|
||||||
|
//
|
||||||
|
//class asdf {
|
||||||
|
// MY_MACRO
|
||||||
|
//
|
||||||
|
//public:
|
||||||
|
// asdf();
|
||||||
|
// ~asdf();
|
||||||
|
//};
|
||||||
|
public void testMacroWithMultipleDeclarations_Bug242053() throws Exception {
|
||||||
|
assertFormatterResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
//void foo() {
|
||||||
|
//for(int i=0;i<50;++i){}
|
||||||
|
//}
|
||||||
|
|
||||||
|
//void foo() {
|
||||||
|
// for (int i = 0 ; i < 50 ; ++i) {
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
public void testSpaceBeforeSemicolonInFor_Bug242232() throws Exception {
|
||||||
|
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_SEMICOLON_IN_FOR, CCorePlugin.INSERT);
|
||||||
|
assertFormatterResult();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue