'pretty' command does not exist anymore; improvements for nimfix

This commit is contained in:
Araq 2014-09-10 02:19:46 +02:00
commit 569fbe8c37
12 changed files with 69 additions and 94 deletions

View file

@ -383,6 +383,5 @@ proc pickSym*(c: PContext, n: PNode; kind: TSymKind;
var a = initOverloadIter(o, c, n) var a = initOverloadIter(o, c, n)
while a != nil: while a != nil:
if a.kind == kind and flags <= a.flags: if a.kind == kind and flags <= a.flags:
incl(a.flags, sfUsed)
return a return a
a = nextOverloadIter(o, c, n) a = nextOverloadIter(o, c, n)

View file

@ -15,8 +15,7 @@ import
wordrecg, sem, semdata, idents, passes, docgen, extccomp, wordrecg, sem, semdata, idents, passes, docgen, extccomp,
cgen, jsgen, json, nversion, cgen, jsgen, json, nversion,
platform, nimconf, importer, passaux, depends, vm, vmdef, types, idgen, platform, nimconf, importer, passaux, depends, vm, vmdef, types, idgen,
tables, docgen2, service, parser, modules, ccgutils, sigmatch, ropes, lists, tables, docgen2, service, parser, modules, ccgutils, sigmatch, ropes, lists
pretty
from magicsys import systemModule, resetSysTypes from magicsys import systemModule, resetSysTypes
@ -164,13 +163,6 @@ proc commandEval(exp: string) =
var echoExp = "echo \"eval\\t\", " & "repr(" & exp & ")" var echoExp = "echo \"eval\\t\", " & "repr(" & exp & ")"
evalNim(echoExp.parseString, makeStdinModule()) evalNim(echoExp.parseString, makeStdinModule())
proc commandPretty =
msgs.gErrorMax = high(int) # do not stop after first error
semanticPasses()
registerPass(prettyPass)
compileProject()
pretty.overwriteFiles()
proc commandScan = proc commandScan =
var f = addFileExt(mainCommandArg(), NimExt) var f = addFileExt(mainCommandArg(), NimExt)
var stream = llStreamOpen(f, fmRead) var stream = llStreamOpen(f, fmRead)

View file

@ -32,7 +32,6 @@ proc mainCommand =
#msgs.gErrorMax = high(int) # do not stop after first error #msgs.gErrorMax = high(int) # do not stop after first error
registerPass verbosePass registerPass verbosePass
registerPass semPass registerPass semPass
registerPass prettyPass
gCmd = cmdPretty gCmd = cmdPretty
appendStr(searchPaths, options.libpath) appendStr(searchPaths, options.libpath)
if gProjectFull.len != 0: if gProjectFull.len != 0:

View file

@ -11,10 +11,9 @@
## to convert Nim code into a consistent style. ## to convert Nim code into a consistent style.
import import
strutils, os, options, ast, astalgo, msgs, ropes, idents, passes, strutils, os, options, ast, astalgo, msgs, ropes, idents,
intsets, strtabs, semdata, prettybase intsets, strtabs, semdata, prettybase
type type
StyleCheck* {.pure.} = enum None, Confirm, Auto StyleCheck* {.pure.} = enum None, Confirm, Auto
@ -23,11 +22,6 @@ var
gStyleCheck*: StyleCheck gStyleCheck*: StyleCheck
gCheckExtern*: bool gCheckExtern*: bool
type
TGen = object of TPassContext
module*: PSym
PGen = ref TGen
proc overwriteFiles*() = proc overwriteFiles*() =
let doStrip = options.getConfigVar("pretty.strip").normalize == "on" let doStrip = options.getConfigVar("pretty.strip").normalize == "on"
for i in 0 .. high(gSourceFiles): for i in 0 .. high(gSourceFiles):
@ -100,21 +94,20 @@ proc checkStyle(info: TLineInfo, s: string, k: TSymKind) =
if s != beau: if s != beau:
message(info, hintName, beau) message(info, hintName, beau)
proc checkDef*(n: PNode; s: PSym) = proc styleCheckDef*(info: TLineInfo; s: PSym; k: TSymKind) =
if gStyleCheck == StyleCheck.None: return if gStyleCheck == StyleCheck.None: return
# operators stay as they are: # operators stay as they are:
if s.kind in {skResult, skTemp} or s.name.s[0] notin prettybase.Letters: if k in {skResult, skTemp} or s.name.s[0] notin prettybase.Letters:
return return
if s.kind in {skType, skGenericParam} and sfAnon in s.flags: return if k in {skType, skGenericParam} and sfAnon in s.flags: return
if {sfImportc, sfExportc} * s.flags == {} or gCheckExtern: if {sfImportc, sfExportc} * s.flags == {} or gCheckExtern:
checkStyle(n.info, s.name.s, s.kind) checkStyle(info, s.name.s, k)
proc checkDef(c: PGen; n: PNode) = proc styleCheckDef*(info: TLineInfo; s: PSym) = styleCheckDef(info, s, s.kind)
if n.kind != nkSym: return proc styleCheckDef*(s: PSym) = styleCheckDef(s.info, s, s.kind)
checkDef(n, n.sym)
proc checkUse*(info: TLineInfo; s: PSym) = proc styleCheckUse*(info: TLineInfo; s: PSym) =
if info.fileIndex < 0: return if info.fileIndex < 0: return
# we simply convert it to what it looks like in the definition # we simply convert it to what it looks like in the definition
# for consistency # for consistency
@ -141,50 +134,3 @@ proc checkUse*(info: TLineInfo; s: PSym) =
var x = line.substr(0, first-1) & newName & line.substr(last+1) var x = line.substr(0, first-1) & newName & line.substr(last+1)
system.shallowCopy(gSourceFiles[info.fileIndex].lines[info.line-1], x) system.shallowCopy(gSourceFiles[info.fileIndex].lines[info.line-1], x)
gSourceFiles[info.fileIndex].dirty = true gSourceFiles[info.fileIndex].dirty = true
proc check(c: PGen, n: PNode) =
case n.kind
of nkSym: checkUse(n.info, n.sym)
of nkBlockStmt, nkBlockExpr, nkBlockType:
checkDef(c, n[0])
check(c, n.sons[1])
of nkForStmt, nkParForStmt:
let L = n.len
for i in countup(0, L-3):
checkDef(c, n[i])
check(c, n[L-2])
check(c, n[L-1])
of nkProcDef, nkLambdaKinds, nkMethodDef, nkIteratorDef, nkTemplateDef,
nkMacroDef, nkConverterDef:
checkDef(c, n[namePos])
for i in namePos+1 .. <n.len: check(c, n.sons[i])
of nkIdentDefs, nkVarTuple:
let a = n
checkMinSonsLen(a, 3)
let L = len(a)
for j in countup(0, L-3): checkDef(c, a.sons[j])
check(c, a.sons[L-2])
check(c, a.sons[L-1])
of nkTypeSection, nkConstSection:
for i in countup(0, sonsLen(n) - 1):
let a = n.sons[i]
if a.kind == nkCommentStmt: continue
checkSonsLen(a, 3)
checkDef(c, a.sons[0])
check(c, a.sons[1])
check(c, a.sons[2])
else:
for i in 0 .. <n.safeLen: check(c, n.sons[i])
proc processSym(c: PPassContext, n: PNode): PNode =
result = n
check(PGen(c), n)
proc myOpen(module: PSym): PPassContext =
var g: PGen
new(g)
g.module = module
result = g
const prettyPass* = makePass(open = myOpen, process = processSym)

View file

@ -33,7 +33,7 @@ proc loadFile*(info: TLineInfo) =
gSourceFiles[i].lines.add(line) gSourceFiles[i].lines.add(line)
# extract line ending of the file: # extract line ending of the file:
var lex: BaseLexer var lex: BaseLexer
open(lex, newFileStream(path)) open(lex, newFileStream(path, fmRead))
var pos = lex.bufpos var pos = lex.bufpos
while true: while true:
case lex.buf[pos] case lex.buf[pos]
@ -43,6 +43,7 @@ proc loadFile*(info: TLineInfo) =
of '\L', '\0': of '\L', '\0':
gSourceFiles[i].newline = "\L" gSourceFiles[i].newline = "\L"
break break
else: discard
inc pos inc pos
close(lex) close(lex)

View file

@ -1897,6 +1897,7 @@ proc semBlock(c: PContext, n: PNode): PNode =
addDecl(c, labl) addDecl(c, labl)
n.sons[0] = newSymNode(labl, n.sons[0].info) n.sons[0] = newSymNode(labl, n.sons[0].info)
suggestSym(n.sons[0].info, labl) suggestSym(n.sons[0].info, labl)
styleCheckDef(labl)
n.sons[1] = semExpr(c, n.sons[1]) n.sons[1] = semExpr(c, n.sons[1])
n.typ = n.sons[1].typ n.typ = n.sons[1].typ
if isEmptyType(n.typ): n.kind = nkBlockStmt if isEmptyType(n.typ): n.kind = nkBlockStmt

View file

@ -47,6 +47,7 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
result = symChoice(c, n, s, scOpen) result = symChoice(c, n, s, scOpen)
of skTemplate: of skTemplate:
if macroToExpand(s): if macroToExpand(s):
styleCheckUse(n.info, s)
let n = fixImmediateParams(n) let n = fixImmediateParams(n)
result = semTemplateExpr(c, n, s, {efNoSemCheck}) result = semTemplateExpr(c, n, s, {efNoSemCheck})
result = semGenericStmt(c, result, {}, ctx) result = semGenericStmt(c, result, {}, ctx)
@ -54,20 +55,26 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
result = symChoice(c, n, s, scOpen) result = symChoice(c, n, s, scOpen)
of skMacro: of skMacro:
if macroToExpand(s): if macroToExpand(s):
styleCheckUse(n.info, s)
result = semMacroExpr(c, n, n, s, {efNoSemCheck}) result = semMacroExpr(c, n, n, s, {efNoSemCheck})
result = semGenericStmt(c, result, {}, ctx) result = semGenericStmt(c, result, {}, ctx)
else: else:
result = symChoice(c, n, s, scOpen) result = symChoice(c, n, s, scOpen)
of skGenericParam: of skGenericParam:
result = newSymNodeTypeDesc(s, n.info) result = newSymNodeTypeDesc(s, n.info)
of skParam: styleCheckUse(n.info, s)
of skParam:
result = n result = n
styleCheckUse(n.info, s)
of skType: of skType:
if (s.typ != nil) and (s.typ.kind != tyGenericParam): if (s.typ != nil) and (s.typ.kind != tyGenericParam):
result = newSymNodeTypeDesc(s, n.info) result = newSymNodeTypeDesc(s, n.info)
else: else:
result = n result = n
else: result = newSymNode(s, n.info) styleCheckUse(n.info, s)
else:
result = newSymNode(s, n.info)
styleCheckUse(n.info, s)
proc lookup(c: PContext, n: PNode, flags: TSemGenericFlags, proc lookup(c: PContext, n: PNode, flags: TSemGenericFlags,
ctx: var IntSet): PNode = ctx: var IntSet): PNode =
@ -117,6 +124,11 @@ proc fuzzyLookup(c: PContext, n: PNode, flags: TSemGenericFlags,
else: else:
result = newDot(result, sym) result = newDot(result, sym)
proc addTempDecl(c: PContext; n: PNode; kind: TSymKind) =
let s = newSymS(skUnknown, getIdentNode(n), c)
addPrelimDecl(c, s)
styleCheckDef(n.info, s, kind)
proc semGenericStmt(c: PContext, n: PNode, proc semGenericStmt(c: PContext, n: PNode,
flags: TSemGenericFlags, ctx: var IntSet): PNode = flags: TSemGenericFlags, ctx: var IntSet): PNode =
result = n result = n
@ -161,6 +173,7 @@ proc semGenericStmt(c: PContext, n: PNode,
case s.kind case s.kind
of skMacro: of skMacro:
if macroToExpand(s): if macroToExpand(s):
styleCheckUse(fn.info, s)
result = semMacroExpr(c, n, n, s, {efNoSemCheck}) result = semMacroExpr(c, n, n, s, {efNoSemCheck})
result = semGenericStmt(c, result, {}, ctx) result = semGenericStmt(c, result, {}, ctx)
else: else:
@ -168,6 +181,7 @@ proc semGenericStmt(c: PContext, n: PNode,
result = n result = n
of skTemplate: of skTemplate:
if macroToExpand(s): if macroToExpand(s):
styleCheckUse(fn.info, s)
let n = fixImmediateParams(n) let n = fixImmediateParams(n)
result = semTemplateExpr(c, n, s, {efNoSemCheck}) result = semTemplateExpr(c, n, s, {efNoSemCheck})
result = semGenericStmt(c, result, {}, ctx) result = semGenericStmt(c, result, {}, ctx)
@ -184,14 +198,17 @@ proc semGenericStmt(c: PContext, n: PNode,
first = 1 first = 1
of skGenericParam: of skGenericParam:
result.sons[0] = newSymNodeTypeDesc(s, n.sons[0].info) result.sons[0] = newSymNodeTypeDesc(s, n.sons[0].info)
styleCheckUse(fn.info, s)
first = 1 first = 1
of skType: of skType:
# bad hack for generics: # bad hack for generics:
if (s.typ != nil) and (s.typ.kind != tyGenericParam): if (s.typ != nil) and (s.typ.kind != tyGenericParam):
result.sons[0] = newSymNodeTypeDesc(s, n.sons[0].info) result.sons[0] = newSymNodeTypeDesc(s, n.sons[0].info)
styleCheckUse(fn.info, s)
first = 1 first = 1
else: else:
result.sons[0] = newSymNode(s, n.sons[0].info) result.sons[0] = newSymNode(s, n.sons[0].info)
styleCheckUse(fn.info, s)
first = 1 first = 1
# Consider 'when defined(globalsSlot): ThreadVarSetValue(globalsSlot, ...)' # Consider 'when defined(globalsSlot): ThreadVarSetValue(globalsSlot, ...)'
# in threads.nim: the subtle preprocessing here binds 'globalsSlot' which # in threads.nim: the subtle preprocessing here binds 'globalsSlot' which
@ -226,14 +243,14 @@ proc semGenericStmt(c: PContext, n: PNode,
openScope(c) openScope(c)
n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags, ctx) n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags, ctx)
for i in countup(0, L - 3): for i in countup(0, L - 3):
addPrelimDecl(c, newSymS(skUnknown, n.sons[i], c)) addTempDecl(c, n.sons[i], skForVar)
n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags, ctx) n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags, ctx)
closeScope(c) closeScope(c)
of nkBlockStmt, nkBlockExpr, nkBlockType: of nkBlockStmt, nkBlockExpr, nkBlockType:
checkSonsLen(n, 2) checkSonsLen(n, 2)
openScope(c) openScope(c)
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
addPrelimDecl(c, newSymS(skUnknown, n.sons[0], c)) addTempDecl(c, n.sons[0], skLabel)
n.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx) n.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx)
closeScope(c) closeScope(c)
of nkTryStmt: of nkTryStmt:
@ -256,7 +273,7 @@ proc semGenericStmt(c: PContext, n: PNode,
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx) a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx) a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
for j in countup(0, L-3): for j in countup(0, L-3):
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c)) addTempDecl(c, getIdentNode(a.sons[j]), skVar)
of nkGenericParams: of nkGenericParams:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
@ -266,23 +283,23 @@ proc semGenericStmt(c: PContext, n: PNode,
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx) a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
# do not perform symbol lookup for default expressions # do not perform symbol lookup for default expressions
for j in countup(0, L-3): for j in countup(0, L-3):
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c)) addTempDecl(c, getIdentNode(a.sons[j]), skType)
of nkConstSection: of nkConstSection:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
if a.kind == nkCommentStmt: continue if a.kind == nkCommentStmt: continue
if (a.kind != nkConstDef): illFormedAst(a) if (a.kind != nkConstDef): illFormedAst(a)
checkSonsLen(a, 3) checkSonsLen(a, 3)
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[0]), c)) addTempDecl(c, getIdentNode(a.sons[0]), skConst)
a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc}, ctx) a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc}, ctx)
a.sons[2] = semGenericStmt(c, a.sons[2], flags, ctx) a.sons[2] = semGenericStmt(c, a.sons[2], flags, ctx)
of nkTypeSection: of nkTypeSection:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
if a.kind == nkCommentStmt: continue if a.kind == nkCommentStmt: continue
if (a.kind != nkTypeDef): illFormedAst(a) if (a.kind != nkTypeDef): illFormedAst(a)
checkSonsLen(a, 3) checkSonsLen(a, 3)
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[0]), c)) addTempDecl(c, getIdentNode(a.sons[0]), skType)
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
if a.kind == nkCommentStmt: continue if a.kind == nkCommentStmt: continue
@ -306,9 +323,9 @@ proc semGenericStmt(c: PContext, n: PNode,
of nkIdent: a = n.sons[i] of nkIdent: a = n.sons[i]
else: illFormedAst(n) else: illFormedAst(n)
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[i]), c)) addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[i]), c))
of nkObjectTy, nkTupleTy: of nkObjectTy, nkTupleTy:
discard discard
of nkFormalParams: of nkFormalParams:
checkMinSonsLen(n, 1) checkMinSonsLen(n, 1)
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx) n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx)
@ -320,17 +337,17 @@ proc semGenericStmt(c: PContext, n: PNode,
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx) a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx) a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
for j in countup(0, L-3): for j in countup(0, L-3):
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c)) addTempDecl(c, getIdentNode(a.sons[j]), skParam)
of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef, of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef,
nkIteratorDef, nkLambdaKinds: nkIteratorDef, nkLambdaKinds:
checkSonsLen(n, bodyPos + 1) checkSonsLen(n, bodyPos + 1)
if n.kind notin nkLambdaKinds: if n.kind notin nkLambdaKinds:
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(n.sons[0]), c)) addTempDecl(c, getIdentNode(n.sons[0]), skProc)
openScope(c) openScope(c)
n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos], n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos],
flags, ctx) flags, ctx)
if n.sons[paramsPos].kind != nkEmpty: if n.sons[paramsPos].kind != nkEmpty:
if n.sons[paramsPos].sons[0].kind != nkEmpty: if n.sons[paramsPos].sons[0].kind != nkEmpty:
addPrelimDecl(c, newSym(skUnknown, getIdent("result"), nil, n.info)) addPrelimDecl(c, newSym(skUnknown, getIdent("result"), nil, n.info))
n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags, ctx) n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags, ctx)
n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos], flags, ctx) n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos], flags, ctx)

