]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: don't let inserted /*line*/ become a // comment
authorIan Lance Taylor <iant@golang.org>
Fri, 21 Dec 2018 21:14:09 +0000 (13:14 -0800)
committerIan Lance Taylor <iant@golang.org>
Sat, 22 Dec 2018 00:45:13 +0000 (00:45 +0000)
Fixes #29383

Change-Id: I0fb2929863e153b96d32d851e25e536231e4ae65
Reviewed-on: https://go-review.googlesource.com/c/155638
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
misc/cgo/test/issue29383.go [new file with mode: 0644]
src/cmd/cgo/gcc.go

diff --git a/misc/cgo/test/issue29383.go b/misc/cgo/test/issue29383.go
new file mode 100644 (file)
index 0000000..462c9a3
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// cgo's /*line*/ comments failed when inserted after '/',
+// because the result looked like a "//" comment.
+// No runtime test; just make sure it compiles.
+
+package cgotest
+
+// #include <stddef.h>
+import "C"
+
+func Issue29383(n, size uint) int {
+       if ^C.size_t(0)/C.size_t(n) < C.size_t(size) {
+               return 0
+       }
+       return 0
+}
index 11c3ff3a9ceae56fc446505fc8cabf4b4c62c2e6..65f9f6e4a19431e1cac60d6bbabcd798b9612d71 100644 (file)
@@ -1389,7 +1389,9 @@ func (p *Package) rewriteRef(f *File) {
 
                // Record source-level edit for cgo output.
                if !r.Done {
-                       repl := gofmtPos(expr, old.Pos())
+                       // Prepend a space in case the earlier code ends
+                       // with '/', which would give us a "//" comment.
+                       repl := " " + gofmtPos(expr, old.Pos())
                        end := fset.Position(old.End())
                        // Subtract 1 from the column if we are going to
                        // append a close parenthesis. That will set the
@@ -1399,7 +1401,7 @@ func (p *Package) rewriteRef(f *File) {
                                sub = 1
                        }
                        if end.Column > sub {
-                               repl = fmt.Sprintf("%s/*line :%d:%d*/", repl, end.Line, end.Column-sub)
+                               repl = fmt.Sprintf("%s /*line :%d:%d*/", repl, end.Line, end.Column-sub)
                        }
                        if r.Name.Kind != "type" {
                                repl = "(" + repl + ")"