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 598432c4258..bdd55b5a029 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 @@ -650,6 +650,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor { final int line= scribe.line; IASTDeclSpecifier declSpec= node.getDeclSpecifier(); declSpec.accept(this); + if (scribe.printComment()) { + scribe.space(); + } IASTFunctionDeclarator decl= node.getDeclarator(); decl.accept(this); IASTStatement bodyStmt= node.getBody(); @@ -781,7 +784,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor { declSpec.accept(this); final List declarators= Arrays.asList(node.getDeclarators()); if (declarators.size() > 0) { - scribe.space(); + if (scribe.printComment()) { + scribe.space(); + } final ListAlignment align= new ListAlignment(Alignment.M_COMPACT_SPLIT); formatList(declarators, align, false, false); } @@ -817,13 +822,11 @@ public class CodeFormatterVisitor extends CPPASTVisitor { private int visit(IASTSimpleDeclSpecifier node) { formatNode(node); - scribe.space(); return PROCESS_SKIP; } private int visit(IASTNamedTypeSpecifier node) { formatNode(node); - scribe.space(); return PROCESS_SKIP; } @@ -1557,7 +1560,9 @@ public class CodeFormatterVisitor extends CPPASTVisitor { } private void formatNode(IASTNode node) { - scribe.printComment(); + if (scribe.printComment()) { + scribe.space(); + } IASTNodeLocation location= node.getFileLocation(); if (location != null) { scribe.printRaw(location.getNodeOffset(), location.getNodeLength()); 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 new file mode 100644 index 00000000000..2d343faaa67 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/After.cpp @@ -0,0 +1,5 @@ +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=169382 +struct x { +}; +struct x getX() { +} 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 new file mode 100644 index 00000000000..2f189437d72 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/bugs/Before.cpp @@ -0,0 +1,3 @@ +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=169382 +struct x {}; +struct x getX() {} diff --git a/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/After.cpp b/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/After.cpp index fdfe7fe0d5a..2792a676739 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/After.cpp +++ b/core/org.eclipse.cdt.ui.tests/resources/formatter/templates/After.cpp @@ -12,16 +12,16 @@ class Foo { // TEMPLATE_STRUCT template struct Map { - Key * keys; - Value * values; - SortAlgorithm * sortAlgorithm; + Key* keys; + Value* values; + SortAlgorithm* sortAlgorithm; Map(); }; // TEMPLATE_CLASS template class nonVector { private: - T * head; + T* head; public: nonVector() { @@ -30,7 +30,7 @@ public: int length() { return 1; } - const T &first() const; + const T& first() const; }; // TEMPLATE_UNION @@ -52,7 +52,7 @@ class TemplateContainer { }; // TEMPLATE_FUNCTION -template const T &nonVector::first() const { +template const T& nonVector::first() const { return *head; } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FormatActionTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FormatActionTest.java index 3c767fc337d..7ad4c79b4dd 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FormatActionTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/FormatActionTest.java @@ -131,4 +131,7 @@ public class FormatActionTest extends TestCase { assertFormatResult(); } + public void testBugs() throws Exception { + + } }