On Monday, 5 January 2015 09:50:43 UTC-5, adon...@google.com wrote:
On Sunday, 4 January 2015 16:27:31 UTC-5, nicolas riesch wrote:

I have a large program written in Go, and I need to find all occurences
of '=' operator with both operands being of type *A:

(expression of type *A) = (expression of type *A)

because I want to replace all these occurences by:

(expression of type *A).Equal(expression of type *A)

Does someone has some idea how to do that, by any chance ?
This problem requires type information (as computed by
golang.org/x/tools/go/types) and a more explicit representation of the
source code in which implicit comparisons (such as in a switch statement)
are explicit; the SSA form (golang.org/x/tools/go/ssa) provides both.

Add the following lines to the golang.org/x/tools/cmd/ssadump program and
run it on your application. It will report the source location of all
equality comparisons of type A:

A := types.NewPointer(types.Typ[types.String]) // A is *string in this
for fn := range ssautil.AllFunctions(prog) {
for _, b := range fn.Blocks {
for _, instr := range b.Instrs {
if binop, ok := instr.(*ssa.BinOp); ok && binop.Op == token.EQL {
if types.Identical(binop.X.Type(), A) {
I nearly forgot: the golang.org/x/tools/cmd/eg tool should be able to do
exactly what you want for explicit assignments (i.e., it won't catch
switches), like so:

% cat template.go
package p
func before(x, y *A) { return x == y }
func after(x, y *A) { return x.Equals(y) }
% eg -t template.go myprog

(If switches are important, you could use the ssadump code above to find
places that eg doesn't catch.)

You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 6 of 8 | next ›
Discussion Overview
groupgolang-nuts @
postedJan 4, '15 at 9:27p
activeJan 5, '15 at 10:28p



site design / logo © 2021 Grokbase