1#
2#
3#           The Nim Compiler
4#        (c) Copyright 2017 Andreas Rumpf
5#
6#    See the file "copying.txt", included in this
7#    distribution, for details about the copyright.
8#
9
10## This module implements the generation of ``.ndi`` files for better debugging
11## support of Nim code. "ndi" stands for "Nim debug info".
12
13import ast, msgs, ropes, options, pathutils
14
15type
16  NdiFile* = object
17    enabled: bool
18    f: File
19    buf: string
20    filename: AbsoluteFile
21    syms: seq[PSym]
22
23proc doWrite(f: var NdiFile; s: PSym; conf: ConfigRef) =
24  f.buf.setLen 0
25  f.buf.addInt s.info.line.int
26  f.buf.add "\t"
27  f.buf.addInt s.info.col.int
28  f.f.write(s.name.s, "\t")
29  f.f.writeRope(s.loc.r)
30  f.f.writeLine("\t", toFullPath(conf, s.info), "\t", f.buf)
31
32template writeMangledName*(f: NdiFile; s: PSym; conf: ConfigRef) =
33  if f.enabled: f.syms.add s
34
35proc open*(f: var NdiFile; filename: AbsoluteFile; conf: ConfigRef) =
36  f.enabled = not filename.isEmpty
37  if f.enabled:
38    f.filename = filename
39    f.buf = newStringOfCap(20)
40
41proc close*(f: var NdiFile, conf: ConfigRef) =
42  if f.enabled:
43    f.f = open(f.filename.string, fmWrite, 8000)
44    doAssert f.f != nil, f.filename.string
45    for s in f.syms:
46      doWrite(f, s, conf)
47    close(f.f)
48    f.syms.reset
49    f.filename.reset
50