Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: consider column for TLineInfo equality #880

Merged
merged 10 commits into from
Sep 10, 2023
6 changes: 6 additions & 0 deletions compiler/ast/lineinfos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type
col*: int16
fileIndex*: FileIndex

SourceLinePosition* = tuple
## Identifies a line in a source file. Only intended for use by
## the profiler.
fileIndex: typeof(TLineInfo.fileIndex)
line: typeof(TLineInfo.line)

LineColPair* = tuple
line: typeof(TLineInfo.line)
col: typeof(TLineInfo.col)
Expand Down
7 changes: 0 additions & 7 deletions compiler/front/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,6 @@ proc errorActions(
elif eh == doRaise:
result = (doRaise, false)


proc `==`*(a, b: TLineInfo): bool =
result = a.line == b.line and a.fileIndex == b.fileIndex

proc exactEquals*(a, b: TLineInfo): bool =
result = a.fileIndex == b.fileIndex and a.line == b.line and a.col == b.col

proc getContext*(conf: ConfigRef; lastinfo: TLineInfo): seq[ReportContext] =
## Get list of context context entries from the current message context
## information. Context messages can later be used in the
Expand Down
4 changes: 2 additions & 2 deletions compiler/front/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ type
count*: int

ProfileData* = ref object
data*: TableRef[TLineInfo, ProfileInfo]
data*: TableRef[SourceLinePosition, ProfileInfo]

StdOrrKind* = enum
stdOrrStdout
Expand Down Expand Up @@ -774,7 +774,7 @@ template newPackageCache*(): untyped =
modeCaseSensitive)

proc newProfileData(): ProfileData =
ProfileData(data: newTable[TLineInfo, ProfileInfo]())
ProfileData(data: newTable[SourceLinePosition, ProfileInfo]())

proc isDefined*(conf: ConfigRef; symbol: string): bool

Expand Down
2 changes: 1 addition & 1 deletion compiler/sem/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,7 @@ proc builtinFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
when defined(nimsuggest):
if c.config.cmd == cmdIdeTools:
suggestExpr(c, n)
if exactEquals(c.config.m.trackPos, n[1].info): suggestExprNoCheck(c, n)
if c.config.m.trackPos == n[1].info: suggestExprNoCheck(c, n)

let s = qualifiedLookUp(c, n, {checkAmbiguity, checkUndeclared, checkModule})
if s.isError:
Expand Down
2 changes: 1 addition & 1 deletion compiler/sem/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags): PNode =
setCaseContextIdx(c, i)
var x = n[i]
when defined(nimsuggest):
if c.config.ideCmd == ideSug and exactEquals(c.config.m.trackPos, x.info) and caseTyp.kind == tyEnum:
if c.config.ideCmd == ideSug and c.config.m.trackPos == x.info and caseTyp.kind == tyEnum:
suggestEnum(c, x, caseTyp)
case x.kind
of nkOfBranch:
Expand Down
11 changes: 3 additions & 8 deletions compiler/tools/suggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -469,17 +469,12 @@ proc isTracked*(current, trackPos: TLineInfo, tokenLen: int): bool =
return true

when defined(nimsuggest):
# Since TLineInfo defined a == operator that doesn't include the column,
# we map TLineInfo to a unique int here for this lookup table:
proc infoToInt(info: TLineInfo): int64 =
info.fileIndex.int64 + info.line.int64 shl 32 + info.col.int64 shl 48

proc addNoDup(s: PSym; info: TLineInfo) =
# ensure nothing gets too slow:
if s.allUsages.len > 500: return
let infoAsInt = info.infoToInt
for infoB in s.allUsages:
if infoB.infoToInt == infoAsInt: return
if infoB == info: return
s.allUsages.add(info)

proc findUsages(g: ModuleGraph; info: TLineInfo; s: PSym; usageSym: var PSym) =
Expand All @@ -496,7 +491,7 @@ when defined(nimsuggest):
proc listUsages*(g: ModuleGraph; s: PSym) =
#echo "usages ", s.allUsages.len
for info in s.allUsages:
let x = if info == s.info and info.col == s.info.col: ideDef else: ideUse
let x = if info == s.info: ideDef else: ideUse
suggestResult(g.config, symToSuggest(g, s, isLocal=false, x, info, 100, PrefixMatch.None, false, 0))

proc findDefinition(g: ModuleGraph; info: TLineInfo; s: PSym; usageSym: var PSym) =
Expand Down Expand Up @@ -608,7 +603,7 @@ proc suggestExprNoCheck*(c: PContext, n: PNode) =
suggestQuit()

proc suggestExpr*(c: PContext, n: PNode) =
if exactEquals(c.config.m.trackPos, n.info): suggestExprNoCheck(c, n)
if c.config.m.trackPos == n.info: suggestExprNoCheck(c, n)

proc suggestDecl*(c: PContext, n: PNode; s: PSym) =
let attached = c.config.m.trackPosAttached
Expand Down
6 changes: 3 additions & 3 deletions compiler/vm/vmprofiler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ proc leaveImpl(prof: var Profiler, c: TCtx) {.noinline.} =
while frameIdx >= 0:
let frame = c.sframes[frameIdx]
if frame.prc != nil:
let li = frame.prc.info
let li = (frame.prc.info.fileIndex, frame.prc.info.line)
if li notin data:
data[li] = ProfileInfo()
data[li].time += tLeave - prof.tEnter
Expand All @@ -48,7 +48,7 @@ proc dump*(conf: ConfigRef, pd: ProfileData): string =
result = "\nprof: µs #instr location\n"
for i in 0..<32:
var infoMax: ProfileInfo
var flMax: TLineInfo
var flMax: SourceLinePosition
for fl, info in data:
if info.time > infoMax.time:
infoMax = info
Expand All @@ -57,5 +57,5 @@ proc dump*(conf: ConfigRef, pd: ProfileData): string =
break
result.add " " & align($int(infoMax.time * 1e6), 10) &
align($int(infoMax.count), 10) & " " &
conf.toFileLineCol(flMax) & "\n"
toMsgFilename(conf, newLineInfo(flMax.fileIndex, flMax.line.int, 0)) & "\n"
bung87 marked this conversation as resolved.
Show resolved Hide resolved
data.del flMax
Loading