[cfe-dev] clang-tidy check: Uninitialised primitive members in constructors

Richard legalize at xmission.com
Mon Feb 2 16:16:43 PST 2015


In article <CAAbBDD9bfbF7aX2_ZFQsMMDEX7cVtOP3wSDBjKN0Ogvx-jrY8A at mail.gmail.com>,
    Jonathan Coe <jbcoe at me.com> writes:

> Thanks, this is very helpful.
> 
> clang-query>match
> recordDecl(hasDescendant(fieldDecl(hasType(anyOf(builtinType(),
> pointerType()))))
> 
> will find a class with pointer or builtin types. I'm not sure how to record
> this to make use of it later.

In the context of clang-tidy, you'll register the above matcher and then
when nodes match it, your check will be called.  You can use this to
stash away pointers to the AST or extract whatever information you
need at the time you see the class declaration.

You can add another matcher for the constructor definition and then
when that matcher invokes your check you can use the information
stashed away to do the more detailed analysis.

I don't have clang-query handy right now, but you should be able to
use a combination of constructorDecl, forEachConstructorInitializer,
hasAnyConstructorInitializer, ctorInitializer, isWritten, forField,
and withInitializer to get the necessary matcher for the initializers.

If writing a single matcher expression is too difficult, you can
always match on something simpler and then traverse the details
through the AST nodes themselves.

I find that using the matchers with bind("id") helps me differentiate
the different matchers that invoked my callback.  You can see an
example of that style here on my "remove void args" tool:
<https://github.com/LegalizeAdulthood/remove-void-args/blob/master/6/RemoveVoidArgs.cpp>

(I'm in the process of reworking that as a clang-tidy check.)

> I can live with false positives in the constructor body for now.

Yeah, I find that its best to start with the simplest case and work
your way up to more complicated cases.  Use a /lit/ test like the
other checks in clang-tidy use to verify your change is working
properly.
-- 
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
         The Terminals Wiki <http://terminals.classiccmp.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>



More information about the cfe-dev mailing list