diff options
author | Hansruedi Patzen | 2018-05-29 13:46:44 +0000 |
---|---|---|
committer | Thomas Corbat | 2018-06-20 06:49:00 +0000 |
commit | 4c66f7c8f3e2f839e6152f522054616b15baa45a (patch) | |
tree | e57002c54424ec3d88f7a28248b5c4097360ca09 /core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter | |
parent | 29ed1fa6c40998edd13cbb39348e7ede951843f5 (diff) | |
download | org.eclipse.cdt-4c66f7c8f3e2f839e6152f522054616b15baa45a.tar.gz org.eclipse.cdt-4c66f7c8f3e2f839e6152f522054616b15baa45a.tar.xz org.eclipse.cdt-4c66f7c8f3e2f839e6152f522054616b15baa45a.zip |
Bug 535274: Allow attributes on namespace definitions
Implementation and tests.
Change-Id: I0cca9dea8630ae66b005856338342b4173c48216
Signed-off-by: Hansruedi Patzen <hansruedi.patzen@hsr.ch>
Signed-off-by: Thomas Corbat <tcorbat@hsr.ch>
Diffstat (limited to 'core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter')
-rw-r--r-- | core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/formatter/CodeFormatterVisitor.java | 32 |
1 files changed, 27 insertions, 5 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 16fef87327f..4971c062091 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 @@ -18,6 +18,7 @@ import java.util.Collections; import java.util.EmptyStackException; import java.util.List; import java.util.Stack; +import java.util.function.Predicate; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ast.ASTVisitor; @@ -104,6 +105,7 @@ import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer; import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator; import org.eclipse.cdt.core.dom.ast.c.ICASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAttributeList; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler; @@ -149,6 +151,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisitor; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTWhileStatement; +import org.eclipse.cdt.core.dom.ast.gnu.IGCCASTAttributeList; import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions; @@ -1103,7 +1106,13 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, // namespace <name> scribe.printNextToken(Token.t_namespace, false); scribe.space(); - node.getName().accept(this); + formatLeadingAttributes(node, ICPPASTAttributeList.class::isInstance); + boolean isNamedNamespace = !CPPVisitor.isAnonymousNamespace(node); + if (isNamedNamespace) { + IASTName name = node.getName(); + name.accept(this); + } + formatAttributes(node, isNamedNamespace, false, IGCCASTAttributeList.class::isInstance); // member declarations IASTDeclaration[] memberDecls= node.getDeclarations(); @@ -1537,13 +1546,22 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, return -1; } + private void formatLeadingAttributes(IASTAttributeOwner owner) { + formatAttributes(owner, false, true); + } + /** * Formats the attributes leading a node. * Same as {@code formatAttributes(owner, false, true);} * @param owner Node containing attributes + * @param filter Filter predicate for specifying which attributes to print */ - private void formatLeadingAttributes(IASTAttributeOwner owner) { - formatAttributes(owner, false, true); + private void formatLeadingAttributes(IASTAttributeOwner owner, Predicate<IASTAttributeSpecifier> predicate) { + formatAttributes(owner, false, true, predicate); + } + + private void formatAttributes(IASTAttributeOwner owner, boolean printLeadingSpace, boolean printTrailingSpace) { + formatAttributes(owner, printLeadingSpace, printTrailingSpace, unsused -> true); } /** @@ -1552,8 +1570,10 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, * @param owner Node containing attributes * @param printLeadingSpace Print a space before the first attribute * @param printTrailingSpace Print a space after the last attribute + * @param filter Filter predicate for specifying which attributes to print */ - private void formatAttributes(IASTAttributeOwner owner, boolean printLeadingSpace, boolean printTrailingSpace) { + private void formatAttributes(IASTAttributeOwner owner, boolean printLeadingSpace, + boolean printTrailingSpace, Predicate<IASTAttributeSpecifier> filter) { if (owner == null) { return; } @@ -1563,7 +1583,9 @@ public class CodeFormatterVisitor extends ASTVisitor implements ICPPASTVisitor, scribe.space(); } for (IASTAttributeSpecifier attributeSpecifier : attributeSpecifiers) { - formatRaw(attributeSpecifier); + if (filter.test(attributeSpecifier)) { + formatRaw(attributeSpecifier); + } } if (printTrailingSpace) { scribe.space(); |