1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Fix formatter issues with macros

This commit is contained in:
Anton Leherbauer 2010-11-18 11:58:33 +00:00
parent 463ffb9990
commit 2016e80de8
3 changed files with 98 additions and 7 deletions

View file

@ -239,7 +239,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.initializeScanner(compilationUnitSource); scribe.initializeScanner(compilationUnitSource);
scribe.setSkipPositions(collectInactiveCodePositions(unit)); scribe.setSkipPositions(collectInactiveCodePositions(unit));
fStatus= new MultiStatus(CCorePlugin.PLUGIN_ID, 0, "Formatting problem(s)", null); //$NON-NLS-1$ fStatus= new MultiStatus(CCorePlugin.PLUGIN_ID, 0, "Formatting problem(s) in '"+unit.getFilePath()+"'", null); //$NON-NLS-1$ //$NON-NLS-2$
try { try {
unit.accept(this); unit.accept(this);
} catch (RuntimeException e) { } catch (RuntimeException e) {
@ -1392,6 +1392,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.space(); scribe.space();
} }
// consider macro expansion
if (withinMacroExpansion(node, scribe.scanner.getCurrentPosition())) {
continueNode(node);
}
switch (node.getKey()) { switch (node.getKey()) {
case IASTCompositeTypeSpecifier.k_struct: case IASTCompositeTypeSpecifier.k_struct:
scribe.printNextToken(Token.t_struct, true); scribe.printNextToken(Token.t_struct, true);
@ -1438,6 +1443,11 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
final int headerIndent= scribe.numberOfIndentations; final int headerIndent= scribe.numberOfIndentations;
// consider macro expansion
if (withinMacroExpansion(node, scribe.scanner.getCurrentPosition())) {
continueNode(node);
}
switch (node.getKey()) { switch (node.getKey()) {
case IASTCompositeTypeSpecifier.k_struct: case IASTCompositeTypeSpecifier.k_struct:
scribe.printNextToken(Token.t_struct, false); scribe.printNextToken(Token.t_struct, false);
@ -2149,6 +2159,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
private int visit(IASTBinaryExpression node) { private int visit(IASTBinaryExpression node) {
if (enclosedInMacroExpansion(node)) {
return PROCESS_SKIP;
}
if (isAssignment(node)) { if (isAssignment(node)) {
return formatAssignment(node); return formatAssignment(node);
} }
@ -2667,7 +2680,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
if (elseStatement != null) { if (elseStatement != null) {
if (!startsWithMacroExpansion(elseStatement)) { if (peekNextToken() == Token.t_else) {
if (thenStatementIsBlock) { if (thenStatementIsBlock) {
scribe.printNextToken(Token.t_else, preferences.insert_space_after_closing_brace_in_block); scribe.printNextToken(Token.t_else, preferences.insert_space_after_closing_brace_in_block);
} else { } else {
@ -3202,6 +3215,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
return false; return false;
} }
private static boolean endsWithMacroExpansion(IASTNode node) { private static boolean endsWithMacroExpansion(IASTNode node) {
IASTNodeLocation[] locations= node.getNodeLocations(); IASTNodeLocation[] locations= node.getNodeLocations();
if (locations.length == 0) { if (locations.length == 0) {
@ -3217,6 +3231,25 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
return locations.length == 1 && locations[0] instanceof IASTMacroExpansionLocation; return locations.length == 1 && locations[0] instanceof IASTMacroExpansionLocation;
} }
private static boolean withinMacroExpansion(IASTNode node, int offset) {
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;
}
}
}
}
return false;
}
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) {
final boolean startsWithMacroExpansion= startsWithMacroExpansion(block); final boolean startsWithMacroExpansion= startsWithMacroExpansion(block);
if (!startsWithMacroExpansion) { if (!startsWithMacroExpansion) {
@ -3301,6 +3334,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
skipToNode(statements.get(1)); skipToNode(statements.get(1));
} }
final boolean previousStatementIsNullStmt= previousStatement instanceof IASTNullStatement; final boolean previousStatementIsNullStmt= previousStatement instanceof IASTNullStatement;
final int indentLevel= scribe.indentationLevel;
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);
if (!startNode(statement)) { if (!startNode(statement)) {
@ -3313,11 +3347,18 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
} }
try { try {
statement.accept(this); statement.accept(this);
} catch (ASTProblemException e) { } catch (RuntimeException e) {
if (i < statementsLength - 1) { if (i < statementsLength - 1) {
final IASTStatement nextStatement= statements.get(i + 1); reportFormattingProblem(e);
skipToNode(nextStatement); exitAlignments();
} skipToNode(statements.get(i + 1));
while (scribe.indentationLevel < indentLevel) {
scribe.indent();
}
while (scribe.indentationLevel > indentLevel) {
scribe.unIndent();
}
} else throw e;
} }
previousStatement= statement; previousStatement= statement;
} }

View file

@ -1237,7 +1237,7 @@ public class Scribe {
} }
public void printNewLine(int insertPosition) { public void printNewLine(int insertPosition) {
if (shouldSkip(insertPosition - 1)) { if (shouldSkip(scanner.getCurrentPosition())) {
return; return;
} }
if (lastNumberOfNewLines >= 1) { if (lastNumberOfNewLines >= 1) {

View file

@ -1466,4 +1466,54 @@ public class CodeFormatterTest extends BaseUITestCase {
Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN)); Integer.toString(Alignment.M_COMPACT_SPLIT | Alignment.M_INDENT_ON_COLUMN));
assertFormatterResult(); assertFormatterResult();
} }
//#define m() f()
//void f() {
//if (1) f();
//else m();
//}
//#define m() f()
//void f() {
// if (1)
// f();
// else
// m();
//}
public void testMacroAfterElse() throws Exception {
assertFormatterResult();
}
//#define M union { double u; void *s; long l; }
//typedef M m_t;
//#define M union { double u; void *s; long l; }
//typedef M m_t;
public void testMacroWithinTypedef() throws Exception {
assertFormatterResult();
}
//#define B() { if (1+2) b(); }
//void g() {
// if (1) {
// B();
// } else {
// x();
// }
// z();
//}
//#define B() { if (1+2) b(); }
//void g() {
// if (1) {
// B();
// } else {
// x();
// }
// z();
//}
public void testBinaryExpressionInMacro() throws Exception {
assertFormatterResult();
}
} }