Could clang produce a warning for definite mis-use of __attribute__((const))

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

Could clang produce a warning for definite mis-use of __attribute__((const))

Kristóf Umann via cfe-dev
Hi,

Could clang detect a C++ methods that has been annotated with __attribute__((const)) that accesses the "this" pointer and produce an error?


The docs do make it clear that the functions must not access any memory even if that memory is accessed through its arguments, but it's quite easy to forget the implicit "this" argument for member functions, and finding the error is, while fun, a bit embarrassing.

Note that a function that has pointer arguments and examines the data pointed to must not be declared const if the pointed-to data might change between successive invocations of the function. In general, since a function cannot distinguish data that might change from data that cannot, const functions should never take pointer or, in C++, reference arguments. Likewise, a function that calls a non-const function usually must not be const itself.



--
Michael



_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Could clang produce a warning for definite mis-use of __attribute__((const))

Kristóf Umann via cfe-dev
On Wed, Aug 28, 2019 at 12:11 AM Michael Worcester via cfe-dev
<[hidden email]> wrote:

>
> Hi,
>
> Could clang detect a C++ methods that has been annotated with __attribute__((const)) that accesses the "this" pointer and produce an error?
>
> https://godbolt.org/z/QUivmv
>
> The docs do make it clear that the functions must not access any memory even if that memory is accessed through its arguments, but it's quite easy to forget the implicit "this" argument for member functions, and finding the error is, while fun, a bit embarrassing.
>
> Note that a function that has pointer arguments and examines the data pointed to must not be declared const if the pointed-to data might change between successive invocations of the function. In general, since a function cannot distinguish data that might change from data that cannot, const functions should never take pointer or, in C++, reference arguments. Likewise, a function that calls a non-const function usually must not be const itself.
>

> "if the pointed-to data might change between successive invocations of the function"
*If*
I think this should *not* be a clang warning, but clang-tidy check.

>
> --
> Michael
Roman

> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Could clang produce a warning for definite mis-use of __attribute__((const))

Kristóf Umann via cfe-dev
I recently dumped a few immediate thoughts on this subject here:
https://reviews.llvm.org/D66338#1633600

On 27.08.2019 14:17, Roman Lebedev via cfe-dev wrote:

> On Wed, Aug 28, 2019 at 12:11 AM Michael Worcester via cfe-dev
> <[hidden email]>  wrote:
>> Hi,
>>
>> Could clang detect a C++ methods that has been annotated with __attribute__((const)) that accesses the "this" pointer and produce an error?
>>
>> https://godbolt.org/z/QUivmv
>>
>> The docs do make it clear that the functions must not access any memory even if that memory is accessed through its arguments, but it's quite easy to forget the implicit "this" argument for member functions, and finding the error is, while fun, a bit embarrassing.
>>
>> Note that a function that has pointer arguments and examines the data pointed to must not be declared const if the pointed-to data might change between successive invocations of the function. In general, since a function cannot distinguish data that might change from data that cannot, const functions should never take pointer or, in C++, reference arguments. Likewise, a function that calls a non-const function usually must not be const itself.
>>
>> "if the pointed-to data might change between successive invocations of the function"
> *If*
> I think this should *not* be a clang warning, but clang-tidy check.
>
>> --
>> Michael
> Roman
>
>> _______________________________________________
>> cfe-dev mailing list
>> [hidden email]
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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