Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMarco Stornelli2019-03-14 18:43:45 +0000
committerJeff Johnston2019-04-04 20:26:54 +0000
commit670f056354f000c94c3e8ff741fd912eb8591eca (patch)
tree9eda6e4c59657243140d99d7d2d55701e32e3920 /core
parentc39ddef02fa22af321e62c84e6eb1c938362929b (diff)
downloadorg.eclipse.cdt-670f056354f000c94c3e8ff741fd912eb8591eca.tar.gz
org.eclipse.cdt-670f056354f000c94c3e8ff741fd912eb8591eca.tar.xz
org.eclipse.cdt-670f056354f000c94c3e8ff741fd912eb8591eca.zip
Bug 467346 - Fix formatting structs with attributes
Change-Id: Iff1be840ec6281bd0de1aaec593d53a033f6dec8 Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
Diffstat (limited to 'core')
-rw-r--r--core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java18
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java39
2 files changed, 57 insertions, 0 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 2129e9d44f1..7dd2826a6d8 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
@@ -1912,6 +1912,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
}
private int visit(ICPPASTCompositeTypeSpecifier node) {
+ boolean formatAttributes = false;
+
scribe.printComment();
final int line = scribe.line;
@@ -1936,6 +1938,20 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
final IASTName name = node.getName();
if (name != null) {
+ if (token == Token.t_struct || token == Token.t_union) {
+ 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.
+ */
+ if (name.getFileLocation() == null || nodeOffset(name) > nodeOffset(attributes[0])) {
+ formatAttributes(node, true, false, IGCCASTAttributeList.TYPE_FILTER);
+ } else {
+ formatAttributes = true;
+ }
+ }
+ }
scribe.space();
name.accept(this);
}
@@ -2027,6 +2043,8 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor,
scribe.indent();
}
formatClosingBrace(preferences.brace_position_for_type_declaration);
+ if (formatAttributes)
+ formatAttributes(node, true, false, IGCCASTAttributeList.TYPE_FILTER);
return PROCESS_SKIP;
}
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 8f133335978..c56437a0282 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
@@ -3811,4 +3811,43 @@ public class CodeFormatterTest extends BaseUITestCase {
public void testFormmatterWithMacro_Bug543947() throws Exception {
assertFormatterResult();
}
+
+ //struct __attribute__((packed))foo{
+ // char a;
+ // char b;
+ //};
+
+ //struct __attribute__((packed)) foo {
+ // char a;
+ // char b;
+ //};
+ public void testAttributesWithStructs1_Bug467346() throws Exception {
+ assertFormatterResult();
+ }
+
+ //struct foo{
+ // char a;
+ // char b;
+ //}__attribute__((packed));
+
+ //struct foo {
+ // char a;
+ // char b;
+ //} __attribute__((packed));
+ public void testAttributesWithStructs2_Bug467346() throws Exception {
+ assertFormatterResult();
+ }
+
+ //struct __attribute__((packed)){
+ // char a;
+ // char b;
+ //}foo;
+
+ //struct __attribute__((packed)) {
+ // char a;
+ // char b;
+ //} foo;
+ public void testAttributesWithStructs3_Bug467346() throws Exception {
+ assertFormatterResult();
+ }
}

Back to the top