From b81075824d01b982d025f1edf6722c9e2ca04cba Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Mon, 19 Feb 2007 15:25:32 +0000 Subject: [PATCH] Fix for Bug 173837 - source formatter has problems with exception specifications --- .../formatter/CodeFormatterVisitor.java | 32 +++++++++++++------ .../cdt/internal/formatter/Scribe.java | 8 +---- .../resources/formatter/bugs/After.cpp | 5 +++ .../resources/formatter/bugs/Before.cpp | 6 +++- .../cdt/ui/tests/text/BasicCEditorTest.java | 16 ++++++---- 5 files changed, 43 insertions(+), 24 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 d26536ae559..39518872e57 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 @@ -715,10 +715,11 @@ public class CodeFormatterVisitor extends CPPASTVisitor { final int line= scribe.line; IASTDeclSpecifier declSpec= node.getDeclSpecifier(); declSpec.accept(this); - if (scribe.printComment()) { + IASTFunctionDeclarator decl= node.getDeclarator(); + boolean needSpace= scribe.printComment() || (decl.getPointerOperators().length == 0 && decl.getNestedDeclarator() == null); + if (needSpace) { scribe.space(); } - IASTFunctionDeclarator decl= node.getDeclarator(); decl.accept(this); IASTStatement bodyStmt= node.getBody(); if (bodyStmt instanceof IASTCompoundStatement) { @@ -742,12 +743,15 @@ public class CodeFormatterVisitor extends CPPASTVisitor { private int visit(ICPPASTFunctionDeclarator node) { visit((IASTStandardFunctionDeclarator)node); + skipConstVolatile(); 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); + if (exceptionSpecification != null) { + if (peekNextToken() == Token.t_throw) { + // 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) { @@ -759,12 +763,21 @@ public class CodeFormatterVisitor extends CPPASTVisitor { final ListAlignment align= new ListAlignment(Alignment.M_COMPACT_SPLIT); formatList(Arrays.asList(constructorChain), align, false, false); scribe.unIndent(); + } else { + // skip the rest (=0) + skipNode(node); } - // skip the rest (const, etc.) - skipNode(node); return PROCESS_SKIP; } + private void skipConstVolatile() { + int token= peekNextToken(); + while (token == Token.t_const || token == Token.t_volatile) { + scribe.printNextToken(token, true); + token= peekNextToken(); + } + } + private int visit(IASTStandardFunctionDeclarator node) { final List parameters = Arrays.asList(node.getParameters()); final ListAlignment align= new ListAlignment(preferences.alignment_for_parameters_in_method_declaration); @@ -905,7 +918,6 @@ public class CodeFormatterVisitor extends CPPASTVisitor { scribe.printComment(); final int line= scribe.line; - // storage class and other modifiers scribe.printModifiers(); diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java index 661eaf7ea72..466541f1f4f 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/Scribe.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 IBM Corporation and others. + * Copyright (c) 2000, 2007 IBM Corporation 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 @@ -627,12 +627,6 @@ public class Scribe { scanner.resetTo(startOffset, startOffset + length - 1); while (true) { boolean hasWhitespace= printComment(); - if (currentToken == null) { - if (hasWhitespace) { - space(); - } - break; - } currentToken= scanner.nextToken(); if (currentToken == null) { if (hasWhitespace) { 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 a8946ad243a..2fb7ce8829a 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 @@ -12,9 +12,14 @@ protected: }; class AClass : public ABaseClass { AClass(int x) throw(int); + void test1() const throw(int); + void test2() throw(); }; AClass::AClass(int x) throw(int) : ABaseClass(x) { for (int i=0; i < 12;i++) { } } +// keep space between decl spec and declarator +int main(int argc, char **argv) { +} 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 dc2ce045347..1163f09a416 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 @@ -5,5 +5,9 @@ struct x getX() {} 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);}; +class AClass : public ABaseClass {AClass(int x) throw(int); void test1() const throw(int); void test2() throw();}; AClass::AClass(int x)throw(int):ABaseClass(x){for (int i=0;i < 12;i++) {}} +// keep space between decl spec and declarator +int +main(int argc, char **argv) { +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java index 521d1be8e25..0e4e73b6db9 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2007 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 @@ -26,6 +26,7 @@ import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.text.source.SourceViewer; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.widgets.Event; +import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PartInitException; import org.eclipse.cdt.core.CCorePlugin; @@ -66,7 +67,6 @@ public class BasicCEditorTest extends BaseUITestCase { protected void tearDown () throws Exception { EditorTestHelper.closeEditor(fEditor); - if (fCProject != null) CProjectHelper.delete(fCProject); if (fNonCProject != null) { @@ -76,8 +76,10 @@ public class BasicCEditorTest extends BaseUITestCase { } private void setUpEditor(String file) throws PartInitException { - fEditor= (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(file), true); - assertNotNull(fEditor); + IEditorPart editor= EditorTestHelper.openInEditor(ResourceTestHelper.findFile(file), true); + assertNotNull(editor); + assertTrue(editor instanceof CEditor); + fEditor= (CEditor) editor; fTextWidget= fEditor.getViewer().getTextWidget(); assertNotNull(fTextWidget); fAccessor= new Accessor(fTextWidget, StyledText.class); @@ -86,8 +88,10 @@ public class BasicCEditorTest extends BaseUITestCase { } private void setUpEditor(File file) throws PartInitException, CModelException { - fEditor= (CEditor) EditorUtility.openInEditor(new ExternalTranslationUnit(fCProject, Path.fromOSString(file.toString()), CCorePlugin.CONTENT_TYPE_CXXSOURCE)); - assertNotNull(fEditor); + IEditorPart editor= EditorUtility.openInEditor(new ExternalTranslationUnit(fCProject, Path.fromOSString(file.toString()), CCorePlugin.CONTENT_TYPE_CXXSOURCE)); + assertNotNull(editor); + assertTrue(editor instanceof CEditor); + fEditor= (CEditor) editor; fTextWidget= fEditor.getViewer().getTextWidget(); assertNotNull(fTextWidget); fAccessor= new Accessor(fTextWidget, StyledText.class);