<div dir="ltr"><div dir="ltr"><div dir="ltr">Hi,<div><br></div><div>Could clang detect a C++ methods that has been annotated with __attribute__((const)) that accesses the "this" pointer and produce an error?</div><div><br></div><div><a href="https://godbolt.org/z/QUivmv">https://godbolt.org/z/QUivmv</a><br></div><div><br></div><div>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.</div><div><br></div><div><dd style="color:rgb(0,0,0);font-family:Times;font-size:medium"><p>Note that a function that has pointer arguments and examines the data pointed to must <em>not</em> be declared <code>const</code> 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.</p><div><br></div></dd></div><div><br></div><div>--</div><div>Michael</div><div><br></div><div><br></div></div></div></div>