How to use Count method of ast Package

Best Syzkaller code snippet using ast.Count

compile.go

Source:compile.go Github

copy

Full Screen

...82 return c83}84type compiler struct {85 Config86 upCountOffset int32 // 1 for files; 0 for expressions87 index adt.StringIndexer88 stack []frame89 inSelector int90 fileScope map[adt.Feature]bool91 num literal.NumInfo92 errs errors.Error93}94func (c *compiler) reset() {95 c.fileScope = nil96 c.stack = c.stack[:0]97 c.errs = nil98}99func (c *compiler) errf(n ast.Node, format string, args ...interface{}) *adt.Bottom {100 err := &compilerError{101 n: n,102 path: c.path(),103 Message: errors.NewMessage(format, args),104 }105 c.errs = errors.Append(c.errs, err)106 return &adt.Bottom{Err: err}107}108func (c *compiler) path() []string {109 a := []string{}110 for _, f := range c.stack {111 if f.label != nil {112 a = append(a, f.label.labelString())113 }114 }115 return a116}117type frame struct {118 label labeler // path name leading to this frame.119 scope ast.Node // *ast.File or *ast.Struct120 field *ast.Field121 // scope map[ast.Node]bool122 upCount int32 // 1 for field, 0 for embedding.123 aliases map[string]aliasEntry124}125type aliasEntry struct {126 label labeler127 srcExpr ast.Expr128 expr adt.Expr129 source ast.Node130 used bool131}132func (c *compiler) insertAlias(id *ast.Ident, a aliasEntry) *adt.Bottom {133 k := len(c.stack) - 1134 m := c.stack[k].aliases135 if m == nil {136 m = map[string]aliasEntry{}137 c.stack[k].aliases = m138 }139 if id == nil || !ast.IsValidIdent(id.Name) {140 return c.errf(a.source, "invalid identifier name")141 }142 if e, ok := m[id.Name]; ok {143 return c.errf(a.source,144 "alias %q already declared; previous declaration at %s",145 id.Name, e.source.Pos())146 }147 m[id.Name] = a148 return nil149}150func (c *compiler) updateAlias(id *ast.Ident, expr adt.Expr) {151 k := len(c.stack) - 1152 m := c.stack[k].aliases153 x := m[id.Name]154 x.expr = expr155 x.label = nil156 x.srcExpr = nil157 m[id.Name] = x158}159// lookupAlias looks up an alias with the given name at the k'th stack position.160func (c compiler) lookupAlias(k int, id *ast.Ident) aliasEntry {161 m := c.stack[k].aliases162 name := id.Name163 entry, ok := m[name]164 if !ok {165 err := c.errf(id, "could not find LetClause associated with identifier %q", name)166 return aliasEntry{expr: err}167 }168 switch {169 case entry.label != nil:170 if entry.srcExpr == nil {171 entry.expr = c.errf(id, "cyclic references in let clause or alias")172 break173 }174 src := entry.srcExpr175 entry.srcExpr = nil // mark to allow detecting cycles176 m[name] = entry177 entry.expr = c.labeledExprAt(k, nil, entry.label, src)178 entry.label = nil179 }180 entry.used = true181 m[name] = entry182 return entry183}184func (c *compiler) pushScope(n labeler, upCount int32, id ast.Node) *frame {185 c.stack = append(c.stack, frame{186 label: n,187 scope: id,188 upCount: upCount,189 })190 return &c.stack[len(c.stack)-1]191}192func (c *compiler) popScope() {193 k := len(c.stack) - 1194 f := c.stack[k]195 for k, v := range f.aliases {196 if !v.used {197 c.errf(v.source, "unreferenced alias or let clause %s", k)198 }199 }200 c.stack = c.stack[:k]201}202func (c *compiler) compileFiles(a []*ast.File) *adt.Vertex { // Or value?203 c.fileScope = map[adt.Feature]bool{}204 c.upCountOffset = 1205 // TODO(resolve): this is also done in the runtime package, do we need both?206 // Populate file scope to handle unresolved references.207 // Excluded from cross-file resolution are:208 // - import specs209 // - aliases210 // - anything in an anonymous file211 //212 for _, f := range a {213 if p := internal.GetPackageInfo(f); p.IsAnonymous() {214 continue215 }216 for _, d := range f.Decls {217 if f, ok := d.(*ast.Field); ok {218 if id, ok := f.Label.(*ast.Ident); ok {219 c.fileScope[c.label(id)] = true220 }221 }222 }223 }224 // TODO: set doc.225 res := &adt.Vertex{}226 // env := &adt.Environment{Vertex: nil} // runtime: c.runtime227 env := &adt.Environment{}228 top := env229 for p := c.Config.Scope; p != nil; p = p.Parent() {230 top.Vertex = p.Vertex()231 top.Up = &adt.Environment{}232 top = top.Up233 }234 for _, file := range a {235 c.pushScope(nil, 0, file) // File scope236 v := &adt.StructLit{Src: file}237 c.addDecls(v, file.Decls)238 res.Conjuncts = append(res.Conjuncts, adt.MakeRootConjunct(env, v))239 c.popScope()240 }241 return res242}243func (c *compiler) compileExpr(x ast.Expr) adt.Conjunct {244 expr := c.expr(x)245 env := &adt.Environment{}246 top := env247 for p := c.Config.Scope; p != nil; p = p.Parent() {248 top.Vertex = p.Vertex()249 top.Up = &adt.Environment{}250 top = top.Up251 }252 return adt.MakeRootConjunct(env, expr)253}254// resolve assumes that all existing resolutions are legal. Validation should255// be done in a separate step if required.256//257// TODO: collect validation pass to verify that all resolutions are258// legal?259func (c *compiler) resolve(n *ast.Ident) adt.Expr {260 // X in import "path/X"261 // X in import X "path"262 if imp, ok := n.Node.(*ast.ImportSpec); ok {263 return &adt.ImportReference{264 Src: n,265 ImportPath: c.label(imp.Path),266 Label: c.label(n),267 }268 }269 label := c.label(n)270 // Unresolved field.271 if n.Node == nil {272 upCount := int32(0)273 for _, c := range c.stack {274 upCount += c.upCount275 }276 if c.fileScope[label] {277 return &adt.FieldReference{278 Src: n,279 UpCount: upCount,280 Label: label,281 }282 }283 upCount += c.upCountOffset284 for p := c.Scope; p != nil; p = p.Parent() {285 for _, a := range p.Vertex().Arcs {286 if a.Label == label {287 return &adt.FieldReference{288 Src: n,289 UpCount: upCount,290 Label: label,291 }292 }293 }294 upCount++295 }296 if c.Config.Imports != nil {297 if pkgPath := c.Config.Imports(n); pkgPath != "" {298 return &adt.ImportReference{299 Src: n,300 ImportPath: adt.MakeStringLabel(c.index, pkgPath),301 Label: c.label(n),302 }303 }304 }305 if p := predeclared(n); p != nil {306 return p307 }308 return c.errf(n, "reference %q not found", n.Name)309 }310 // X in [X=x]: y Scope: Field Node: Expr (x)311 // X in X=[x]: y Scope: Field Node: Field312 // X in x: X=y Scope: Field Node: Alias313 if f, ok := n.Scope.(*ast.Field); ok {314 upCount := int32(0)315 k := len(c.stack) - 1316 for ; k >= 0; k-- {317 if c.stack[k].field == f {318 break319 }320 upCount += c.stack[k].upCount321 }322 label := &adt.LabelReference{323 Src: n,324 UpCount: upCount,325 }326 switch f := n.Node.(type) {327 case *ast.Field:328 _ = c.lookupAlias(k, f.Label.(*ast.Alias).Ident) // mark as used329 return &adt.DynamicReference{330 Src: n,331 UpCount: upCount,332 Label: label,333 }334 case *ast.Alias:335 _ = c.lookupAlias(k, f.Ident) // mark as used336 return &adt.ValueReference{337 Src: n,338 UpCount: upCount,339 Label: c.label(f.Ident),340 }341 }342 return label343 }344 upCount := int32(0)345 k := len(c.stack) - 1346 for ; k >= 0; k-- {347 if c.stack[k].scope == n.Scope {348 break349 }350 upCount += c.stack[k].upCount351 }352 if k < 0 {353 // This is a programmatic error and should never happen if the users354 // just builds with the cue command or if astutil.Resolve is used355 // correctly.356 c.errf(n, "reference %q set to unknown node in AST; "+357 "this can result from incorrect API usage or a compiler bug",358 n.Name)359 }360 if n.Scope == nil {361 // Package.362 // Should have been handled above.363 return c.errf(n, "unresolved identifier %v", n.Name)364 }365 switch f := n.Node.(type) {366 // Local expressions367 case *ast.LetClause:368 entry := c.lookupAlias(k, n)369 // let x = y370 return &adt.LetReference{371 Src: n,372 UpCount: upCount,373 Label: label,374 X: entry.expr,375 }376 // TODO: handle new-style aliases377 case *ast.Field:378 // X=x: y379 // X=(x): y380 // X="\(x)": y381 a, ok := f.Label.(*ast.Alias)382 if !ok {383 return c.errf(n, "illegal reference %s", n.Name)384 }385 aliasInfo := c.lookupAlias(k, a.Ident) // marks alias as used.386 lab, ok := a.Expr.(ast.Label)387 if !ok {388 return c.errf(a.Expr, "invalid label expression")389 }390 name, _, err := ast.LabelName(lab)391 switch {392 case errors.Is(err, ast.ErrIsExpression):393 if aliasInfo.expr == nil {394 panic("unreachable")395 }396 return &adt.DynamicReference{397 Src: n,398 UpCount: upCount,399 Label: aliasInfo.expr,400 }401 case err != nil:402 return c.errf(n, "invalid label: %v", err)403 case name != "":404 label = c.label(lab)405 default:406 return c.errf(n, "unsupported field alias %q", name)407 }408 }409 return &adt.FieldReference{410 Src: n,411 UpCount: upCount,412 Label: label,413 }414}415func (c *compiler) addDecls(st *adt.StructLit, a []ast.Decl) {416 for _, d := range a {417 c.markAlias(d)418 }419 for _, d := range a {420 c.addLetDecl(d)421 }422 for _, d := range a {423 if x := c.decl(d); x != nil {424 st.Decls = append(st.Decls, x)425 }...

Full Screen

Full Screen

func_count_test.go

Source:func_count_test.go Github

copy

Full Screen

...20 "github.com/pingcap/parser/mysql"21 "github.com/pingcap/tidb/executor/aggfuncs"22)23func genApproxDistinctMergePartialResult(begin, end uint64) string {24 o := aggfuncs.NewPartialResult4ApproxCountDistinct()25 encodedBytes := make([]byte, 8)26 for i := begin; i < end; i++ {27 binary.LittleEndian.PutUint64(encodedBytes, i)28 x := farm.Hash64(encodedBytes)29 o.InsertHash64(x)30 }31 return string(o.Serialize())32}33func (s *testSuite) TestMergePartialResult4Count(c *C) {34 tester := buildAggTester(ast.AggFuncCount, mysql.TypeLonglong, 5, 5, 3, 8)35 s.testMergePartialResult(c, tester)36 tester = buildAggTester(ast.AggFuncApproxCountDistinct, mysql.TypeLonglong, 5, genApproxDistinctMergePartialResult(0, 5), genApproxDistinctMergePartialResult(2, 5), 5)37 s.testMergePartialResult(c, tester)38}39func (s *testSuite) TestCount(c *C) {40 tests := []aggTest{41 buildAggTester(ast.AggFuncCount, mysql.TypeLonglong, 5, 0, 5),42 buildAggTester(ast.AggFuncCount, mysql.TypeFloat, 5, 0, 5),43 buildAggTester(ast.AggFuncCount, mysql.TypeDouble, 5, 0, 5),44 buildAggTester(ast.AggFuncCount, mysql.TypeNewDecimal, 5, 0, 5),45 buildAggTester(ast.AggFuncCount, mysql.TypeString, 5, 0, 5),46 buildAggTester(ast.AggFuncCount, mysql.TypeDate, 5, 0, 5),47 buildAggTester(ast.AggFuncCount, mysql.TypeDuration, 5, 0, 5),48 buildAggTester(ast.AggFuncCount, mysql.TypeJSON, 5, 0, 5),49 }50 for _, test := range tests {51 s.testAggFunc(c, test)52 }53 tests2 := []multiArgsAggTest{54 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeLonglong, mysql.TypeLonglong}, mysql.TypeLonglong, 5, 0, 5),55 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeFloat, mysql.TypeFloat}, mysql.TypeLonglong, 5, 0, 5),56 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeDouble, mysql.TypeDouble}, mysql.TypeLonglong, 5, 0, 5),57 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeNewDecimal, mysql.TypeNewDecimal}, mysql.TypeLonglong, 5, 0, 5),58 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeString, mysql.TypeString}, mysql.TypeLonglong, 5, 0, 5),59 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeDate, mysql.TypeDate}, mysql.TypeLonglong, 5, 0, 5),60 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeDuration, mysql.TypeDuration}, mysql.TypeLonglong, 5, 0, 5),61 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeJSON, mysql.TypeJSON}, mysql.TypeLonglong, 5, 0, 5),62 }63 for _, test := range tests2 {64 s.testMultiArgsAggFunc(c, test)65 }66 tests3 := []aggTest{67 buildAggTester(ast.AggFuncCount, mysql.TypeLonglong, 5, 0, 5),68 buildAggTester(ast.AggFuncCount, mysql.TypeFloat, 5, 0, 5),69 buildAggTester(ast.AggFuncCount, mysql.TypeDouble, 5, 0, 5),70 buildAggTester(ast.AggFuncCount, mysql.TypeNewDecimal, 5, 0, 5),71 buildAggTester(ast.AggFuncCount, mysql.TypeString, 5, 0, 5),72 buildAggTester(ast.AggFuncCount, mysql.TypeDate, 5, 0, 5),73 buildAggTester(ast.AggFuncCount, mysql.TypeDuration, 5, 0, 5),74 buildAggTester(ast.AggFuncCount, mysql.TypeJSON, 5, 0, 5),75 }76 for _, test := range tests3 {77 s.testAggFunc(c, test)78 }79 tests4 := []multiArgsAggTest{80 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeLonglong, mysql.TypeLonglong}, mysql.TypeLonglong, 5, 0, 5),81 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeFloat, mysql.TypeFloat}, mysql.TypeLonglong, 5, 0, 5),82 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeDouble, mysql.TypeDouble}, mysql.TypeLonglong, 5, 0, 5),83 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeNewDecimal, mysql.TypeNewDecimal}, mysql.TypeLonglong, 5, 0, 5),84 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeString, mysql.TypeString}, mysql.TypeLonglong, 5, 0, 5),85 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeDate, mysql.TypeDate}, mysql.TypeLonglong, 5, 0, 5),86 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeDuration, mysql.TypeDuration}, mysql.TypeLonglong, 5, 0, 5),87 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeJSON, mysql.TypeJSON}, mysql.TypeLonglong, 5, 0, 5),88 }89 for _, test := range tests4 {90 s.testMultiArgsAggFunc(c, test)91 }92}93func BenchmarkCount(b *testing.B) {94 s := testSuite{}95 s.SetUpSuite(nil)96 rowNum := 5000097 tests := []aggTest{98 buildAggTester(ast.AggFuncCount, mysql.TypeLonglong, rowNum, 0, rowNum),99 buildAggTester(ast.AggFuncCount, mysql.TypeFloat, rowNum, 0, rowNum),100 buildAggTester(ast.AggFuncCount, mysql.TypeDouble, rowNum, 0, rowNum),101 buildAggTester(ast.AggFuncCount, mysql.TypeNewDecimal, rowNum, 0, rowNum),102 buildAggTester(ast.AggFuncCount, mysql.TypeString, rowNum, 0, rowNum),103 buildAggTester(ast.AggFuncCount, mysql.TypeDate, rowNum, 0, rowNum),104 buildAggTester(ast.AggFuncCount, mysql.TypeDuration, rowNum, 0, rowNum),105 buildAggTester(ast.AggFuncCount, mysql.TypeJSON, rowNum, 0, rowNum),106 }107 for _, test := range tests {108 s.benchmarkAggFunc(b, test)109 }110 tests2 := []multiArgsAggTest{111 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeLonglong, mysql.TypeLonglong}, mysql.TypeLonglong, rowNum, 0, rowNum),112 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeFloat, mysql.TypeFloat}, mysql.TypeLonglong, rowNum, 0, rowNum),113 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeDouble, mysql.TypeDouble}, mysql.TypeLonglong, rowNum, 0, rowNum),114 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeNewDecimal, mysql.TypeNewDecimal}, mysql.TypeLonglong, rowNum, 0, rowNum),115 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeString, mysql.TypeString}, mysql.TypeLonglong, rowNum, 0, rowNum),116 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeDate, mysql.TypeDate}, mysql.TypeLonglong, rowNum, 0, rowNum),117 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeDuration, mysql.TypeDuration}, mysql.TypeLonglong, rowNum, 0, rowNum),118 buildMultiArgsAggTester(ast.AggFuncCount, []byte{mysql.TypeJSON, mysql.TypeJSON}, mysql.TypeLonglong, rowNum, 0, rowNum),119 }120 for _, test := range tests2 {121 s.benchmarkMultiArgsAggFunc(b, test)122 }123 tests3 := []multiArgsAggTest{124 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeLonglong, mysql.TypeLonglong}, mysql.TypeLonglong, rowNum, 0, rowNum),125 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeFloat, mysql.TypeFloat}, mysql.TypeLonglong, rowNum, 0, rowNum),126 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeDouble, mysql.TypeDouble}, mysql.TypeLonglong, rowNum, 0, rowNum),127 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeNewDecimal, mysql.TypeNewDecimal}, mysql.TypeLonglong, rowNum, 0, rowNum),128 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeString, mysql.TypeString}, mysql.TypeLonglong, rowNum, 0, rowNum),129 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeDate, mysql.TypeDate}, mysql.TypeLonglong, rowNum, 0, rowNum),130 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeDuration, mysql.TypeDuration}, mysql.TypeLonglong, rowNum, 0, rowNum),131 buildMultiArgsAggTester(ast.AggFuncApproxCountDistinct, []byte{mysql.TypeJSON, mysql.TypeJSON}, mysql.TypeLonglong, rowNum, 0, rowNum),132 }133 for _, test := range tests3 {134 s.benchmarkMultiArgsAggFunc(b, test)135 }136}...

