]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: don't replace newlines with semicolons in expressions
authorIan Lance Taylor <iant@golang.org>
Fri, 18 Jan 2019 02:35:43 +0000 (18:35 -0800)
committerIan Lance Taylor <iant@golang.org>
Fri, 18 Jan 2019 03:25:40 +0000 (03:25 +0000)
Fixes #29781

Change-Id: Id032d07a54b8c24f0c6d3f6e512932f76920ee04
Reviewed-on: https://go-review.googlesource.com/c/158457
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
misc/cgo/test/issue29781.go [new file with mode: 0644]
src/cmd/cgo/godefs.go

diff --git a/misc/cgo/test/issue29781.go b/misc/cgo/test/issue29781.go
new file mode 100644 (file)
index 0000000..0fd8c08
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2019 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.
+
+// Error with newline inserted into constant expression.
+// Compilation test only, nothing to run.
+
+package cgotest
+
+// static void issue29781F(char **p, int n) {}
+// #define ISSUE29781C 0
+import "C"
+
+func issue29781G() {
+       var p *C.char
+       C.issue29781F(&p, C.ISSUE29781C+1)
+}
index c0cd8e002fea4d89e60c848e5edfb04a0d55d72e..7185ea0de7db0e520b525621d23c30aeff0b0167 100644 (file)
@@ -131,12 +131,27 @@ func gofmt(n interface{}) string {
 // AST expression onto a single line. The lexer normally inserts a
 // semicolon at each newline, so we can replace newline with semicolon.
 // However, we can't do that in cases where the lexer would not insert
-// a semicolon. Fortunately we only have to worry about cases that
-// can occur in an expression passed through gofmt, which just means
-// composite literals.
+// a semicolon. We only have to worry about cases that can occur in an
+// expression passed through gofmt, which means composite literals and
+// (due to the printer possibly inserting newlines because of position
+// information) operators.
 var gofmtLineReplacer = strings.NewReplacer(
        "{\n", "{",
        ",\n", ",",
+       "++\n", "++;",
+       "--\n", "--;",
+       "+\n", "+",
+       "-\n", "-",
+       "*\n", "*",
+       "/\n", "/",
+       "%\n", "%",
+       "&\n", "&",
+       "|\n", "|",
+       "^\n", "^",
+       "<\n", "<",
+       ">\n", ">",
+       "=\n", "=",
+       ",\n", ",",
        "\n", ";",
 )