linter.nim 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 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 style checker.
  10. import strutils
  11. import options, ast, msgs, idents, lineinfos, wordrecg
  12. const
  13. Letters* = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF', '_'}
  14. proc identLen*(line: string, start: int): int =
  15. while start+result < line.len and line[start+result] in Letters:
  16. inc result
  17. proc `=~`(s: string, a: openArray[string]): bool =
  18. for x in a:
  19. if s.startsWith(x): return true
  20. proc beautifyName(s: string, k: TSymKind): string =
  21. # minimal set of rules here for transition:
  22. # GC_ is allowed
  23. let allUpper = allCharsInSet(s, {'A'..'Z', '0'..'9', '_'})
  24. if allUpper and k in {skConst, skEnumField, skType}: return s
  25. result = newStringOfCap(s.len)
  26. var i = 0
  27. case k
  28. of skType, skGenericParam:
  29. # Types should start with a capital unless builtins like 'int' etc.:
  30. if s =~ ["int", "uint", "cint", "cuint", "clong", "cstring", "string",
  31. "char", "byte", "bool", "openArray", "seq", "array", "void",
  32. "pointer", "float", "csize", "csize_t", "cdouble", "cchar", "cschar",
  33. "cshort", "cu", "nil", "typedesc", "auto", "any",
  34. "range", "openarray", "varargs", "set", "cfloat", "ref", "ptr",
  35. "untyped", "typed", "static", "sink", "lent", "type", "owned", "iterable"]:
  36. result.add s[i]
  37. else:
  38. result.add toUpperAscii(s[i])
  39. of skConst, skEnumField:
  40. # for 'const' we keep how it's spelt; either upper case or lower case:
  41. result.add s[0]
  42. else:
  43. # as a special rule, don't transform 'L' to 'l'
  44. if s.len == 1 and s[0] == 'L': result.add 'L'
  45. elif '_' in s: result.add(s[i])
  46. else: result.add toLowerAscii(s[0])
  47. inc i
  48. while i < s.len:
  49. if s[i] == '_':
  50. if i+1 >= s.len:
  51. discard "trailing underscores should be stripped off"
  52. elif i > 0 and s[i-1] in {'A'..'Z'}:
  53. # don't skip '_' as it's essential for e.g. 'GC_disable'
  54. result.add('_')
  55. inc i
  56. result.add s[i]
  57. else:
  58. inc i
  59. result.add toUpperAscii(s[i])
  60. elif allUpper:
  61. result.add toLowerAscii(s[i])
  62. else:
  63. result.add s[i]
  64. inc i
  65. proc differ*(line: string, a, b: int, x: string): string =
  66. proc substrEq(s: string, pos, last: int, substr: string): bool =
  67. result = true
  68. for i in 0..<substr.len:
  69. if pos+i > last or s[pos+i] != substr[i]: return false
  70. result = ""
  71. if not substrEq(line, a, b, x):
  72. let y = line[a..b]
  73. if cmpIgnoreStyle(y, x) == 0:
  74. result = y
  75. proc nep1CheckDefImpl(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) =
  76. # operators stay as they are:
  77. if k in {skResult, skTemp} or s.name.s[0] notin Letters: return
  78. if k in {skType, skGenericParam} and sfAnon in s.flags: return
  79. if s.typ != nil and s.typ.kind == tyTypeDesc: return
  80. if {sfImportc, sfExportc} * s.flags != {}: return
  81. if optStyleCheck notin s.options: return
  82. let beau = beautifyName(s.name.s, k)
  83. if s.name.s != beau:
  84. lintReport(conf, info, beau, s.name.s)
  85. template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) =
  86. if {optStyleHint, optStyleError} * conf.globalOptions != {}:
  87. nep1CheckDefImpl(conf, info, s, k)
  88. template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym) =
  89. styleCheckDef(conf, info, s, s.kind)
  90. template styleCheckDef*(conf: ConfigRef; s: PSym) =
  91. styleCheckDef(conf, s.info, s, s.kind)
  92. proc differs(conf: ConfigRef; info: TLineInfo; newName: string): string =
  93. let line = sourceLine(conf, info)
  94. var first = min(info.col.int, line.len)
  95. if first < 0: return
  96. #inc first, skipIgnoreCase(line, "proc ", first)
  97. while first > 0 and line[first-1] in Letters: dec first
  98. if first < 0: return
  99. if first+1 < line.len and line[first] == '`': inc first
  100. let last = first+identLen(line, first)-1
  101. result = differ(line, first, last, newName)
  102. proc styleCheckUse*(conf: ConfigRef; info: TLineInfo; s: PSym) =
  103. if info.fileIndex.int < 0: return
  104. # we simply convert it to what it looks like in the definition
  105. # for consistency
  106. # operators stay as they are:
  107. if s.kind == skTemp or s.name.s[0] notin Letters or sfAnon in s.flags:
  108. return
  109. let newName = s.name.s
  110. let badName = differs(conf, info, newName)
  111. if badName.len > 0:
  112. # special rules for historical reasons
  113. let forceHint = badName == "nnkArgList" and newName == "nnkArglist" or badName == "nnkArglist" and newName == "nnkArgList"
  114. lintReport(conf, info, newName, badName, forceHint = forceHint)
  115. proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; w: TSpecialWord; pragmaName: string) =
  116. let wanted = $w
  117. if pragmaName != wanted:
  118. lintReport(conf, info, wanted, pragmaName)