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

Refactored ast nodes into separate package #45

Merged
merged 8 commits into from
Apr 17, 2017

Conversation

zoeyfyi
Copy link
Contributor

@zoeyfyi zoeyfyi commented Apr 16, 2017

All the ast nodes seemed like a good candidate for separating out into a separate package so the root folder is cleaner.

I also merged Render() and RenderLine() into one Render() function since having both seemed redundant, this should also cover #38. This means we can get rid of all the slow reflection since all nodes now implement the ast.Node interface.

TODO

  • Remove reflection code from buildTree
  • Fix issue with the following variable declarations not working after the first test: _LIB_VERSION, _IO_2_1_stdin_, _IO_2_1_stdout_, _IO_2_1_stderr_, stdin, stdout, stderr
  • Fix incorrect coverage

If this is not how you want the project structured feel free to discard this PR.


This change is Reviewable

@codecov
Copy link

codecov bot commented Apr 16, 2017

Codecov Report

Merging #45 into master will decrease coverage by 19.72%.
The diff coverage is 73.2%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master      #45       +/-   ##
===========================================
- Coverage   93.32%   73.59%   -19.73%     
===========================================
  Files          62       66        +4     
  Lines        1318     1530      +212     
===========================================
- Hits         1230     1126      -104     
- Misses         63      378      +315     
- Partials       25       26        +1
Impacted Files Coverage Δ
ast/util.go 71.42% <ø> (ø)
ast/function_definition.go 100% <ø> (ø)
ast/enum.go 0% <0%> (ø)
ast/qual_type.go 0% <0%> (ø)
ast/typedef.go 0% <0%> (ø)
ast/typedef_type.go 0% <0%> (ø)
ast/record_type.go 0% <0%> (ø)
ast/function_proto_type.go 0% <0%> (ø)
ast/record.go 0% <0%> (ø)
ast/restrict_attr.go 0% <0%> (ø)
... and 78 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8952a01...802065d. Read the comment docs.

@elliotchance
Copy link
Owner

Hi @bongo227 - interestingly I did actually have all of these files in an ast package in earlier versions. I moved them out to the root package to get the code coverage working with the integration tests. I would much rather have several small packages that one massive main package.

For each TODO:

  1. Nothing needs to be said on this, sounds good!
  2. I ran in a similar issue on ctype.h #30 . Global state is evil and now that the integration tests are working (thanks for that!) we should really refactor out the global state, or at least move all the initialisation to a single method that we can repeatedly call cleanly for now.
  3. Either my laziness or haste I did not check if code coverage works across packages, but it looks like it can with the -coverpkg.

@elliotchance
Copy link
Owner

Reviewed 66 of 125 files at r1.
Review status: 36 of 66 files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


main.go, line 15 at r1 (raw file):

	"go/format"

	"github.com/bongo227/c2go/ast"

Can we use a relative package name here?


ast/function_decl.go, line 57 at r1 (raw file):

	out := bytes.NewBuffer([]byte{})

	ast.functionName = strings.TrimSpace(n.Name)

This trim is not necessary anymore. It was ported from the original Python.


Comments from Reviewable

@elliotchance
Copy link
Owner

Reviewed 59 of 125 files at r1.
Review status: all files reviewed at latest revision, 5 unresolved discussions, some commit checks failed.


ast/common.go, line 51 at r1 (raw file):

}

// func Render(out *bytes.Buffer, node interface{}, functionName string, indent int, returnType string) {

You can remove this now.


ast/compound_stmt.go, line 35 at r1 (raw file):

}

func (n *CompoundStmt) RenderLine(out *bytes.Buffer, functionName string, indent int, returnType string) {

Is this supposed to be here?


ast/resolve.go, line 77 at r1 (raw file):

}

