[cfe-commits] r165769 - /cfe/trunk/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h

Eli Friedman eli.friedman at gmail.com
Thu Oct 11 19:10:52 PDT 2012


On Thu, Oct 11, 2012 at 4:31 PM, Sean Silva <silvas at purdue.edu> wrote:
> Author: silvas
> Date: Thu Oct 11 18:31:18 2012
> New Revision: 165769
>
> URL: http://llvm.org/viewvc/llvm-project?rev=165769&view=rev
> Log:
> Add missing classof().
>
> Somewhat troublingly, without this implemented, the check inside
> isa_impl<> would silently use the parent's `classof()` when determining
> whether it was okay to downcast from the parent to the child!
>
> Bug analysis:
>
> A build failure after removing the parent's `classof()` initially
> alerted me to the bug, after which a little bit of thinking and reading
> of the code identified the root cause.
>
> The compiler could be made to prevent this bug from happening if there
> were a way to ensure that in the code
>
>     template <typename To, typename From, typename Enabler = void>
>     struct isa_impl {
>       static inline bool doit(const From &Val) {
>         return To::classof(&Val);
>       }
>     };
>
> that `To::classof` is actually inside the class `To`, and not in a base
> class. I am not aware of a way to check this in C++. If there is a means
> to perform that check, please bring it up on the list and this will be
> fixed.

Stupid attempt:

struct A { void f(); };
struct B : A {
#ifdef B_HAS_F
  void f();
#endif
};
decltype(&B::f) x;
extern void (B::*x)(); // error unless B_HAS_F

Unfortunately, not a complete solution, because you don't know the
precise signature of the classof you're looking for.

-Eli



More information about the cfe-commits mailing list