From 24d603a17822c483de68459894cc264aa808a3c8 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Mon, 8 Aug 2011 16:15:39 +0200 Subject: [PATCH] Bug 350689 - [formatter] Problems formatting Lua source code --- .../internal/formatter/CCodeFormatter.java | 6 ++- .../formatter/CodeFormatterVisitor.java | 41 +++++++++++-------- .../cdt/ui/tests/text/CodeFormatterTest.java | 40 ++++++++++++++++++ 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java index a06323d54d5..12a08dbb697 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CCodeFormatter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 QNX Software Systems and others. + * Copyright (c) 2000, 2011 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -178,6 +178,10 @@ public class CCodeFormatter extends CodeFormatter { ParserUtil.getParserLogService()); CodeFormatterVisitor codeFormatter = new CodeFormatterVisitor(preferences, offset, length); edit= codeFormatter.format(source, ast); + IStatus status= codeFormatter.getStatus(); + if (!status.isOK()) { + CCorePlugin.log(status); + } } catch (CoreException e) { throw new AbortFormatting(e); } diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java index 72400cfe0e2..e93c0e53547 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java @@ -1774,7 +1774,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, // Consider macro expansion if (withinMacroExpansion(node, scribe.scanner.getCurrentPosition())) { + scribe.printNextToken(peekNextToken()); continueNode(node); + if (scribe.printComment()) scribe.space(); } switch (node.getKey()) { @@ -1825,7 +1827,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, // Consider macro expansion if (withinMacroExpansion(node, scribe.scanner.getCurrentPosition())) { + scribe.printNextToken(peekNextToken()); continueNode(node); + if (scribe.printComment()) scribe.space(); } switch (node.getKey()) { @@ -2080,7 +2084,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, int i; for (i = 0; i < elementsLength; i++) { final IASTNode node= elements.get(i); - if (i < alignment.fragmentCount - 1) { + if (i < elementsLength - 1) { scribe.setTailFormatter( new TrailingTokenFormatter(options.fSeparatorToken, options.fSpaceBeforeSeparator, @@ -2095,7 +2099,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } else { node.accept(this); } - if (i < alignment.fragmentCount - 1) { + if (i < elementsLength - 1) { scribe.runTailFormatter(); } } @@ -3314,7 +3318,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } } - if (elseStatement instanceof IASTCompoundStatement) { + if (elseStatement instanceof IASTCompoundStatement && !enclosedInMacroExpansion(elseStatement)) { elseStatement.accept(this); } else if (elseStatement instanceof IASTIfStatement) { if (!preferences.compact_else_if) { @@ -3819,8 +3823,13 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, if (preferences.insert_space_after_opening_paren_in_parenthesized_expression ) { scribe.space(); } - if (operand != null) { - operand.accept(this); + Runnable tailFormatter = scribe.takeTailFormatter(); + try { + if (operand != null) { + operand.accept(this); + } + } finally { + scribe.setTailFormatter(tailFormatter); } if (peekNextToken() != Token.tRPAREN) { if (!enclosedInMacroExpansion(operand)) { @@ -3897,22 +3906,22 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } private static boolean withinMacroExpansion(IASTNode node, int offset) { + IASTFileLocation loc = node.getFileLocation(); + if (loc == null || offset < loc.getNodeOffset() || offset >= loc.getNodeOffset() + loc.getNodeLength()) { + return false; + } IASTNodeLocation[] locations= node.getNodeLocations(); for (IASTNodeLocation location : locations) { - if (location instanceof IASTMacroExpansionLocation) { - IASTFileLocation fileLocation = location.asFileLocation(); - if (fileLocation != null) { - final int nodeOffset = fileLocation.getNodeOffset(); - final int endOffset = nodeOffset + fileLocation.getNodeLength(); - if (offset >= nodeOffset && offset < endOffset) { - return true; - } else if (offset < nodeOffset) { - return false; - } + IASTFileLocation fileLocation = location.asFileLocation(); + if (fileLocation != null) { + final int nodeOffset = fileLocation.getNodeOffset(); + final int endOffset = nodeOffset + fileLocation.getNodeLength(); + if (offset >= nodeOffset && offset < endOffset) { + return location instanceof IASTMacroExpansionLocation; } } } - return false; + return true; } private static boolean doNodeLocationsOverlap(IASTNode node1, IASTNode node2) { diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java index 2ffe4df3e33..c5942087a49 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java @@ -2537,4 +2537,44 @@ public class CodeFormatterTest extends BaseUITestCase { public void testExpressionInArrayDeclarator_Bug350816() throws Exception { assertFormatterResult(); } + + //void f(int p0 ,... ){} + + //void f(int p0, ...) { + //} + public void testEllipsisInFunctionDefinition_Bug350689() throws Exception { + assertFormatterResult(); + } + + //struct{int n;}* l; + //void f(int p0, int p1) { f((p0 + 2), l->n); } + + //struct { + // int n; + //}* l; + //void f(int p0, int p1) { + // f((p0 + 2), l->n); + //} + public void testParenthesizedExpressionInArgumentList_Bug350689() throws Exception { + assertFormatterResult(); + } + + //#define m(x) { x=1; } + //void f() { + // int i; + // if (1) i=1; + // else m(i); + //} + + //#define m(x) { x=1; } + //void f() { + // int i; + // if (1) + // i = 1; + // else + // m(i); + //} + public void testMacroInElseBranch_Bug350689() throws Exception { + assertFormatterResult(); + } }