pushpoppragmas.nim 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import pragmas, options, ast, trees, lineinfos, idents, wordrecg
  2. import std/assertions
  3. import renderer
  4. proc processNote(config: ConfigRef, n: PNode) =
  5. template handleNote(enumVals, notes) =
  6. let x = findStr(enumVals.a, enumVals.b, n[0][1].ident.s, errUnknown)
  7. assert x != errUnknown
  8. assert n[1].kind == nkIntLit
  9. nk = TNoteKind(x)
  10. if n[1].intVal != 0: incl(notes, nk)
  11. else: excl(notes, nk)
  12. var nk: TNoteKind
  13. case whichKeyword(n[0][0].ident)
  14. of wHint: handleNote(hintMin .. hintMax, config.notes)
  15. of wWarning: handleNote(warnMin .. warnMax, config.notes)
  16. of wWarningAsError: handleNote(warnMin .. warnMax, config.warningAsErrors)
  17. of wHintAsError: handleNote(hintMin .. hintMax, config.warningAsErrors)
  18. else: discard
  19. proc pushBackendOption(optionsStack: var seq[(TOptions, TNoteKinds)], options: TOptions, notes: TNoteKinds) =
  20. optionsStack.add (options, notes)
  21. proc popBackendOption(config: ConfigRef, optionsStack: var seq[(TOptions, TNoteKinds)], options: var TOptions) =
  22. let entry = optionsStack[^1]
  23. options = entry[0]
  24. config.notes = entry[1]
  25. optionsStack.setLen(optionsStack.len-1)
  26. proc processPushBackendOption*(config: ConfigRef, optionsStack: var seq[(TOptions, TNoteKinds)], options: var TOptions,
  27. n: PNode, start: int) =
  28. pushBackendOption(optionsStack, options, config.notes)
  29. for i in start..<n.len:
  30. let it = n[i]
  31. if it.kind in nkPragmaCallKinds and it.len == 2:
  32. if it[0].kind == nkBracketExpr and
  33. it[0].len == 2 and
  34. it[0][1].kind == nkIdent and it[0][0].kind == nkIdent:
  35. processNote(config, it)
  36. elif it[1].kind == nkIntLit:
  37. let sw = whichPragma(it[0])
  38. let opts = pragmaToOptions(sw)
  39. if opts != {}:
  40. if it[1].intVal != 0:
  41. options.incl opts
  42. else:
  43. options.excl opts
  44. template processPopBackendOption*(config: ConfigRef, optionsStack: var seq[(TOptions, TNoteKinds)], options: var TOptions) =
  45. popBackendOption(config, optionsStack, options)