]> Cypherpunks repositories - gostls13.git/commitdiff
go/token: let FileSet.AddFile take a negative base
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 14 May 2013 16:30:13 +0000 (09:30 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 14 May 2013 16:30:13 +0000 (09:30 -0700)
Negative base now means "automatic". Fixes a higher
level race.

Fixes #5418

R=gri
CC=golang-dev
https://golang.org/cl/9269043

src/pkg/go/parser/parser.go
src/pkg/go/token/position.go
src/pkg/go/token/position_test.go

index f4a690a6f289812e2b3ce2912125e0434150add5..db27a25b8392c82c5757de5d4865fa9117f7d50c 100644 (file)
@@ -64,7 +64,7 @@ type parser struct {
 }
 
 func (p *parser) init(fset *token.FileSet, filename string, src []byte, mode Mode) {
-       p.file = fset.AddFile(filename, fset.Base(), len(src))
+       p.file = fset.AddFile(filename, -1, len(src))
        var m scanner.Mode
        if mode&ParseComments != 0 {
                m = scanner.ScanComments
index f5d999561862581049092f0473c3767c53a19229..c9acab1d44d6603609d8cffc9e5046f1de2c396d 100644 (file)
@@ -314,7 +314,8 @@ func (s *FileSet) Base() int {
 // AddFile adds a new file with a given filename, base offset, and file size
 // to the file set s and returns the file. Multiple files may have the same
 // name. The base offset must not be smaller than the FileSet's Base(), and
-// size must not be negative.
+// size must not be negative. As a special case, if a negative base is provided,
+// the current value of the FileSet's Base() is used instead.
 //
 // Adding the file will set the file set's Base() value to base + size + 1
 // as the minimum base value for the next file. The following relationship
@@ -329,6 +330,9 @@ func (s *FileSet) Base() int {
 func (s *FileSet) AddFile(filename string, base, size int) *File {
        s.mutex.Lock()
        defer s.mutex.Unlock()
+       if base < 0 {
+               base = s.base
+       }
        if base < s.base || size < 0 {
                panic("illegal base or size")
        }
index 1d36c22268d3f6b94b3bde4448e0bac9c84576cd..ef6cfd93c25e0c47d8a1b1c098e5f9a43f7a13be 100644 (file)
@@ -167,7 +167,13 @@ func TestLineInfo(t *testing.T) {
 func TestFiles(t *testing.T) {
        fset := NewFileSet()
        for i, test := range tests {
-               fset.AddFile(test.filename, fset.Base(), test.size)
+               base := fset.Base()
+               if i%2 == 1 {
+                       // Setting a negative base is equivalent to
+                       // fset.Base(), so test some of each.
+                       base = -1
+               }
+               fset.AddFile(test.filename, base, test.size)
                j := 0
                fset.Iterate(func(f *File) bool {
                        if f.Name() != tests[j].filename {