View file

@ -35,6 +35,7 @@ proc semBreakOrContinue(c: PContext, n: PNode): PNode =
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
n.sons[0] = x n.sons[0] = x
suggestSym(x.info, s) suggestSym(x.info, s)
styleCheckUse(x.info, s)
else: else:
localError(n.info, errInvalidControlFlowX, s.name.s) localError(n.info, errInvalidControlFlowX, s.name.s)
else: else:
@ -323,6 +324,7 @@ proc semIdentDef(c: PContext, n: PNode, kind: TSymKind): PSym =
else: else:
result = semIdentWithPragma(c, kind, n, {}) result = semIdentWithPragma(c, kind, n, {})
suggestSym(n.info, result) suggestSym(n.info, result)
styleCheckDef(result)
proc checkNilable(v: PSym) = proc checkNilable(v: PSym) =
if sfGlobal in v.flags and {tfNotNil, tfNeedsInit} * v.typ.flags != {}: if sfGlobal in v.flags and {tfNotNil, tfNeedsInit} * v.typ.flags != {}:
@ -619,6 +621,7 @@ proc addForVarDecl(c: PContext, v: PSym) =
proc symForVar(c: PContext, n: PNode): PSym = proc symForVar(c: PContext, n: PNode): PSym =
let m = if n.kind == nkPragmaExpr: n.sons[0] else: n let m = if n.kind == nkPragmaExpr: n.sons[0] else: n
result = newSymG(skForVar, m, c) result = newSymG(skForVar, m, c)
styleCheckDef(result)
proc semForVars(c: PContext, n: PNode): PNode = proc semForVars(c: PContext, n: PNode): PNode =
result = n result = n

