func (v *fileVisitor) method(n *ast.FuncDecl) *Method {
	method := &Method{Name: n.Name.Name}
	method.Lines = []*Line{}

	start := v.fset.Position(n.Pos())
	end := v.fset.Position(n.End())
	startLine := start.Line
	startCol := start.Column
	endLine := end.Line
	endCol := end.Column
	// The blocks are sorted, so we can stop counting as soon as we reach the end of the relevant block.
	for _, b := range v.profile.Blocks {
		if b.StartLine > endLine || (b.StartLine == endLine && b.StartCol >= endCol) {
			// Past the end of the function.
			break
		}
		if b.EndLine < startLine || (b.EndLine == startLine && b.EndCol <= startCol) {
			// Before the beginning of the function
			continue
		}
		for i := b.StartLine; i <= b.EndLine; i++ {
			method.Lines = append(method.Lines, &Line{Number: i, Hits: int64(b.Count)})
		}
	}
	return method
}
Esempio n. 2
0
func (p *Parser) checkFuncLength(x *ast.FuncDecl) {
	numStatements := statementCount(x)
	if numStatements <= *statementThreshold {
		return
	}

	p.summary.addStatement(p.offender(x.Name.String(), numStatements, x.Pos()))
}
Esempio n. 3
0
func (f *file) checkFunctionBody(funcDecl *ast.FuncDecl) {
	if funcDecl.Body != nil {
		return
	}
	start := f.fset.Position(funcDecl.Pos())
	problem := genFuncBodyProblem(funcDecl.Name.Name, start)
	f.problems = append(f.problems, problem)
}
Esempio n. 4
0
func (p *Parser) checkResultCount(x *ast.FuncDecl) {
	numResults := x.Type.Results.NumFields()
	if numResults <= *resultThreshold {
		return
	}

	p.summary.addResult(p.offender(x.Name.String(), numResults, x.Pos()))
}
Esempio n. 5
0
File: test.go Progetto: Greentor/go
func checkTestFunc(fn *ast.FuncDecl, arg string) error {
	if !isTestFunc(fn, arg) {
		name := fn.Name.String()
		pos := testFileSet.Position(fn.Pos())
		return fmt.Errorf("%s: wrong signature for %s, must be: func %s(%s *testing.%s)", pos, name, name, strings.ToLower(arg), arg)
	}
	return nil
}
Esempio n. 6
0
func (p *Parser) checkParamCount(x *ast.FuncDecl) {
	numFields := x.Type.Params.NumFields()
	if numFields <= *paramThreshold {
		return
	}

	p.summary.addParam(p.offender(x.Name.String(), numFields, x.Pos()))
}
Esempio n. 7
0
func processFunction(funcDecl *ast.FuncDecl) {
	m := function{}

	m.bodyStart = fset.Position(funcDecl.Pos()).Line
	m.bodyEnd = fset.Position(funcDecl.End()).Line
	m.variables = getFunctionVariables(funcDecl)

	addFoundFunctions(m)
}
func parseFunction(funcDecl *ast.FuncDecl, fileInBytes []byte) (string, bool) {
	if ast.IsExported(funcDecl.Name.Name) {
		fmt.Printf("Function exported: %s\n", funcDecl.Name.Name)
		return string(fileInBytes[funcDecl.Pos()-1 : funcDecl.Body.Lbrace-1]), true
	} else {
		fmt.Printf("Function not exported: %s\n", funcDecl.Name.Name)
	}
	return "", false
}
Esempio n. 9
0
// Functions
//
// http://golang.org/doc/go_spec.html#Function_declarations
// https://developer.mozilla.org/en/JavaScript/Reference/Statements/function
func (tr *transform) getFunc(decl *ast.FuncDecl) {
	// godoc go/ast FuncDecl
	//  Doc  *CommentGroup // associated documentation; or nil
	//  Recv *FieldList    // receiver (methods); or nil (functions)
	//  Name *Ident        // function/method name
	//  Type *FuncType     // position of Func keyword, parameters and results
	//  Body *BlockStmt    // function body; or nil (forward declaration)

	// Check empty functions
	if len(decl.Body.List) == 0 {
		return
	}

	isFuncInit := false // function init()

	// == Initialization to save variables created on this function
	if decl.Name != nil { // discard literal functions
		tr.funcTotal++
		tr.funcId = tr.funcTotal
		tr.blockId = 0

		tr.vars[tr.funcId] = make(map[int]map[string]bool)
		tr.addr[tr.funcId] = make(map[int]map[string]bool)
		tr.maps[tr.funcId] = make(map[int]map[string]struct{})
		tr.slices[tr.funcId] = make(map[int]map[string]struct{})
		tr.zeroType[tr.funcId] = make(map[int]map[string]string)
	}
	// ==

	tr.addLine(decl.Pos())
	tr.addIfExported(decl.Name)

	if decl.Name.Name != "init" {
		tr.writeFunc(decl.Recv, decl.Name, decl.Type)
	} else {
		isFuncInit = true
		tr.WriteString("(function()" + SP)
	}

	tr.getStatement(decl.Body)

	if isFuncInit {
		tr.WriteString("());")
	}

	// At exiting of the function, it returns at the global scope.
	if decl.Name != nil {
		tr.funcId = 0
		tr.blockId = 0
	}
	if decl.Recv != nil {
		tr.recvVar = ""
	}
}
Esempio n. 10
0
File: main.go Progetto: ArjenL/taggo
// Handle a function declaration.
func funcDecl(fset *token.FileSet, decl *ast.FuncDecl) {
	var recvType string
	if decl.Recv != nil {
		// Method definition.  There's always only one receiver.
		recvType = "class:" + typeName(decl.Recv.List[0].Type)
	} else {
		// Normal function
		recvType = ""
	}
	emitTag(decl.Name.Name, decl.Pos(), fset, FUNC, recvType)
}
Esempio n. 11
0
func (p *printer) funcDecl(d *ast.FuncDecl) {
	p.setComment(d.Doc)
	p.print(d.Pos(), token.FUNC, blank)
	if d.Recv != nil {
		p.parameters(d.Recv) // method: print receiver
		p.print(blank)
	}
	p.expr(d.Name)
	p.signature(d.Type.Params, d.Type.Results)
	p.adjBlock(p.distanceFrom(d.Pos()), vtab, d.Body)
}
Esempio n. 12
0
// Sets multiLine to true if the declaration spans multiple lines.
func (p *printer) funcDecl(d *ast.FuncDecl, multiLine *bool) {
	p.setComment(d.Doc)
	p.print(d.Pos(), token.FUNC, blank)
	if d.Recv != nil {
		p.parameters(d.Recv, multiLine) // method: print receiver
		p.print(blank)
	}
	p.expr(d.Name, multiLine)
	p.signature(d.Type.Params, d.Type.Results, multiLine)
	p.funcBody(d.Body, distance(d.Pos(), p.pos), false, multiLine)
}
Esempio n. 13
0
func (p *Parser) checkBoolParams(x *ast.FuncDecl) {
	if *skipBoolParamCheck {
		return
	}
	for _, f := range x.Type.Params.List {
		// this is ugly, but:
		if fmt.Sprintf("%s", f.Type) != "bool" {
			continue
		}
		p.summary.addBoolParam(p.offender(x.Name.String(), 0, x.Pos()))
	}
}
Esempio n. 14
0
func (c *compiler) createFunctionMetadata(f *ast.FuncDecl, fn *LLVMValue) llvm.DebugDescriptor {
	if len(c.debug_context) == 0 {
		return nil
	}

	file := c.fileset.File(f.Pos())
	fnptr := fn.value
	fun := fnptr.IsAFunction()
	if fun.IsNil() {
		fnptr = llvm.ConstExtractValue(fn.value, []uint32{0})
	}
	meta := &llvm.SubprogramDescriptor{
		Name:        fnptr.Name(),
		DisplayName: f.Name.Name,
		Path:        llvm.FileDescriptor(file.Name()),
		Line:        uint32(file.Line(f.Pos())),
		ScopeLine:   uint32(file.Line(f.Body.Pos())),
		Context:     &llvm.ContextDescriptor{llvm.FileDescriptor(file.Name())},
		Function:    fnptr}

	var result types.Type
	var metaparams []llvm.DebugDescriptor
	if ftyp, ok := fn.Type().(*types.Signature); ok {
		if recv := ftyp.Recv(); recv != nil {
			metaparams = append(metaparams, c.tollvmDebugDescriptor(recv.Type()))
		}
		if ftyp.Params() != nil {
			for i := 0; i < ftyp.Params().Len(); i++ {
				p := ftyp.Params().At(i)
				metaparams = append(metaparams, c.tollvmDebugDescriptor(p.Type()))
			}
		}
		if ftyp.Results() != nil {
			result = ftyp.Results().At(0).Type()
			// TODO: what to do with multiple returns?
			for i := 1; i < ftyp.Results().Len(); i++ {
				p := ftyp.Results().At(i)
				metaparams = append(metaparams, c.tollvmDebugDescriptor(p.Type()))
			}
		}
	}

	meta.Type = llvm.NewSubroutineCompositeType(
		c.tollvmDebugDescriptor(result),
		metaparams,
	)

	// compile unit is the first context object pushed
	compileUnit := c.debug_context[0].(*llvm.CompileUnitDescriptor)
	compileUnit.Subprograms = append(compileUnit.Subprograms, meta)
	return meta
}
Esempio n. 15
0
func (check *checker) funcDecl(obj *Func, fdecl *ast.FuncDecl) {
	// func declarations cannot use iota
	assert(check.iota == nil)

	obj.typ = Typ[Invalid] // guard against cycles
	sig := check.funcType(fdecl.Recv, fdecl.Type, nil)
	if sig.recv == nil && obj.name == "init" && (sig.params.Len() > 0 || sig.results.Len() > 0) {
		check.errorf(fdecl.Pos(), "func init must have no arguments and no return values")
		// ok to continue
	}
	obj.typ = sig

	check.later(obj, sig, fdecl.Body)
}
Esempio n. 16
0
func (f *file) checkFunctionLine(funcDecl *ast.FuncDecl) {
	lineLimit := f.config.FunctionLine
	if lineLimit <= 0 {
		return
	}
	start := f.fset.Position(funcDecl.Pos())

	startLine := start.Line
	endLine := f.fset.Position(funcDecl.End()).Line
	lineCount := endLine - startLine
	if lineCount > lineLimit {
		problem := genFuncLineProblem(funcDecl.Name.Name, lineCount, lineLimit, start)
		f.problems = append(f.problems, problem)
	}
}
Esempio n. 17
0
File: nodes.go Progetto: 8l/go-learn
// Sets multiLine to true if the declaration spans multiple lines.
func (p *printer) funcDecl(d *ast.FuncDecl, multiLine *bool) {
	p.leadComment(d.Doc)
	p.print(d.Pos(), token.FUNC, blank)
	if recv := d.Recv; recv != nil {
		// method: print receiver
		p.print(token.LPAREN)
		if len(recv.Names) > 0 {
			p.expr(recv.Names[0], multiLine)
			p.print(blank)
		}
		p.expr(recv.Type, multiLine)
		p.print(token.RPAREN, blank)
	}
	p.expr(d.Name, multiLine)
	p.signature(d.Type.Params, d.Type.Results, multiLine)
	p.funcBody(d.Body, false, multiLine)
}
Esempio n. 18
0
File: nodes.go Progetto: droundy/ogo
func (p *printer) funcDecl(d *ast.FuncDecl) {
	p.setComment(d.Doc)
	if d.Recv != nil {
		p.parameters(d.Recv) // method: print receiver
		p.print(blank)
	}
	if d.Name.Name == "main" {
		p.print("int ")
	} else {
		p.funcreturn(d.Type.Results)
	}
	p.expr(d.Name)
	p.signature(d.Type.Params)
	if d.Name.Name == "main" {
		zero := &ast.BasicLit{Kind: token.INT, Value: "0"}
		d.Body.List = append(d.Body.List, &ast.ReturnStmt{Results: []ast.Expr{zero}})
	}
	p.funcBody(d.Body, p.distance(d.Pos(), p.pos), false)
}
Esempio n. 19
0
// parseFunction creates a tag for function declaration f.
func (p *tagParser) parseFunction(f *ast.FuncDecl) {
	tag := p.createTag(f.Name.Name, f.Pos(), Function)

	tag.Fields[Access] = getAccess(tag.Name)
	tag.Fields[Signature] = fmt.Sprintf("(%s)", getTypes(f.Type.Params, true))
	tag.Fields[TypeField] = getTypes(f.Type.Results, false)

	if f.Recv != nil && len(f.Recv.List) > 0 {
		// this function has a receiver, set the type to Method
		tag.Fields[ReceiverType] = getType(f.Recv.List[0].Type, false)
		tag.Type = Method
	} else if name, ok := p.belongsToReceiver(f.Type.Results); ok {
		// this function does not have a receiver, but it belongs to one based
		// on its return values; its type will be Function instead of Method.
		tag.Fields[ReceiverType] = name
		tag.Type = Function
	}

	p.tags = append(p.tags, tag)
}
Esempio n. 20
0
func exampleOutput(fun *ast.FuncDecl, comments []*ast.CommentGroup) string {
	// find the last comment in the function
	var last *ast.CommentGroup
	for _, cg := range comments {
		if cg.Pos() < fun.Pos() {
			continue
		}
		if cg.End() > fun.End() {
			break
		}
		last = cg
	}
	if last != nil {
		// test that it begins with the correct prefix
		text := last.Text()
		if loc := outputPrefix.FindStringIndex(text); loc != nil {
			return strings.TrimSpace(text[loc[1]:])
		}
	}
	return "" // no suitable comment found
}
Esempio n. 21
0
func (p *printer) funcDecl(d *ast.FuncDecl) {
	p.setComment(d.Doc)
	p.print(d.Pos(), token.FUNC, blank)
	if d.Recv != nil {
		p.expr(d.Recv.List[0].Type) // method: print receiver
		p.print(d.Pos(), ".")
		if names := d.Recv.List[0].Names; len(names) > 0 {
			if name := names[0]; name != nil && name.Name != "_" {
				p.rcvName = name
				defer func() {
					p.rcvName = nil
				}()
			}
		}
	}
	p.expr(d.Name)
	p.signature(d.Type.Params, d.Type.Results)
	p.inFunc = true
	p.adjBlock(d.Body)
	p.inFunc = false
	p.print(unindent)
}
Esempio n. 22
0
File: decl.go Progetto: qioixiy/llgo
func (c *compiler) VisitFuncDecl(f *ast.FuncDecl) Value {
	fn := c.Resolve(f.Name).(*LLVMValue)
	attributes := parseAttributes(f.Doc)
	for _, attr := range attributes {
		attr.Apply(fn)
	}
	if f.Body == nil {
		return fn
	}

	var paramVars []*types.Var
	ftyp := fn.Type().(*types.Signature)
	if recv := ftyp.Recv(); recv != nil {
		paramVars = append(paramVars, recv)
	}
	if ftyp.Params() != nil {
		for i := 0; i < ftyp.Params().Len(); i++ {
			p := ftyp.Params().At(i)
			paramVars = append(paramVars, p)
		}
	}

	c.pushDebugContext(c.createFunctionMetadata(f, fn))
	defer c.popDebugContext()
	c.setDebugLine(f.Pos())

	paramVarsTuple := types.NewTuple(paramVars...)
	c.buildFunction(fn, nil, paramVarsTuple, ftyp.Results(), f.Body)

	if f.Recv == nil && f.Name.Name == "init" {
		// Is it an 'init' function? Then record it.
		fnptr := llvm.ConstExtractValue(fn.value, []uint32{0})
		c.initfuncs = append(c.initfuncs, fnptr)
	}
	return fn
}
Esempio n. 23
0
func (v *CompileVisitor) FunctionPrologue(fn *ast.FuncDecl) {
	v.Stack = v.Stack.New(fn.Name.Name)
	ftype := ast.NewType(ast.Function)
	ftype.N = uint(fn.Type.Results.NumFields())
	ftype.Params = ast.NewScope(nil)
	fmt.Println("Working on function", fn.Name.Name)
	if fn.Type.Results != nil {
		resultnum := 0
		for _, resultfield := range fn.Type.Results.List {
			names := []string{"_"}
			if resultfield.Names != nil {
				names = []string{}
				for _, i := range resultfield.Names {
					names = append(names, i.Name)
				}
			}
			t := TypeExpression(resultfield.Type)
			for _, n := range names {
				ftype.Params.Insert(&ast.Object{ast.Fun, n, t, resultfield, 0})
				resultnum++
				v.Stack.DefineVariable(n, t, fmt.Sprintf("return_value_%d", resultnum))

				// The return values are actually allocated elsewhere... here
				// we just need to define the function type properly so it
				// gets called properly.
			}
		}
	}
	fmt.Println("Stack size after results is", v.Stack.Size)
	v.Stack.ReturnSize = v.Stack.Size
	// The arguments are pushed last argument first, so that eventually
	// the types of the "later" arguments can depend on the first
	// arguments, which seems nice to me.
	for pi := len(fn.Type.Params.List) - 1; pi >= 0; pi-- {
		paramfield := fn.Type.Params.List[pi]
		names := []string{"_"}
		if paramfield.Names != nil {
			names = []string{}
			for _, i := range paramfield.Names {
				names = append(names, i.Name)
			}
		}
		t := TypeExpression(paramfield.Type)
		for i := len(names) - 1; i >= 0; i-- {
			n := names[i]
			ftype.Params.Insert(&ast.Object{ast.Fun, n, t, paramfield, 0})
			v.Stack.DefineVariable(n, t)

			// The function parameters are actually allocated
			// elsewhere... here we just need to define the function type
			// properly so it gets called properly.
		}
	}
	fmt.Println("Stack size after params is", v.Stack.Size)
	v.Stack.DefineVariable("return", IntType)
	fmt.Println("Stack size after return is", v.Stack.Size)
	v.Stack = v.Stack.New("_")
	DefineGlobal(fn.Name.Name, ftype)
	// symbol for the start name
	pos := myfiles.Position(fn.Pos())
	v.Append(x86.Commented(x86.GlobalSymbol("main_"+fn.Name.Name),
		fmt.Sprint(pos.Filename, ": line ", pos.Line)))
	// If we had arguments, we'd want to swap them with the return
	// address here...
}