ndi.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. ## This module implements the generation of ``.ndi`` files for better debugging
  10. ## support of Nim code. "ndi" stands for "Nim debug info".
  11. import ast, msgs, ropes, options, pathutils
  12. type
  13. NdiFile* = object
  14. enabled: bool
  15. f: File
  16. buf: string
  17. filename: AbsoluteFile
  18. syms: seq[PSym]
  19. proc doWrite(f: var NdiFile; s: PSym; conf: ConfigRef) =
  20. f.buf.setLen 0
  21. f.buf.addInt s.info.line.int
  22. f.buf.add "\t"
  23. f.buf.addInt s.info.col.int
  24. f.f.write(s.name.s, "\t")
  25. f.f.writeRope(s.loc.r)
  26. f.f.writeLine("\t", toFullPath(conf, s.info), "\t", f.buf)
  27. template writeMangledName*(f: NdiFile; s: PSym; conf: ConfigRef) =
  28. if f.enabled: f.syms.add s
  29. proc open*(f: var NdiFile; filename: AbsoluteFile; conf: ConfigRef) =
  30. f.enabled = not filename.isEmpty
  31. if f.enabled:
  32. f.filename = filename
  33. f.buf = newStringOfCap(20)
  34. proc close*(f: var NdiFile, conf: ConfigRef) =
  35. if f.enabled:
  36. f.f = open(f.filename.string, fmWrite, 8000)
  37. doAssert f.f != nil, f.filename.string
  38. for s in f.syms:
  39. doWrite(f, s, conf)
  40. close(f.f)
  41. f.syms.reset
  42. f.filename.reset