From 14fc0631d7cfa7436b9703fd8d08235b2b5929b7 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 19 Nov 2012 00:16:25 +0100 Subject: [PATCH] fixes #250 --- compiler/ast.nim | 2 +- compiler/msgs.nim | 9 +++++---- compiler/semstmts.nim | 4 +++- doc/nimrodc.txt | 32 ++++++++++++++++++++++++++++++++ lib/impure/db_mongo.nim | 4 ++-- lib/impure/db_mysql.nim | 4 ++-- lib/impure/db_postgres.nim | 4 ++-- lib/impure/db_sqlite.nim | 4 ++-- lib/system/ansi_c.nim | 16 +++++++++------- todo.txt | 3 ++- web/news.txt | 2 ++ 11 files changed, 62 insertions(+), 22 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index cdca68c204..f4b0d00a05 100755 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -726,7 +726,7 @@ const nkSymChoices* = {nkClosedSymChoice, nkOpenSymChoice} nkStrKinds* = {nkStrLit..nkTripleStrLit} - skLocalVars* = {skVar, skLet, skForVar, skParam} + skLocalVars* = {skVar, skLet, skForVar, skParam, skResult} # creator procs: diff --git a/compiler/msgs.nim b/compiler/msgs.nim index 45075cba84..0f795c07d7 100755 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -104,7 +104,7 @@ type warnUnknownSubstitutionX, warnLanguageXNotSupported, warnCommentXIgnored, warnNilStatement, warnAnalysisLoophole, warnDifferentHeaps, warnWriteToForeignHeap, warnImplicitClosure, - warnEachIdentIsTuple, warnUser, + warnEachIdentIsTuple, warnShadowIdent, warnUser, hintSuccess, hintSuccessX, hintLineTooLong, hintXDeclaredButNotUsed, hintConvToBaseNotNeeded, hintConvFromXtoItselfNotNeeded, hintExprAlwaysX, hintQuitCalled, @@ -351,6 +351,7 @@ const warnWriteToForeignHeap: "write to foreign heap [WriteToForeignHeap]", warnImplicitClosure: "implicit closure convention: '$1' [ImplicitClosure]", warnEachIdentIsTuple: "each identifier is a tuple [EachIdentIsTuple]", + warnShadowIdent: "shadowed identifier: '$1' [ShadowIdent]", warnUser: "$1 [User]", hintSuccess: "operation successful [Success]", hintSuccessX: "operation successful ($# lines compiled; $# sec total; $#) [SuccessX]", @@ -370,14 +371,14 @@ const hintUser: "$1 [User]"] const - WarningsToStr*: array[0..18, string] = ["CannotOpenFile", "OctalEscape", + WarningsToStr*: array[0..19, string] = ["CannotOpenFile", "OctalEscape", "XIsNeverRead", "XmightNotBeenInit", "Deprecated", "ConfigDeprecated", "SmallLshouldNotBeUsed", "UnknownMagic", "RedefinitionOfLabel", "UnknownSubstitutionX", "LanguageXNotSupported", "CommentXIgnored", "NilStmt", "AnalysisLoophole", "DifferentHeaps", "WriteToForeignHeap", - "ImplicitClosure", "EachIdentIsTuple", "User"] + "ImplicitClosure", "EachIdentIsTuple", "ShadowIdent", "User"] HintsToStr*: array[0..15, string] = ["Success", "SuccessX", "LineTooLong", "XDeclaredButNotUsed", "ConvToBaseNotNeeded", "ConvFromXtoItselfNotNeeded", @@ -464,7 +465,7 @@ proc raiseRecoverableError*(msg: string) {.noinline, noreturn.} = raise newException(ERecoverableError, msg) var - gNotes*: TNoteKinds = {low(TNoteKind)..high(TNoteKind)} + gNotes*: TNoteKinds = {low(TNoteKind)..high(TNoteKind)} - {warnShadowIdent} gErrorCounter*: int = 0 # counts the number of errors gHintCounter*: int = 0 gWarnCounter*: int = 0 diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index b0cef2df79..e613f23afa 100755 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -237,7 +237,9 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode = if c.InUnrolledContext > 0: v.flags.incl(sfShadowed) else: let shadowed = findShadowedVar(c, v) - if shadowed != nil: shadowed.flags.incl(sfShadowed) + if shadowed != nil: + shadowed.flags.incl(sfShadowed) + Message(a.info, warnShadowIdent, v.name.s) if def != nil and def.kind != nkEmpty: # this is only needed for the evaluation pass: v.ast = def diff --git a/doc/nimrodc.txt b/doc/nimrodc.txt index 8e43ce32c2..06a0f45002 100755 --- a/doc/nimrodc.txt +++ b/doc/nimrodc.txt @@ -36,6 +36,38 @@ Advanced command line switches are: .. include:: advopt.txt + + +List of warnings +---------------- + +Each warning can be activated individually with ``--warning[NAME]:on|off`` or +in a ``push`` pragma. + +========================== ============================================ +Name Description +========================== ============================================ +CannotOpenFile Some file not essential for the compiler's + working could not be opened. +OctalEscape The code contains an unsupported octal + sequence. +Deprecated The code uses a deprecated symbol. +ConfigDeprecated The project makes use of a deprecated config + file. +SmallLshouldNotBeUsed The letter 'l' should not be used as an + identifier. +AnalysisLoophole The thread analysis was incomplete due to + an indirect call. +DifferentHeaps The code mixes different local heaps in a + very dangerous way. +WriteToForeignHeap The code contains a threading error. +EachIdentIsTuple The code contains a confusing ``var`` + declaration. +ShadowIdent A local variable shadows another local + variable of an outer scope. +User Some user defined warning. +========================== ============================================ + Configuration files ------------------- diff --git a/lib/impure/db_mongo.nim b/lib/impure/db_mongo.nim index b7fb325f94..b11db78f88 100644 --- a/lib/impure/db_mongo.nim +++ b/lib/impure/db_mongo.nim @@ -34,8 +34,8 @@ type TDbConn* = TMongo ## a database connection; alias for ``TMongo`` FDb* = object of FIO ## effect that denotes a database operation - FReadDb* = object of FReadIO ## effect that denotes a read operation - FWriteDb* = object of FWriteIO ## effect that denotes a write operation + FReadDb* = object of FDB ## effect that denotes a read operation + FWriteDb* = object of FDB ## effect that denotes a write operation proc dbError*(db: TDbConn, msg: string) {.noreturn.} = ## raises an EDb exception with message `msg`. diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim index 118ed39bb9..5c71fc90d9 100755 --- a/lib/impure/db_mysql.nim +++ b/lib/impure/db_mysql.nim @@ -20,8 +20,8 @@ type TSqlQuery* = distinct string ## an SQL query string FDb* = object of FIO ## effect that denotes a database operation - FReadDb* = object of FReadIO ## effect that denotes a read operation - FWriteDb* = object of FWriteIO ## effect that denotes a write operation + FReadDb* = object of FDb ## effect that denotes a read operation + FWriteDb* = object of FDb ## effect that denotes a write operation proc dbError(db: TDbConn) {.noreturn.} = ## raises an EDb exception. diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim index 2e2b09bc25..8709601408 100755 --- a/lib/impure/db_postgres.nim +++ b/lib/impure/db_postgres.nim @@ -20,8 +20,8 @@ type TSqlQuery* = distinct string ## an SQL query string FDb* = object of FIO ## effect that denotes a database operation - FReadDb* = object of FReadIO ## effect that denotes a read operation - FWriteDb* = object of FWriteIO ## effect that denotes a write operation + FReadDb* = object of FDB ## effect that denotes a read operation + FWriteDb* = object of FDB ## effect that denotes a write operation proc sql*(query: string): TSqlQuery {.noSideEffect, inline.} = ## constructs a TSqlQuery from the string `query`. This is supposed to be diff --git a/lib/impure/db_sqlite.nim b/lib/impure/db_sqlite.nim index e9864c5991..fbc097784b 100755 --- a/lib/impure/db_sqlite.nim +++ b/lib/impure/db_sqlite.nim @@ -20,8 +20,8 @@ type TSqlQuery* = distinct string ## an SQL query string FDb* = object of FIO ## effect that denotes a database operation - FReadDb* = object of FReadIO ## effect that denotes a read operation - FWriteDb* = object of FWriteIO ## effect that denotes a write operation + FReadDb* = object of FDB ## effect that denotes a read operation + FWriteDb* = object of FDB ## effect that denotes a write operation proc sql*(query: string): TSqlQuery {.noSideEffect, inline.} = ## constructs a TSqlQuery from the string `query`. This is supposed to be diff --git a/lib/system/ansi_c.nim b/lib/system/ansi_c.nim index 486f5dd26f..195bc2e608 100755 --- a/lib/system/ansi_c.nim +++ b/lib/system/ansi_c.nim @@ -35,12 +35,13 @@ var c_stderr {.importc: "stderr", noDecl.}: C_TextFileStar # constants faked as variables: -var - SIGINT {.importc: "SIGINT", nodecl.}: cint - SIGSEGV {.importc: "SIGSEGV", nodecl.}: cint - SIGABRT {.importc: "SIGABRT", nodecl.}: cint - SIGFPE {.importc: "SIGFPE", nodecl.}: cint - SIGILL {.importc: "SIGILL", nodecl.}: cint +when not defined(SIGINT): + var + SIGINT {.importc: "SIGINT", nodecl.}: cint + SIGSEGV {.importc: "SIGSEGV", nodecl.}: cint + SIGABRT {.importc: "SIGABRT", nodecl.}: cint + SIGFPE {.importc: "SIGFPE", nodecl.}: cint + SIGILL {.importc: "SIGILL", nodecl.}: cint when defined(macosx): var @@ -95,7 +96,8 @@ proc c_malloc(size: int): pointer {.importc: "malloc", nodecl.} proc c_free(p: pointer) {.importc: "free", nodecl.} proc c_realloc(p: pointer, newsize: int): pointer {.importc: "realloc", nodecl.} -var errno {.importc, header: "".}: cint ## error variable +when not defined(errno): + var errno {.importc, header: "".}: cint ## error variable proc strerror(errnum: cint): cstring {.importc, header: "".} proc c_remove(filename: CString): cint {.importc: "remove", noDecl.} diff --git a/todo.txt b/todo.txt index f31edfa39e..a754bc6502 100755 --- a/todo.txt +++ b/todo.txt @@ -152,7 +152,8 @@ Version 2 and beyond "stop the world". However, it may be worthwhile to generate explicit (or implicit) syncGC() calls in loops. Automatic loop injection seems troublesome, but maybe we can come up with a simple heuristic. (All procs - that `new` shared memory are syncGC() candidates...) + that `new` shared memory are syncGC() candidates... But then 'new' itself + calls syncGC() so that's pointless.) - const ptr/ref --> pointless because of aliasing; much better: 'writes: []' effect diff --git a/web/news.txt b/web/news.txt index b73a32268d..8474db4f9c 100755 --- a/web/news.txt +++ b/web/news.txt @@ -27,6 +27,8 @@ Changes affecting backwards compatibility Compiler Additions ------------------ +- The compiler can now warn about shadowed local variables. However, this needs + to be turned on explicitly via ``--warning[ShadowIdent]:on``. Language Additions