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

Jonathan Coe jonathanbcoe at gmail.com
Tue Feb 3 08:00:35 PST 2015


Excellent

I'm close to having this working for simple cases. 

I'm struggling to see how I can find the location for the replacement text.
I want the start of the initialisers but I don't see how to get a location for that.

Jon



> On 3 Feb 2015, at 00:16, Richard <legalize at xmission.com> wrote:
> 
> 
> 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>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list