mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/cgo: readability improvements to generated _cgo_export.h
Also copy doc comments from Go code to _cgo_export.h. This is a step toward installing this generated file when using -buildmode=c-archive or c-shared, so that C code can #include it. Change-Id: I3a243f7b386b58ec5c5ddb9a246bb9f9eddc5fb8 Reviewed-on: https://go-review.googlesource.com/9790 Reviewed-by: Minux Ma <minux@golang.org> Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
parent
da4fc529d9
commit
2f9acc13c1
4 changed files with 46 additions and 7 deletions
|
|
@ -235,9 +235,17 @@ func (f *File) saveExport(x interface{}, context string) {
|
||||||
error_(c.Pos(), "export comment has wrong name %q, want %q", name, n.Name.Name)
|
error_(c.Pos(), "export comment has wrong name %q, want %q", name, n.Name.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc := ""
|
||||||
|
for _, c1 := range n.Doc.List {
|
||||||
|
if c1 != c {
|
||||||
|
doc += c1.Text + "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
f.ExpFunc = append(f.ExpFunc, &ExpFunc{
|
f.ExpFunc = append(f.ExpFunc, &ExpFunc{
|
||||||
Func: n,
|
Func: n,
|
||||||
ExpName: name,
|
ExpName: name,
|
||||||
|
Doc: doc,
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -239,6 +239,9 @@ The following options are available when running cgo directly:
|
||||||
syscall package when bootstrapping a new target.
|
syscall package when bootstrapping a new target.
|
||||||
-objdir directory
|
-objdir directory
|
||||||
Put all generated files in directory.
|
Put all generated files in directory.
|
||||||
|
-importpath string
|
||||||
|
The import path for the Go package. Optional; used for
|
||||||
|
nicer comments in the generated files.
|
||||||
-gccgo
|
-gccgo
|
||||||
Generate output for the gccgo compiler rather than the
|
Generate output for the gccgo compiler rather than the
|
||||||
gc compiler.
|
gc compiler.
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,7 @@ func (n *Name) IsVar() bool {
|
||||||
type ExpFunc struct {
|
type ExpFunc struct {
|
||||||
Func *ast.FuncDecl
|
Func *ast.FuncDecl
|
||||||
ExpName string // name to use from C
|
ExpName string // name to use from C
|
||||||
|
Doc string
|
||||||
}
|
}
|
||||||
|
|
||||||
// A TypeRepr contains the string representation of a type.
|
// A TypeRepr contains the string representation of a type.
|
||||||
|
|
@ -161,11 +162,13 @@ var dynout = flag.String("dynout", "", "write -dynimport output to this file")
|
||||||
var dynpackage = flag.String("dynpackage", "main", "set Go package for -dynimport output")
|
var dynpackage = flag.String("dynpackage", "main", "set Go package for -dynimport output")
|
||||||
var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information in -dynimport mode")
|
var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information in -dynimport mode")
|
||||||
|
|
||||||
// These flags are for bootstrapping a new Go implementation,
|
// This flag is for bootstrapping a new Go implementation,
|
||||||
// to generate Go types that match the data layout and
|
// to generate Go types that match the data layout and
|
||||||
// constant values used in the host's C libraries and system calls.
|
// constant values used in the host's C libraries and system calls.
|
||||||
var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
|
var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
|
||||||
|
|
||||||
var objDir = flag.String("objdir", "", "object directory")
|
var objDir = flag.String("objdir", "", "object directory")
|
||||||
|
var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
|
||||||
|
|
||||||
var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
|
var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
|
||||||
var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
|
var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
|
||||||
|
|
|
||||||
|
|
@ -638,9 +638,7 @@ func (p *Package) writeExports(fgo2, fm io.Writer) {
|
||||||
fgcc := creat(*objDir + "_cgo_export.c")
|
fgcc := creat(*objDir + "_cgo_export.c")
|
||||||
fgcch := creat(*objDir + "_cgo_export.h")
|
fgcch := creat(*objDir + "_cgo_export.h")
|
||||||
|
|
||||||
fmt.Fprintf(fgcch, "/* Created by cgo - DO NOT EDIT. */\n/* This file is arch-specific. */\n")
|
p.writeExportHeader(fgcch)
|
||||||
fmt.Fprintf(fgcch, "%s\n", p.Preamble)
|
|
||||||
fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog())
|
|
||||||
|
|
||||||
fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n")
|
fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n")
|
||||||
fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n\n")
|
fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n\n")
|
||||||
|
|
@ -737,6 +735,10 @@ func (p *Package) writeExports(fgo2, fm io.Writer) {
|
||||||
s += fmt.Sprintf("%s p%d", p.cgoType(atype).C, i)
|
s += fmt.Sprintf("%s p%d", p.cgoType(atype).C, i)
|
||||||
})
|
})
|
||||||
s += ")"
|
s += ")"
|
||||||
|
|
||||||
|
if len(exp.Doc) > 0 {
|
||||||
|
fmt.Fprintf(fgcch, "\n%s", exp.Doc)
|
||||||
|
}
|
||||||
fmt.Fprintf(fgcch, "\nextern %s;\n", s)
|
fmt.Fprintf(fgcch, "\nextern %s;\n", s)
|
||||||
|
|
||||||
fmt.Fprintf(fgcc, "extern void _cgoexp%s_%s(void *, int);\n", cPrefix, exp.ExpName)
|
fmt.Fprintf(fgcc, "extern void _cgoexp%s_%s(void *, int);\n", cPrefix, exp.ExpName)
|
||||||
|
|
@ -833,9 +835,7 @@ func (p *Package) writeGccgoExports(fgo2, fm io.Writer) {
|
||||||
|
|
||||||
gccgoSymbolPrefix := p.gccgoSymbolPrefix()
|
gccgoSymbolPrefix := p.gccgoSymbolPrefix()
|
||||||
|
|
||||||
fmt.Fprintf(fgcch, "/* Created by cgo - DO NOT EDIT. */\n")
|
p.writeExportHeader(fgcch)
|
||||||
fmt.Fprintf(fgcch, "%s\n", p.Preamble)
|
|
||||||
fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog())
|
|
||||||
|
|
||||||
fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n")
|
fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n")
|
||||||
fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n")
|
fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n")
|
||||||
|
|
@ -861,6 +861,7 @@ func (p *Package) writeGccgoExports(fgo2, fm io.Writer) {
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
// Declare a result struct.
|
// Declare a result struct.
|
||||||
|
fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
|
||||||
fmt.Fprintf(fgcch, "struct %s_result {\n", exp.ExpName)
|
fmt.Fprintf(fgcch, "struct %s_result {\n", exp.ExpName)
|
||||||
forFieldList(fntype.Results,
|
forFieldList(fntype.Results,
|
||||||
func(i int, atype ast.Expr) {
|
func(i int, atype ast.Expr) {
|
||||||
|
|
@ -890,6 +891,10 @@ func (p *Package) writeGccgoExports(fgo2, fm io.Writer) {
|
||||||
fmt.Fprintf(cdeclBuf, ")")
|
fmt.Fprintf(cdeclBuf, ")")
|
||||||
cParams := cdeclBuf.String()
|
cParams := cdeclBuf.String()
|
||||||
|
|
||||||
|
if len(exp.Doc) > 0 {
|
||||||
|
fmt.Fprintf(fgcch, "\n%s", exp.Doc)
|
||||||
|
}
|
||||||
|
|
||||||
// We need to use a name that will be exported by the
|
// We need to use a name that will be exported by the
|
||||||
// Go code; otherwise gccgo will make it static and we
|
// Go code; otherwise gccgo will make it static and we
|
||||||
// will not be able to link against it from the C
|
// will not be able to link against it from the C
|
||||||
|
|
@ -989,6 +994,22 @@ func (p *Package) writeGccgoExports(fgo2, fm io.Writer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// writeExportHeader writes out the start of the _cgo_export.h file.
|
||||||
|
func (p *Package) writeExportHeader(fgcch io.Writer) {
|
||||||
|
fmt.Fprintf(fgcch, "/* Created by \"go tool cgo\" - DO NOT EDIT. */\n\n")
|
||||||
|
pkg := *importPath
|
||||||
|
if pkg == "" {
|
||||||
|
pkg = p.PackagePath
|
||||||
|
}
|
||||||
|
fmt.Fprintf(fgcch, "/* package %s */\n\n", pkg)
|
||||||
|
|
||||||
|
fmt.Fprintf(fgcch, "/* Start of preamble from import \"C\" comments. */\n\n")
|
||||||
|
fmt.Fprintf(fgcch, "%s\n", p.Preamble)
|
||||||
|
fmt.Fprintf(fgcch, "\n/* End of preamble from import \"C\" comments. */\n\n")
|
||||||
|
|
||||||
|
fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog())
|
||||||
|
}
|
||||||
|
|
||||||
// Return the package prefix when using gccgo.
|
// Return the package prefix when using gccgo.
|
||||||
func (p *Package) gccgoSymbolPrefix() string {
|
func (p *Package) gccgoSymbolPrefix() string {
|
||||||
if !*gccgo {
|
if !*gccgo {
|
||||||
|
|
@ -1302,6 +1323,8 @@ func (p *Package) gccExportHeaderProlog() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
const gccExportHeaderProlog = `
|
const gccExportHeaderProlog = `
|
||||||
|
/* Start of boilerplate cgo prologue. */
|
||||||
|
|
||||||
typedef signed char GoInt8;
|
typedef signed char GoInt8;
|
||||||
typedef unsigned char GoUint8;
|
typedef unsigned char GoUint8;
|
||||||
typedef short GoInt16;
|
typedef short GoInt16;
|
||||||
|
|
@ -1327,6 +1350,8 @@ typedef void *GoMap;
|
||||||
typedef void *GoChan;
|
typedef void *GoChan;
|
||||||
typedef struct { void *t; void *v; } GoInterface;
|
typedef struct { void *t; void *v; } GoInterface;
|
||||||
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
||||||
|
|
||||||
|
/* End of boilerplate cgo prologue. */
|
||||||
`
|
`
|
||||||
|
|
||||||
// gccgoExportFileProlog is written to the _cgo_export.c file when
|
// gccgoExportFileProlog is written to the _cgo_export.c file when
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue