From 35a1923321902e9348a6f8181ff4a4b8b279d39b Mon Sep 17 00:00:00 2001 From: Marco Stornelli Date: Sun, 30 Jun 2019 07:49:50 +0200 Subject: [PATCH] Bug 467346 - Fix format structs/unions with attributes Change-Id: I516c53978c7dea0191fc66d2820e1dbe5a664b48 Signed-off-by: Marco Stornelli --- .../formatter/CodeFormatterVisitor.java | 21 +++++++++- .../cdt/ui/tests/text/CodeFormatterTest.java | 39 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) 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 406bbf40390..a50ba70a5bb 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 @@ -2070,6 +2070,7 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, } private int visit(ICASTCompositeTypeSpecifier node) { + boolean formatAttributes = false; scribe.printComment(); final int line = scribe.line; @@ -2099,6 +2100,20 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, final IASTName name = node.getName(); if (name != null) { + IASTAttributeSpecifier[] attributes = node.getAttributeSpecifiers(); + if (attributes.length > 0) { + /** + * According to GCC docs, attributes can be defined just after struct + * or union keywords or just after the closing brace. + */ + int token = peekTokenAtPosition(nodeEndOffset(attributes[0])); + if (token == Token.tLBRACE + || (name.getFileLocation() != null && nodeOffset(name) > nodeOffset(attributes[0]))) { + formatAttributes(node, true, false, IGCCASTAttributeList.TYPE_FILTER); + } else { + formatAttributes = true; + } + } scribe.space(); name.accept(this); } @@ -2120,6 +2135,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, scribe.unIndent(); } formatClosingBrace(preferences.brace_position_for_type_declaration); + if (formatAttributes) + formatAttributes(node, true, false, IGCCASTAttributeList.TYPE_FILTER); return PROCESS_SKIP; } @@ -2157,7 +2174,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, * According to GCC docs, attributes can be defined just after struct * or union keywords or just after the closing brace. */ - if (name.getFileLocation() == null || nodeOffset(name) > nodeOffset(attributes[0])) { + token = peekTokenAtPosition(nodeEndOffset(attributes[0])); + if (token == Token.tLBRACE + || (name.getFileLocation() != null && nodeOffset(name) > nodeOffset(attributes[0]))) { formatAttributes(node, true, false, IGCCASTAttributeList.TYPE_FILTER); } else { formatAttributes = true; 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 c21ebb187c5..f37f7f6b3f1 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 @@ -3913,6 +3913,45 @@ public class CodeFormatterTest extends BaseUITestCase { assertFormatterResult(); } + //struct{ + // char a; + // char b; + //}__attribute__((packed)) foo; + + //struct { + // char a; + // char b; + //} __attribute__((packed)) foo; + public void testAttributesWithStructs4_Bug467346() throws Exception { + assertFormatterResult(); + } + + //struct __attribute__((packed)){ + // char a; + // char b; + //}; + + //struct __attribute__((packed)) { + // char a; + // char b; + //}; + public void testAttributesWithStructs5_Bug467346() throws Exception { + assertFormatterResult(); + } + + //struct{ + // char a; + // char b; + //}__attribute__((packed)); + + //struct { + // char a; + // char b; + //} __attribute__((packed)); + public void testAttributesWithStructs6_Bug467346() throws Exception { + assertFormatterResult(); + } + //extern "C" { //void func(); //}