View file

@ -146,6 +146,7 @@ proc onlyReplaceParams(c: var TemplCtx, n: PNode): PNode =
if s.owner == c.owner and s.kind == skParam: if s.owner == c.owner and s.kind == skParam:
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
result = newSymNode(s, n.info) result = newSymNode(s, n.info)
styleCheckUse(n.info, s)
else: else:
for i in 0 .. <n.safeLen: for i in 0 .. <n.safeLen:
result.sons[i] = onlyReplaceParams(c, n.sons[i]) result.sons[i] = onlyReplaceParams(c, n.sons[i])
@ -183,6 +184,7 @@ proc addLocalDecl(c: var TemplCtx, n: var PNode, k: TSymKind) =
if not isTemplParam(c, ident): if not isTemplParam(c, ident):
let local = newGenSym(k, ident, c) let local = newGenSym(k, ident, c)
addPrelimDecl(c.c, local) addPrelimDecl(c.c, local)
styleCheckDef(n.info, local)
replaceIdentBySym(n, newSymNode(local, n.info)) replaceIdentBySym(n, newSymNode(local, n.info))
else: else:
replaceIdentBySym(n, ident) replaceIdentBySym(n, ident)
@ -197,14 +199,19 @@ proc semTemplSymbol(c: PContext, n: PNode, s: PSym): PNode =
result = symChoice(c, n, s, scOpen) result = symChoice(c, n, s, scOpen)
of skGenericParam: of skGenericParam:
result = newSymNodeTypeDesc(s, n.info) result = newSymNodeTypeDesc(s, n.info)
styleCheckUse(n.info, s)
of skParam: of skParam:
result = n result = n
styleCheckUse(n.info, s)
of skType: of skType:
if (s.typ != nil) and (s.typ.kind != tyGenericParam): if (s.typ != nil) and (s.typ.kind != tyGenericParam):
result = newSymNodeTypeDesc(s, n.info) result = newSymNodeTypeDesc(s, n.info)
else: else:
result = n result = n
else: result = newSymNode(s, n.info) styleCheckUse(n.info, s)
else:
result = newSymNode(s, n.info)
styleCheckUse(n.info, s)
proc semRoutineInTemplName(c: var TemplCtx, n: PNode): PNode = proc semRoutineInTemplName(c: var TemplCtx, n: PNode): PNode =
result = n result = n
@ -214,6 +221,7 @@ proc semRoutineInTemplName(c: var TemplCtx, n: PNode): PNode =
if s.owner == c.owner and (s.kind == skParam or sfGenSym in s.flags): if s.owner == c.owner and (s.kind == skParam or sfGenSym in s.flags):
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
result = newSymNode(s, n.info) result = newSymNode(s, n.info)
styleCheckUse(n.info, s)
else: else:
for i in countup(0, safeLen(n) - 1): for i in countup(0, safeLen(n) - 1):
result.sons[i] = semRoutineInTemplName(c, n.sons[i]) result.sons[i] = semRoutineInTemplName(c, n.sons[i])
@ -228,6 +236,7 @@ proc semRoutineInTemplBody(c: var TemplCtx, n: PNode, k: TSymKind): PNode =
var s = newGenSym(k, ident, c) var s = newGenSym(k, ident, c)
s.ast = n s.ast = n
addPrelimDecl(c.c, s) addPrelimDecl(c.c, s)
styleCheckDef(n.info, s)
n.sons[namePos] = newSymNode(s, n.sons[namePos].info) n.sons[namePos] = newSymNode(s, n.sons[namePos].info)
else: else:
n.sons[namePos] = ident n.sons[namePos] = ident
@ -261,6 +270,7 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
if s.owner == c.owner and s.kind == skParam: if s.owner == c.owner and s.kind == skParam:
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
result = newSymNode(s, n.info) result = newSymNode(s, n.info)
styleCheckUse(n.info, s)
elif contains(c.toBind, s.id): elif contains(c.toBind, s.id):
result = symChoice(c.c, n, s, scClosed) result = symChoice(c.c, n, s, scClosed)
elif contains(c.toMixin, s.name.id): elif contains(c.toMixin, s.name.id):
@ -270,6 +280,7 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
# var yz: T # var yz: T
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
result = newSymNode(s, n.info) result = newSymNode(s, n.info)
styleCheckUse(n.info, s)
else: else:
result = semTemplSymbol(c.c, n, s) result = semTemplSymbol(c.c, n, s)
of nkBind: of nkBind:
@ -322,6 +333,7 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
# labels are always 'gensym'ed: # labels are always 'gensym'ed:
let s = newGenSym(skLabel, n.sons[0], c) let s = newGenSym(skLabel, n.sons[0], c)
addPrelimDecl(c.c, s) addPrelimDecl(c.c, s)
styleCheckDef(s)
n.sons[0] = newSymNode(s, n.sons[0].info) n.sons[0] = newSymNode(s, n.sons[0].info)
n.sons[1] = semTemplBody(c, n.sons[1]) n.sons[1] = semTemplBody(c, n.sons[1])
closeScope(c) closeScope(c)
@ -456,6 +468,7 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
incl(s.flags, sfGlobal) incl(s.flags, sfGlobal)
else: else:
s = semIdentVis(c, skTemplate, n.sons[0], {}) s = semIdentVis(c, skTemplate, n.sons[0], {})
styleCheckDef(s)
# check parameter list: # check parameter list:
s.scope = c.currentScope s.scope = c.currentScope
pushOwner(s) pushOwner(s)
@ -527,6 +540,7 @@ proc semPatternBody(c: var TemplCtx, n: PNode): PNode =
# semtypes.addParamOrResult). Within the pattern we have to ensure # semtypes.addParamOrResult). Within the pattern we have to ensure
# to use the param with the proper type though: # to use the param with the proper type though:
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
styleCheckUse(n.info, s)
let x = c.owner.typ.n.sons[s.position+1].sym let x = c.owner.typ.n.sons[s.position+1].sym
assert x.name == s.name assert x.name == s.name
result = newSymNode(x, n.info) result = newSymNode(x, n.info)

View file

@ -82,6 +82,7 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
incl(e.flags, sfExported) incl(e.flags, sfExported)
if not isPure: strTableAdd(c.module.tab, e) if not isPure: strTableAdd(c.module.tab, e)
addSon(result.n, newSymNode(e)) addSon(result.n, newSymNode(e))
styleCheckDef(e)
if sfGenSym notin e.flags and not isPure: addDecl(c, e) if sfGenSym notin e.flags and not isPure: addDecl(c, e)
inc(counter) inc(counter)
if not hasNull: incl(result.flags, tfNeedsInit) if not hasNull: incl(result.flags, tfNeedsInit)
@ -92,7 +93,7 @@ proc semSet(c: PContext, n: PNode, prev: PType): PType =
var base = semTypeNode(c, n.sons[1], nil) var base = semTypeNode(c, n.sons[1], nil)
addSonSkipIntLit(result, base) addSonSkipIntLit(result, base)
if base.kind == tyGenericInst: base = lastSon(base) if base.kind == tyGenericInst: base = lastSon(base)
if base.kind != tyGenericParam: if base.kind != tyGenericParam:
if not isOrdinalType(base): if not isOrdinalType(base):
localError(n.info, errOrdinalTypeExpected) localError(n.info, errOrdinalTypeExpected)
elif lengthOrd(base) > MaxSetElements: elif lengthOrd(base) > MaxSetElements:
@ -363,7 +364,7 @@ proc semTuple(c: PContext, n: PNode, prev: PType): PType =
else: else:
addSon(result.n, newSymNode(field)) addSon(result.n, newSymNode(field))
addSonSkipIntLit(result, typ) addSonSkipIntLit(result, typ)
if gCmd == cmdPretty: checkDef(a.sons[j], field) if gCmd == cmdPretty: styleCheckDef(a.sons[j].info, field)
proc semIdentVis(c: PContext, kind: TSymKind, n: PNode, proc semIdentVis(c: PContext, kind: TSymKind, n: PNode,
allowed: TSymFlags): PSym = allowed: TSymFlags): PSym =
@ -399,7 +400,7 @@ proc semIdentWithPragma(c: PContext, kind: TSymKind, n: PNode,
else: discard else: discard
else: else:
result = semIdentVis(c, kind, n, allowed) result = semIdentVis(c, kind, n, allowed)
if gCmd == cmdPretty: checkDef(n, result) if gCmd == cmdPretty: styleCheckDef(n.info, result)
proc checkForOverlap(c: PContext, t: PNode, currentEx, branchIndex: int) = proc checkForOverlap(c: PContext, t: PNode, currentEx, branchIndex: int) =
let ex = t[branchIndex][currentEx].skipConv let ex = t[branchIndex][currentEx].skipConv
@ -579,6 +580,7 @@ proc semRecordNodeAux(c: PContext, n: PNode, check: var IntSet, pos: var int,
localError(n.sons[i].info, errAttemptToRedefine, f.name.s) localError(n.sons[i].info, errAttemptToRedefine, f.name.s)
if a.kind == nkEmpty: addSon(father, newSymNode(f)) if a.kind == nkEmpty: addSon(father, newSymNode(f))
else: addSon(a, newSymNode(f)) else: addSon(a, newSymNode(f))
styleCheckDef(f)
if a.kind != nkEmpty: addSon(father, a) if a.kind != nkEmpty: addSon(father, a)
of nkEmpty: discard of nkEmpty: discard
else: illFormedAst(n) else: illFormedAst(n)
@ -918,7 +920,7 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
addSon(result.n, newSymNode(arg)) addSon(result.n, newSymNode(arg))
rawAddSon(result, finalType) rawAddSon(result, finalType)
addParamOrResult(c, arg, kind) addParamOrResult(c, arg, kind)
if gCmd == cmdPretty: checkDef(a.sons[j], arg) if gCmd == cmdPretty: styleCheckDef(a.sons[j].info, arg)
var r: PType var r: PType
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:

View file

@ -331,7 +331,7 @@ proc markUsed(info: TLineInfo; s: PSym) =
if sfDeprecated in s.flags: message(info, warnDeprecated, s.name.s) if sfDeprecated in s.flags: message(info, warnDeprecated, s.name.s)
if sfError in s.flags: localError(info, errWrongSymbolX, s.name.s) if sfError in s.flags: localError(info, errWrongSymbolX, s.name.s)
suggestSym(info, s) suggestSym(info, s)
if gCmd == cmdPretty: checkUse(info, s) if gCmd == cmdPretty: styleCheckUse(info, s)
proc useSym*(sym: PSym): PNode = proc useSym*(sym: PSym): PNode =
result = newSymNode(sym) result = newSymNode(sym)

View file

@ -3,6 +3,7 @@ version 0.10
- Test nimfix on various babel packages - Test nimfix on various babel packages
- Pegs do not work at compile-time - Pegs do not work at compile-time
- rename cgi.URLencode to encodeUrl
version 0.9.6 version 0.9.6