...
▽

Source file src/cmd/internal/src/xpos.go

Documentation: cmd/internal/src

     1  // Copyright 2016 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  // This file implements the compressed encoding of source
     6  // positions using a lookup table.
     7  
     8  package src
     9  
    10  import "fmt"
    11  
    12  // XPos is a more compact representation of Pos.
    13  type XPos struct {
    14  	index int32
    15  	lico
    16  }
    17  
    18  // NoXPos is a valid unknown position.
    19  var NoXPos XPos
    20  
    21  // IsKnown reports whether the position p is known.
    22  // XPos.IsKnown() matches Pos.IsKnown() for corresponding
    23  // positions.
    24  func (p XPos) IsKnown() bool {
    25  	return p.index != 0 || p.Line() != 0
    26  }
    27  
    28  // Before reports whether the position p comes before q in the source.
    29  // For positions with different bases, ordering is by base index.
    30  func (p XPos) Before(q XPos) bool {
    31  	n, m := p.index, q.index
    32  	return n < m || n == m && p.lico < q.lico
    33  }
    34  
    35  // SameFile reports whether p and q are positions in the same file.
    36  func (p XPos) SameFile(q XPos) bool {
    37  	return p.index == q.index
    38  }
    39  
    40  // SameFileAndLine reports whether p and q are positions on the same line in the same file.
    41  func (p XPos) SameFileAndLine(q XPos) bool {
    42  	return p.index == q.index && p.lico.SameLine(q.lico)
    43  }
    44  
    45  // After reports whether the position p comes after q in the source.
    46  // For positions with different bases, ordering is by base index.
    47  func (p XPos) After(q XPos) bool {
    48  	n, m := p.index, q.index
    49  	return n > m || n == m && p.lico > q.lico
    50  }
    51  
    52  // WithNotStmt returns the same location to be marked with DWARF is_stmt=0
    53  func (p XPos) WithNotStmt() XPos {
    54  	p.lico = p.lico.withNotStmt()
    55  	return p
    56  }
    57  
    58  // WithDefaultStmt returns the same location with undetermined is_stmt
    59  func (p XPos) WithDefaultStmt() XPos {
    60  	p.lico = p.lico.withDefaultStmt()
    61  	return p
    62  }
    63  
    64  // WithIsStmt returns the same location to be marked with DWARF is_stmt=1
    65  func (p XPos) WithIsStmt() XPos {
    66  	p.lico = p.lico.withIsStmt()
    67  	return p
    68  }
    69  
    70  // WithBogusLine returns a bogus line that won't match any recorded for the source code.
    71  // Its use is to disrupt the statements within an infinite loop so that the debugger
    72  // will not itself loop infinitely waiting for the line number to change.
    73  // gdb chooses not to display the bogus line; delve shows it with a complaint, but the
    74  // alternative behavior is to hang.
    75  func (p XPos) WithBogusLine() XPos {
    76  	if p.index == 0 {
    77  		// See #35652
    78  		panic("Assigning a bogus line to XPos with no file will cause mysterious downstream failures.")
    79  	}
    80  	p.lico = makeBogusLico()
    81  	return p
    82  }
    83  
    84  // WithXlogue returns the same location but marked with DWARF function prologue/epilogue
    85  func (p XPos) WithXlogue(x PosXlogue) XPos {
    86  	p.lico = p.lico.withXlogue(x)
    87  	return p
    88  }
    89  
    90  // LineNumber returns a string for the line number, "?" if it is not known.
    91  func (p XPos) LineNumber() string {
    92  	if !p.IsKnown() {
    93  		return "?"
    94  	}
    95  	return p.lico.lineNumber()
    96  }
    97  
    98  // ColumnNumber returns a string for the column number, "?" if it is not known.
    99  func (p XPos) ColumnNumber() string {
   100  	if !p.IsKnown() {
   101  		return "?"
   102  	}
   103  	return fmt.Sprintf("%d", p.lico.Col())
   104  }
   105  
   106  // FileIndex returns a smallish non-negative integer corresponding to the
   107  // file for this source position.  Smallish is relative; it can be thousands
   108  // large, but not millions.
   109  func (p XPos) FileIndex() int32 {
   110  	return p.index
   111  }
   112  
   113  func (p XPos) LineNumberHTML() string {
   114  	if !p.IsKnown() {
   115  		return "?"
   116  	}
   117  	return p.lico.lineNumberHTML()
   118  }
   119  
   120  // AtColumn1 returns the same location but shifted to column 1.
   121  func (p XPos) AtColumn1() XPos {
   122  	p.lico = p.lico.atColumn1()
   123  	return p
   124  }
   125  
   126  // A PosTable tracks Pos -> XPos conversions and vice versa.
   127  // Its zero value is a ready-to-use PosTable.
   128  type PosTable struct {
   129  	baseList []*PosBase
   130  	indexMap map[*PosBase]int
   131  	nameMap  map[string]int // Maps file symbol name to index for debug information.
   132  }
   133  
   134  // XPos returns the corresponding XPos for the given pos,
   135  // adding pos to t if necessary.
   136  func (t *PosTable) XPos(pos Pos) XPos {
   137  	return XPos{t.baseIndex(pos.base), pos.lico}
   138  }
   139  
   140  func (t *PosTable) baseIndex(base *PosBase) int32 {
   141  	if base == nil {
   142  		return 0
   143  	}
   144  
   145  	if i, ok := t.indexMap[base]; ok {
   146  		return int32(i)
   147  	}
   148  
   149  	if base.fileIndex >= 0 {
   150  		panic("PosBase already registered with a PosTable")
   151  	}
   152  
   153  	if t.indexMap == nil {
   154  		t.baseList = append(t.baseList, nil)
   155  		t.indexMap = make(map[*PosBase]int)
   156  		t.nameMap = make(map[string]int)
   157  	}
   158  
   159  	i := len(t.baseList)
   160  	t.indexMap[base] = i
   161  	t.baseList = append(t.baseList, base)
   162  
   163  	fileIndex, ok := t.nameMap[base.absFilename]
   164  	if !ok {
   165  		fileIndex = len(t.nameMap)
   166  		t.nameMap[base.absFilename] = fileIndex
   167  	}
   168  	base.fileIndex = fileIndex
   169  
   170  	return int32(i)
   171  }
   172  
   173  // Pos returns the corresponding Pos for the given p.
   174  // If p cannot be translated via t, the function panics.
   175  func (t *PosTable) Pos(p XPos) Pos {
   176  	var base *PosBase
   177  	if p.index != 0 {
   178  		base = t.baseList[p.index]
   179  	}
   180  	return Pos{base, p.lico}
   181  }
   182  
   183  // FileTable returns a slice of all files used to build this package.
   184  func (t *PosTable) FileTable() []string {
   185  	// Create a LUT of the global package level file indices. This table is what
   186  	// is written in the debug_lines header, the file[N] will be referenced as
   187  	// N+1 in the debug_lines table.
   188  	fileLUT := make([]string, len(t.nameMap))
   189  	for str, i := range t.nameMap {
   190  		fileLUT[i] = str
   191  	}
   192  	return fileLUT
   193  }
   194  

View as plain text