diff options
author | Araq <rumpf_a@web.de> | 2019-07-10 09:07:03 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2019-07-10 09:07:03 +0200 |
commit | 18182e4bfd046f38c22dc95b29263fb40ac75e5b (patch) | |
tree | 1848da24003a90722ed8f18bf63a524f59c419d6 /compiler | |
parent | ff4a9a226f484e3c07c0d2fb2e9ae74e2047a312 (diff) | |
download | Nim-18182e4bfd046f38c22dc95b29263fb40ac75e5b.tar.gz |
linter: refactorings
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/linter.nim | 57 | ||||
-rw-r--r-- | compiler/pragmas.nim | 2 | ||||
-rw-r--r-- | compiler/wordrecg.nim | 36 |
3 files changed, 40 insertions, 55 deletions
diff --git a/compiler/linter.nim b/compiler/linter.nim index d1d3140c0..40b3841f7 100644 --- a/compiler/linter.nim +++ b/compiler/linter.nim @@ -13,7 +13,7 @@ import strutils, os, intsets, strtabs import options, ast, astalgo, msgs, semdata, ropes, idents, - lineinfos, pathutils + lineinfos, pathutils, wordrecg const 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: 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 = for x in a: 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 = let y = line[a..b] - if cmpIgnoreStyle(y, x) == 0 and y != x: + if y != x and cmpIgnoreStyle(y, x) == 0: result = y else: 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) = if {optStyleHint, optStyleError} * conf.globalOptions != {}: 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) = styleCheckDef(conf, info, s, s.kind) @@ -159,32 +135,7 @@ proc styleCheckUse*(conf: ConfigRef; info: TLineInfo; s: PSym) = if oldName.len > 0: lintReport(conf, info, newName, oldName) -proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; pragmaName: string) = - const inMixedCase = [ - "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("_", "") +proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; w: TSpecialWord; pragmaName: string) = + let wanted = canonPragmaSpelling(w) if pragmaName != wanted: lintReport(conf, info, wanted, pragmaName) - - -template styleCheckUse*(info: TLineInfo; s: PSym) = - when defined(nimfix): - if gStyleCheck != StyleCheck.None: styleCheckUseImpl(conf, info, s) diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index b50b9ece2..327a6ff9b 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -778,7 +778,7 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int, let k = whichKeyword(ident) if k in validPragmas: if {optStyleHint, optStyleError} * c.config.globalOptions != {}: - checkPragmaUse(c.config, key.info, ident.s) + checkPragmaUse(c.config, key.info, k, ident.s) case k of wExportc: makeExternExport(c, sym, getOptionalStr(c, it, "$1"), it.info) diff --git a/compiler/wordrecg.nim b/compiler/wordrecg.nim index 07921f0b3..a26b86c75 100644 --- a/compiler/wordrecg.nim +++ b/compiler/wordrecg.nim @@ -44,7 +44,7 @@ type wImportCompilerProc, wImportc, wExportc, wExportNims, wIncompleteStruct, wRequiresInit, wAlign, wNodecl, wPure, wSideeffect, wHeader, - wNosideeffect, wGcSafe, wNoreturn, wMerge, wLib, wDynlib, + wNoSideEffect, wGcSafe, wNoreturn, wMerge, wLib, wDynlib, wCompilerproc, wCore, wProcVar, wBase, wUsed, wFatal, wError, wWarning, wHint, wLine, wPush, wPop, wDefine, wUndef, wLinedir, wStacktrace, wLinetrace, wLink, wCompile, @@ -184,3 +184,37 @@ proc findStr*(a: openArray[string], s: string): int = if cmpIgnoreStyle(a[i], s) == 0: return i 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] |