From b9baf5b478ba8123db821cbd4f28ed0f33f70eb0 Mon Sep 17 00:00:00 2001 From: Thomas Corbat Date: Tue, 13 Dec 2016 18:01:07 +0100 Subject: [PATCH] Bug 509150 - Formatter fails on nested template IDs Fix for exception in deeply nested template IDs + test Change-Id: I0c158599c5b8c974823a400251d76ff441b795f3 Signed-off-by: Thomas Corbat --- .../formatter/CodeFormatterVisitor.java | 21 ++---- .../cdt/ui/tests/text/CodeFormatterTest.java | 68 ++++++++++++++++++- 2 files changed, 74 insertions(+), 15 deletions(-) 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 b76967f03ec..04e7e7cd289 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 @@ -401,7 +401,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, private boolean fExpectSemicolonAfterDeclaration= true; private MultiStatus fStatus; - private int fOpenAngleBrackets; private IASTTranslationUnit ast; public CodeFormatterVisitor(DefaultCodeFormatterOptions preferences, int offset, int length) { @@ -3470,7 +3469,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, if (preferences.insert_space_after_opening_angle_bracket_in_template_arguments) { scribe.space(); } - int angleBrackets = fOpenAngleBrackets++; final IASTNode[] templateArguments= node.getTemplateArguments(); if (templateArguments.length > 0) { final ListOptions options= new ListOptions(Alignment.M_COMPACT_SPLIT); @@ -3480,20 +3478,15 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, formatList(Arrays.asList(templateArguments), options, false, false, null); } if (peekNextToken() == Token.tSHIFTR) { - if (fOpenAngleBrackets == angleBrackets + 2) { - fOpenAngleBrackets -= 2; - scribe.printNextToken(Token.tSHIFTR, preferences.insert_space_before_closing_angle_bracket_in_template_arguments); - } else { - scribe.printComment(); - if (preferences.insert_space_before_closing_angle_bracket_in_template_arguments) { - scribe.space(); - } - return PROCESS_SKIP; + scribe.printComment(); + if (preferences.insert_space_before_closing_angle_bracket_in_template_arguments) { + scribe.space(); } - } else { - --fOpenAngleBrackets; - scribe.printNextToken(Token.tGT, preferences.insert_space_before_closing_angle_bracket_in_template_arguments); + return PROCESS_SKIP; } + + scribe.printNextToken(Token.tGT, preferences.insert_space_before_closing_angle_bracket_in_template_arguments); + int nextToken= peekNextToken(); if (node.getPropertyInParent() != ICPPASTQualifiedName.SEGMENT_NAME || nextToken == Token.tGT) { if (nextToken == Token.tLPAREN) { 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 a4636d1d369..12b2f18a031 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2015 Wind River Systems, Inc. and others. + * Copyright (c) 2007, 2016 Wind River Systems, Inc. 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 @@ -3130,4 +3130,70 @@ public class CodeFormatterTest extends BaseUITestCase { public void testLineCommentAsBlocks8() throws Exception { assertFormatterResult(); } + + //template + //struct Tpl { + //}; + //Tpl>>tpl3_int { }; + //Tpl>>>tpl4_int { }; + //Tpl>>>>tpl5_int { }; + //Tpl>>>>>tpl6_int { }; + + //template + //struct Tpl { + //}; + //Tpl>> tpl3_int { }; + //Tpl>>> tpl4_int { }; + //Tpl>>>> tpl5_int { }; + //Tpl>>>>> tpl6_int { }; + public void testNestedTemplates_509150() throws Exception { + fOptions.put( + DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_ANGLE_BRACKET_IN_TEMPLATE_ARGUMENTS, + CCorePlugin.DO_NOT_INSERT); + assertFormatterResult(); + } + + //template + //struct Tpl { + //}; + //Tpl>>tpl3_int { }; + //Tpl>>>tpl4_int { }; + //Tpl>>>>tpl5_int { }; + //Tpl>>>>>tpl6_int { }; + + //template + //struct Tpl { + //}; + //Tpl > > tpl3_int { }; + //Tpl > > > tpl4_int { }; + //Tpl > > > > tpl5_int { }; + //Tpl > > > > > tpl6_int { }; + public void testNestedTemplatesWithSpaces_509150() throws Exception { + fOptions.put( + DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_ANGLE_BRACKET_IN_TEMPLATE_ARGUMENTS, + CCorePlugin.INSERT); + assertFormatterResult(); + } + + //template + //struct Tpl { + //}; + //Tpl> > tpl3_int { }; + //Tpl>> >tpl4_int { }; + //Tpl> >> >tpl5_int { }; + //Tpl>> >> >tpl6_int { }; + + //template + //struct Tpl { + //}; + //Tpl> > tpl3_int { }; + //Tpl>> > tpl4_int { }; + //Tpl> >> > tpl5_int { }; + //Tpl>> >> > tpl6_int { }; + public void testNestedTemplatesMixedSpacingUnchanged_509150() throws Exception { + fOptions.put( + DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_ANGLE_BRACKET_IN_TEMPLATE_ARGUMENTS, + CCorePlugin.DO_NOT_INSERT); + assertFormatterResult(); + } }