func resolveType(ast *Ast, s string) string {

Not for this PR. But I would like to make the whole type resolving system a separate package (resolve?) with its own unit tests. This should also make it easier to handle more complex and state-oriented type resolutions like function pointers.


Comments from Reviewable

@zoeyfyi
Copy link
Contributor Author

zoeyfyi commented Apr 17, 2017

Review status: all files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


main.go, line 15 at r1 (raw file):

Previously, elliotchance (Elliot Chance) wrote…

Can we use a relative package name here?

Relative import paths are highly discouraged, I was going to switch these out at the end with the correct path.


Comments from Reviewable

@elliotchance
Copy link
Owner

Review status: all files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


main.go, line 15 at r1 (raw file):

Previously, bongo227 (Ben Sheffield) wrote…

Relative import paths are highly discouraged, I was going to switch these out at the end with the correct path.

That's fair enough. Or maybe it would be easier to rename your directory path to be the same when this pops up in the future.


Comments from Reviewable

@elliotchance
Copy link
Owner

Reviewed 55 of 125 files at r1, 59 of 59 files at r2.
Review status: all files reviewed at latest revision, 4 unresolved discussions, some commit checks failed.


ast/ast.go, line 52 at r2 (raw file):

type Node interface {
	render(ast *Ast) (string, string)
	AddChild(node Node)

It occurs to me that not all node types will have children, like a StringLiteral. For now it's easier to assume they all do, but maybe something like this?

type NodeWithChildren interface {
    *Node
    AddChild(node Node)
}

However, this might be a bit too pedantic and maybe more appropriate for a separate PR where each node can be checked if it can indeed have children.


ast/common.go, line 51 at r1 (raw file):

Previously, elliotchance (Elliot Chance) wrote…

You can remove this now.

I'm not sure if you have used reviewable before or if you simply haven't responded to these yet. Discussions stay open until you respond (notice the handy "Done" button).


Comments from Reviewable

@zoeyfyi
Copy link
Contributor Author

zoeyfyi commented Apr 17, 2017

Review status: all files reviewed at latest revision, 5 unresolved discussions, some commit checks failed.


main.go, line 15 at r1 (raw file):

Previously, elliotchance (Elliot Chance) wrote…

That's fair enough. Or maybe it would be easier to rename your directory path to be the same when this pops up in the future.

Done.


ast/ast.go, line 52 at r2 (raw file):

Previously, elliotchance (Elliot Chance) wrote…

It occurs to me that not all node types will have children, like a StringLiteral. For now it's easier to assume they all do, but maybe something like this?

type NodeWithChildren interface {
    *Node
    AddChild(node Node)
}

However, this might be a bit too pedantic and maybe more appropriate for a separate PR where each node can be checked if it can indeed have children.

Is their a spec for the clang AST anywhere? I haven't managed to find one. I agree that a separate PR is probably best for this one.


ast/common.go, line 51 at r1 (raw file):

Previously, elliotchance (Elliot Chance) wrote…

I'm not sure if you have used reviewable before or if you simply haven't responded to these yet. Discussions stay open until you respond (notice the handy "Done" button).

Done.


ast/compound_stmt.go, line 35 at r1 (raw file):

Previously, elliotchance (Elliot Chance) wrote…

Is this supposed to be here?

Done.


ast/function_decl.go, line 57 at r1 (raw file):

Previously, elliotchance (Elliot Chance) wrote…

This trim is not necessary anymore. It was ported from the original Python.

Done.


ast/resolve.go, line 77 at r1 (raw file):

Previously, elliotchance (Elliot Chance) wrote…

Not for this PR. But I would like to make the whole type resolving system a separate package (resolve?) with its own unit tests. This should also make it easier to handle more complex and state-oriented type resolutions like function pointers.

Done. I created an issue.


Comments from Reviewable

@elliotchance
Copy link
Owner

@bongo227 this is a really great change. I'm happy to merge this in if your ready?

Codecov is failing because of the drop in coverage, that's understandable since all the new code. That will be cleaned up over time.

@zoeyfyi
Copy link
Contributor Author

zoeyfyi commented Apr 17, 2017

Their is still a problem with those variable declarations (I just ignore them to parse the tests). This can probably be fixed when resolve is refactored out so I would say merging now would be fine.

@elliotchance elliotchance merged commit 9bb200e into elliotchance:master Apr 17, 2017
@elliotchance
Copy link
Owner

@elliotchance elliotchance modified the milestone: v0.8.x Apr 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants