Source file src/go/token/position.go
1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package token 6 7 import ( 8 "fmt" 9 "sort" 10 "sync" 11 "sync/atomic" 12 ) 13 14 // ----------------------------------------------------------------------------- 15 // Positions 16 17 // Position describes an arbitrary source position 18 // including the file, line, and column location. 19 // A Position is valid if the line number is > 0. 20 type Position struct { 21 Filename string // filename, if any 22 Offset int // offset, starting at 0 23 Line int // line number, starting at 1 24 Column int // column number, starting at 1 (byte count) 25 } 26 27 // IsValid reports whether the position is valid. 28 func (pos *Position) IsValid() bool { return pos.Line > 0 } 29 30 // String returns a string in one of several forms: 31 // 32 // file:line:column valid position with file name 33 // file:line valid position with file name but no column (column == 0) 34 // line:column valid position without file name 35 // line valid position without file name and no column (column == 0) 36 // file invalid position with file name 37 // - invalid position without file name 38 func (pos Position) String() string { 39 s := pos.Filename 40 if pos.IsValid() { 41 if s != "" { 42 s += ":" 43 } 44 s += fmt.Sprintf("%d", pos.Line) 45 if pos.Column != 0 { 46 s += fmt.Sprintf(":%d", pos.Column) 47 } 48 } 49 if s == "" { 50 s = "-" 51 } 52 return s 53 } 54 55 // Pos is a compact encoding of a source position within a file set. 56 // It can be converted into a Position for a more convenient, but much 57 // larger, representation. 58 // 59 // The Pos value for a given file is a number in the range [base, base+size], 60 // where base and size are specified when a file is added to the file set. 61 // The difference between a Pos value and the corresponding file base 62 // corresponds to the byte offset of that position (represented by the Pos value) 63 // from the beginning of the file. Thus, the file base offset is the Pos value 64 // representing the first byte in the file. 65 // 66 // To create the Pos value for a specific source offset (measured in bytes), 67 // first add the respective file to the current file set using FileSet.AddFile 68 // and then call File.Pos(offset) for that file. Given a Pos value p 69 // for a specific file set fset, the corresponding Position value is 70 // obtained by calling fset.Position(p). 71 // 72 // Pos values can be compared directly with the usual comparison operators: 73 // If two Pos values p and q are in the same file, comparing p and q is 74 // equivalent to comparing the respective source file offsets. If p and q 75 // are in different files, p < q is true if the file implied by p was added 76 // to the respective file set before the file implied by q. 77 type Pos int 78 79 // The zero value for Pos is NoPos; there is no file and line information 80 // associated with it, and NoPos.IsValid() is false. NoPos is always 81 // smaller than any other Pos value. The corresponding Position value 82 // for NoPos is the zero value for Position. 83 const NoPos Pos = 0 84 85 // IsValid reports whether the position is valid. 86 func (p Pos) IsValid() bool { 87 return p != NoPos 88 } 89 90 // ----------------------------------------------------------------------------- 91 // File 92 93 // A File is a handle for a file belonging to a FileSet. 94 // A File has a name, size, and line offset table. 95 type File struct { 96 name string // file name as provided to AddFile 97 base int // Pos value range for this file is [base...base+size] 98 size int // file size as provided to AddFile 99 100 // lines and infos are protected by mutex 101 mutex sync.Mutex 102 lines []int // lines contains the offset of the first character for each line (the first entry is always 0) 103 infos []lineInfo 104 } 105 106 // Name returns the file name of file f as registered with AddFile. 107 func (f *File) Name() string { 108 return f.name 109 } 110 111 // Base returns the base offset of file f as registered with AddFile. 112 func (f *File) Base() int { 113 return f.base 114 } 115 116 // Size returns the size of file f as registered with AddFile. 117 func (f *File) Size() int { 118 return f.size 119 } 120 121 // LineCount returns the number of lines in file f. 122 func (f *File) LineCount() int { 123 f.mutex.Lock() 124 n := len(f.lines) 125 f.mutex.Unlock() 126 return n 127 } 128 129 // AddLine adds the line offset for a new line. 130 // The line offset must be larger than the offset for the previous line 131 // and smaller than the file size; otherwise the line offset is ignored. 132 func (f *File) AddLine(offset int) { 133 f.mutex.Lock() 134 if i := len(f.lines); (i == 0 || f.lines[i-1] < offset) && offset < f.size { 135 f.lines = append(f.lines, offset) 136 } 137 f.mutex.Unlock() 138 } 139 140 // MergeLine merges a line with the following line. It is akin to replacing 141 // the newline character at the end of the line with a space (to not change the 142 // remaining offsets). To obtain the line number, consult e.g. Position.Line. 143 // MergeLine will panic if given an invalid line number. 144 func (f *File) MergeLine(line int) { 145 if line < 1 { 146 panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line)) 147 } 148 f.mutex.Lock() 149 defer f.mutex.Unlock() 150 if line >= len(f.lines) { 151 panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines))) 152 } 153 // To merge the line numbered <line> with the line numbered <line+1>, 154 // we need to remove the entry in lines corresponding to the line 155 // numbered <line+1>. The entry in lines corresponding to the line 156 // numbered <line+1> is located at index <line>, since indices in lines 157 // are 0-based and line numbers are 1-based. 158 copy(f.lines[line:], f.lines[line+1:]) 159 f.lines = f.lines[:len(f.lines)-1] 160 } 161 162 // SetLines sets the line offsets for a file and reports whether it succeeded. 163 // The line offsets are the offsets of the first character of each line; 164 // for instance for the content "ab\nc\n" the line offsets are {0, 3}. 165 // An empty file has an empty line offset table. 166 // Each line offset must be larger than the offset for the previous line 167 // and smaller than the file size; otherwise SetLines fails and returns 168 // false. 169 // Callers must not mutate the provided slice after SetLines returns. 170 func (f *File) SetLines(lines []int) bool { 171 // verify validity of lines table 172 size := f.size 173 for i, offset := range lines { 174 if i > 0 && offset <= lines[i-1] || size <= offset { 175 return false 176 } 177 } 178 179 // set lines table 180 f.mutex.Lock() 181 f.lines = lines 182 f.mutex.Unlock() 183 return true 184 } 185 186 // SetLinesForContent sets the line offsets for the given file content. 187 // It ignores position-altering //line comments. 188 func (f *File) SetLinesForContent(content []byte) { 189 var lines []int 190 line := 0 191 for offset, b := range content { 192 if line >= 0 { 193 lines = append(lines, line) 194 } 195 line = -1 196 if b == '\n' { 197 line = offset + 1 198 } 199 } 200 201 // set lines table 202 f.mutex.Lock() 203 f.lines = lines 204 f.mutex.Unlock() 205 } 206 207 // LineStart returns the Pos value of the start of the specified line. 208 // It ignores any alternative positions set using AddLineColumnInfo. 209 // LineStart panics if the 1-based line number is invalid. 210 func (f *File) LineStart(line int) Pos { 211 if line < 1 { 212 panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line)) 213 } 214 f.mutex.Lock() 215 defer f.mutex.Unlock() 216 if line > len(f.lines) { 217 panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines))) 218 } 219 return Pos(f.base + f.lines[line-1]) 220 } 221 222 // A lineInfo object describes alternative file, line, and column 223 // number information (such as provided via a //line directive) 224 // for a given file offset. 225 type lineInfo struct { 226 // fields are exported to make them accessible to gob 227 Offset int 228 Filename string 229 Line, Column int 230 } 231 232 // AddLineInfo is like AddLineColumnInfo with a column = 1 argument. 233 // It is here for backward-compatibility for code prior to Go 1.11. 234 func (f *File) AddLineInfo(offset int, filename string, line int) { 235 f.AddLineColumnInfo(offset, filename, line, 1) 236 } 237 238 // AddLineColumnInfo adds alternative file, line, and column number 239 // information for a given file offset. The offset must be larger 240 // than the offset for the previously added alternative line info 241 // and smaller than the file size; otherwise the information is 242 // ignored. 243 // 244 // AddLineColumnInfo is typically used to register alternative position 245 // information for line directives such as //line filename:line:column. 246 func (f *File) AddLineColumnInfo(offset int, filename string, line, column int) { 247 f.mutex.Lock() 248 if i := len(f.infos); (i == 0 || f.infos[i-1].Offset < offset) && offset < f.size { 249 f.infos = append(f.infos, lineInfo{offset, filename, line, column}) 250 } 251 f.mutex.Unlock() 252 } 253 254 // Pos returns the Pos value for the given file offset; 255 // the offset must be <= f.Size(). 256 // f.Pos(f.Offset(p)) == p. 257 func (f *File) Pos(offset int) Pos { 258 if offset > f.size { 259 panic(fmt.Sprintf("invalid file offset %d (should be <= %d)", offset, f.size)) 260 } 261 return Pos(f.base + offset) 262 } 263 264 // Offset returns the offset for the given file position p; 265 // p must be a valid Pos value in that file. 266 // f.Offset(f.Pos(offset)) == offset. 267 func (f *File) Offset(p Pos) int { 268 if int(p) < f.base || int(p) > f.base+f.size { 269 panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d])", p, f.base, f.base+f.size)) 270 } 271 return int(p) - f.base 272 } 273 274 // Line returns the line number for the given file position p; 275 // p must be a Pos value in that file or NoPos. 276 func (f *File) Line(p Pos) int { 277 return f.Position(p).Line 278 } 279 280 func searchLineInfos(a []lineInfo, x int) int { 281 return sort.Search(len(a), func(i int) bool { return a[i].Offset > x }) - 1 282 } 283 284 // unpack returns the filename and line and column number for a file offset. 285 // If adjusted is set, unpack will return the filename and line information 286 // possibly adjusted by //line comments; otherwise those comments are ignored. 287 func (f *File) unpack(offset int, adjusted bool) (filename string, line, column int) { 288 f.mutex.Lock() 289 filename = f.name 290 if i := searchInts(f.lines, offset); i >= 0 { 291 line, column = i+1, offset-f.lines[i]+1 292 } 293 if adjusted && len(f.infos) > 0 { 294 // few files have extra line infos 295 if i := searchLineInfos(f.infos, offset); i >= 0 { 296 alt := &f.infos[i] 297 filename = alt.Filename 298 if i := searchInts(f.lines, alt.Offset); i >= 0 { 299 // i+1 is the line at which the alternative position was recorded 300 d := line - (i + 1) // line distance from alternative position base 301 line = alt.Line + d 302 if alt.Column == 0 { 303 // alternative column is unknown => relative column is unknown 304 // (the current specification for line directives requires 305 // this to apply until the next PosBase/line directive, 306 // not just until the new newline) 307 column = 0 308 } else if d == 0 { 309 // the alternative position base is on the current line 310 // => column is relative to alternative column 311 column = alt.Column + (offset - alt.Offset) 312 } 313 } 314 } 315 } 316 // TODO(mvdan): move Unlock back under Lock with a defer statement once 317 // https://go.dev/issue/38471 is fixed to remove the performance penalty. 318 f.mutex.Unlock() 319 return 320 } 321 322 func (f *File) position(p Pos, adjusted bool) (pos Position) { 323 offset := int(p) - f.base 324 pos.Offset = offset 325 pos.Filename, pos.Line, pos.Column = f.unpack(offset, adjusted) 326 return 327 } 328 329 // PositionFor returns the Position value for the given file position p. 330 // If adjusted is set, the position may be adjusted by position-altering 331 // //line comments; otherwise those comments are ignored. 332 // p must be a Pos value in f or NoPos. 333 func (f *File) PositionFor(p Pos, adjusted bool) (pos Position) { 334 if p != NoPos { 335 if int(p) < f.base || int(p) > f.base+f.size { 336 panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d])", p, f.base, f.base+f.size)) 337 } 338 pos = f.position(p, adjusted) 339 } 340 return 341 } 342 343 // Position returns the Position value for the given file position p. 344 // Calling f.Position(p) is equivalent to calling f.PositionFor(p, true). 345 func (f *File) Position(p Pos) (pos Position) { 346 return f.PositionFor(p, true) 347 } 348 349 // ----------------------------------------------------------------------------- 350 // FileSet 351 352 // A FileSet represents a set of source files. 353 // Methods of file sets are synchronized; multiple goroutines 354 // may invoke them concurrently. 355 // 356 // The byte offsets for each file in a file set are mapped into 357 // distinct (integer) intervals, one interval [base, base+size] 358 // per file. Base represents the first byte in the file, and size 359 // is the corresponding file size. A Pos value is a value in such 360 // an interval. By determining the interval a Pos value belongs 361 // to, the file, its file base, and thus the byte offset (position) 362 // the Pos value is representing can be computed. 363 // 364 // When adding a new file, a file base must be provided. That can 365 // be any integer value that is past the end of any interval of any 366 // file already in the file set. For convenience, FileSet.Base provides 367 // such a value, which is simply the end of the Pos interval of the most 368 // recently added file, plus one. Unless there is a need to extend an 369 // interval later, using the FileSet.Base should be used as argument 370 // for FileSet.AddFile. 371 // 372 // A File may be removed from a FileSet when it is no longer needed. 373 // This may reduce memory usage in a long-running application. 374 type FileSet struct { 375 mutex sync.RWMutex // protects the file set 376 base int // base offset for the next file 377 files []*File // list of files in the order added to the set 378 last atomic.Pointer[File] // cache of last file looked up 379 } 380 381 // NewFileSet creates a new file set. 382 func NewFileSet() *FileSet { 383 return &FileSet{ 384 base: 1, // 0 == NoPos 385 } 386 } 387 388 // Base returns the minimum base offset that must be provided to 389 // AddFile when adding the next file. 390 func (s *FileSet) Base() int { 391 s.mutex.RLock() 392 b := s.base 393 s.mutex.RUnlock() 394 return b 395 396 } 397 398 // AddFile adds a new file with a given filename, base offset, and file size 399 // to the file set s and returns the file. Multiple files may have the same 400 // name. The base offset must not be smaller than the FileSet's Base(), and 401 // size must not be negative. As a special case, if a negative base is provided, 402 // the current value of the FileSet's Base() is used instead. 403 // 404 // Adding the file will set the file set's Base() value to base + size + 1 405 // as the minimum base value for the next file. The following relationship 406 // exists between a Pos value p for a given file offset offs: 407 // 408 // int(p) = base + offs 409 // 410 // with offs in the range [0, size] and thus p in the range [base, base+size]. 411 // For convenience, File.Pos may be used to create file-specific position 412 // values from a file offset. 413 func (s *FileSet) AddFile(filename string, base, size int) *File { 414 // Allocate f outside the critical section. 415 f := &File{name: filename, size: size, lines: []int{0}} 416 417 s.mutex.Lock() 418 defer s.mutex.Unlock() 419 if base < 0 { 420 base = s.base 421 } 422 if base < s.base { 423 panic(fmt.Sprintf("invalid base %d (should be >= %d)", base, s.base)) 424 } 425 f.base = base 426 if size < 0 { 427 panic(fmt.Sprintf("invalid size %d (should be >= 0)", size)) 428 } 429 // base >= s.base && size >= 0 430 base += size + 1 // +1 because EOF also has a position 431 if base < 0 { 432 panic("token.Pos offset overflow (> 2G of source code in file set)") 433 } 434 // add the file to the file set 435 s.base = base 436 s.files = append(s.files, f) 437 s.last.Store(f) 438 return f 439 } 440 441 // RemoveFile removes a file from the FileSet so that subsequent 442 // queries for its Pos interval yield a negative result. 443 // This reduces the memory usage of a long-lived FileSet that 444 // encounters an unbounded stream of files. 445 // 446 // Removing a file that does not belong to the set has no effect. 447 func (s *FileSet) RemoveFile(file *File) { 448 s.last.CompareAndSwap(file, nil) // clear last file cache 449 450 s.mutex.Lock() 451 defer s.mutex.Unlock() 452 453 if i := searchFiles(s.files, file.base); i >= 0 && s.files[i] == file { 454 last := &s.files[len(s.files)-1] 455 s.files = append(s.files[:i], s.files[i+1:]...) 456 *last = nil // don't prolong lifetime when popping last element 457 } 458 } 459 460 // Iterate calls f for the files in the file set in the order they were added 461 // until f returns false. 462 func (s *FileSet) Iterate(f func(*File) bool) { 463 for i := 0; ; i++ { 464 var file *File 465 s.mutex.RLock() 466 if i < len(s.files) { 467 file = s.files[i] 468 } 469 s.mutex.RUnlock() 470 if file == nil || !f(file) { 471 break 472 } 473 } 474 } 475 476 func searchFiles(a []*File, x int) int { 477 return sort.Search(len(a), func(i int) bool { return a[i].base > x }) - 1 478 } 479 480 func (s *FileSet) file(p Pos) *File { 481 // common case: p is in last file. 482 if f := s.last.Load(); f != nil && f.base <= int(p) && int(p) <= f.base+f.size { 483 return f 484 } 485 486 s.mutex.RLock() 487 defer s.mutex.RUnlock() 488 489 // p is not in last file - search all files 490 if i := searchFiles(s.files, int(p)); i >= 0 { 491 f := s.files[i] 492 // f.base <= int(p) by definition of searchFiles 493 if int(p) <= f.base+f.size { 494 // Update cache of last file. A race is ok, 495 // but an exclusive lock causes heavy contention. 496 s.last.Store(f) 497 return f 498 } 499 } 500 return nil 501 } 502 503 // File returns the file that contains the position p. 504 // If no such file is found (for instance for p == NoPos), 505 // the result is nil. 506 func (s *FileSet) File(p Pos) (f *File) { 507 if p != NoPos { 508 f = s.file(p) 509 } 510 return 511 } 512 513 // PositionFor converts a Pos p in the fileset into a Position value. 514 // If adjusted is set, the position may be adjusted by position-altering 515 // //line comments; otherwise those comments are ignored. 516 // p must be a Pos value in s or NoPos. 517 func (s *FileSet) PositionFor(p Pos, adjusted bool) (pos Position) { 518 if p != NoPos { 519 if f := s.file(p); f != nil { 520 return f.position(p, adjusted) 521 } 522 } 523 return 524 } 525 526 // Position converts a Pos p in the fileset into a Position value. 527 // Calling s.Position(p) is equivalent to calling s.PositionFor(p, true). 528 func (s *FileSet) Position(p Pos) (pos Position) { 529 return s.PositionFor(p, true) 530 } 531 532 // ----------------------------------------------------------------------------- 533 // Helper functions 534 535 func searchInts(a []int, x int) int { 536 // This function body is a manually inlined version of: 537 // 538 // return sort.Search(len(a), func(i int) bool { return a[i] > x }) - 1 539 // 540 // With better compiler optimizations, this may not be needed in the 541 // future, but at the moment this change improves the go/printer 542 // benchmark performance by ~30%. This has a direct impact on the 543 // speed of gofmt and thus seems worthwhile (2011-04-29). 544 // TODO(gri): Remove this when compilers have caught up. 545 i, j := 0, len(a) 546 for i < j { 547 h := int(uint(i+j) >> 1) // avoid overflow when computing h 548 // i ≤ h < j 549 if a[h] <= x { 550 i = h + 1 551 } else { 552 j = h 553 } 554 } 555 return i - 1 556 } 557