ndi.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. when defined(nimPreviewSlimSystem):
  13. import std/[syncio, assertions]
  14. type
  15. NdiFile* = object
  16. enabled: bool
  17. f: File
  18. buf: string
  19. filename: AbsoluteFile
  20. syms: seq[PSym]
  21. proc doWrite(f: var NdiFile; s: PSym; conf: ConfigRef) =
  22. f.buf.setLen 0
  23. f.buf.addInt s.info.line.int
  24. f.buf.add "\t"
  25. f.buf.addInt s.info.col.int
  26. f.f.write(s.name.s, "\t")
  27. f.f.writeRope(s.loc.r)
  28. f.f.writeLine("\t", toFullPath(conf, s.info), "\t", f.buf)
  29. template writeMangledName*(f: NdiFile; s: PSym; conf: ConfigRef) =
  30. if f.enabled: f.syms.add s
  31. proc open*(f: var NdiFile; filename: AbsoluteFile; conf: ConfigRef) =
  32. f.enabled = not filename.isEmpty
  33. if f.enabled:
  34. f.filename = filename
  35. f.buf = newStringOfCap(20)
  36. proc close*(f: var NdiFile, conf: ConfigRef) =
  37. if f.enabled:
  38. f.f = open(f.filename.string, fmWrite, 8000)
  39. doAssert f.f != nil, f.filename.string
  40. for s in f.syms:
  41. doWrite(f, s, conf)
  42. close(f.f)
  43. f.syms.reset
  44. f.filename.reset