本文整理汇总了Golang中go/ast.FuncDecl类的典型用法代码示例。如果您正苦于以下问题:Golang FuncDecl类的具体用法?Golang FuncDecl怎么用?Golang FuncDecl使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FuncDecl类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: method
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
}
示例2: checkFuncLength
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()))
}
示例3: checkFunctionBody
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)
}
示例4: checkResultCount
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()))
}
示例5: checkTestFunc
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
}
示例6: checkParamCount
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()))
}
示例7: parseFunction
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
}
示例8: processFunction
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)
}
示例9: getFunc
// 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 = ""
}
}
示例10: funcDecl
// 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)
}
示例11: funcDecl
// 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)
}
示例12: funcDecl
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)
}
示例13: checkBoolParams
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()))
}
}
示例14: createFunctionMetadata
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
}
示例15: parseFuncDecl
func (p *parser) parseFuncDecl(n *parse.Node) ast.Decl {
scope := ast.NewScope(p.topScope)
funcDecl := ast.FuncDecl{
Name: p.parseIdent(n.Child(1)),
}
funcOrSig := n.Child(2).Child(0)
if funcOrSig.Rule() == signature {
funcDecl.Type = p.parseSignature(funcOrSig, scope)
} else {
funcDecl.Type, funcDecl.Body = p.parseFunc(funcOrSig, scope)
}
funcDecl.Type.Func = token.Pos(n.Child(0).Pos())
return &funcDecl
}