Skip to content

Commit

Permalink
Added AST cases for var_decl and visibility_attr (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchance authored Mar 21, 2018
1 parent 440eeec commit de2c077
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
3 changes: 3 additions & 0 deletions ast/var_decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
// VarDecl is node represents a variable declaration.
type VarDecl struct {
Addr Address
Parent Address
Pos Position
Position2 string
Name string
Expand All @@ -24,6 +25,7 @@ type VarDecl struct {
func parseVarDecl(line string) *VarDecl {
groups := groupsFromRegex(
`(?:prev (?P<prev>0x[0-9a-f]+) )?
(?:parent (?P<parent>0x[0-9a-f]+) )?
<(?P<position>.*)>(?P<position2> .+:\d+)?
(?P<used> used)?
(?P<referenced> referenced)?
Expand All @@ -45,6 +47,7 @@ func parseVarDecl(line string) *VarDecl {

return &VarDecl{
Addr: ParseAddress(groups["address"]),
Parent: ParseAddress(groups["parent"]),
Pos: NewPositionFromString(groups["position"]),
Position2: strings.TrimSpace(groups["position2"]),
Name: strings.TrimSpace(groups["name"]),
Expand Down
25 changes: 25 additions & 0 deletions ast/var_decl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestVarDecl(t *testing.T) {
IsReferenced: false,
IsStatic: false,
IsRegister: false,
Parent: 0,
ChildNodes: []Node{},
},
`0x7fd5e90e9078 <line:156:1, col:14> col:14 __stdinp 'FILE *' extern`: &VarDecl{
Expand All @@ -34,6 +35,7 @@ func TestVarDecl(t *testing.T) {
IsReferenced: false,
IsStatic: false,
IsRegister: false,
Parent: 0,
ChildNodes: []Node{},
},
`0x7fd5e90ed630 <col:40, col:47> col:47 __size 'size_t':'unsigned long'`: &VarDecl{
Expand All @@ -49,6 +51,7 @@ func TestVarDecl(t *testing.T) {
IsReferenced: false,
IsStatic: false,
IsRegister: false,
Parent: 0,
ChildNodes: []Node{},
},
`0x7fee35907a78 <col:4, col:8> col:8 used c 'int'`: &VarDecl{
Expand All @@ -64,6 +67,7 @@ func TestVarDecl(t *testing.T) {
IsReferenced: false,
IsStatic: false,
IsRegister: false,
Parent: 0,
ChildNodes: []Node{},
},
`0x7fb0fd90ba30 <col:3, /usr/include/sys/_types.h:52:33> tests/assert/assert.c:13:9 used b 'int *' cinit`: &VarDecl{
Expand All @@ -79,6 +83,7 @@ func TestVarDecl(t *testing.T) {
IsReferenced: false,
IsStatic: false,
IsRegister: false,
Parent: 0,
ChildNodes: []Node{},
},
`0x7fb20308bd40 <col:5, col:11> col:11 referenced a 'short'`: &VarDecl{
Expand All @@ -94,6 +99,7 @@ func TestVarDecl(t *testing.T) {
IsReferenced: true,
IsStatic: false,
IsRegister: false,
Parent: 0,
ChildNodes: []Node{},
},
`0x55a040ddd798 <sqlite3.c:66:1, line:770:1> line:66:27 used sqlite3azCompileOpt 'const char *const [2]' static cinit`: &VarDecl{
Expand All @@ -109,6 +115,7 @@ func TestVarDecl(t *testing.T) {
IsReferenced: false,
IsStatic: true,
IsRegister: false,
Parent: 0,
ChildNodes: []Node{},
},
`0x55772c7774d0 <col:3, col:27> col:27 used a 'unsigned char *' register`: &VarDecl{
Expand All @@ -124,6 +131,7 @@ func TestVarDecl(t *testing.T) {
IsReferenced: false,
IsStatic: false,
IsRegister: true,
Parent: 0,
ChildNodes: []Node{},
},
`0x26fd180 <col:4, col:32> col:13 used aExt 'extCoord':'extCoord' cinit`: &VarDecl{
Expand All @@ -139,6 +147,23 @@ func TestVarDecl(t *testing.T) {
IsReferenced: false,
IsStatic: false,
IsRegister: false,
Parent: 0,
ChildNodes: []Node{},
},
`0x7f985e0ffb10 parent 0x7f985e0246d0 <col:3, col:20> col:20 used DEFAULT_MEM_ALLOCATOR 'cmark_mem':'struct cmark_mem' extern`: &VarDecl{
Addr: 0x7f985e0ffb10,
Pos: NewPositionFromString("col:3, col:20"),
Position2: "col:20",
Name: "DEFAULT_MEM_ALLOCATOR",
Type: "cmark_mem",
Type2: "struct cmark_mem",
IsExtern: true,
IsUsed: true,
IsCInit: false,
IsReferenced: false,
IsStatic: false,
IsRegister: false,
Parent: 0x7f985e0246d0,
ChildNodes: []Node{},
},
}
Expand Down
19 changes: 11 additions & 8 deletions ast/visibility_attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ package ast

// VisibilityAttr contains information for a VisibilityAttr AST line.
type VisibilityAttr struct {
Addr Address
Pos Position
ChildNodes []Node
IsDefault bool
Addr Address
Pos Position
ChildNodes []Node
IsDefault bool
IsInherited bool
}

func parseVisibilityAttr(line string) *VisibilityAttr {
groups := groupsFromRegex(
`<(?P<position>.*)>
(?P<inherited> Inherited)?
(?P<default> Default)?
`,
line,
)

return &VisibilityAttr{
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
ChildNodes: []Node{},
IsDefault: len(groups["default"]) > 0,
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
ChildNodes: []Node{},
IsDefault: len(groups["default"]) > 0,
IsInherited: len(groups["inherited"]) > 0,
}
}

Expand Down
16 changes: 12 additions & 4 deletions ast/visibility_attr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import (
func TestVisibilityAttr(t *testing.T) {
nodes := map[string]Node{
`0x55c49d8dd1d8 <col:16, col:36> Default`: &VisibilityAttr{
Addr: 0x55c49d8dd1d8,
Pos: NewPositionFromString("col:16, col:36"),
ChildNodes: []Node{},
IsDefault: true,
Addr: 0x55c49d8dd1d8,
Pos: NewPositionFromString("col:16, col:36"),
ChildNodes: []Node{},
IsInherited: false,
IsDefault: true,
},
`0x7f8e7b00bb80 </cmark/src/cmark.h:497:16, col:36> Inherited Default`: &VisibilityAttr{
Addr: 0x7f8e7b00bb80,
Pos: NewPositionFromString("/cmark/src/cmark.h:497:16, col:36"),
ChildNodes: []Node{},
IsInherited: true,
IsDefault: true,
},
}

Expand Down

0 comments on commit de2c077

Please sign in to comment.