Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct typedef without name #270

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3d78ab6
struct typedef without name
Konstantin8105 Oct 23, 2017
235717e
add more tests
Konstantin8105 Oct 23, 2017
68590bf
FIX BUG in stdlib.h
Konstantin8105 Oct 23, 2017
e515276
after review
Konstantin8105 Oct 24, 2017
7099195
add ast attributes
Konstantin8105 Oct 30, 2017
3822400
Merge branch 'master' of https://github.com/elliotchance/c2go into st…
Konstantin8105 Nov 2, 2017
4418ed7
change recursive founding struct
Konstantin8105 Nov 2, 2017
75daf87
change recursive founding struct
Konstantin8105 Nov 2, 2017
f6b7829
Remove temp files in stdio.c. Fixes #299 (#305)
Konstantin8105 Nov 2, 2017
66e07fc
Added Atos() to help debug AST issues. Fixes #276 (#306)
Konstantin8105 Nov 2, 2017
6b1da87
Added AllocSizeAttr and DisableTailCallsAttr AST node types (#307)
Konstantin8105 Nov 2, 2017
8f5bd37
Fix misspellings in comments (#308)
Konstantin8105 Nov 2, 2017
22f50f9
Added IsInherited to DeprecatedAttr. Fixes #205 and #184 (#309)
Konstantin8105 Nov 2, 2017
0820d77
Fixed sizeof(char *const [5]). Fixes #286 (#310)
yulvil Nov 2, 2017
13fd588
Bump version: v0.16.12 Radium 2017-11-02
elliotchance Nov 2, 2017
02cbbf0
Implemented CompoundLiteralExpr. Fixes #277 (#315)
yulvil Nov 3, 2017
ea7dd6f
Fixed CStyleCastExpr. Fixes #145 (#314)
yulvil Nov 3, 2017
b52b693
Added tests for multi-dimensional arrays cast. Fixes #286 (#317)
yulvil Nov 3, 2017
9ab1dbc
struct typedef without name
Konstantin8105 Oct 23, 2017
a41deb3
add more tests
Konstantin8105 Oct 23, 2017
5eeffa1
FIX BUG in stdlib.h
Konstantin8105 Oct 23, 2017
1fc5932
after review
Konstantin8105 Oct 24, 2017
38fa2ce
add ast attributes
Konstantin8105 Oct 30, 2017
17e48c2
change recursive founding struct
Konstantin8105 Nov 2, 2017
ff1da77
change recursive founding struct
Konstantin8105 Nov 2, 2017
b9101fa
Merge branch 'structTypedef' of https://github.com/Konstantin8105/c2g…
Konstantin8105 Nov 3, 2017
5b97304
struct typedef without name
Konstantin8105 Oct 23, 2017
da60408
Merge branch 'structTypedef' of https://github.com/Konstantin8105/c2g…
Konstantin8105 Nov 3, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions ast/alloc_size_attr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ast

import (
"strings"

"github.com/elliotchance/c2go/util"
)

// AllocSizeAttr is a type of attribute that is optionally attached to a variable
// or struct field definition.
type AllocSizeAttr struct {
Addr Address
Pos Position
Inherited bool
A int
B int
ChildNodes []Node
}

func parseAllocSizeAttr(line string) *AllocSizeAttr {
groups := groupsFromRegex(
`<(?P<position>.*)>(?P<inherited> Inherited)?(?P<a> \d+)(?P<b> \d+)?`,
line,
)

return &AllocSizeAttr{
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
Inherited: len(groups["inherited"]) > 0,
A: util.Atoi(strings.TrimSpace(groups["a"])),
B: util.Atoi(strings.TrimSpace(groups["b"])),
ChildNodes: []Node{},
}
}

// AddChild adds a new child node. Child nodes can then be accessed with the
// Children attribute.
func (n *AllocSizeAttr) 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 *AllocSizeAttr) 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 *AllocSizeAttr) Children() []Node {
return n.ChildNodes
}

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

import (
"testing"
)

