1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 350689 - [formatter] Problems formatting Lua source code

This commit is contained in:
Anton Leherbauer 2011-08-08 16:15:39 +02:00
parent 3796041aa8
commit 24d603a178
3 changed files with 70 additions and 17 deletions

View file

@ -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);
}

View file

@ -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) {

View file

@ -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();
}
}