File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -402,6 +402,18 @@ func TestDarwinCgoImportDynamic(t *testing.T) {
402402 if strings .Contains (ir , "@bad_remote" ) {
403403 t .Error ("a declaration was created for the remote symbol of a non-uintptr trampoline global" )
404404 }
405+ if ! strings .Contains (ir , "load i64, ptr @main.libc_misplaced_trampoline_addr" ) {
406+ t .Error ("a directive that shares a line with code was honored" )
407+ }
408+ if strings .Contains (ir , "@misplaced_remote" ) {
409+ t .Error ("a declaration was created for the remote symbol of a misplaced directive" )
410+ }
411+ if strings .Contains (ir , "@indented_remote" ) {
412+ t .Error ("an indented directive was honored" )
413+ }
414+ if strings .Contains (ir , "@brace_remote" ) {
415+ t .Error ("a directive after code on a function line was honored" )
416+ }
405417}
406418
407419// normalizeIR canonicalizes LLVM-version-specific IR spellings for comparison
Original file line number Diff line number Diff line change @@ -779,13 +779,19 @@ func (c *compilerContext) loadASTComments(pkg *loader.Package) {
779779 for _ , group := range file .Comments {
780780 for _ , comment := range group .List {
781781 parts := strings .Fields (comment .Text )
782- if len (parts ) >= 2 && parts [0 ] == "//go:cgo_import_dynamic" {
783- local , remote := parts [1 ], parts [1 ]
784- if len (parts ) >= 3 {
785- remote = parts [2 ]
786- }
787- c .cgoImportDynamic [local ] = remote
782+ if len (parts ) < 2 || parts [0 ] != "//go:cgo_import_dynamic" {
783+ continue
784+ }
785+ // Accept a directive only in column 1. The gc compiler also
786+ // accepts an indented directive on a line of its own.
787+ if c .program .Fset .Position (comment .Slash ).Column != 1 {
788+ continue
789+ }
790+ local , remote := parts [1 ], parts [1 ]
791+ if len (parts ) >= 3 {
792+ remote = parts [2 ]
788793 }
794+ c .cgoImportDynamic [local ] = remote
789795 }
790796 }
791797
Original file line number Diff line number Diff line change @@ -8,6 +8,9 @@ var libc_fcntl_trampoline_addr uintptr
88var libc_nolib_trampoline_addr uintptr
99var libc_self_trampoline_addr uintptr
1010var libc_badtype_trampoline_addr uint32
11+ var libc_misplaced_trampoline_addr uintptr //go:cgo_import_dynamic libc_misplaced misplaced_remote "/usr/lib/libSystem.B.dylib"
12+ var libc_indented_trampoline_addr uintptr
13+ var libc_brace_trampoline_addr uintptr
1114
1215//go:cgo_import_dynamic libc_test remote$INODE64 "/usr/lib/libSystem.B.dylib"
1316//go:cgo_import_dynamic libc_ioctl ioctl "/usr/lib/libSystem.B.dylib"
@@ -49,3 +52,16 @@ func loadImportedSelfAddress() uintptr {
4952func loadImportedBadTypeAddress () uint32 {
5053 return libc_badtype_trampoline_addr
5154}
55+
56+ func loadMisplacedAddress () uintptr {
57+ return libc_misplaced_trampoline_addr
58+ }
59+
60+ func loadIndentedAddress () uintptr {
61+ //go:cgo_import_dynamic libc_indented indented_remote "/usr/lib/libSystem.B.dylib"
62+ return libc_indented_trampoline_addr
63+ }
64+
65+ func loadBraceAddress () uintptr { //go:cgo_import_dynamic libc_brace brace_remote "/usr/lib/libSystem.B.dylib"
66+ return libc_brace_trampoline_addr
67+ }
You can’t perform that action at this time.
0 commit comments