[cfe-dev] Implementing -Wunused-variables
Oscar Bonilla
ob at bitmover.com
Mon Oct 5 10:57:19 PDT 2009
On Oct 5, 2009, at 7:54 AM, Douglas Gregor wrote:
> Generally, the best way to figure out how to add a new warning like
> this is to find a similar warning. Clang has -Wunused-parameter
> already implemented, so that's a great start. There are a few places
> you'll need to look at and (in some cases) modify to implement -
> Wunused-variables:
>
> include/clang/Basic/DiagnosticSemaKinds.td: define a new
> warn_unused_variable warning here
> lib/Sema/Sema.h: DiagnoseUnusedParameters shows how to emit a
> similar warning
> lib/Sema/SemaDecl.cpp: Sema::ActOnPopScope is where you probably
> want to look for unused variables (when they are being popped out of
> scope)
> lib/Sema/SemaExpr.cpp: Sema::MarkDeclarationReferenced is called
> whenever any declaration (variable, function, method, etc.) is used
> by the source code.
>
> Most of the documentation for the ASTs is within the AST headers,
> which is also accessible via Doxygen:
>
> http://clang.llvm.org/doxygen/
>
> - Doug
Okay, this seems to do it:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ob.patch
Type: application/octet-stream
Size: 1108 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20091005/ec4ca4b4/attachment.obj>
-------------- next part --------------
and here's the testcase (I put it in test/Sema/warn-unused-variables.c
-------------- next part --------------
A non-text attachment was scrubbed...
Name: warn-unused-variables.c
Type: application/octet-stream
Size: 152 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20091005/ec4ca4b4/attachment-0001.obj>
-------------- next part --------------
Let me know if I did it right ;)
Cheers,
-Oscar
More information about the cfe-dev
mailing list