Sorry, there was a typo in my question, I was looking for '==' equality
operators, and not assignments.
ssadump works like a charm, analyzing 80,000 lines in 15 seconds and
printing the exact line position of all the expressions I was looking for.
That's quite amazing !
Really a great and invaluable tool ;-))))))
For those who wants to try, I rewrite herebelow the code from
adon...@google.com's answer, with the code to look for equality of
*foo.Bar type.
Recompile with:
go install golang.org/x/tools/cmd/ssadump
And run:
ssadump myprogram
-------------- add this at he end of the doMain() function in the file
main.go of golang.org/x/tools/cmd/ssadump program --------------------
// Also add these two packages in the import statement.
// "go/token"
// "golang.org/x/tools/go/ssa/ssautil"
var (
my_package_name string = "foo" // <------- the package in which
your type is declared (only last element of package path)
my_type_name string = "Bar" // <------- the name of the type
you are looking for
my_package *ssa.Package
my_ssa_type *ssa.Type
my_type types.Type
)
pkgs := prog.AllPackages()
for _, pkg := range pkgs {
if pkg.Object.Name() == my_package_name {
my_package = pkg
if my_ssa_type = my_package.Type(my_type_name); my_ssa_type ==
nil {
return fmt.Errorf("type %s not found in package %s",
my_type_name, my_package_name)
}
my_type = my_ssa_type.Type()
break
}
}
// A := types.NewPointer(types.Typ[types.String]) // A is *string in
this example
// A := my_type // if i am looking
for my_type (foo.Bar)
A := types.NewPointer(my_type) // if I am looking
for *my_type (*foo.Bar)
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) {
fmt.Println(prog.Fset.Position(binop.Pos()))
}
}
}
}
}
------------------------
I have just a last question.
The program also prints expressions of the kind a == nil (where a is of
type *foo.Bar)
It is correct, but I would like to filter them out.
I haven't found the proper method in ssa or types package that returns
whether an operand is the nil value.
Can you give me a pointer ?
--
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.