Full Screen

Full Screen

dive.go

Source:dive.go Github

copy

Full Screen

1package dive2import (3 "go/ast"4 "golang.org/x/tools/go/analysis"5 "golang.org/x/tools/go/analysis/passes/inspect"6 "golang.org/x/tools/go/ast/inspector"7)8const doc = "dive finds low readability if-blocks"9var Analyzer = &analysis.Analyzer{10 Name: "dive",11 Doc: doc,12 Run: run,13 Requires: []*analysis.Analyzer{14 inspect.Analyzer,15 },16}17var (18 flagBlockLength int19 flagReturns int20 flagDepthNest int21)22func init() {23 Analyzer.Flags.IntVar(&flagBlockLength, "len", 5, "max length of if block")24 Analyzer.Flags.IntVar(&flagReturns, "ret", 2, "max number of returns in a if block")25 Analyzer.Flags.IntVar(&flagDepthNest, "nest", 2, "max depth of nest")26}27func run(pass *analysis.Pass) (interface{}, error) {28 inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)29 nodeFilter := []ast.Node{30 (*ast.FuncDecl)(nil),31 (*ast.FuncLit)(nil),32 }33 inspect.Preorder(nodeFilter, func(n ast.Node) {34 switch n := n.(type) {35 case *ast.FuncDecl:36 checkTopBlock(pass, n.Body)37 case *ast.FuncLit:38 checkTopBlock(pass, n.Body)39 }40 })41 return nil, nil42}43func checkTopBlock(pass *analysis.Pass, body *ast.BlockStmt) {44 if body == nil {45 return46 }47 for _, stmt := range body.List {48 switch stmt := stmt.(type) {49 case *ast.IfStmt:50 checkIf(pass, stmt)51 }52 }53}54func checkIf(pass *analysis.Pass, ifstmt *ast.IfStmt) {55 checkBlock(pass, ifstmt.Body)56 switch elsestmt := ifstmt.Else.(type) {57 case *ast.BlockStmt:58 checkBlock(pass, elsestmt)59 case *ast.IfStmt:60 checkIf(pass, elsestmt)61 }62}63func checkBlock(pass *analysis.Pass, body *ast.BlockStmt) {64 if body == nil || len(body.List) == 0 {65 return66 }67 checkLongBlock(pass, body)68 checkManyReturns(pass, body)69 checkHasLoop(pass, body)70 checkDeeplyNest(pass, body)71}72func checkLongBlock(pass *analysis.Pass, body *ast.BlockStmt) {73 if body != nil && len(body.List) > flagBlockLength {74 pass.Reportf(body.Pos(), "too long block")75 }76}77func countLength(body *ast.BlockStmt) int {78 if body == nil {79 return 080 }81 var count int82 for _, stmt := range body.List {83 switch stmt := stmt.(type) {84 case *ast.ForStmt:85 count += 1 + countLength(stmt.Body)86 case *ast.IfStmt:87 count += 1 + countLength(stmt.Body)88 case *ast.SwitchStmt:89 count += 1 + countLength(stmt.Body)90 case *ast.TypeSwitchStmt:91 count += 1 + countLength(stmt.Body)92 case *ast.SelectStmt:93 count += 1 + countLength(stmt.Body)94 default:95 count++96 }97 }98 return count99}100func checkManyReturns(pass *analysis.Pass, body *ast.BlockStmt) {101 var count int102 ast.Inspect(body, func(n ast.Node) bool {103 switch n.(type) {104 case *ast.FuncLit, *ast.SelectStmt, *ast.SwitchStmt, *ast.TypeSwitchStmt:105 return false106 case *ast.ReturnStmt:107 count++108 }109 return true110 })111 if count > flagReturns {112 pass.Reportf(body.Pos(), "too many returns in the block")113 }114}115func checkHasLoop(pass *analysis.Pass, body *ast.BlockStmt) {116 ast.Inspect(body, func(n ast.Node) bool {117 switch n.(type) {118 case *ast.FuncLit:119 return false120 case *ast.ForStmt:121 pass.Reportf(body.Pos(), "loop in if block")122 }123 return true124 })125}126func checkDeeplyNest(pass *analysis.Pass, body *ast.BlockStmt) {127 if 1+countDepth(body) > flagDepthNest {128 pass.Reportf(body.Pos(), "too deeply nest")129 }130}131func countDepth(body *ast.BlockStmt) int {132 if body == nil {133 return 0134 }135 for _, stmt := range body.List {136 switch stmt := stmt.(type) {137 case *ast.ForStmt:138 return 1 + countDepth(stmt.Body)139 case *ast.IfStmt:140 return 1 + countDepth(stmt.Body)141 case *ast.SwitchStmt:142 return 1 + countDepth(stmt.Body)143 case *ast.TypeSwitchStmt:144 return 1 + countDepth(stmt.Body)145 case *ast.SelectStmt:146 return 1 + countDepth(stmt.Body)147 }148 }149 return 0150}...

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "C:/Users/HP/go/src/ast/1.go", nil, 0)4 if err != nil {5 fmt.Println(err)6 }7 ast.Print(fset, f)8 fmt.Println("Number of declarations:", f.Decls.Count())9}10Recommended Posts: Go | ast.FieldList.Count() method11Go | ast.FieldList.Len() method12Go | ast.FieldList.NumFields() method13Go | ast.FieldList.Append() method14Go | ast.FieldList.Set() method15Go | ast.FieldList.Filter() method16Go | ast.FieldList.Elem() method17Go | ast.FieldList.First() method18Go | ast.FieldList.Last() method19Go | ast.FieldList.List() method20Go | ast.FieldList.Pos() method21Go | ast.FieldList.End() method22Go | ast.FieldList.SetPos() method23Go | ast.FieldList.SetEnd() method24Go | ast.FieldList.SetList() method25Go | ast.FieldList.Add() method26Go | ast.FieldList.Get() method27Go | ast.FieldList.Remove() method28Go | ast.FieldList.InsertBefore() method29Go | ast.FieldList.InsertAfter() method30Go | ast.File.Name() method31Go | ast.File.Decls() method32Go | ast.File.Scope() method33Go | ast.File.Imports() method34Go | ast.File.Unresolved() method35Go | ast.File.Comments() method36Go | ast.File.AddDecl() method37Go | ast.File.AddImport() method38Go | ast.File.AddComment() method39Go | ast.File.RemoveDecl() method40Go | ast.File.RemoveImport() method41Go | ast.File.RemoveComment() method42Go | ast.File.Package() method43Go | ast.File.SetPackage() method44Go | ast.File.Pos() method

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "test.go", nil, parser.ParseComments)4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(fset)8 fmt.Println(f)9 fmt.Println(f.Name)10 fmt.Println("Imports:")11 for _, s := range f.Imports {12 fmt.Println(s.Path.Value)13 }14 fmt.Println("Comments:")15 for _, c := range f.Comments {16 fmt.Println(c.Text())17 }18 fmt.Println("Identifiers and literals:")19 ast.Inspect(f, func(n ast.Node) bool {20 switch x := n.(type) {21 fmt.Println(x.Name)22 fmt.Println(x.Value)23 }24 })25 fmt.Println("Function declarations:")26 ast.Inspect(f, func(n ast.Node) bool {27 switch x := n.(type) {28 fmt.Println(x.Name.Name)29 }30 })31 fmt.Println("Function declarations:")32 ast.Inspect(f, func(n ast.Node) bool {33 switch x := n.(type) {34 fmt.Println(x.Name.Name)35 }36 })37 fmt.Println("Function declarations:")38 ast.Inspect(f, func(n ast.Node) bool {39 switch x := n.(type) {40 fmt.Println(x.Name.Name)41 }42 })43 fmt.Println("Function declarations:")44 ast.Inspect(f, func(n ast.Node) bool {45 switch x := n.(type) {46 fmt.Println(x.Name.Name)47 }48 })49 fmt.Println("Function declarations:")50 ast.Inspect(f, func(n ast.Node) bool {

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)4 if err != nil {5 panic(err)6 }7 ast.Inspect(f, func(n ast.Node) bool {8 switch x := n.(type) {9 fmt.Println(x.Name)10 fmt.Println(x.Elts)11 fmt.Println(x.Name)12 fmt.Println(x.Fun)13 fmt.Println(x.Value)14 fmt.Println(x.Op)15 fmt.Println(x.Tok)16 fmt.Println(x.Tok)17 fmt.Println(x.X)18 }19 })20}211.go:6:2: expected declaration, found 'IDENT' import221.go:7:3: expected ';', found 'IDENT' "fmt"231.go:8:3: expected ';', found 'IDENT' "go"241.go:8:7: expected ';', found 'IDENT' ast251.go:9:3: expected ';', found 'IDENT' "go"261.go:9:7: expected ';', found 'IDENT' parser271.go:10:3: expected ';', found 'IDENT' "go"281.go:10:7: expected ';', found 'IDENT' token291.go:12:11: expected ';', found 'IDENT' main301.go:13:3: expected ';', found 'IDENT' fset311.go:13:8: expected ';', found

