linter: refactorings
This commit is contained in:
parent
ff4a9a226f
commit
18182e4bfd
3 changed files with 40 additions and 55 deletions
|
|
@ -13,7 +13,7 @@ import
|
||||||
strutils, os, intsets, strtabs
|
strutils, os, intsets, strtabs
|
||||||
|
|
||||||
import options, ast, astalgo, msgs, semdata, ropes, idents,
|
import options, ast, astalgo, msgs, semdata, ropes, idents,
|
||||||
lineinfos, pathutils
|
lineinfos, pathutils, wordrecg
|
||||||
|
|
||||||
const
|
const
|
||||||
Letters* = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF', '_'}
|
Letters* = {'a'..'z', 'A'..'Z', '0'..'9', '\x80'..'\xFF', '_'}
|
||||||
|
|
@ -22,28 +22,6 @@ proc identLen*(line: string, start: int): int =
|
||||||
while start+result < line.len and line[start+result] in Letters:
|
while start+result < line.len and line[start+result] in Letters:
|
||||||
inc result
|
inc result
|
||||||
|
|
||||||
type
|
|
||||||
StyleCheck* {.pure.} = enum None, Warn, Auto
|
|
||||||
|
|
||||||
proc overwriteFiles*(conf: ConfigRef) =
|
|
||||||
let doStrip = options.getConfigVar(conf, "pretty.strip").normalize == "on"
|
|
||||||
for i in 0 .. high(conf.m.fileInfos):
|
|
||||||
if conf.m.fileInfos[i].dirty and
|
|
||||||
(FileIndex(i) == conf.projectMainIdx):
|
|
||||||
let newFile = if false: conf.m.fileInfos[i].fullpath
|
|
||||||
else: conf.m.fileInfos[i].fullpath.changeFileExt(".pretty.nim")
|
|
||||||
try:
|
|
||||||
var f = open(newFile.string, fmWrite)
|
|
||||||
for line in conf.m.fileInfos[i].lines:
|
|
||||||
if doStrip:
|
|
||||||
f.write line.strip(leading = false, trailing = true)
|
|
||||||
else:
|
|
||||||
f.write line
|
|
||||||
f.write(conf.m.fileInfos[i], "\L")
|
|
||||||
f.close
|
|
||||||
except IOError:
|
|
||||||
rawMessage(conf, errGenerated, "cannot open file: " & newFile.string)
|
|
||||||
|
|
||||||
proc `=~`(s: string, a: openArray[string]): bool =
|
proc `=~`(s: string, a: openArray[string]): bool =
|
||||||
for x in a:
|
for x in a:
|
||||||
if s.startsWith(x): return true
|
if s.startsWith(x): return true
|
||||||
|
|
@ -95,7 +73,7 @@ proc beautifyName(s: string, k: TSymKind): string =
|
||||||
|
|
||||||
proc differ*(line: string, a, b: int, x: string): string =
|
proc differ*(line: string, a, b: int, x: string): string =
|
||||||
let y = line[a..b]
|
let y = line[a..b]
|
||||||
if cmpIgnoreStyle(y, x) == 0 and y != x:
|
if y != x and cmpIgnoreStyle(y, x) == 0:
|
||||||
result = y
|
result = y
|
||||||
else:
|
else:
|
||||||
result = ""
|
result = ""
|
||||||
|
|
@ -125,8 +103,6 @@ proc nep1CheckDefImpl(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) =
|
||||||
template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) =
|
template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) =
|
||||||
if {optStyleHint, optStyleError} * conf.globalOptions != {}:
|
if {optStyleHint, optStyleError} * conf.globalOptions != {}:
|
||||||
nep1CheckDefImpl(conf, info, s, k)
|
nep1CheckDefImpl(conf, info, s, k)
|
||||||
when defined(nimfix):
|
|
||||||
if gStyleCheck != StyleCheck.None: styleCheckDefImpl(conf, cache, info, s, k)
|
|
||||||
|
|
||||||
template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym) =
|
template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym) =
|
||||||
styleCheckDef(conf, info, s, s.kind)
|
styleCheckDef(conf, info, s, s.kind)
|
||||||
|
|
@ -159,32 +135,7 @@ proc styleCheckUse*(conf: ConfigRef; info: TLineInfo; s: PSym) =
|
||||||
if oldName.len > 0:
|
if oldName.len > 0:
|
||||||
lintReport(conf, info, newName, oldName)
|
lintReport(conf, info, newName, oldName)
|
||||||
|
|
||||||
proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; pragmaName: string) =
|
proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; w: TSpecialWord; pragmaName: string) =
|
||||||
const inMixedCase = [
|
let wanted = canonPragmaSpelling(w)
|
||||||
"noSideEffect", "importCompilerProc", "incompleteStruct", "requiresInit",
|
|
||||||
"sideEffect", "compilerProc", "lineDir", "stackTrace", "lineTrace",
|
|
||||||
"rangeChecks", "boundChecks",
|
|
||||||
"overflowChecks", "nilChecks",
|
|
||||||
"floatChecks", "nanChecks", "infChecks", "moveChecks",
|
|
||||||
"nonReloadable", "executeOnReload",
|
|
||||||
"deadCodeElim",
|
|
||||||
"compileTime", "noInit", "fieldChecks",
|
|
||||||
"linearScanEnd",
|
|
||||||
"computedGoto", "injectStmt",
|
|
||||||
"asmNoStackframe", "implicitStatic", "codegenDecl", "liftLocals"
|
|
||||||
]
|
|
||||||
let name = pragmaName.normalize
|
|
||||||
for x in inMixedCase:
|
|
||||||
if x.normalize == name:
|
|
||||||
if pragmaName != x:
|
|
||||||
lintReport(conf, info, x, pragmaName)
|
|
||||||
return
|
|
||||||
|
|
||||||
let wanted = pragmaName.toLowerAscii.replace("_", "")
|
|
||||||
if pragmaName != wanted:
|
if pragmaName != wanted:
|
||||||
lintReport(conf, info, wanted, pragmaName)
|
lintReport(conf, info, wanted, pragmaName)
|
||||||
|
|
||||||
|
|
||||||
template styleCheckUse*(info: TLineInfo; s: PSym) =
|
|
||||||
when defined(nimfix):
|
|
||||||
if gStyleCheck != StyleCheck.None: styleCheckUseImpl(conf, info, s)
|
|
||||||
|
|
|
||||||
|
|
@ -778,7 +778,7 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int,
|
||||||
let k = whichKeyword(ident)
|
let k = whichKeyword(ident)
|
||||||
if k in validPragmas:
|
if k in validPragmas:
|
||||||
if {optStyleHint, optStyleError} * c.config.globalOptions != {}:
|
if {optStyleHint, optStyleError} * c.config.globalOptions != {}:
|
||||||
checkPragmaUse(c.config, key.info, ident.s)
|
checkPragmaUse(c.config, key.info, k, ident.s)
|
||||||
case k
|
case k
|
||||||
of wExportc:
|
of wExportc:
|
||||||
makeExternExport(c, sym, getOptionalStr(c, it, "$1"), it.info)
|
makeExternExport(c, sym, getOptionalStr(c, it, "$1"), it.info)
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ type
|
||||||
wImportCompilerProc,
|
wImportCompilerProc,
|
||||||
wImportc, wExportc, wExportNims, wIncompleteStruct, wRequiresInit,
|
wImportc, wExportc, wExportNims, wIncompleteStruct, wRequiresInit,
|
||||||
wAlign, wNodecl, wPure, wSideeffect, wHeader,
|
wAlign, wNodecl, wPure, wSideeffect, wHeader,
|
||||||
wNosideeffect, wGcSafe, wNoreturn, wMerge, wLib, wDynlib,
|
wNoSideEffect, wGcSafe, wNoreturn, wMerge, wLib, wDynlib,
|
||||||
wCompilerproc, wCore, wProcVar, wBase, wUsed,
|
wCompilerproc, wCore, wProcVar, wBase, wUsed,
|
||||||
wFatal, wError, wWarning, wHint, wLine, wPush, wPop, wDefine, wUndef,
|
wFatal, wError, wWarning, wHint, wLine, wPush, wPop, wDefine, wUndef,
|
||||||
wLinedir, wStacktrace, wLinetrace, wLink, wCompile,
|
wLinedir, wStacktrace, wLinetrace, wLink, wCompile,
|
||||||
|
|
@ -184,3 +184,37 @@ proc findStr*(a: openArray[string], s: string): int =
|
||||||
if cmpIgnoreStyle(a[i], s) == 0:
|
if cmpIgnoreStyle(a[i], s) == 0:
|
||||||
return i
|
return i
|
||||||
result = - 1
|
result = - 1
|
||||||
|
|
||||||
|
proc canonPragmaSpelling*(w: TSpecialWord): string =
|
||||||
|
case w
|
||||||
|
of wNoSideEffect: "noSideEffect"
|
||||||
|
of wImportCompilerProc: "importCompilerProc"
|
||||||
|
of wIncompleteStruct: "incompleteStruct"
|
||||||
|
of wRequiresInit: "requiresInit"
|
||||||
|
of wSideEffect: "sideEffect"
|
||||||
|
of wCompilerProc: "compilerProc"
|
||||||
|
of wLineDir: "lineDir"
|
||||||
|
of wStackTrace: "stackTrace"
|
||||||
|
of wLineTrace: "lineTrace"
|
||||||
|
of wRangeChecks: "rangeChecks"
|
||||||
|
of wBoundChecks: "boundChecks"
|
||||||
|
of wOverflowChecks: "overflowChecks"
|
||||||
|
of wNilChecks: "nilChecks"
|
||||||
|
of wFloatChecks: "floatChecks"
|
||||||
|
of wNanChecks: "nanChecks"
|
||||||
|
of wInfChecks: "infChecks"
|
||||||
|
of wMoveChecks: "moveChecks"
|
||||||
|
of wNonReloadable: "nonReloadable"
|
||||||
|
of wExecuteOnReload: "executeOnReload"
|
||||||
|
of wDeadCodeElimUnused: "deadCodeElim"
|
||||||
|
of wCompileTime: "compileTime"
|
||||||
|
of wNoInit: "noInit"
|
||||||
|
of wFieldChecks: "fieldChecks"
|
||||||
|
of wLinearScanEnd: "linearScanEnd"
|
||||||
|
of wComputedGoto: "computedGoto"
|
||||||
|
of wInjectStmt: "injectStmt"
|
||||||
|
of wAsmNoStackFrame: "asmNoStackframe"
|
||||||
|
of wImplicitStatic: "implicitStatic"
|
||||||
|
of wCodegenDecl: "codegenDecl"
|
||||||
|
of wLiftLocals: "liftLocals"
|
||||||
|
else: specialWords[w]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue