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

x/tools/go/ssa: no position information in *ssa.If #45758

Open
aykevl opened this issue Apr 25, 2021 · 3 comments
Open

x/tools/go/ssa: no position information in *ssa.If #45758

aykevl opened this issue Apr 25, 2021 · 3 comments
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@aykevl
Copy link

aykevl commented Apr 25, 2021

At the moment, the *ssa.If instruction does not track source code position information:

The If instruction transfers control to one of the two successors of its owning block, depending on the boolean Cond: the first if true, the second if false.

[...]

Pos() returns NoPos.

However, in most cases there is a well defined position: for if statements it's the position of the keyword and for switch and select statements it's the position of the case keyword. I would like to use this position information but it's not currently available as indicated in the documentation.

(I have written a patch to add this position information which I will post shortly).

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Apr 25, 2021
@gopherbot gopherbot added this to the Unreleased milestone Apr 25, 2021
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/313389 mentions this issue: go/ssa: implement position information for If

@cherrymui
Copy link
Member

cc @findleyr

@cherrymui cherrymui added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Apr 26, 2021
@timothy-king
Copy link
Contributor

I am of a couple of minds about this. I see it as helpful in a lot of cases to go from ssa back to the source. However, ssa does not always smoothly translate back into source code and it does not have to as it is targeting different problems. If/conditional jump feels like it might be different enough that it is not obvious whether translating back into source with a pos makes sense.

  if a && b { foo() }

This is going to be translated into a couple of different If instructions. Which of the If statements, gets the token position of the If? Is it the one branching on "a", the one on "b" or a fake If to wrap the statement (see builder.cond). (FWIW dumping the ssa for one of these cases might help to really dig in here.)

As I mentioned on cl/313389, optimizations might not preserve this information. If instructions tend to get removed if possible.

Other options for this problem include debug instructions. Is there any chance this could work for you? Another is to add utilities that recover some of the original structure like: ssautil/switch.go. I don't know if there is anything here that would help you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants