aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2021-09-17 02:24:22 +0200
committerRon Evans <[email protected]>2021-09-28 18:44:11 +0200
commit138add2b96532fee1f132a69b706bbc5d10ed457 (patch)
tree7486e3dfa9148abd773a11338a40aa2e8b33c81d /cgo
parentbf9dab36f757eafbf5858784d14e4c0e96fbca9d (diff)
downloadtinygo-138add2b96532fee1f132a69b706bbc5d10ed457.tar.gz
tinygo-138add2b96532fee1f132a69b706bbc5d10ed457.zip
cgo: fix line/column reporting in syntax error messages
Diffstat (limited to 'cgo')
-rw-r--r--cgo/cgo.go22
-rw-r--r--cgo/libclang.go2
-rw-r--r--cgo/testdata/basic.out.go11
-rw-r--r--cgo/testdata/errors.go5
-rw-r--r--cgo/testdata/errors.out.go2
5 files changed, 29 insertions, 13 deletions
diff --git a/cgo/cgo.go b/cgo/cgo.go
index 2718062d0..a509e4b9a 100644
--- a/cgo/cgo.go
+++ b/cgo/cgo.go
@@ -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
+}
diff --git a/cgo/libclang.go b/cgo/libclang.go
index 22ce68cdd..3a496aa28 100644
--- a/cgo/libclang.go
+++ b/cgo/libclang.go
@@ -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))
diff --git a/cgo/testdata/basic.out.go b/cgo/testdata/basic.out.go
index 1fb2c4d1a..e83d71f43 100644
--- a/cgo/testdata/basic.out.go
+++ b/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
diff --git a/cgo/testdata/errors.go b/cgo/testdata/errors.go
index cdb28cf32..0f23f5854 100644
--- a/cgo/testdata/errors.go
+++ b/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
)
diff --git a/cgo/testdata/errors.out.go b/cgo/testdata/errors.out.go
index 065af2d54..d6bfd255b 100644
--- a/cgo/testdata/errors.out.go
+++ b/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