1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-30 21:55:31 +02:00

Bug 467346 - Fix format structs/unions with attributes

Change-Id: I516c53978c7dea0191fc66d2820e1dbe5a664b48
Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
Marco Stornelli 2019-06-30 07:49:50 +02:00
parent 451fa86e35
commit 35a1923321
2 changed files with 59 additions and 1 deletions

View file

@ -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;

View file

@ -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();
//}