]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: skip string literal while findEmbed
authorMeng Zhuo <mzh@golangcn.org>
Sat, 26 Dec 2020 02:13:57 +0000 (10:13 +0800)
committerRuss Cox <rsc@golang.org>
Fri, 8 Jan 2021 02:18:40 +0000 (02:18 +0000)
The findEmbed function looking for comment by readbyte,
however it might have constant or variables that contains
comment.
Maybe we should use ast parser in the future.

Fixes #43373

Change-Id: I92544384fc4c11363d8b2f6b9898c8dea1602767
Reviewed-on: https://go-review.googlesource.com/c/go/+/280332
Trust: Matthew Dempsky <mdempsky@google.com>
Trust: Meng Zhuo <mzh@golangcn.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
src/go/build/read.go
src/go/build/read_test.go

index 6806a51c2474d8633653505b999586335972112b..6da921d4718942b5bdb9d31bd8a6470f649f4dc5 100644 (file)
@@ -171,6 +171,41 @@ func (r *importReader) findEmbed(first bool) bool {
                case ' ', '\t':
                        // leave startLine alone
 
+               case '"':
+                       startLine = false
+                       for r.err == nil {
+                               if r.eof {
+                                       r.syntaxError()
+                               }
+                               c = r.readByteNoBuf()
+                               if c == '\\' {
+                                       r.readByteNoBuf()
+                                       if r.err != nil {
+                                               r.syntaxError()
+                                               return false
+                                       }
+                                       continue
+                               }
+                               if c == '"' {
+                                       c = r.readByteNoBuf()
+                                       goto Reswitch
+                               }
+                       }
+                       goto Reswitch
+
+               case '`':
+                       startLine = false
+                       for r.err == nil {
+                               if r.eof {
+                                       r.syntaxError()
+                               }
+                               c = r.readByteNoBuf()
+                               if c == '`' {
+                                       c = r.readByteNoBuf()
+                                       goto Reswitch
+                               }
+                       }
+
                case '/':
                        c = r.readByteNoBuf()
                        switch c {
index 9264d2606f1b72e2697bcd4e09219264a46da908..36c773eceaf86746dca0c84e4ad507ff25ae68b7 100644 (file)
@@ -255,6 +255,26 @@ var readEmbedTests = []struct {
                "package p\nimport \"embed\"\n//go:embed x y z\nvar files embed.FS",
                []string{"x", "y", "z"},
        },
+       {
+               "package p\nimport \"embed\"\nvar s = \"/*\"\n//go:embed x\nvar files embed.FS",
+               []string{"x"},
+       },
+       {
+               `package p
+                import "embed"
+                var s = "\"\\\\"
+                //go:embed x
+                var files embed.FS`,
+               []string{"x"},
+       },
+       {
+               "package p\nimport \"embed\"\nvar s = `/*`\n//go:embed x\nvar files embed.FS",
+               []string{"x"},
+       },
+       {
+               "package p\nimport \"embed\"\nvar s = z/ *y\n//go:embed pointer\nvar pointer embed.FS",
+               []string{"pointer"},
+       },
        {
                "package p\n//go:embed x y z\n", // no import, no scan
                nil,