[RFC] new checker: function pointer parameter can be const

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[RFC] new checker: function pointer parameter can be const

Daniel Marjamäki

Hello!

I am working on a new checker; function parameter can be const.

I plan to warn when:
 * a function parameter is not const
 * the function parameter is a pointer
 * it is dereferenced in the function (otherwise there will be -Wunused-parameter warnings)
 * the pointer address is not taken
 * the data is not written

Example code:

    int x;
    void warn(int *p) { x = *p; }   // <- Warning
    void dontwarn(int *p) { *p = 0; }  // <- No warning, data is written

My checker works a bit like the -Wunused-parameter warning. I add a flag to Decl that says if there is non-const usage.

I attach a proof-of-concept patch. It does not work well yet but shows how I intend to implement this.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden

Mobile:                 +46 (0)709 12 42 62
E-mail:                 [hidden email]

www.evidente.se
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

150805-constpar.diff (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] new checker: function pointer parameter can be const

Ted Kremenek
Hi Daniel,

I think your logic handles it, but does this cover...

- passing the parameter to another function/method where that parameter is non-const
- return'ing a parameter where the type is non-const?  Actually, if the inference is powerful enough, you could infer that both the parameter and return value should be const.  On second thought, that might be undesirable.  You may want to strengthen the const'ness of the parameter,  as that doesn't usually negatively impact the caller, but strengthening the const'ness of the return value may break code that calls the function.

What I don't think this check handles:

- overloads.  There may already be another method with this signature in the same class/struct.
- overrides.  A given method may override a method in a super class, and thus you can't change the signature.

I also think this should be an opt-in check in its own warning group, not in UnusedParameter.

Cheers,
Ted

> On Aug 5, 2015, at 5:47 AM, Daniel Marjamäki <[hidden email]> wrote:
>
>
> Hello!
>
> I am working on a new checker; function parameter can be const.
>
> I plan to warn when:
> * a function parameter is not const
> * the function parameter is a pointer
> * it is dereferenced in the function (otherwise there will be -Wunused-parameter warnings)
> * the pointer address is not taken
> * the data is not written
>
> Example code:
>
>   int x;
>   void warn(int *p) { x = *p; }   // <- Warning
>   void dontwarn(int *p) { *p = 0; }  // <- No warning, data is written
>
> My checker works a bit like the -Wunused-parameter warning. I add a flag to Decl that says if there is non-const usage.
>
> I attach a proof-of-concept patch. It does not work well yet but shows how I intend to implement this.
>
> Best regards,
> Daniel Marjamäki
>
> ..................................................................................................................
> Daniel Marjamäki Senior Engineer
> Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden
>
> Mobile:                 +46 (0)709 12 42 62
> E-mail:                 [hidden email]
>
> www.evidente.se
> <150805-constpar.diff>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev