Best Syzkaller code snippet using compiler.templateBase
check.go
Source:check.go
...299 name string300 fields []*ast.Field301}302// templateName return the part before '[' for full template names.303func templateBase(name string) string {304 if pos := strings.IndexByte(name, '['); pos != -1 {305 return name[:pos]306 }307 return name308}309func parentTargetName(s *ast.Struct) string {310 // For template parents name is "struct_name[ARG1, ARG2]", strip the part after '['.311 return templateBase(s.Name.Name)312}313func (comp *compiler) checkLenType(t0, t *ast.Type, parents []parentDesc,314 checked, warned map[string]bool, isArg bool) {315 desc := comp.getTypeDesc(t)316 if desc == typeStruct {317 s := comp.structs[t.Ident]318 // Prune recursion, can happen even on correct tree via opt pointers.319 if checked[s.Name.Name] {320 return321 }322 checked[s.Name.Name] = true323 fields := s.Fields324 if s.IsUnion {325 fields = nil326 }327 parentName := parentTargetName(s)328 parents = append(parents, parentDesc{name: parentName, fields: fields})329 for _, fld := range s.Fields {330 comp.checkLenType(fld.Type, fld.Type, parents, checked, warned, false)331 }332 warned[parentName] = true333 return334 }335 _, args, _ := comp.getArgsBase(t, isArg)336 for i, arg := range args {337 argDesc := desc.Args[i]338 if argDesc.Type == typeArgLenTarget {339 comp.checkLenTarget(arg, t0, t, parents, warned)340 } else if argDesc.Type == typeArgType {341 comp.checkLenType(t0, arg, parents, checked, warned, argDesc.IsArg)342 }343 }344}345func (comp *compiler) checkLenTarget(arg, t0, t *ast.Type, parents []parentDesc, warned map[string]bool) {346 targets := append([]*ast.Type{arg}, arg.Colon...)347 for i, target := range targets {348 if target.Ident == prog.ParentRef && len(targets) != 1 {349 comp.error(target.Pos, "%v can't be part of path expressions", prog.ParentRef)350 return351 }352 if target.Ident == prog.SyscallRef {353 if i != 0 {354 comp.error(target.Pos, "syscall can't be in the middle of path expressions")355 return356 }357 if len(targets) == 1 {358 comp.error(targets[0].Pos, "no argument name after syscall reference")359 return360 }361 }362 }363 comp.checkLenTargetRec(t0, t, targets, parents, warned)364}365func (comp *compiler) checkLenTargetRec(t0, t *ast.Type, targets []*ast.Type,366 parents []parentDesc, warned map[string]bool) {367 if len(targets) == 0 {368 return369 }370 target := targets[0]371 targets = targets[1:]372 fields := parents[len(parents)-1].fields373 for _, fld := range fields {374 if target.Ident != fld.Name.Name {375 continue376 }377 if fld.Type == t0 {378 comp.error(target.Pos, "%v target %v refers to itself", t.Ident, target.Ident)379 return380 }381 if len(targets) == 0 {382 if t.Ident == "len" {383 typ, desc := comp.derefPointers(fld.Type)384 if desc == typeArray && comp.isVarlen(typ.Args[0]) {385 // We can reach the same struct multiple times starting from different386 // syscall arguments. Warn only once.387 if !warned[parents[len(parents)-1].name] {388 comp.warning(target.Pos, "len target %v refer to an array with"+389 " variable-size elements (do you mean bytesize?)",390 target.Ident)391 }392 }393 }394 return395 }396 typ, desc := comp.derefPointers(fld.Type)397 if desc != typeStruct {398 comp.error(target.Pos, "%v path %v does not refer to a struct", t.Ident, target.Ident)399 return400 }401 s := comp.structs[typ.Ident]402 if s.IsUnion {403 comp.error(target.Pos, "%v path %v does not refer to a struct", t.Ident, target.Ident)404 return405 }406 parents = append(parents, parentDesc{name: parentTargetName(s), fields: s.Fields})407 comp.checkLenTargetRec(t0, t, targets, parents, warned)408 return409 }410 for pi := len(parents) - 1; pi >= 0; pi-- {411 parent := parents[pi]412 if parent.name != "" && (parent.name == target.Ident || target.Ident == prog.ParentRef) ||413 parent.name == "" && target.Ident == prog.SyscallRef {414 if len(targets) == 0 {415 if t.Ident == "offsetof" {416 comp.error(target.Pos, "%v must refer to fields", t.Ident)417 return418 }419 } else {420 parents1 := make([]parentDesc, pi+1)421 copy(parents1, parents[:pi+1])422 comp.checkLenTargetRec(t0, t, targets, parents1, warned)423 }424 return425 }426 }427 comp.error(target.Pos, "%v target %v does not exist", t.Ident, target.Ident)428}429func CollectUnused(desc *ast.Description, target *targets.Target, eh ast.ErrorHandler) ([]ast.Node, error) {430 comp := createCompiler(desc, target, eh)431 comp.typecheck()432 if comp.errors > 0 {433 return nil, errors.New("typecheck failed")434 }435 nodes := comp.collectUnused()436 if comp.errors > 0 {437 return nil, errors.New("collectUnused failed")438 }439 return nodes, nil440}441func (comp *compiler) collectUnused() []ast.Node {442 var unused []ast.Node443 comp.used, _, _ = comp.collectUsed(false)444 structs, flags, strflags := comp.collectUsed(true)445 note := func(n ast.Node) {446 if pos, _, _ := n.Info(); pos.Builtin() {447 return448 }449 unused = append(unused, n)450 }451 for name, n := range comp.intFlags {452 if !flags[name] {453 note(n)454 }455 }456 for name, n := range comp.strFlags {457 if !strflags[name] {458 note(n)459 }460 }461 for name, n := range comp.resources {462 if !structs[name] {463 note(n)464 }465 }466 for name, n := range comp.structs {467 if !structs[name] {468 note(n)469 }470 }471 for name, n := range comp.typedefs {472 if !comp.usedTypedefs[name] {473 note(n)474 }475 }476 return unused477}478func (comp *compiler) collectUsed(all bool) (structs, flags, strflags map[string]bool) {479 structs = make(map[string]bool)480 flags = make(map[string]bool)481 strflags = make(map[string]bool)482 for _, decl := range comp.desc.Nodes {483 switch n := decl.(type) {484 case *ast.Call:485 if !all && n.NR == ^uint64(0) {486 break487 }488 for _, arg := range n.Args {489 comp.collectUsedType(structs, flags, strflags, arg.Type, true)490 }491 if n.Ret != nil {492 comp.collectUsedType(structs, flags, strflags, n.Ret, true)493 }494 }495 }496 return497}498func (comp *compiler) collectUsedType(structs, flags, strflags map[string]bool, t *ast.Type, isArg bool) {499 desc := comp.getTypeDesc(t)500 if desc == typeResource {501 r := comp.resources[t.Ident]502 for r != nil && !structs[r.Name.Name] {503 structs[r.Name.Name] = true504 r = comp.resources[r.Base.Ident]505 }506 return507 }508 if desc == typeStruct {509 if structs[t.Ident] {510 return511 }512 structs[t.Ident] = true513 s := comp.structs[t.Ident]514 for _, fld := range s.Fields {515 comp.collectUsedType(structs, flags, strflags, fld.Type, false)516 }517 return518 }519 if desc == typeFlags {520 flags[t.Args[0].Ident] = true521 return522 }523 if desc == typeString {524 if len(t.Args) != 0 && t.Args[0].Ident != "" {525 strflags[t.Args[0].Ident] = true526 }527 return528 }529 _, args, _ := comp.getArgsBase(t, isArg)530 for i, arg := range args {531 if desc.Args[i].Type == typeArgType {532 comp.collectUsedType(structs, flags, strflags, arg, desc.Args[i].IsArg)533 }534 }535}536func (comp *compiler) checkUnused() {537 for _, n := range comp.collectUnused() {538 pos, typ, name := n.Info()539 comp.error(pos, fmt.Sprintf("unused %v %v", typ, name))540 }541}542type structDir struct {543 Struct string544 Dir prog.Dir545}546func (comp *compiler) checkConstructors() {547 ctors := make(map[string]bool) // resources for which we have ctors548 inputs := make(map[string]bool) // resources which are used as inputs549 checked := make(map[structDir]bool)550 for _, decl := range comp.desc.Nodes {551 switch n := decl.(type) {552 case *ast.Call:553 for _, arg := range n.Args {554 comp.checkTypeCtors(arg.Type, prog.DirIn, true, ctors, inputs, checked)555 }556 if n.Ret != nil {557 comp.checkTypeCtors(n.Ret, prog.DirOut, true, ctors, inputs, checked)558 }559 }560 }561 for _, decl := range comp.desc.Nodes {562 switch n := decl.(type) {563 case *ast.Resource:564 name := n.Name.Name565 if !comp.used[name] {566 continue567 }568 if !ctors[name] {569 comp.error(n.Pos, "resource %v can't be created"+570 " (never mentioned as a syscall return value or output argument/field)", name)571 }572 if !inputs[name] {573 comp.error(n.Pos, "resource %v is never used as an input"+574 "(such resources are not useful)", name)575 }576 }577 }578}579func (comp *compiler) checkTypeCtors(t *ast.Type, dir prog.Dir, isArg bool,580 ctors, inputs map[string]bool, checked map[structDir]bool) {581 desc := comp.getTypeDesc(t)582 if desc == typeResource {583 // TODO(dvyukov): consider changing this to "dir == prog.DirOut".584 // We have few questionable cases where resources can be created585 // only by inout struct fields. These structs should be split586 // into two different structs: one is in and second is out.587 // But that will require attaching dir to individual fields.588 if dir != prog.DirIn {589 r := comp.resources[t.Ident]590 for r != nil && !ctors[r.Name.Name] {591 ctors[r.Name.Name] = true592 r = comp.resources[r.Base.Ident]593 }594 }595 if dir != prog.DirOut {596 r := comp.resources[t.Ident]597 for r != nil && !inputs[r.Name.Name] {598 inputs[r.Name.Name] = true599 r = comp.resources[r.Base.Ident]600 }601 }602 return603 }604 if desc == typeStruct {605 s := comp.structs[t.Ident]606 name := s.Name.Name607 key := structDir{name, dir}608 if checked[key] {609 return610 }611 checked[key] = true612 for _, fld := range s.Fields {613 fldDir, fldHasDir := comp.genFieldDir(fld)614 if !fldHasDir {615 fldDir = dir616 }617 comp.checkTypeCtors(fld.Type, fldDir, false, ctors, inputs, checked)618 }619 return620 }621 if desc == typePtr {622 dir = genDir(t.Args[0])623 }624 _, args, _ := comp.getArgsBase(t, isArg)625 for i, arg := range args {626 if desc.Args[i].Type == typeArgType {627 comp.checkTypeCtors(arg, dir, desc.Args[i].IsArg, ctors, inputs, checked)628 }629 }630}631func (comp *compiler) checkRecursion() {632 checked := make(map[string]bool)633 for _, decl := range comp.desc.Nodes {634 switch n := decl.(type) {635 case *ast.Resource:636 comp.checkResourceRecursion(n)637 case *ast.Struct:638 var path []pathElem639 comp.checkStructRecursion(checked, n, path)640 }641 }642}643func (comp *compiler) checkResourceRecursion(n *ast.Resource) {644 var seen []string645 for n != nil {646 if arrayContains(seen, n.Name.Name) {647 chain := ""648 for _, r := range seen {649 chain += r + "->"650 }651 chain += n.Name.Name652 comp.error(n.Pos, "recursive resource %v", chain)653 return654 }655 seen = append(seen, n.Name.Name)656 n = comp.resources[n.Base.Ident]657 }658}659type pathElem struct {660 Pos ast.Pos661 Struct string662 Field string663}664func (comp *compiler) checkStructRecursion(checked map[string]bool, n *ast.Struct, path []pathElem) {665 name := n.Name.Name666 if checked[name] {667 return668 }669 for i, elem := range path {670 if elem.Struct != name {671 continue672 }673 path = path[i:]674 str := ""675 for _, elem := range path {676 str += fmt.Sprintf("%v.%v -> ", elem.Struct, elem.Field)677 }678 str += name679 comp.error(path[0].Pos, "recursive declaration: %v (mark some pointers as opt)", str)680 checked[name] = true681 return682 }683 for _, f := range n.Fields {684 path = append(path, pathElem{685 Pos: f.Pos,686 Struct: name,687 Field: f.Name.Name,688 })689 comp.recurseField(checked, f.Type, path)690 path = path[:len(path)-1]691 }692 checked[name] = true693}694func (comp *compiler) recurseField(checked map[string]bool, t *ast.Type, path []pathElem) {695 desc := comp.getTypeDesc(t)696 if desc == typeStruct {697 comp.checkStructRecursion(checked, comp.structs[t.Ident], path)698 return699 }700 _, args, base := comp.getArgsBase(t, false)701 if desc == typePtr && base.IsOptional {702 return // optional pointers prune recursion703 }704 for i, arg := range args {705 if desc.Args[i].Type == typeArgType {706 comp.recurseField(checked, arg, path)707 }708 }709}710func (comp *compiler) checkStruct(ctx checkCtx, n *ast.Struct) {711 var flags checkFlags712 if !n.IsUnion {713 flags |= checkIsStruct714 }715 for _, f := range n.Fields {716 comp.checkType(ctx, f.Type, flags)717 }718 comp.parseAttrs(structOrUnionAttrs(n), n, n.Attrs)719}720func (comp *compiler) checkCall(n *ast.Call) {721 for _, a := range n.Args {722 comp.checkType(checkCtx{}, a.Type, checkIsArg)723 }724 if n.Ret != nil {725 comp.checkType(checkCtx{}, n.Ret, checkIsArg|checkIsRet)726 }727 comp.parseAttrs(callAttrs, n, n.Attrs)728}729type checkFlags int730const (731 checkIsArg checkFlags = 1 << iota // immediate syscall arg type732 checkIsRet // immediate syscall ret type733 checkIsStruct // immediate struct field type734 checkIsResourceBase // immediate resource base type735 checkIsTypedef // immediate type alias/template type736)737type checkCtx struct {738 instantiationStack []string739}740func (comp *compiler) checkType(ctx checkCtx, t *ast.Type, flags checkFlags) {741 if unexpected, _, ok := checkTypeKind(t, kindIdent); !ok {742 comp.error(t.Pos, "unexpected %v, expect type", unexpected)743 return744 }745 desc := comp.getTypeDesc(t)746 if desc == nil {747 comp.error(t.Pos, "unknown type %v", t.Ident)748 return749 }750 if desc == typeTypedef {751 err0 := comp.errors752 // Replace t with type alias/template target type inplace,753 // and check the replaced type recursively.754 comp.replaceTypedef(&ctx, t, flags)755 if err0 == comp.errors {756 comp.checkType(ctx, t, flags)757 }758 return759 }760 err0 := comp.errors761 comp.checkTypeBasic(t, desc, flags)762 if err0 != comp.errors {763 return764 }765 args := comp.checkTypeArgs(t, desc, flags)766 if err0 != comp.errors {767 return768 }769 for i, arg := range args {770 if desc.Args[i].Type == typeArgType {771 var innerFlags checkFlags772 if desc.Args[i].IsArg {773 innerFlags |= checkIsArg774 }775 comp.checkType(ctx, arg, innerFlags)776 } else {777 comp.checkTypeArg(t, arg, desc.Args[i])778 }779 }780 if err0 != comp.errors {781 return782 }783 if desc.Check != nil {784 _, args, base := comp.getArgsBase(t, flags&checkIsArg != 0)785 desc.Check(comp, t, args, base)786 }787}788func (comp *compiler) checkTypeBasic(t *ast.Type, desc *typeDesc, flags checkFlags) {789 for i, col := range t.Colon {790 if i >= desc.MaxColon {791 comp.error(col.Pos, "unexpected ':'")792 return793 }794 if flags&checkIsStruct == 0 {795 comp.error(col.Pos, "unexpected ':', only struct fields can be bitfields")796 return797 }798 }799 if flags&checkIsTypedef != 0 && !desc.CanBeTypedef {800 comp.error(t.Pos, "%v can't be type alias target", t.Ident)801 return802 }803 if flags&checkIsResourceBase != 0 &&804 (desc.CanBeResourceBase == nil || !desc.CanBeResourceBase(comp, t)) {805 comp.error(t.Pos, "%v can't be resource base (int types can)", t.Ident)806 return807 }808 canBeArg, canBeRet := false, false809 if desc.CanBeArgRet != nil {810 canBeArg, canBeRet = desc.CanBeArgRet(comp, t)811 }812 if flags&checkIsRet != 0 && !canBeRet {813 comp.error(t.Pos, "%v can't be syscall return", t.Ident)814 return815 }816 if flags&checkIsArg != 0 && !canBeArg {817 comp.error(t.Pos, "%v can't be syscall argument", t.Ident)818 return819 }820}821func (comp *compiler) checkTypeArgs(t *ast.Type, desc *typeDesc, flags checkFlags) []*ast.Type {822 args, opt := removeOpt(t)823 if opt != nil {824 if len(opt.Args) != 0 {825 comp.error(opt.Pos, "opt can't have arguments")826 }827 if flags&checkIsResourceBase != 0 || desc.CantBeOpt {828 what := "resource base"829 if desc.CantBeOpt {830 what = t.Ident831 }832 comp.error(opt.Pos, "%v can't be marked as opt", what)833 return nil834 }835 }836 addArgs := 0837 needBase := flags&checkIsArg == 0 && desc.NeedBase838 if needBase {839 addArgs++ // last arg must be base type, e.g. const[0, int32]840 }841 if len(args) > len(desc.Args)+addArgs || len(args) < len(desc.Args)-desc.OptArgs+addArgs {842 comp.error(t.Pos, "wrong number of arguments for type %v, expect %v",843 t.Ident, expectedTypeArgs(desc, needBase))844 return nil845 }846 if needBase {847 base := args[len(args)-1]848 args = args[:len(args)-1]849 comp.checkTypeArg(t, base, typeArgBase)850 }851 return args852}853func (comp *compiler) replaceTypedef(ctx *checkCtx, t *ast.Type, flags checkFlags) {854 typedefName := t.Ident855 typedef := comp.typedefs[typedefName]856 fullTypeName := ast.SerializeNode(t)857 if comp.brokenTypedefs[fullTypeName] {858 // We've already produced some errors for this exact type instantiation.859 // Don't produce duplicates, also helps to prevent exponential860 // slowdown due to some cases of recursion. But increment the number861 // of errors so that callers can understand that we did not succeed.862 comp.errors++863 return864 }865 comp.usedTypedefs[typedefName] = true866 err0 := comp.errors867 defer func() {868 comp.brokenTypedefs[fullTypeName] = err0 != comp.errors869 }()870 if len(t.Colon) != 0 {871 comp.error(t.Pos, "type alias %v with ':'", t.Ident)872 return873 }874 // Handling optional BASE argument.875 if len(typedef.Args) > 0 && typedef.Args[len(typedef.Args)-1].Name == argBase {876 if flags&checkIsArg != 0 && len(t.Args) == len(typedef.Args)-1 {877 t.Args = append(t.Args, &ast.Type{878 Pos: t.Pos,879 Ident: "intptr",880 })881 } else if len(t.Args) == len(typedef.Args) {882 comp.checkTypeArg(t, t.Args[len(t.Args)-1], typeArgBase)883 }884 }885 recursion := 0886 ctx.instantiationStack = append(ctx.instantiationStack, fullTypeName)887 for i, prev := range ctx.instantiationStack[:len(ctx.instantiationStack)-1] {888 if typedefName == templateBase(prev) {889 recursion++890 if recursion > 10 {891 comp.error(t.Pos, "type instantiation recursion: %v", strings.Join(ctx.instantiationStack, " -> "))892 return893 }894 }895 if prev != fullTypeName {896 continue897 }898 comp.error(t.Pos, "type instantiation loop: %v", strings.Join(ctx.instantiationStack[i:], " -> "))899 return900 }901 nargs := len(typedef.Args)902 args := t.Args...
templateBase
Using AI Code Generation
1import (2func main() {3 t, err := template.ParseFiles("template1.gohtml")4 if err != nil {5 fmt.Println(err)6 }7 t.Execute(os.Stdout, nil)8}9{{define "T1"}} This is template 1 {{end}}10import (11func main() {12 t, err := template.ParseFiles("template1.gohtml", "template2.gohtml")13 if err != nil {14 fmt.Println(err)15 }16 t.ExecuteTemplate(os.Stdout, "T2", nil)17}18{{define "T2"}} This is template 2 {{end}}19import (20func main() {21 t, err := template.ParseFiles("template1.gohtml", "template2.gohtml")22 if err != nil {23 fmt.Println(err)24 }25 t.ExecuteTemplate(os.Stdout, "T1", nil)26}27{{define "T1"}} This is template 1 {{end}}28import (29func main() {30 t, err := template.ParseFiles("template1.gohtml", "template2.gohtml")31 if err != nil {32 fmt.Println(err)33 }34 t.ExecuteTemplate(os.Stdout, "T3", nil)35}36{{define "T3"}} This is template 3 {{end}}37import (38func main() {39 t, err := template.ParseFiles("template1.gohtml", "template2.gohtml")40 if err != nil {
templateBase
Using AI Code Generation
1compiler := new Compiler()2compiler.templateBase()3compiler := new Compiler()4compiler.template1()5compiler := new Compiler()6compiler.template2()7compiler := new Compiler()8compiler.template3()9compiler := new Compiler()10compiler.template4()11compiler := new Compiler()12compiler.template5()13compiler := new Compiler()14compiler.template6()15compiler := new Compiler()16compiler.template7()17compiler := new Compiler()18compiler.template8()19compiler := new Compiler()20compiler.template9()21compiler := new Compiler()22compiler.template10()23compiler := new Compiler()24compiler.template11()25compiler := new Compiler()26compiler.template12()27compiler := new Compiler()28compiler.template13()29compiler := new Compiler()30compiler.template14()31compiler := new Compiler()32compiler.template15()33compiler := new Compiler()34compiler.template16()35compiler := new Compiler()36compiler.template17()37compiler := new Compiler()38compiler.template18()39compiler := new Compiler()40compiler.template19()41compiler := new Compiler()42compiler.template20()43compiler := new Compiler()44compiler.template21()45compiler := new Compiler()46compiler.template22()47compiler := new Compiler()48compiler.template23()49compiler := new Compiler()50compiler.template24()
templateBase
Using AI Code Generation
1import "fmt"2type compiler struct {3}4func (c compiler) templateBase() {5fmt.Println("TemplateBase method of compiler class")6}7type java struct {8}9func (j java) templateBase() {10fmt.Println("TemplateBase method of java class")11}12func main() {13c.templateBase()14j.templateBase()15}16import "fmt"17type compiler struct {18}19func (c compiler) templateBase() {20fmt.Println("TemplateBase method of compiler class")21}22type java struct {23}24func (j java) templateBase() {25fmt.Println("TemplateBase method of java class")26}27func main() {28c.templateBase()29j.compiler.templateBase()30}31import "fmt"32type compiler struct {33}34func (c compiler) templateBase() {35fmt.Println("TemplateBase method of compiler class")36}37type java struct {38}39func (j java) templateBase() {40fmt.Println("TemplateBase method of java class")41}42func main() {43c.templateBase()44j.compiler.templateBase()45j.templateBase()46}47import "fmt"48type compiler struct {49}50func (c compiler) templateBase() {51fmt.Println("TemplateBase method of compiler class")52}53type java struct {54}55func (j java) templateBase() {56fmt.Println("TemplateBase method of java class")57}58func main() {59c.templateBase()60j.compiler.templateBase()61j.templateBase()62j.compiler.templateBase()63}64import "
templateBase
Using AI Code Generation
1import (2func main() {3 t := template.New("test")4 t, _ = t.Parse("{{.}}")5 t = t.Lookup("test")6 fmt.Println(t.Name())7 fmt.Println(t.DefinedTemplates())8 fmt.Println(t.Tree.Root.String())9}10func (t *Template) ParseFiles(filenames ...string) (*Template, error)11import (12func main() {13 t := template.New("test")14 t, _ = t.Parse("{{.}}")15 t = t.Lookup("test")16 fmt.Println(t.Name())17 fmt.Println(t.DefinedTemplates())18 fmt.Println(t.Tree.Root.String())19}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!