cgo: fix line/column reporting in syntax error messages

Этот коммит содержится в:
Ayke van Laethem 2021-09-17 02:24:22 +02:00 коммит произвёл Ron Evans
родитель bf9dab36f7
коммит 138add2b96
5 изменённых файлов: 29 добавлений и 13 удалений

Просмотреть файл

@ -391,7 +391,10 @@ func Process(files []*ast.File, dir string, fset *token.FileSet, cflags []string
// Process all CGo imports.
for _, genDecl := range statements {
cgoComment := genDecl.Doc.Text()
if genDecl.Doc == nil {
continue
}
cgoComment := getCommentText(genDecl.Doc)
pos := genDecl.Pos()
if genDecl.Doc != nil {
@ -1402,3 +1405,20 @@ func renameFieldName(fieldList *ast.FieldList, name string) {
renameFieldName(fieldList, "_"+name)
ident.Name = "_" + ident.Name
}
// getCommentText returns the raw text of an *ast.CommentGroup. It is similar to
// the Text() method but differs in that it doesn't strip anything and tries to
// keep all offsets correct by adding spaces and newlines where necessary.
func getCommentText(g *ast.CommentGroup) string {
var text string
for _, comment := range g.List {
c := comment.Text
if c[1] == '/' { /* comment */
c = " " + c[2:]
} else { // comment
c = " " + c[2:len(c)-2]
}
text += c + "\n"
}
return text
}

Просмотреть файл

@ -81,7 +81,7 @@ func (p *cgoPackage) parseFragment(fragment string, cflags []string, posFilename
defer C.free(unsafe.Pointer(filenameC))
// fix up error locations
fragment = fmt.Sprintf("# %d %#v\n", posLine+1, posFilename) + fragment
fragment = fmt.Sprintf("# %d %#v\n", posLine, posFilename) + fragment
fragmentC := C.CString(fragment)
defer C.free(unsafe.Pointer(fragmentC))

11
cgo/testdata/basic.out.go предоставленный
Просмотреть файл

@ -13,14 +13,3 @@ type C.uint32_t = uint32
type C.uint64_t = uint64
type C.uint8_t = uint8
type C.uintptr_t = uintptr
type C.char uint8
type C.int int32
type C.long int32
type C.longlong int64
type C.schar int8
type C.short int16
type C.uchar uint8
type C.uint uint32
type C.ulong uint32
type C.ulonglong uint64
type C.ushort uint16

5
cgo/testdata/errors.go предоставленный
Просмотреть файл

@ -14,6 +14,9 @@ typedef someType noType; // undefined type
#define SOME_CONST_2 6) // const not used (so no error)
#define SOME_CONST_3 1234 // const too large for byte
*/
//
//
// #define SOME_CONST_4 8) // after some empty lines
import "C"
// Make sure that errors for the following lines won't change with future
@ -30,4 +33,6 @@ var (
_ = C.SOME_CONST_1
_ byte = C.SOME_CONST_3
_ = C.SOME_CONST_4
)

2
cgo/testdata/errors.out.go предоставленный
Просмотреть файл

@ -2,12 +2,14 @@
// testdata/errors.go:4:2: warning: some warning
// testdata/errors.go:11:9: error: unknown type name 'someType'
// testdata/errors.go:13:23: unexpected token ), expected end of expression
// testdata/errors.go:19:26: unexpected token ), expected end of expression
// Type checking errors after CGo processing:
// testdata/errors.go:102: cannot use 2 << 10 (untyped int constant 2048) as uint8 value in variable declaration (overflows)
// testdata/errors.go:105: unknown field z in struct literal
// testdata/errors.go:108: undeclared name: C.SOME_CONST_1
// testdata/errors.go:110: cannot use C.SOME_CONST_3 (untyped int constant 1234) as byte value in variable declaration (overflows)
// testdata/errors.go:112: undeclared name: C.SOME_CONST_4
package main