1
0
mirror of https://github.com/golang/go synced 2024-11-20 09:34:52 -07:00

cmd/cgo: add #line directives to avoid printing bogus references to Go source files

A bit contrived to come up with an example, but it happened in #15836, somehow.

	$ cat /tmp/x.go
	package main

	/*
	#include <stddef.h>

	int foo(void);

	int foo(void) {
		return 2;
	}

	#define int asdf
	*/
	import "C"

	func main() {
		println(C.foo())
	}

	$ go run /tmp/x.go
	# command-line-arguments
	cgo-builtin-prolog:9:31: error: unknown type name 'asdf'   <<<<<
	_GoString_ GoStringN(char *p, int l);
	                              ^
	/tmp/x.go:12:13: note: expanded from macro 'int'
	#define int asdf
	            ^
	cgo-builtin-prolog:10:28: error: unknown type name 'asdf'  <<<<<
	_GoBytes_ GoBytes(void *p, int n);
	                           ^
	/tmp/x.go:12:13: note: expanded from macro 'int'
	#define int asdf
	            ^
	2 errors generated.

The two marked lines used to refer incorrectly to /tmp/x.go.

Fixes #15836.

Change-Id: I08ef60a53cfd148112fceb651eaf7b75d94a7a8d
Reviewed-on: https://go-review.googlesource.com/32613
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Russ Cox 2016-11-02 19:41:01 -04:00
parent a4a4d43028
commit b2290229c2
3 changed files with 15 additions and 0 deletions

View File

@ -87,6 +87,7 @@ func (f *File) ReadGo(name string) {
if cg != nil { if cg != nil {
f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), name) f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), name)
f.Preamble += commentText(cg) + "\n" f.Preamble += commentText(cg) + "\n"
f.Preamble += "#line 1 \"cgo-generated-wrapper\"\n"
} }
} }
} }

View File

@ -429,6 +429,7 @@ func (p *Package) loadDWARF(f *File, names []*Name) {
var b bytes.Buffer var b bytes.Buffer
b.WriteString(f.Preamble) b.WriteString(f.Preamble)
b.WriteString(builtinProlog) b.WriteString(builtinProlog)
b.WriteString("#line 1 \"cgo-dwarf-inference\"\n")
for i, n := range names { for i, n := range names {
fmt.Fprintf(&b, "__typeof__(%s) *__cgo__%d;\n", n.C, i) fmt.Fprintf(&b, "__typeof__(%s) *__cgo__%d;\n", n.C, i)
if n.Kind == "const" { if n.Kind == "const" {

View File

@ -126,6 +126,13 @@ func (p *Package) writeDefs() {
fmt.Fprint(fgo2, goProlog) fmt.Fprint(fgo2, goProlog)
} }
if fc != nil {
fmt.Fprintf(fc, "#line 1 \"cgo-generated-wrappers\"\n")
}
if fm != nil {
fmt.Fprintf(fm, "#line 1 \"cgo-generated-wrappers\"\n")
}
gccgoSymbolPrefix := p.gccgoSymbolPrefix() gccgoSymbolPrefix := p.gccgoSymbolPrefix()
cVars := make(map[string]bool) cVars := make(map[string]bool)
@ -1301,6 +1308,7 @@ func (p *Package) cgoType(e ast.Expr) *Type {
} }
const gccProlog = ` const gccProlog = `
#line 1 "cgo-gcc-prolog"
/* /*
If x and y are not equal, the type will be invalid If x and y are not equal, the type will be invalid
(have a negative array count) and an inscrutable error will come (have a negative array count) and an inscrutable error will come
@ -1334,6 +1342,7 @@ const noTsanProlog = `
// This must match the TSAN code in runtime/cgo/libcgo.h. // This must match the TSAN code in runtime/cgo/libcgo.h.
const yesTsanProlog = ` const yesTsanProlog = `
#line 1 "cgo-tsan-prolog"
#define CGO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize_thread)) #define CGO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize_thread))
long long _cgo_sync __attribute__ ((common)); long long _cgo_sync __attribute__ ((common));
@ -1356,6 +1365,7 @@ static void _cgo_tsan_release() {
var tsanProlog = noTsanProlog var tsanProlog = noTsanProlog
const builtinProlog = ` const builtinProlog = `
#line 1 "cgo-builtin-prolog"
#include <stddef.h> /* for ptrdiff_t and size_t below */ #include <stddef.h> /* for ptrdiff_t and size_t below */
/* Define intgo when compiling with GCC. */ /* Define intgo when compiling with GCC. */
@ -1508,6 +1518,7 @@ func (p *Package) cPrologGccgo() string {
} }
const cPrologGccgo = ` const cPrologGccgo = `
#line 1 "cgo-c-prolog-gccgo"
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -1605,6 +1616,7 @@ func (p *Package) gccExportHeaderProlog() string {
const gccExportHeaderProlog = ` const gccExportHeaderProlog = `
/* Start of boilerplate cgo prologue. */ /* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"
#ifndef GO_CGO_PROLOGUE_H #ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H #define GO_CGO_PROLOGUE_H
@ -1658,6 +1670,7 @@ const gccExportHeaderEpilog = `
// We use weak declarations, and test the addresses, so that this code // We use weak declarations, and test the addresses, so that this code
// works with older versions of gccgo. // works with older versions of gccgo.
const gccgoExportFileProlog = ` const gccgoExportFileProlog = `
#line 1 "cgo-gccgo-export-file-prolog"
extern _Bool runtime_iscgo __attribute__ ((weak)); extern _Bool runtime_iscgo __attribute__ ((weak));
static void GoInit(void) __attribute__ ((constructor)); static void GoInit(void) __attribute__ ((constructor));