func TestAllocSizeAttr(t *testing.T) {
nodes := map[string]Node{
`0x7f8e390a5d38 <col:100, col:114> 1 2`: &AllocSizeAttr{
Addr: 0x7f8e390a5d38,
Pos: NewPositionFromString("col:100, col:114"),
A: 1,
B: 2,
ChildNodes: []Node{},
},
`0x7fbd1a167f48 </usr/include/stdlib.h:342:37> Inherited 1 0`: &AllocSizeAttr{
Addr: 0x7fbd1a167f48,
Pos: NewPositionFromString("/usr/include/stdlib.h:342:37"),
Inherited: true,
A: 1,
B: 0,
ChildNodes: []Node{},
},
}

runNodeTests(t, nodes)
}
8 changes: 8 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func Parse(line string) Node {
switch nodeName {
case "AlignedAttr":
return parseAlignedAttr(line)
case "AllocSizeAttr":
return parseAllocSizeAttr(line)
case "AlwaysInlineAttr":
return parseAlwaysInlineAttr(line)
case "ArraySubscriptExpr":
Expand All @@ -66,6 +68,8 @@ func Parse(line string) Node {
return parseCaseStmt(line)
case "CharacterLiteral":
return parseCharacterLiteral(line)
case "CompoundLiteralExpr":
return parseCompoundLiteralExpr(line)
case "CompoundStmt":
return parseCompoundStmt(line)
case "ConditionalOperator":
Expand All @@ -86,8 +90,12 @@ func Parse(line string) Node {
return parseDeclStmt(line)
case "DefaultStmt":
return parseDefaultStmt(line)
case "DisableTailCallsAttr":
return parseDisableTailCallsAttr(line)
case "DeprecatedAttr":
return parseDeprecatedAttr(line)
case "DisableTailCallsAttr":
return parseDisableTailCallsAttr(line)
case "DoStmt":
return parseDoStmt(line)
case "ElaboratedType":
Expand Down
20 changes: 20 additions & 0 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ func runNodeTests(t *testing.T, tests map[string]Node) {
})
}
}

func TestPrint(t *testing.T) {
cond := &ConditionalOperator{}
cond.AddChild(&ImplicitCastExpr{})
cond.AddChild(&ImplicitCastExpr{})
s := Atos(cond)
if len(s) == 0 {
t.Fatalf("Cannot convert AST tree : %#v", cond)
}
lines := strings.Split(s, "\n")
var amount int
for _, l := range lines {
if strings.Contains(l, "ImplicitCastExpr") {
amount++
}
}
if amount != 2 {
t.Error("Not correct design of output")
}
}
48 changes: 48 additions & 0 deletions ast/compound_literal_expr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ast

// CompoundLiteralExpr C99 6.5.2.5
type CompoundLiteralExpr struct {
Addr Address
Pos Position
Type1 string
Type2 string
ChildNodes []Node
}

func parseCompoundLiteralExpr(line string) *CompoundLiteralExpr {
groups := groupsFromRegex(
`<(?P<position>.*)> '(?P<type1>.*?)'(:'(?P<type2>.*)')? lvalue`,
line,
)

return &CompoundLiteralExpr{
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
Type1: groups["type1"],
Type2: groups["type2"],
ChildNodes: []Node{},
}
}

// AddChild adds a new child node. Child nodes can then be accessed with the
// Children attribute.
func (n *CompoundLiteralExpr) 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 *CompoundLiteralExpr) 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 *CompoundLiteralExpr) Children() []Node {
return n.ChildNodes
}

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

import (
"testing"
)

func TestCompoundLiteralExpr(t *testing.T) {
nodes := map[string]Node{
`0x5575acce81f0 <col:21, col:40> 'struct node':'struct node' lvalue`: &CompoundLiteralExpr{
Addr: 0x5575acce81f0,
Pos: NewPositionFromString("col:21, col:40"),
Type1: "struct node",
Type2: "struct node",
ChildNodes: []Node{},
},
}

runNodeTests(t, nodes)
}
24 changes: 13 additions & 11 deletions ast/deprecated_attr.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package ast

type DeprecatedAttr struct {
Addr Address
Pos Position
Message1 string
Message2 string
ChildNodes []Node
Addr Address
Pos Position
Message1 string
Message2 string
IsInherited bool
ChildNodes []Node
}

func parseDeprecatedAttr(line string) *DeprecatedAttr {
groups := groupsFromRegex(
`<(?P<position>.*)> "(?P<message1>.*?)"(?P<message2> ".*?")?`,
`<(?P<position>.*)>(?P<inherited> Inherited)? "(?P<message1>.*?)"(?P<message2> ".*?")?`,
line,
)

return &DeprecatedAttr{
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
Message1: removeQuotes(groups["message1"]),
Message2: removeQuotes(groups["message2"]),
ChildNodes: []Node{},
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
Message1: removeQuotes(groups["message1"]),
Message2: removeQuotes(groups["message2"]),
IsInherited: len(groups["inherited"]) > 0,
ChildNodes: []Node{},
}
}

