Skip to main content
summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMarco Stornelli2019-06-30 01:49:50 -0400
committerMarco Stornelli2019-07-01 11:57:48 -0400
commit35a1923321902e9348a6f8181ff4a4b8b279d39b (patch)
tree2c747ca55224dff5a383c017300823d7a5d7da28 /core
parent451fa86e3557e8be5617f85d7a980c7fb0a53ce7 (diff)
downloadorg.eclipse.cdt-35a1923321902e9348a6f8181ff4a4b8b279d39b.tar.gz
org.eclipse.cdt-35a1923321902e9348a6f8181ff4a4b8b279d39b.tar.xz
org.eclipse.cdt-35a1923321902e9348a6f8181ff4a4b8b279d39b.zip
Bug 467346 - Fix format structs/unions with attributes
Change-Id: I516c53978c7dea0191fc66d2820e1dbe5a664b48 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.java21
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CodeFormatterTest.java39
2 files changed, 59 insertions, 1 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 406bbf4039..a50ba70a5b 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 c21ebb187c..f37f7f6b3f 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();
//}

Back to the top