Skip to content

Comments in empty function/loop/if-statement bodies not found #48

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

Closed
ec-m opened this issue Jun 23, 2020 · 12 comments
Closed

Comments in empty function/loop/if-statement bodies not found #48

ec-m opened this issue Jun 23, 2020 · 12 comments

Comments

@ec-m
Copy link

ec-m commented Jun 23, 2020

Comments that are placed inside empty function, loop or if statement bodies are not found in the respective node's decorations. I would expect them to be placed somewhere in the e.g. dst.FuncDeclDecorations of the function or similarly the dst.ForStmtDecorations of the loop declaration. This snipped demonstrates the issue:

package main

func main() {
}

func empty() {
	// inside empty function, not found
}

func foo() {
	i := 1
	for i < 2 {
		// inside empty for statement, not found
	}
	// after empty for statement, found and associated with for statement

	if i == 3 {
		// inside empty if statement, not found
	}
	i = 4
}

And here you can find code to reproduce the issue: https://play.golang.org/p/Yg9OOkoP6IF [Disclaimer: sometimes the playground needs multiple tries to import all packages, so if you encounter a timeout running go build error, just execute the code again.]

I tried using dst in order to fix the issue golang/go#39753 I had with go/ast. As I am a fairly new user of this module, I might also have overlooked something - Please let me know if this is the case and how I could get the correct comment associations :) Thanks!

$ go version
go version go1.14.2 darwin/amd64
go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/.../Library/Caches/go-build"
GOENV="/Users/.../Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/.../go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/.../go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/yb/hqncynqs0b5_3hyxcjpdsyr00000gr/T/go-build447564702=/tmp/go-build -gno-record-gcc-switches -fno-common"
@dave
Copy link
Owner

dave commented Jun 23, 2020

I'll take a look at this now...

@dave
Copy link
Owner

dave commented Jun 23, 2020

Yeah if there's no decoration point after the comment, it'll attach it to the previous attachment point, which in the "empty function" example is f1.Body.Decs.Lbrace.

@dave
Copy link
Owner

dave commented Jun 23, 2020

Same with the for statement, in forStmt.Body.Decs.Lbrace, and the if statement ifStmt.Body.Decs.Lbrace

@dave
Copy link
Owner

dave commented Jun 23, 2020

Take a look at positions.go in the gendst package... it shows exactly where each decoration point in on each node type. Might help track things down in future...

...

@dave
Copy link
Owner

dave commented Jun 23, 2020

e.g.:

	// BlockStmt(0)
	if true /*Start*/ { /*Lbrace*/
		i++
	} /*End*/

	// BlockStmt(1)
	func() /*Start*/ { /*Lbrace*/ i++ } /*End*/ ()

@dave
Copy link
Owner

dave commented Jun 23, 2020

The algorithm that associates comments with decoration attachment points is one of those things that doesn't have a well defined correct answer... so sometimes you get situations like this where it's not exactly straightforward.

@ec-m
Copy link
Author

ec-m commented Jun 23, 2020

Alright, thanks for the clarification! This helps a lot :) One last thing: Does dst provide a comment map like ast.CommentMap such that I can iterate over all comments that exists and see where they are attached to? I wasn't able to find this yet but might have simply overlooked something.

@dave
Copy link
Owner

dave commented Jun 23, 2020

Hmm ... just been looking at this and it's not easy in a generalised way. Let me take another look...

@dave
Copy link
Owner

dave commented Jun 23, 2020

Yeah you'd probably want something like this:

https://play.golang.org/p/NhLPbXeTCGo

... that getDecorationInfo function is copied from here... this is a valid use case that I would like to support... so I'm considering making it public... Not sure if it belongs in the decorator package or in the dstutil package. hmm

@ec-m
Copy link
Author

ec-m commented Jun 23, 2020

Perfect! Thanks a lot for the help :)

@ec-m ec-m closed this as completed Jun 23, 2020
@dave
Copy link
Owner

dave commented Jun 24, 2020

Hey @ec-m, I've just released v0.25.5 which makes that helper function public in the dstutil package. See documentation here.

@ec-m
Copy link
Author

ec-m commented Jun 24, 2020

Awesome! Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants