From a6e6bc11d3e9c434f69a6b082656b60fcde90b88 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Mon, 12 Feb 2007 16:59:50 +0000 Subject: [PATCH] Fix for Bug 173837 - source formatters do not work in .cpp files --- .../formatter/CodeFormatterVisitor.java | 20 ++++++++++++++----- .../resources/formatter/bugs/After.cpp | 13 ++++++++++++ .../resources/formatter/bugs/Before.cpp | 4 ++++ 3 files changed, 32 insertions(+), 5 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 aeb45786ea1..d26536ae559 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 @@ -177,6 +177,8 @@ public class CodeFormatterVisitor extends CPPASTVisitor { public Scribe scribe; private String fTranslationUnitFile; + private boolean fInsideFor; + public CodeFormatterVisitor(DefaultCodeFormatterOptions preferences, Map settings, int offset, int length) { localScanner = new Scanner() { public Token nextToken() { @@ -740,6 +742,13 @@ public class CodeFormatterVisitor extends CPPASTVisitor { private int visit(ICPPASTFunctionDeclarator node) { visit((IASTStandardFunctionDeclarator)node); + final IASTTypeId[] exceptionSpecification= node.getExceptionSpecification(); + if (exceptionSpecification != null && exceptionSpecification.length > 0) { + // TLETODO [formatter] need special alignment for exception specification + scribe.printNextToken(Token.t_throw, true); + final ListAlignment align= new ListAlignment(Alignment.M_COMPACT_SPLIT); + formatList(Arrays.asList(exceptionSpecification), align, true, false); + } final ICPPASTConstructorChainInitializer[] constructorChain= node.getConstructorChain(); if (constructorChain != null && constructorChain.length > 0) { // TLETODO [formatter] need special constructor chain alignment @@ -751,7 +760,7 @@ public class CodeFormatterVisitor extends CPPASTVisitor { formatList(Arrays.asList(constructorChain), align, false, false); scribe.unIndent(); } - // skip the rest (const, throw, etc.) + // skip the rest (const, etc.) skipNode(node); return PROCESS_SKIP; } @@ -855,7 +864,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon); scribe.printTrailingComment(); - scribe.startNewLine(); return PROCESS_SKIP; } @@ -1339,9 +1347,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor { private int visit(IASTDeclarationStatement node) { node.getDeclaration().accept(this); - // semicolon is already part of declaration -// scribe.printNextToken(Token.tSEMI, preferences.insert_space_before_semicolon); -// scribe.printTrailingComment(); + if (!fInsideFor) { + scribe.startNewLine(); + } return PROCESS_SKIP; } @@ -1361,7 +1369,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor { scribe.space(); } IASTStatement initializerStmt= node.getInitializerStatement(); + fInsideFor= true; initializerStmt.accept(this); + fInsideFor= false; final IASTExpression condition = node.getConditionExpression(); if (condition != null) { if (preferences.insert_space_after_semicolon_in_for) { diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp index 5114e384474..a8946ad243a 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp @@ -5,3 +5,16 @@ struct x getX() { } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=171520 int bug=sizeof(int); +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=173837 +class ABaseClass { +protected: + ABaseClass(int x); +}; +class AClass : public ABaseClass { + AClass(int x) throw(int); +}; +AClass::AClass(int x) throw(int) : + ABaseClass(x) { + for (int i=0; i < 12;i++) { + } +} diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp index 480253bb29c..dc2ce045347 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp @@ -3,3 +3,7 @@ struct x {}; struct x getX() {} // https://bugs.eclipse.org/bugs/show_bug.cgi?id=171520 int bug=sizeof(int); +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=173837 +class ABaseClass {protected:ABaseClass(int x);}; +class AClass : public ABaseClass {AClass(int x) throw(int);}; +AClass::AClass(int x)throw(int):ABaseClass(x){for (int i=0;i < 12;i++) {}}