| 1 |
//go:build !no_antithesis_sdk |
| 2 |
|
| 3 |
package assert |
| 4 |
|
| 5 |
import ( |
| 6 |
"path" |
| 7 |
"runtime" |
| 8 |
"strings" |
| 9 |
) |
| 10 |
|
| 11 |
// stackFrameOffset indicates how many frames to go up in the |
| 12 |
// call stack to find the filename/location/line info. As |
| 13 |
// this work is always done in NewLocationInfo(), the offset is |
| 14 |
// specified from the perspective of NewLocationInfo |
| 15 |
type stackFrameOffset int |
| 16 |
|
| 17 |
// Order is important here since iota is being used |
| 18 |
const ( |
| 19 |
offsetNewLocationInfo stackFrameOffset = iota |
| 20 |
offsetHere |
| 21 |
offsetAPICaller |
| 22 |
offsetAPICallersCaller |
| 23 |
) |
| 24 |
|
| 25 |
// locationInfo represents the attributes known at instrumentation time |
| 26 |
// for each Antithesis assertion discovered |
| 27 |
type locationInfo struct { |
| 28 |
Classname string `json:"class"` |
| 29 |
Funcname string `json:"function"` |
| 30 |
Filename string `json:"file"` |
| 31 |
Line int `json:"begin_line"` |
| 32 |
Column int `json:"begin_column"` |
| 33 |
} |
| 34 |
|
| 35 |
// columnUnknown is used when the column associated with |
| 36 |
// a locationInfo is not available |
| 37 |
const columnUnknown = 0 |
| 38 |
|
| 39 |
// NewLocationInfo creates a locationInfo directly from |
| 40 |
// the current execution context |
| 41 |
func newLocationInfo(nframes stackFrameOffset) *locationInfo { |
| 42 |
// Get location info and add to details |
| 43 |
funcname := "*function*" |
| 44 |
classname := "*class*" |
| 45 |
pc, filename, line, ok := runtime.Caller(int(nframes)) |
| 46 |
if !ok { |
| 47 |
filename = "*file*" |
| 48 |
line = 0 |
| 49 |
} else { |
| 50 |
if this_func := runtime.FuncForPC(pc); this_func != nil { |
| 51 |
fullname := this_func.Name() |
| 52 |
funcname = path.Ext(fullname) |
| 53 |
classname, _ = strings.CutSuffix(fullname, funcname) |
| 54 |
funcname = funcname[1:] |
| 55 |
} |
| 56 |
} |
| 57 |
return &locationInfo{classname, funcname, filename, line, columnUnknown} |
| 58 |
} |