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

Bug 547684 - Fix format assignment with init list

Change-Id: I4fbdc1c65eb25688231e8020bbc3baa750d97be0
Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
Marco Stornelli 2019-05-30 18:28:33 +02:00
parent 90358f5374
commit 862e8222ea
2 changed files with 25 additions and 4 deletions

View file

@ -3034,12 +3034,15 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
if (enclosedInMacroExpansion(node)) { if (enclosedInMacroExpansion(node)) {
return PROCESS_SKIP; return PROCESS_SKIP;
} }
if (doNodeLocationsOverlap(node.getOperand1(), node.getOperand2())) { IASTNode op2 = node.getOperand2();
if (op2 == null)
op2 = node.getInitOperand2();
if (doNodeLocationsOverlap(node.getOperand1(), op2)) {
// Overlapping of operands is possible if the central part of the binary expression is // Overlapping of operands is possible if the central part of the binary expression is
// a result of macro expansion. There is no need to print the operator in such case, // a result of macro expansion. There is no need to print the operator in such case,
// so we simply delegate to each of the operands. // so we simply delegate to each of the operands.
node.getOperand1().accept(this); node.getOperand1().accept(this);
node.getOperand2().accept(this); op2.accept(this);
return PROCESS_SKIP; return PROCESS_SKIP;
} }
if (isAssignment(node)) { if (isAssignment(node)) {
@ -3114,7 +3117,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
op1.accept(this); op1.accept(this);
// In case of macros we may have already passed the equal sign position. // In case of macros we may have already passed the equal sign position.
if (getCurrentPosition() < nodeOffset(node.getOperand2())) { IASTNode op2 = node.getOperand2();
if (op2 == null)
op2 = node.getInitOperand2();
if (getCurrentPosition() < nodeOffset(op2)) {
// Operator // Operator
final int nextToken = peekNextToken(); final int nextToken = peekNextToken();
// In case of C++ alternative operators, like 'and', 'not', etc. a space // In case of C++ alternative operators, like 'and', 'not', etc. a space
@ -3137,7 +3143,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.setTailFormatter(tailFormatter); scribe.setTailFormatter(tailFormatter);
// Operand 2 // Operand 2
final IASTExpression op2 = node.getOperand2(); op2 = node.getOperand2();
if (op2 == null)
op2 = node.getInitOperand2();
op2.accept(this); op2.accept(this);
scribe.runTailFormatter(); scribe.runTailFormatter();
ok = true; ok = true;

View file

@ -4324,4 +4324,17 @@ public class CodeFormatterTest extends BaseUITestCase {
fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL, CCorePlugin.INSERT); fOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_LABEL, CCorePlugin.INSERT);
assertFormatterResult(); assertFormatterResult();
} }
//int main() {
// int x;
// x = {42};
//}
//int main() {
// int x;
// x = { 42 };
//}
public void testAssigmentWithInitList_Bug547684() throws Exception {
assertFormatterResult();
}
} }