diff options
author | Araq <rumpf_a@web.de> | 2017-02-17 18:03:56 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2017-02-17 18:03:56 +0100 |
commit | 8f426b974a38d141205cac182d1883e1ecf8c9ce (patch) | |
tree | 0ab2d29435251472a0fdcf26e8580ff104f0c84f | |
parent | dd8cbf5fca7becf43b5cec07ecf7e579308e2e7a (diff) | |
download | Nim-8f426b974a38d141205cac182d1883e1ecf8c9ce.tar.gz |
new feature: .used pragma to suppress declared-but-not-used warning
-rw-r--r-- | compiler/pragmas.nim | 20 | ||||
-rw-r--r-- | compiler/wordrecg.nim | 4 | ||||
-rw-r--r-- | doc/manual/pragmas.txt | 20 | ||||
-rw-r--r-- | tests/pragmas/tused.nim | 13 | ||||
-rw-r--r-- | web/news/e031_version_0_16_2.rst | 4 |
5 files changed, 51 insertions, 10 deletions
diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 04dbd3612..1fe1c81d2 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -25,19 +25,19 @@ const wBorrow, wExtern, wImportCompilerProc, wThread, wImportCpp, wImportObjC, wAsmNoStackFrame, wError, wDiscardable, wNoInit, wDestructor, wCodegenDecl, wGensym, wInject, wRaises, wTags, wLocks, wDelegator, wGcSafe, - wOverride, wConstructor, wExportNims} + wOverride, wConstructor, wExportNims, wUsed} converterPragmas* = procPragmas methodPragmas* = procPragmas+{wBase}-{wImportCpp} templatePragmas* = {wImmediate, wDeprecated, wError, wGensym, wInject, wDirty, - wDelegator, wExportNims} + wDelegator, wExportNims, wUsed} macroPragmas* = {FirstCallConv..LastCallConv, wImmediate, wImportc, wExportc, wNodecl, wMagic, wNosideeffect, wCompilerproc, wDeprecated, wExtern, wImportCpp, wImportObjC, wError, wDiscardable, wGensym, wInject, wDelegator, - wExportNims} + wExportNims, wUsed} iteratorPragmas* = {FirstCallConv..LastCallConv, wNosideeffect, wSideeffect, wImportc, wExportc, wNodecl, wMagic, wDeprecated, wBorrow, wExtern, wImportCpp, wImportObjC, wError, wDiscardable, wGensym, wInject, wRaises, - wTags, wLocks, wGcSafe, wExportNims} + wTags, wLocks, wGcSafe, wExportNims, wUsed} exprPragmas* = {wLine, wLocks, wNoRewrite, wGcSafe} stmtPragmas* = {wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks, wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints, @@ -55,16 +55,16 @@ const wPure, wHeader, wCompilerproc, wFinal, wSize, wExtern, wShallow, wImportCpp, wImportObjC, wError, wIncompleteStruct, wByCopy, wByRef, wInheritable, wGensym, wInject, wRequiresInit, wUnchecked, wUnion, wPacked, - wBorrow, wGcSafe, wExportNims, wPartial} + wBorrow, wGcSafe, wExportNims, wPartial, wUsed} fieldPragmas* = {wImportc, wExportc, wDeprecated, wExtern, - wImportCpp, wImportObjC, wError, wGuard, wBitsize} + wImportCpp, wImportObjC, wError, wGuard, wBitsize, wUsed} varPragmas* = {wImportc, wExportc, wVolatile, wRegister, wThreadVar, wNodecl, wMagic, wHeader, wDeprecated, wCompilerproc, wDynlib, wExtern, wImportCpp, wImportObjC, wError, wNoInit, wCompileTime, wGlobal, - wGensym, wInject, wCodegenDecl, wGuard, wGoto, wExportNims} + wGensym, wInject, wCodegenDecl, wGuard, wGoto, wExportNims, wUsed} constPragmas* = {wImportc, wExportc, wHeader, wDeprecated, wMagic, wNodecl, wExtern, wImportCpp, wImportObjC, wError, wGensym, wInject, wExportNims, - wIntDefine, wStrDefine} + wIntDefine, wStrDefine, wUsed} letPragmas* = varPragmas procTypePragmas* = {FirstCallConv..LastCallConv, wVarargs, wNosideeffect, wThread, wRaises, wLocks, wTags, wGcSafe} @@ -961,6 +961,10 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int, sym.magic = mIntDefine of wStrDefine: sym.magic = mStrDefine + of wUsed: + noVal(it) + if sym == nil: invalidPragma(it) + else: sym.flags.incl sfUsed else: invalidPragma(it) else: invalidPragma(it) diff --git a/compiler/wordrecg.nim b/compiler/wordrecg.nim index cf66b6358..6072bd64c 100644 --- a/compiler/wordrecg.nim +++ b/compiler/wordrecg.nim @@ -45,7 +45,7 @@ type wImportc, wExportc, wExportNims, wIncompleteStruct, wRequiresInit, wAlign, wNodecl, wPure, wSideeffect, wHeader, wNosideeffect, wGcSafe, wNoreturn, wMerge, wLib, wDynlib, - wCompilerproc, wProcVar, wBase, + wCompilerproc, wProcVar, wBase, wUsed, wFatal, wError, wWarning, wHint, wLine, wPush, wPop, wDefine, wUndef, wLinedir, wStacktrace, wLinetrace, wLink, wCompile, wLinksys, wDeprecated, wVarargs, wCallconv, wBreakpoint, wDebugger, @@ -131,7 +131,7 @@ const "incompletestruct", "requiresinit", "align", "nodecl", "pure", "sideeffect", "header", "nosideeffect", "gcsafe", "noreturn", "merge", "lib", "dynlib", - "compilerproc", "procvar", "base", + "compilerproc", "procvar", "base", "used", "fatal", "error", "warning", "hint", "line", "push", "pop", "define", "undef", "linedir", "stacktrace", "linetrace", "link", "compile", "linksys", "deprecated", "varargs", diff --git a/doc/manual/pragmas.txt b/doc/manual/pragmas.txt index af955a3e0..d30c37ff7 100644 --- a/doc/manual/pragmas.txt +++ b/doc/manual/pragmas.txt @@ -503,6 +503,26 @@ identifier that can be used to enable or disable it: This is often better than disabling all warnings at once. +used pragma +----------- + +Nim produces a warning for symbols that are not exported and not used either. +The ``used`` pragma can be attached to a symbol to suppress this warning. This +is particularly useful when the symbol was generated by a macro: + +.. code-block:: nim + template implementArithOps(T) = + proc echoAdd(a, b: T) {.used.} = + echo a + b + proc echoSub(a, b: T) {.used.} = + echo a - b + + # no warning produced for the unused 'echoSub' + implementArithOps(int) + echoAdd 3, 5 + + + experimental pragma ------------------- diff --git a/tests/pragmas/tused.nim b/tests/pragmas/tused.nim new file mode 100644 index 000000000..f3126bd45 --- /dev/null +++ b/tests/pragmas/tused.nim @@ -0,0 +1,13 @@ +discard """ + output: '''8''' +""" + +template implementArithOps(T) = + proc echoAdd(a, b: T) {.used.} = + echo a + b + proc echoSub(a, b: T) {.used.} = + echo a - b + +# no warning produced for the unused 'echoSub' +implementArithOps(int) +echoAdd 3, 5 diff --git a/web/news/e031_version_0_16_2.rst b/web/news/e031_version_0_16_2.rst index 171e4cef1..785285eaf 100644 --- a/web/news/e031_version_0_16_2.rst +++ b/web/news/e031_version_0_16_2.rst @@ -66,6 +66,10 @@ these procedures. In the near future we will be converting all exception types to refs to remove the need for the ``newException`` template. +- A new pragma ``.used`` can be used for symbols to prevent +the "declared but not used" warning. More details can be found `here <http://nim-lang.org/docs/manual.html#pragmas-used-pragma>`_. + + Bugfixes -------- |