1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 17:26:01 +02:00

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 <tcorbat@hsr.ch>
This commit is contained in:
Thomas Corbat 2016-12-13 18:01:07 +01:00 committed by Jonah Graham
parent 10428dd53a
commit b9baf5b478
2 changed files with 74 additions and 15 deletions

View file

@ -401,7 +401,6 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
private boolean fExpectSemicolonAfterDeclaration= true; private boolean fExpectSemicolonAfterDeclaration= true;
private MultiStatus fStatus; private MultiStatus fStatus;
private int fOpenAngleBrackets;
private IASTTranslationUnit ast; private IASTTranslationUnit ast;
public CodeFormatterVisitor(DefaultCodeFormatterOptions preferences, int offset, int length) { 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) { if (preferences.insert_space_after_opening_angle_bracket_in_template_arguments) {
scribe.space(); scribe.space();
} }
int angleBrackets = fOpenAngleBrackets++;
final IASTNode[] templateArguments= node.getTemplateArguments(); final IASTNode[] templateArguments= node.getTemplateArguments();
if (templateArguments.length > 0) { if (templateArguments.length > 0) {
final ListOptions options= new ListOptions(Alignment.M_COMPACT_SPLIT); 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); formatList(Arrays.asList(templateArguments), options, false, false, null);
} }
if (peekNextToken() == Token.tSHIFTR) { 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(); scribe.printComment();
if (preferences.insert_space_before_closing_angle_bracket_in_template_arguments) { if (preferences.insert_space_before_closing_angle_bracket_in_template_arguments) {
scribe.space(); scribe.space();
} }
return PROCESS_SKIP; return PROCESS_SKIP;
} }
} else {
--fOpenAngleBrackets;
scribe.printNextToken(Token.tGT, preferences.insert_space_before_closing_angle_bracket_in_template_arguments); scribe.printNextToken(Token.tGT, preferences.insert_space_before_closing_angle_bracket_in_template_arguments);
}
int nextToken= peekNextToken(); int nextToken= peekNextToken();
if (node.getPropertyInParent() != ICPPASTQualifiedName.SEGMENT_NAME || nextToken == Token.tGT) { if (node.getPropertyInParent() != ICPPASTQualifiedName.SEGMENT_NAME || nextToken == Token.tGT) {
if (nextToken == Token.tLPAREN) { if (nextToken == Token.tLPAREN) {

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -3130,4 +3130,70 @@ public class CodeFormatterTest extends BaseUITestCase {
public void testLineCommentAsBlocks8() throws Exception { public void testLineCommentAsBlocks8() throws Exception {
assertFormatterResult(); assertFormatterResult();
} }
//template<typename T>
//struct Tpl {
//};
//Tpl<Tpl<Tpl<int>>>tpl3_int { };
//Tpl<Tpl<Tpl<Tpl<int>>>>tpl4_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<int>>>>>tpl5_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<Tpl<int>>>>>>tpl6_int { };
//template<typename T>
//struct Tpl {
//};
//Tpl<Tpl<Tpl<int>>> tpl3_int { };
//Tpl<Tpl<Tpl<Tpl<int>>>> tpl4_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<int>>>>> tpl5_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<Tpl<int>>>>>> 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<typename T>
//struct Tpl {
//};
//Tpl<Tpl<Tpl<int>>>tpl3_int { };
//Tpl<Tpl<Tpl<Tpl<int>>>>tpl4_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<int>>>>>tpl5_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<Tpl<int>>>>>>tpl6_int { };
//template<typename T>
//struct Tpl {
//};
//Tpl<Tpl<Tpl<int > > > tpl3_int { };
//Tpl<Tpl<Tpl<Tpl<int > > > > tpl4_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<int > > > > > tpl5_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<Tpl<int > > > > > > 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<typename T>
//struct Tpl {
//};
//Tpl<Tpl<Tpl<int>> > tpl3_int { };
//Tpl<Tpl<Tpl<Tpl<int>>> >tpl4_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<int>> >> >tpl5_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<Tpl<int>>> >> >tpl6_int { };
//template<typename T>
//struct Tpl {
//};
//Tpl<Tpl<Tpl<int>> > tpl3_int { };
//Tpl<Tpl<Tpl<Tpl<int>>> > tpl4_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<int>> >> > tpl5_int { };
//Tpl<Tpl<Tpl<Tpl<Tpl<Tpl<int>>> >> > 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();
}
} }