Skip to content

Commit

Permalink
Added AST node type AttributedType. Fixes #772 (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamphaus authored and elliotchance committed Jun 25, 2018
1 parent f7700c8 commit 89c9ff2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func Parse(fullline string) Node {
return parseArraySubscriptExpr(line)
case "AsmLabelAttr":
return parseAsmLabelAttr(line)
case "AttributedType":
return parseAttributedType(line)
case "AvailabilityAttr":
return parseAvailabilityAttr(line)
case "BinaryOperator":
Expand Down
43 changes: 43 additions & 0 deletions ast/attributed_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ast

// AttributedType is an attribute type
type AttributedType struct {
Addr Address
Type string
Sugar bool
ChildNodes []Node
}

func parseAttributedType(line string) *AttributedType {
groups := groupsFromRegex(`'(?P<type>.*?)' sugar`, line)

return &AttributedType{
Addr: ParseAddress(groups["address"]),
Type: groups["type"],
Sugar: true,
ChildNodes: []Node{},
}
}

// AddChild adds a new child node. Child nodes can then be accessed with the
// Children attribute.
func (n *AttributedType) AddChild(node Node) {
n.ChildNodes = append(n.ChildNodes, node)
}

// Address returns the numeric address of the node. See the documentation for
// the Address type for more information.
func (n *AttributedType) Address() Address {
return n.Addr
}

// Children returns the child nodes. If this node does not have any children or
// this node does not support children it will always return an empty slice.
func (n *AttributedType) Children() []Node {
return n.ChildNodes
}

// Position returns the position in the original source code.
func (n *AttributedType) Position() Position {
return Position{}
}
18 changes: 18 additions & 0 deletions ast/attributed_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ast

import (
"testing"
)

func TestAttributedType(t *testing.T) {
nodes := map[string]Node{
`0x2b6c359e30 'int (void) __attribute__((cdecl))' sugar`: &AttributedType{
Addr: 0x2b6c359e30,
Type: "int (void) __attribute__((cdecl))",
Sugar: true,
ChildNodes: []Node{},
},
}

runNodeTests(t, nodes)
}
2 changes: 1 addition & 1 deletion ast/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func setPosition(node Node, position Position) {
*QualType, *PointerType, *DecayedType, *ParenType,
*IncompleteArrayType, *FunctionProtoType, *EnumType, *Enum,
*ElaboratedType, *ConstantArrayType, *BuiltinType, *ArrayFiller,
*Field:
*Field, *AttributedType:
// These do not have positions so they can be ignored.
default:
panic(fmt.Sprintf("unknown node type: %+#v", node))
Expand Down

0 comments on commit 89c9ff2

Please sign in to comment.