How to use Ok method of ast Package

Best Syzkaller code snippet using ast.Ok

lint.go

Source:lint.go Github

copy

Full Screen

...1582 }1583 return nil, nil1584}1585func LintGuardedDelete(pass *analysis.Pass) (interface{}, error) {1586 isCommaOkMapIndex := func(stmt ast.Stmt) (b *ast.Ident, m ast.Expr, key ast.Expr, ok bool) {1587 // Has to be of the form `_, <b:*ast.Ident> = <m:*types.Map>[<key>]1588 assign, ok := stmt.(*ast.AssignStmt)1589 if !ok {1590 return nil, nil, nil, false1591 }1592 if len(assign.Lhs) != 2 || len(assign.Rhs) != 1 {1593 return nil, nil, nil, false1594 }1595 if !IsBlank(assign.Lhs[0]) {1596 return nil, nil, nil, false1597 }1598 ident, ok := assign.Lhs[1].(*ast.Ident)1599 if !ok {1600 return nil, nil, nil, false1601 }1602 index, ok := assign.Rhs[0].(*ast.IndexExpr)1603 if !ok {1604 return nil, nil, nil, false1605 }1606 if _, ok := pass.TypesInfo.TypeOf(index.X).(*types.Map); !ok {1607 return nil, nil, nil, false1608 }1609 key = index.Index1610 return ident, index.X, key, true1611 }1612 fn := func(node ast.Node) {1613 stmt := node.(*ast.IfStmt)1614 if len(stmt.Body.List) != 1 {1615 return1616 }1617 if stmt.Else != nil {1618 return1619 }1620 expr, ok := stmt.Body.List[0].(*ast.ExprStmt)1621 if !ok {1622 return1623 }1624 call, ok := expr.X.(*ast.CallExpr)1625 if !ok {1626 return1627 }1628 if !IsCallToAST(pass, call, "delete") {1629 return1630 }1631 b, m, key, ok := isCommaOkMapIndex(stmt.Init)1632 if !ok {1633 return1634 }1635 if cond, ok := stmt.Cond.(*ast.Ident); !ok || pass.TypesInfo.ObjectOf(cond) != pass.TypesInfo.ObjectOf(b) {1636 return1637 }1638 if Render(pass, call.Args[0]) != Render(pass, m) || Render(pass, call.Args[1]) != Render(pass, key) {1639 return1640 }1641 ReportNodefFG(pass, stmt, "unnecessary guard around call to delete")1642 }1643 pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.IfStmt)(nil)}, fn)1644 return nil, nil1645}...

Full Screen

Full Screen

match.go

Source:match.go Github

copy

Full Screen

1package pattern2import (3 "fmt"4 "go/ast"5 "go/token"6 "go/types"7 "reflect"8)9var tokensByString = map[string]Token{10 "INT": Token(token.INT),11 "FLOAT": Token(token.FLOAT),12 "IMAG": Token(token.IMAG),13 "CHAR": Token(token.CHAR),14 "STRING": Token(token.STRING),15 "+": Token(token.ADD),16 "-": Token(token.SUB),17 "*": Token(token.MUL),18 "/": Token(token.QUO),19 "%": Token(token.REM),20 "&": Token(token.AND),21 "|": Token(token.OR),22 "^": Token(token.XOR),23 "<<": Token(token.SHL),24 ">>": Token(token.SHR),25 "&^": Token(token.AND_NOT),26 "+=": Token(token.ADD_ASSIGN),27 "-=": Token(token.SUB_ASSIGN),28 "*=": Token(token.MUL_ASSIGN),29 "/=": Token(token.QUO_ASSIGN),30 "%=": Token(token.REM_ASSIGN),31 "&=": Token(token.AND_ASSIGN),32 "|=": Token(token.OR_ASSIGN),33 "^=": Token(token.XOR_ASSIGN),34 "<<=": Token(token.SHL_ASSIGN),35 ">>=": Token(token.SHR_ASSIGN),36 "&^=": Token(token.AND_NOT_ASSIGN),37 "&&": Token(token.LAND),38 "||": Token(token.LOR),39 "<-": Token(token.ARROW),40 "++": Token(token.INC),41 "--": Token(token.DEC),42 "==": Token(token.EQL),43 "<": Token(token.LSS),44 ">": Token(token.GTR),45 "=": Token(token.ASSIGN),46 "!": Token(token.NOT),47 "!=": Token(token.NEQ),48 "<=": Token(token.LEQ),49 ">=": Token(token.GEQ),50 ":=": Token(token.DEFINE),51 "...": Token(token.ELLIPSIS),52 "IMPORT": Token(token.IMPORT),53 "VAR": Token(token.VAR),54 "TYPE": Token(token.TYPE),55 "CONST": Token(token.CONST),56 "BREAK": Token(token.BREAK),57 "CONTINUE": Token(token.CONTINUE),58 "GOTO": Token(token.GOTO),59 "FALLTHROUGH": Token(token.FALLTHROUGH),60}61func maybeToken(node Node) (Node, bool) {62 if node, ok := node.(String); ok {63 if tok, ok := tokensByString[string(node)]; ok {64 return tok, true65 }66 return node, false67 }68 return node, false69}70func isNil(v interface{}) bool {71 if v == nil {72 return true73 }74 if _, ok := v.(Nil); ok {75 return true76 }77 return false78}79type matcher interface {80 Match(*Matcher, interface{}) (interface{}, bool)81}82type State = map[string]interface{}83type Matcher struct {84 TypesInfo *types.Info85 State State86}87func (m *Matcher) fork() *Matcher {88 state := make(State, len(m.State))89 for k, v := range m.State {90 state[k] = v91 }92 return &Matcher{93 TypesInfo: m.TypesInfo,94 State: state,95 }96}97func (m *Matcher) merge(mc *Matcher) {98 m.State = mc.State99}100func (m *Matcher) Match(a Node, b ast.Node) bool {101 m.State = State{}102 _, ok := match(m, a, b)103 return ok104}105func Match(a Node, b ast.Node) (*Matcher, bool) {106 m := &Matcher{}107 ret := m.Match(a, b)108 return m, ret109}110// Match two items, which may be (Node, AST) or (AST, AST)111func match(m *Matcher, l, r interface{}) (interface{}, bool) {112 if _, ok := r.(Node); ok {113 panic("Node mustn't be on right side of match")114 }115 switch l := l.(type) {116 case *ast.ParenExpr:117 return match(m, l.X, r)118 case *ast.ExprStmt:119 return match(m, l.X, r)120 case *ast.DeclStmt:121 return match(m, l.Decl, r)122 case *ast.LabeledStmt:123 return match(m, l.Stmt, r)124 case *ast.BlockStmt:125 return match(m, l.List, r)126 case *ast.FieldList:127 return match(m, l.List, r)128 }129 switch r := r.(type) {130 case *ast.ParenExpr:131 return match(m, l, r.X)132 case *ast.ExprStmt:133 return match(m, l, r.X)134 case *ast.DeclStmt:135 return match(m, l, r.Decl)136 case *ast.LabeledStmt:137 return match(m, l, r.Stmt)138 case *ast.BlockStmt:139 if r == nil {140 return match(m, l, nil)141 }142 return match(m, l, r.List)143 case *ast.FieldList:144 if r == nil {145 return match(m, l, nil)146 }147 return match(m, l, r.List)148 case *ast.BasicLit:149 if r == nil {150 return match(m, l, nil)151 }152 }153 if l, ok := l.(matcher); ok {154 return l.Match(m, r)155 }156 if l, ok := l.(Node); ok {157 // Matching of pattern with concrete value158 return matchNodeAST(m, l, r)159 }160 if l == nil || r == nil {161 return nil, l == r162 }163 {164 ln, ok1 := l.(ast.Node)165 rn, ok2 := r.(ast.Node)166 if ok1 && ok2 {167 return matchAST(m, ln, rn)168 }169 }170 {171 obj, ok := l.(types.Object)172 if ok {173 switch r := r.(type) {174 case *ast.Ident:175 return obj, obj == m.TypesInfo.ObjectOf(r)176 case *ast.SelectorExpr:177 return obj, obj == m.TypesInfo.ObjectOf(r.Sel)178 default:179 return obj, false180 }181 }182 }183 {184 ln, ok1 := l.([]ast.Expr)185 rn, ok2 := r.([]ast.Expr)186 if ok1 || ok2 {187 if ok1 && !ok2 {188 rn = []ast.Expr{r.(ast.Expr)}189 } else if !ok1 && ok2 {190 ln = []ast.Expr{l.(ast.Expr)}191 }192 if len(ln) != len(rn) {193 return nil, false194 }195 for i, ll := range ln {196 if _, ok := match(m, ll, rn[i]); !ok {197 return nil, false198 }199 }200 return r, true201 }202 }203 {204 ln, ok1 := l.([]ast.Stmt)205 rn, ok2 := r.([]ast.Stmt)206 if ok1 || ok2 {207 if ok1 && !ok2 {208 rn = []ast.Stmt{r.(ast.Stmt)}209 } else if !ok1 && ok2 {210 ln = []ast.Stmt{l.(ast.Stmt)}211 }212 if len(ln) != len(rn) {213 return nil, false214 }215 for i, ll := range ln {216 if _, ok := match(m, ll, rn[i]); !ok {217 return nil, false218 }219 }220 return r, true221 }222 }223 {224 ln, ok1 := l.([]*ast.Field)225 rn, ok2 := r.([]*ast.Field)226 if ok1 || ok2 {227 if ok1 && !ok2 {228 rn = []*ast.Field{r.(*ast.Field)}229 } else if !ok1 && ok2 {230 ln = []*ast.Field{l.(*ast.Field)}231 }232 if len(ln) != len(rn) {233 return nil, false234 }235 for i, ll := range ln {236 if _, ok := match(m, ll, rn[i]); !ok {237 return nil, false238 }239 }240 return r, true241 }242 }243 panic(fmt.Sprintf("unsupported comparison: %T and %T", l, r))244}245// Match a Node with an AST node246func matchNodeAST(m *Matcher, a Node, b interface{}) (interface{}, bool) {247 switch b := b.(type) {248 case []ast.Stmt:249 // 'a' is not a List or we'd be using its Match250 // implementation.251 if len(b) != 1 {252 return nil, false253 }254 return match(m, a, b[0])255 case []ast.Expr:256 // 'a' is not a List or we'd be using its Match257 // implementation.258 if len(b) != 1 {259 return nil, false260 }261 return match(m, a, b[0])262 case ast.Node:263 ra := reflect.ValueOf(a)264 rb := reflect.ValueOf(b).Elem()265 if ra.Type().Name() != rb.Type().Name() {266 return nil, false267 }268 for i := 0; i < ra.NumField(); i++ {269 af := ra.Field(i)270 fieldName := ra.Type().Field(i).Name271 bf := rb.FieldByName(fieldName)272 if (bf == reflect.Value{}) {273 panic(fmt.Sprintf("internal error: could not find field %s in type %t when comparing with %T", fieldName, b, a))274 }275 ai := af.Interface()276 bi := bf.Interface()277 if ai == nil {278 return b, bi == nil279 }280 if _, ok := match(m, ai.(Node), bi); !ok {281 return b, false282 }283 }284 return b, true285 case nil:286 return nil, a == Nil{}287 default:288 panic(fmt.Sprintf("unhandled type %T", b))289 }290}291// Match two AST nodes292func matchAST(m *Matcher, a, b ast.Node) (interface{}, bool) {293 ra := reflect.ValueOf(a)294 rb := reflect.ValueOf(b)295 if ra.Type() != rb.Type() {296 return nil, false297 }298 if ra.IsNil() || rb.IsNil() {299 return rb, ra.IsNil() == rb.IsNil()300 }301 ra = ra.Elem()302 rb = rb.Elem()303 for i := 0; i < ra.NumField(); i++ {304 af := ra.Field(i)305 bf := rb.Field(i)306 if af.Type() == rtTokPos || af.Type() == rtObject || af.Type() == rtCommentGroup {307 continue308 }309 switch af.Kind() {310 case reflect.Slice:311 if af.Len() != bf.Len() {312 return nil, false313 }314 for j := 0; j < af.Len(); j++ {315 if _, ok := match(m, af.Index(j).Interface().(ast.Node), bf.Index(j).Interface().(ast.Node)); !ok {316 return nil, false317 }318 }319 case reflect.String:320 if af.String() != bf.String() {321 return nil, false322 }323 case reflect.Int:324 if af.Int() != bf.Int() {325 return nil, false326 }327 case reflect.Bool:328 if af.Bool() != bf.Bool() {329 return nil, false330 }331 case reflect.Ptr, reflect.Interface:332 if _, ok := match(m, af.Interface(), bf.Interface()); !ok {333 return nil, false334 }335 default:336 panic(fmt.Sprintf("internal error: unhandled kind %s (%T)", af.Kind(), af.Interface()))337 }338 }339 return b, true340}341func (b Binding) Match(m *Matcher, node interface{}) (interface{}, bool) {342 if isNil(b.Node) {343 v, ok := m.State[b.Name]344 if ok {345 // Recall value346 return match(m, v, node)347 }348 // Matching anything349 b.Node = Any{}350 }351 // Store value352 if _, ok := m.State[b.Name]; ok {353 panic(fmt.Sprintf("binding already created: %s", b.Name))354 }355 new, ret := match(m, b.Node, node)356 if ret {357 m.State[b.Name] = new358 }359 return new, ret360}361func (Any) Match(m *Matcher, node interface{}) (interface{}, bool) {362 return node, true363}364func (l List) Match(m *Matcher, node interface{}) (interface{}, bool) {365 v := reflect.ValueOf(node)366 if v.Kind() == reflect.Slice {367 if isNil(l.Head) {368 return node, v.Len() == 0369 }370 if v.Len() == 0 {371 return nil, false372 }373 // OPT(dh): don't check the entire tail if head didn't match374 _, ok1 := match(m, l.Head, v.Index(0).Interface())375 _, ok2 := match(m, l.Tail, v.Slice(1, v.Len()).Interface())376 return node, ok1 && ok2377 }378 // Our empty list does not equal an untyped Go nil. This way, we can379 // tell apart an if with no else and an if with an empty else.380 return nil, false381}382func (s String) Match(m *Matcher, node interface{}) (interface{}, bool) {383 switch o := node.(type) {384 case token.Token:385 if tok, ok := maybeToken(s); ok {386 return match(m, tok, node)387 }388 return nil, false389 case string:390 return o, string(s) == o391 default:392 return nil, false393 }394}395func (tok Token) Match(m *Matcher, node interface{}) (interface{}, bool) {396 o, ok := node.(token.Token)397 if !ok {398 return nil, false399 }400 return o, token.Token(tok) == o401}402func (Nil) Match(m *Matcher, node interface{}) (interface{}, bool) {403 return nil, isNil(node) || reflect.ValueOf(node).IsNil()404}405func (builtin Builtin) Match(m *Matcher, node interface{}) (interface{}, bool) {406 r, ok := match(m, Ident(builtin), node)407 if !ok {408 return nil, false409 }410 ident := r.(*ast.Ident)411 obj := m.TypesInfo.ObjectOf(ident)412 if obj != types.Universe.Lookup(ident.Name) {413 return nil, false414 }415 return ident, true416}417func (obj Object) Match(m *Matcher, node interface{}) (interface{}, bool) {418 r, ok := match(m, Ident(obj), node)419 if !ok {420 return nil, false421 }422 ident := r.(*ast.Ident)423 id := m.TypesInfo.ObjectOf(ident)424 _, ok = match(m, obj.Name, ident.Name)425 return id, ok426}427func (fn Function) Match(m *Matcher, node interface{}) (interface{}, bool) {428 var name string429 var obj types.Object430 r, ok := match(m, Or{Nodes: []Node{Ident{Any{}}, SelectorExpr{Any{}, Any{}}}}, node)431 if !ok {432 return nil, false433 }434 switch r := r.(type) {435 case *ast.Ident:436 obj = m.TypesInfo.ObjectOf(r)437 switch obj := obj.(type) {438 case *types.Func:439 // OPT(dh): optimize this similar to code.FuncName440 name = obj.FullName()441 case *types.Builtin:442 name = obj.Name()443 default:444 return nil, false445 }446 case *ast.SelectorExpr:447 var ok bool448 obj, ok = m.TypesInfo.ObjectOf(r.Sel).(*types.Func)449 if !ok {450 return nil, false451 }452 // OPT(dh): optimize this similar to code.FuncName453 name = obj.(*types.Func).FullName()454 default:455 panic("unreachable")456 }457 _, ok = match(m, fn.Name, name)458 return obj, ok459}460func (or Or) Match(m *Matcher, node interface{}) (interface{}, bool) {461 for _, opt := range or.Nodes {462 mc := m.fork()463 if ret, ok := match(mc, opt, node); ok {464 m.merge(mc)465 return ret, true466 }467 }468 return nil, false469}470func (not Not) Match(m *Matcher, node interface{}) (interface{}, bool) {471 _, ok := match(m, not.Node, node)472 if ok {473 return nil, false474 }475 return node, true476}477var (478 // Types of fields in go/ast structs that we want to skip479 rtTokPos = reflect.TypeOf(token.Pos(0))480 rtObject = reflect.TypeOf((*ast.Object)(nil))481 rtCommentGroup = reflect.TypeOf((*ast.CommentGroup)(nil))482)483var (484 _ matcher = Binding{}485 _ matcher = Any{}486 _ matcher = List{}487 _ matcher = String("")488 _ matcher = Token(0)489 _ matcher = Nil{}490 _ matcher = Builtin{}491 _ matcher = Object{}492 _ matcher = Function{}493 _ matcher = Or{}494 _ matcher = Not{}495)...

Full Screen

Full Screen

Ok

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fset := token.NewFileSet()4 f, err := parser.ParseFile(fset, "1.go", nil, 0)5 if err != nil {6 fmt.Println(err)7 }8 ast.Inspect(f, func(n ast.Node) bool {9 switch x := n.(type) {10 if x.Name == "main" {11 fmt.Println(x.NamePos)12 fmt.Println(x.Obj)13 fmt.Println(x.Obj.Kind)14 fmt.Println(x.Obj.Name)15 fmt.Println(x.Obj.Decl)16 fmt.Println(x.Obj.Data)17 fmt.Println(x.Obj.Pos())18 fmt.Println(x.Obj.End())19 fmt.Println(x.Obj.Parent())20 fmt.Println(x.Obj.Kind == ast.Var)21 fmt.Println(x.Obj.Kind == ast.Con)22 fmt.Println(x.Obj.Kind == ast.Typ)23 fmt.Println(x.Obj.Kind == ast.Fun)24 fmt.Println(x.Obj.Kind == ast.Lbl)25 fmt.Println(x.Obj.Kind == ast.Pkg)26 fmt.Println(x.Obj.Kind == ast.Bad)27 fmt.Println(x.Obj.Kind == ast.Fil)28 fmt.Println(x.Obj.Kind == ast.Pkg)29 fmt.Println(x.Obj.Kind == ast.Bad)30 fmt.Println(x.Obj.Kind == ast.Fil)31 fmt.Println(x.Obj.Kind == ast.Pkg)32 fmt.Println(x.Obj.Kind == ast.Bad)33 fmt.Println(x.Obj.Kind == ast.Fil)34 }35 }36 })37}38import (39func main() {40 fset := token.NewFileSet()41 f, err := parser.ParseFile(fset, "1.go", nil, 0)42 if err != nil {43 fmt.Println(err)44 }45 ast.Inspect(f, func(n ast.Node) bool {46 switch x := n.(type) {47 if x.Name == "main" {48 fmt.Println(x.NamePos)49 fmt.Println(x.Obj)50 fmt.Println(x.Obj.Kind)51 fmt.Println(x.Obj.Name)52 fmt.Println(x.Obj.Decl)53 fmt.Println(x.Obj.Data)

Full Screen

Full Screen

Ok

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fset := token.NewFileSet()4 node, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)5 if err != nil {6 fmt.Println(err)7 }8 ast.Inspect(node, func(n ast.Node) bool {9 switch x := n.(type) {10 fmt.Println(x.Name, x.Type, x.Body, x.Doc)11 }12 })13}14main *ast.FuncType 0xc0000c8000 &ast.CommentGroup{List:[]*ast.Comment{(*ast.Comment)(0xc0000c8000)}}

Full Screen

Full Screen

Ok

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "2.go", nil, parser.ImportsOnly)4 if err != nil {5 fmt.Println(err)6 }7 for _, s := range f.Imports {8 fmt.Println(s.Path.Value)9 }10}11import (12func main() {13 f, err := parser.ParseFile(fset, "2.go", nil, parser.ParseComments)14 if err != nil {15 fmt.Println(err)16 }17 ast.Inspect(f, func(n ast.Node) bool {18 switch x := n.(type) {19 fmt.Println(x.Path.Value)20 }21 })22}

Full Screen

Full Screen

Ok

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ident := ast.NewIdent("main")4 fmt.Println(ident.Ok())5}6import (7func main() {8 ident := ast.NewIdent("main")9 pos := token.Pos(1)10 ident.SetPos(pos)11 fmt.Println(ident.Pos())12}13import (14func main() {15 ident := ast.NewIdent("main")16 pos := token.Pos(1)17 ident.SetPos(pos)18 fmt.Println(ident.Pos())19}20import (21func main() {22 ident := ast.NewIdent("main")23 fmt.Println(ident.String())24}25import (26func main() {27 ident := ast.NewIdent("main")28 fmt.Println(ident.Value())29}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful