mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-30 21:55:31 +02:00
Bug 467346 - Fix formatting structs with attributes
Change-Id: Iff1be840ec6281bd0de1aaec593d53a033f6dec8 Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
parent
c39ddef02f
commit
670f056354
2 changed files with 57 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue