Best Syzkaller code snippet using compiler.checkTypes
collect_api.go
Source:collect_api.go
...162 // 0: {AdventureInfoIn{}, AdventureInfoOut{}},163 // 1: {StartAdventureIn{}, StartAdventureOut{}},164 // }165 // }166 if x.Type.Results == nil || len(x.Type.Params.List) != 0 || len(x.Type.Results.List) != 1 || x.Name.String() != apiFuncName || !strings.HasSuffix(ps.checkTypes(x.Type.Results.List[0].Type).Key, apiStructKey) {167 // 没æè¿åï¼é空请æ±åæ°ï¼å¤ä¸ªè¿åå¼ï¼å½æ°åä¸ä¸ºapiï¼è¿ååæ°ç±»åä¸ä¸ºgithub.com/funny/fastapi.APIs168 return true169 }170 api := new(apiFunc)171 api.recv = ps.checkTypes(x.Recv.List[0].Type)172 api.req = make(map[string]*common.NewType)173 api.resp = make(map[string]*common.NewType)174 for _, v := range x.Body.List {175 switch xv := v.(type) {176 case *ast.ReturnStmt:177 // è¿å声æ178 ps.checkResults(api, xv.Results[0])179 return true180 default:181 // fmt.Printf("unsupported type %v", xv)182 // return true183 continue184 }185 }186 }187 return true188 }189 ast.Inspect(f, findStruct)190}191func (ps *FastPkgStructs) checkResults(api *apiFunc, vv ast.Expr) {192 switch vvv := vv.(type) {193 case *ast.CompositeLit:194 // key value195 for _, v := range vvv.Elts {196 // ç±»åä¸å¯¹ï¼ä¼panicï¼ ä¸éè¦æ£æµ197 kv := v.(*ast.KeyValueExpr)198 tk := ps.checkTypes(kv.Key)199 tv := kv.Value.(*ast.CompositeLit)200 var reqStruct *common.NewType201 var respStruct *common.NewType202 _, ok := tv.Elts[0].(*ast.Ident)203 if !ok {204 // not nil205 req := tv.Elts[0].(*ast.CompositeLit)206 reqStruct = ps.checkTypes(req.Type)207 }208 _, ok = tv.Elts[1].(*ast.Ident)209 if !ok {210 // not nil211 resp := tv.Elts[1].(*ast.CompositeLit)212 respStruct = ps.checkTypes(resp.Type)213 }214 api.req[tk.Value] = reqStruct215 api.resp[tk.Value] = respStruct216 }217 ps.api = *api218 case *ast.Ident:219 // api := fastapi.APIs{}220 t := vvv.Obj.Decl.(*ast.AssignStmt)221 tr := t.Rhs[0].(ast.Expr)222 ps.checkResults(api, tr)223 default:224 _ = vv.(*ast.CompositeLit)225 }226}227func (ps *FastPkgStructs) genField(node *ast.StructType, srcPath string) []common.ApiField {228 var field []common.ApiField229 for i := range node.Fields.List {230 f := node.Fields.List[i]231 if f.Names == nil || !ast.IsExported(f.Names[0].Name) {232 continue233 }234 newField := new(common.ApiField)235 newField.Name = f.Names[0].Name236 if f.Comment.Text() != "" {237 newField.SetDesc(f.Comment.Text())238 } else {239 newField.SetDesc(f.Doc.Text())240 }241 nt := ps.checkTypes(f.Type)242 if nt == nil {243 continue244 }245 newField.TypeName = nt.TypeName246 newField.PkgPath = nt.PkgPath247 newField.SetKey(nt.Key)248 field = append(field, *newField)249 }250 return field251}252// æ£æ¥ç±»å253func (ps *FastPkgStructs) checkTypes(typeToCheck ast.Expr) *common.NewType {254 switch t := typeToCheck.(type) {255 case *ast.Ident:256 obj := ps.info.ObjectOf(t)257 newType := new(common.NewType)258 if obj == nil {259 // åºæ¬ç±»å260 newType.TypeName = t.Name261 } else {262 newType.TypeName = obj.Name()263 if obj.Pkg() != nil {264 newType.PkgPath = obj.Pkg().Path()265 newType.Key = newType.PkgPath + "." + newType.TypeName266 }267 }268 return newType269 case *ast.SelectorExpr:270 // å
¶å®å
éé¢çç±»åï¼ å¦x.t271 return ps.checkTypes(t.Sel)272 case *ast.ArrayType:273 // å表274 elemType := ps.checkTypes(t.Elt)275 newType := new(common.NewType)276 if t.Len == nil {277 // slice278 newType.TypeName = "[]" + elemType.TypeName279 } else {280 v := t.Len.(*ast.BasicLit).Value281 newType.TypeName = fmt.Sprintf("[%s]"+elemType.TypeName, v)282 }283 newType.PkgPath = elemType.PkgPath284 if elemType.PkgPath != "" {285 // èªå®ä¹ç»æä½286 newType.Key = elemType.PkgPath + "." + elemType.TypeName287 }288 return newType289 case *ast.MapType:290 k := ps.checkTypes(t.Key)291 v := ps.checkTypes(t.Value)292 newType := new(common.NewType)293 newType.TypeName = fmt.Sprintf("map[%s]%s", k.TypeName, v.TypeName)294 newType.PkgPath = v.PkgPath295 newType.Key = v.Key296 return newType297 case *ast.StarExpr:298 // å¼ç¨ç±»å299 return ps.checkTypes(t.X)300 case *ast.BasicLit:301 newType := new(common.NewType)302 newType.TypeName = t.Kind.String()303 newType.PkgPath = ""304 newType.Key = newType.TypeName305 newType.Value = t.Value306 return newType307 case *ast.StructType:308 // t := map[string]struct {}309 newType := new(common.NewType)310 newType.TypeName = "struct"311 newType.PkgPath = ""312 newType.Key = ""313 newType.Value = "struct{}"...
scope.go
Source:scope.go
1package scope2import (3 "fmt"4 "strings"5)6const (7 maxNestLevel = 648)9// Kind defines a kind10type Kind int11// Kinds of Kind12const (13 KindVar Kind = iota14 KindParam15 KindFunction16 KindField17 KindArrayType18 KindStructType19 KindAliasType20 KindScalarType21 KindUniversal22 KindUndefined = -123)24func (k Kind) IsType() bool {25 return k == KindArrayType ||26 k == KindStructType ||27 k == KindAliasType ||28 k == KindScalarType29}30var (31 IntObj = Object{-1, nil, KindScalarType, nil}32 PIntObj = &IntObj33 CharObj = Object{-1, nil, KindScalarType, nil}34 PCharObj = &CharObj35 BoolObj = Object{-1, nil, KindScalarType, nil}36 PBoolObj = &BoolObj37 StringObj = Object{-1, nil, KindScalarType, nil}38 PStringObj = &StringObj39 UniversalObj = Object{-1, nil, KindScalarType, nil}40 PUniversalObj = &UniversalObj41)42// Object defines a scope object43type Object struct {44 Name int45 Next *Object46 Kind Kind47 // we use this to mimic the polymorphism the professor uses48 // on his compiler. shrug49 T ObjectType50}51// ObjectType object types have to implement this interface52type ObjectType interface {53 objType()54}55func (a Alias) objType() {}56func (a Type) objType() {}57func (a Array) objType() {}58func (a Struct) objType() {}59func (a Function) objType() {}60func (a Var) objType() {}61func (a Param) objType() {}62func (a Field) objType() {}63// Alias defines the alias object type64type Alias struct {65 BaseType *Object66 Size int67}68// Type defines the alias object type69type Type Alias70// Array defines the array object type71type Array struct {72 ElemType *Object73 NumElements int74 Size int75}76// Struct defines the struct object type77type Struct struct {78 Fields *Object79 Size int80}81// Function defines the function object type82type Function struct {83 PRetType *Object84 PParams *Object85 Index int86 Params int87 Vars int88}89// Var defines the var object type90type Var struct {91 PType *Object92 Index int93 Size int94}95// Param defines the var object type96type Param Var97// Field defines the var object type98type Field struct {99 PType *Object100 Index int101 Size int102}103// Analyser is the scope analyser104type Analyser struct {105 symbolTable [maxNestLevel]*Object106 level int107}108// NewBlock opens a new block109func (a *Analyser) NewBlock() int {110 a.level++111 a.symbolTable[a.level] = nil112 return a.level113}114// EndBlock ends a block115func (a *Analyser) EndBlock() int {116 a.level--117 return a.level118}119// DefineSymbol defines a symbol given its name120func (a *Analyser) DefineSymbol(name int) *Object {121 obj := &Object{}122 obj.Name = name123 obj.Kind = KindUndefined124 obj.Next = a.symbolTable[a.level]125 a.symbolTable[a.level] = obj126 return obj127}128// SearchLocalSymbol searches for a symbol locally129func (a *Analyser) SearchLocalSymbol(name int) *Object {130 obj := a.symbolTable[a.level]131 for obj != nil {132 if obj.Name == name {133 return obj134 }135 obj = obj.Next136 }137 return obj138}139// SearchGlobalSymbol searches for a symbol globally140func (a *Analyser) SearchGlobalSymbol(name int) *Object {141 var obj *Object142 for i := a.level; i >= 0; i-- {143 obj = a.symbolTable[i]144 for obj != nil {145 if obj.Name == name {146 return obj147 }148 obj = obj.Next149 }150 }151 return obj152}153// CheckTypes returns true if objects are of same type154func (a *Analyser) CheckTypes(p1, p2 *Object) bool {155 if p1 == p2 {156 return true157 } else if p1 == PUniversalObj || p2 == PUniversalObj {158 return true159 } else if p1.Kind == KindUniversal || p2.Kind == KindUniversal {160 return true161 } else if p1.Kind == KindAliasType && p2.Kind != KindAliasType {162 alias := p1.T.(Alias)163 return a.CheckTypes(alias.BaseType, p2)164 } else if p1.Kind != KindAliasType && p2.Kind == KindAliasType {165 alias := p2.T.(Alias)166 return a.CheckTypes(p1, alias.BaseType)167 } else if p1.Kind == p1.Kind {168 if p1.Kind == KindAliasType {169 a1 := p1.T.(Alias)170 a2 := p2.T.(Alias)171 return a.CheckTypes(a1.BaseType, a2.BaseType)172 } else if p1.Kind == KindArrayType {173 a1 := p1.T.(Array)174 a2 := p2.T.(Array)175 if a1.NumElements == a2.NumElements {176 return a.CheckTypes(a1.ElemType, a2.ElemType)177 }178 } else if p1.Kind == KindStructType {179 s1 := p1.T.(Struct)180 s2 := p2.T.(Struct)181 f1 := s1.Fields182 f2 := s2.Fields183 if f1 != nil && f2 != nil {184 // TODO185 }186 }187 }188 return false189}190func (o *Object) String() string {191 var sb strings.Builder192 sb.WriteString(fmt.Sprintf("Kind: %v Name: %v Type: ", o.Kind, o.Name))193 switch o.T.(type) {194 case Alias:195 sb.WriteString("Alias")196 case Type:197 sb.WriteString("Type")198 case Array:199 sb.WriteString("Array")200 case Struct:201 sb.WriteString("Struct")202 case Function:203 sb.WriteString("Function")204 case Var:205 sb.WriteString("Var")206 case Param:207 sb.WriteString("Param")208 case Field:209 sb.WriteString("Field")210 default:211 sb.WriteString("INVALID")212 }213 return sb.String()214}...
check.go
Source:check.go
...3func (c *Compiler) check(order parser.Order, ast parser.AST) {4 if order == parser.Prefix {5 c.createSymbols(ast)6 } else {7 c.checkTypes(ast)8 }9}10func (c *Compiler) createSymbols(ast parser.AST) {11 switch ast := ast.(type) {12 case *parser.Program:13 c.globalNFA.AddStates(1)14 case *parser.Transform:15 c.curTransform = &transform{}16 c.curTransform.NFA.AddStates(1)17 c.curTransform.GlobalPath = c.createPath(&c.globalNFA, ast.Foreach)18 c.transforms = append(c.transforms, c.curTransform)19 case *parser.PathExpr:20 c.createPath(&c.curTransform.NFA, ast.Path)21 c.exprs[ast] = &expr{typ: exprPath}22 case *parser.BinaryExpr:23 c.exprs[ast] = &expr{typ: exprUnkn}24 case *parser.CallExpr:25 panic("not implemented")26 case *parser.LiteralExpr:27 var typ exprType28 switch ast.Val.(type) {29 case int64:30 typ = exprNum31 case string:32 typ = exprStr33 default:34 panic("not reached")35 }36 c.exprs[ast] = &expr{typ: typ, val: ast.Val}37 }38}39func (c *Compiler) checkTypes(ast parser.AST) {40S:41 switch ast := ast.(type) {42 case *parser.Program:43 case *parser.Transform:44 case *parser.PathExpr:45 case *parser.BinaryExpr:46 expr := c.exprs[ast]47 exprLHS := c.exprs[ast.LHS]48 exprRHS := c.exprs[ast.RHS]49 if exprLHS.typ == exprUnkn || exprRHS.typ == exprUnkn {50 // Type-checking has already failed for a child expression and an error51 // has already been logged.52 break53 }...
checkTypes
Using AI Code Generation
1import "fmt"2import "go/ast"3import "go/parser"4import "go/token"5import "go/types"6import "golang.org/x/tools/go/loader"7import "golang.org/x/tools/go/ssa/ssautil"8func main() {9 prog := loader.NewProgram()10 conf := loader.Config{11 TypeCheckFuncBodies: func(path string) bool { return true },12 }13 conf.ParseFile("1.go", nil)14 prog, err := conf.Load()15 if err != nil {16 fmt.Println(err)17 }18 comp := prog.Create()19 comp.CheckTypes()20 ssaPkg := ssautil.CreateProgram(comp, 0).CreatePackage(comp.Created[0].Pkg)21 for _, f := range ssaPkg.Members {22 if fun, ok := f.(*ssa.Function); ok {23 for _, bb := range fun.Blocks {24 for _, inst := range bb.Instrs {25 if call, ok := inst.(*ssa.Call); ok {26 if fun, ok := call.Call.Value.(*ssa.Function); ok {27 if fun.Pkg == nil {28 fmt.Println(fun.Name())29 }30 }31 }32 }33 }34 }35 }36}37import "fmt"38import "go/ast"39import "go/parser"40import "go/token"41import "go/types"42import "golang.org/x/tools/go/loader"43import "golang
checkTypes
Using AI Code Generation
1import (2func main() {3 var e []int = []int{1, 2, 3, 4, 5}4 var f [5]int = [5]int{1, 2, 3, 4, 5}5 var g map[string]int = map[string]int{"A": 1, "B": 2, "C": 3}6 var h struct {7 } = struct {8 }{"John", 20}9 fmt.Println("Type of a is ", reflect.TypeOf(a))10 fmt.Println("Type of b is ", reflect.TypeOf(b))11 fmt.Println("Type of c is ", reflect.TypeOf(c))12 fmt.Println("Type of d is ", reflect.TypeOf(d))13 fmt.Println("Type of e is ", reflect.TypeOf(e))14 fmt.Println("Type of f is ", reflect.TypeOf(f))15 fmt.Println("Type of g is ", reflect.TypeOf(g))16 fmt.Println("Type of h is ", reflect.TypeOf(h))17}18Type of h is struct { name string; age int }19import (20func checkTypes(a interface{}) {21 fmt.Println("Type of a is ", reflect.TypeOf(a))22}23func main() {24 checkTypes(10)25 checkTypes(10.10)26 checkTypes("Hello")27 checkTypes(true)28 checkTypes([]int{1, 2, 3, 4, 5})29 checkTypes([5]int{1, 2, 3, 4, 5})30 checkTypes(map[string]int{"A": 1, "B": 2, "C": 3})31 checkTypes(struct {32 }{"John", 20})33}
checkTypes
Using AI Code Generation
1import "fmt"2import "reflect"3func main() {4 fmt.Println(reflect.TypeOf(i))5 fmt.Println(reflect.TypeOf(s))6 fmt.Println(reflect.TypeOf(b))7}8import "fmt"9import "reflect"10func main() {11 fmt.Println(reflect.TypeOf(i))12 fmt.Println(reflect.TypeOf(s))13 fmt.Println(reflect.TypeOf(b))14}15import "fmt"16import "reflect"17func main() {18 fmt.Println(reflect.TypeOf(i))19 fmt.Println(reflect.TypeOf(s))20 fmt.Println(reflect.TypeOf(b))21}22import "fmt"23import "reflect"24func main() {25 fmt.Println(reflect.TypeOf(i))26 fmt.Println(reflect.TypeOf(s))27 fmt.Println(reflect.TypeOf(b))28}29import "fmt"30import "reflect"31func main() {32 fmt.Println(reflect.TypeOf(i))33 fmt.Println(reflect.TypeOf(s))34 fmt.Println(reflect.TypeOf(b))35}36import "fmt"37import "reflect"38func main() {
checkTypes
Using AI Code Generation
1import "fmt"2type compiler struct {3}4func main() {5 fmt.Println("Name:", c.name)6 fmt.Println("Version:", c.version)7 c.checkTypes()8}9func (c compiler) checkTypes() {10 fmt.Printf("Type of name is %T, type of version is %T", c.name, c.version)11}
checkTypes
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 c := gas.NewCompiler()5 fs := gas.NewFileSet()6 f := fs.AddFile("2.go")7 n := gas.NewNode(f, 0, 0)8 c.AddType("int")9 c.AddType("string")10 c.AddType("bool")11 fmt.Println(c.CheckTypes(n, "int"))12 fmt.Println(c.CheckTypes(n, "string"))13 fmt.Println(c.CheckTypes(n, "bool"))14 fmt.Println(c.CheckTypes(n, "float"))15}
checkTypes
Using AI Code Generation
1import (2func main() {3 scanner := bufio.NewScanner(os.Stdin)4 compiler := compiler{}5 for scanner.Scan() {6 input := scanner.Text()7 if comment.MatchString(input) {8 }9 variable := regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*\s*=\s*[a-zA-Z_][a-zA-Z0-9_]*\s*;`)10 if variable.MatchString(input) {11 compiler.addVariable(input)12 }13 function := regexp.MustCompile(`^func\s*[a-zA-Z_][a-zA-Z0-9_]*\s*\(.*\)\s*{`)14 if function.MatchString(input) {15 compiler.addFunction(input)16 }17 assignment := regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*\s*=\s*[a-zA-Z_][a-zA-Z0-9_]*\s*;`)18 if assignment.MatchString(input) {19 compiler.addAssignment(input)20 }21 ifStatement := regexp.MustCompile(`^if\s*\(.*\)\s*{`)22 if ifStatement.MatchString(input) {23 compiler.addIfStatement(input)24 }25 elseStatement := regexp.MustCompile(`^} else\s*{`)26 if elseStatement.MatchString(input) {27 compiler.addElseStatement(input)28 }29 forStatement := regexp.MustCompile(`^for\s*\(.*\)\s*{`)30 if forStatement.MatchString(input) {
checkTypes
Using AI Code Generation
1import (2type compiler struct {3}4func (c *compiler) checkTypes(a interface{}, b interface{}) bool {5}6func main() {7 fmt.Println(c.checkTypes(1, 1))8 fmt.Println(c.checkTypes(1, "1"))9 fmt.Println(c.checkTypes(1, 1.0))10}11import (12type compiler struct {13}14func (c *compiler) checkTypes(a interface{}, b interface{}) bool {15}16func main() {17 fmt.Println(c.checkTypes(1, 1))18 fmt.Println(c.checkTypes(1, "1"))19 fmt.Println(c.checkTypes(1, 1.0))20 fmt.Println(c.checkTypes(1.0, 1))21}22import (23type compiler struct {24}25func (c *compiler) checkTypes(a interface{}, b interface{}) bool {26}27func main() {28 fmt.Println(c.checkTypes(1, 1))29 fmt.Println(c.checkTypes(1, "1"))30 fmt.Println(c.checkTypes(1, 1.0))31 fmt.Println(c.checkTypes(1.0, 1))32 fmt.Println(c.checkTypes(1.0, 1.0))33}34import (35type compiler struct {36}37func (c *compiler) checkTypes(a interface{}, b interface{}) bool {38}39func main() {40 fmt.Println(c.checkTypes(
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!!