Expand Down
27 changes: 22 additions & 5 deletions ast/deprecated_attr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,28 @@ import (
func TestDeprecatedAttr(t *testing.T) {
nodes := map[string]Node{
`0x7fec4b0ab9c0 <line:180:48, col:63> "This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tempnam(3), it is highly recommended that you use mkstemp(3) instead." ""`: &DeprecatedAttr{
Addr: 0x7fec4b0ab9c0,
Pos: NewPositionFromString("line:180:48, col:63"),
Message1: "This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tempnam(3), it is highly recommended that you use mkstemp(3) instead.",
Message2: "",
ChildNodes: []Node{},
Addr: 0x7fec4b0ab9c0,
Pos: NewPositionFromString("line:180:48, col:63"),
Message1: "This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tempnam(3), it is highly recommended that you use mkstemp(3) instead.",
Message2: "",
IsInherited: false,
ChildNodes: []Node{},
},
`0xb75d00 <line:1107:12> "This function or variable may be unsafe. Consider using _snwprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details." ""`: &DeprecatedAttr{
Addr: 0xb75d00,
Pos: NewPositionFromString("line:1107:12"),
Message1: "This function or variable may be unsafe. Consider using _snwprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.",
Message2: "",
IsInherited: false,
ChildNodes: []Node{},
},
`0xb75d00 <line:1107:12> Inherited "This function or variable may be unsafe. Consider using _snwprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details." ""`: &DeprecatedAttr{
Addr: 0xb75d00,
Pos: NewPositionFromString("line:1107:12"),
Message1: "This function or variable may be unsafe. Consider using _snwprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.",
Message2: "",
IsInherited: true,
ChildNodes: []Node{},
},
}

Expand Down
45 changes: 45 additions & 0 deletions ast/disable_tail_calls_attr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ast

// DisableTailCallsAttr is a type of attribute that is optionally attached to a variable
// or struct field definition.
type DisableTailCallsAttr struct {
Addr Address
Pos Position
ChildNodes []Node
}

func parseDisableTailCallsAttr(line string) *DisableTailCallsAttr {
groups := groupsFromRegex(
"<(?P<position>.*)>",
line,
)

return &DisableTailCallsAttr{
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
ChildNodes: []Node{},
}
}

// AddChild adds a new child node. Child nodes can then be accessed with the
// Children attribute.
func (n *DisableTailCallsAttr) 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 *DisableTailCallsAttr) 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 *DisableTailCallsAttr) Children() []Node {
return n.ChildNodes
}

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

import (
"testing"
)

func TestDisableTailCallsAttr(t *testing.T) {
nodes := map[string]Node{
`0x7fc8fa094558 <col:107> `: &DisableTailCallsAttr{
Addr: 0x7fc8fa094558,
Pos: NewPositionFromString("col:107"),
ChildNodes: []Node{},
},
}

runNodeTests(t, nodes)
}
7 changes: 7 additions & 0 deletions ast/floating_literal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ func TestFloatingLiteral(t *testing.T) {
Value: 1.23,
ChildNodes: []Node{},
},
`0x21c65b8 <col:41> 'double' 2.718282e+00`: &FloatingLiteral{
Addr: 0x21c65b8,
Pos: NewPositionFromString("col:41"),
Type: "double",
Value: 2.718282e+00,
ChildNodes: []Node{},
},
}

runNodeTests(t, nodes)
Expand Down
6 changes: 6 additions & 0 deletions ast/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ func setPosition(node Node, position Position) {
switch n := node.(type) {
case *AlignedAttr:
n.Pos = position
case *AllocSizeAttr:
n.Pos = position
case *AlwaysInlineAttr:
n.Pos = position
case *ArraySubscriptExpr:
Expand Down Expand Up @@ -245,6 +247,8 @@ func setPosition(node Node, position Position) {
n.Pos = position
case *CompoundAssignOperator:
n.Pos = position
case *CompoundLiteralExpr:
n.Pos = position
case *CStyleCastExpr:
n.Pos = position
case *DeclRefExpr:
Expand All @@ -255,6 +259,8 @@ func setPosition(node Node, position Position) {
n.Pos = position
case *DeprecatedAttr:
n.Pos = position
case *DisableTailCallsAttr:
n.Pos = position
case *DoStmt:
n.Pos = position
case *EnumConstantDecl:
Expand Down
Loading