Full Screen

Full Screen

Count

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 fmt.Println("Total number of comments: ", len(node.Comments))9 fmt.Println("Total number of comments: ", ast.CommentMap(fset, node, node.Comments).Count())10}

Full Screen

Full Screen

Count

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 log.Fatal(err)7 }8 log.Println(f.Name.Name)9 log.Println(f.Decls)10 log.Println(len(f.Decls))11}

Full Screen

Full Screen

Count

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 panic(err)7 }8 fmt.Println("Number of nodes in ast:", ast.Count(node))9}10Go | ast.NewIdent()11Go | ast.NewObj()12Go | ast.NewPackage()13Go | ast.NewScope()14Go | ast.NewValueSpec()15Go | ast.NewAssignStmt()16Go | ast.NewBadDecl()17Go | ast.NewBadExpr()

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f := ast.File{4 Name: &ast.Ident{5 },6 Decls: []ast.Decl{7 &ast.GenDecl{8 Specs: []ast.Spec{9 &ast.ValueSpec{10 Names: []*ast.Ident{11 &ast.Ident{12 },13 },14 Type: &ast.Ident{15 },16 },17 &ast.ValueSpec{18 Names: []*ast.Ident{19 &ast.Ident{20 },21 },22 Type: &ast.Ident{23 },24 },25 },26 },27 },28 }29 n := ast.New(nil)30 n.Count(&f, nil)31 fmt.Println(n.NumVars)32}

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