[cfe-dev] using __attribute__((override)) in C++

Eli Friedman eli.friedman at gmail.com
Fri Oct 8 16:16:25 PDT 2010


On Fri, Oct 8, 2010 at 3:50 PM, Evan Martin <evan at chromium.org> wrote:
> Hello,
>
> MSVC, at least since 2005, supports an "override" keyword on virtual
> methods.  It produces a compiler error when you tag a function with
> the keyword but fail to override a superclass method.  It would've
> been handy to catch some troublesome typos in the past.  (The worst is
> when you change the type in a common superclass and then miss
> mirroring the change in one of a set of subclasses!)
>  http://msdn.microsoft.com/en-us/library/41w3sh1c(v=VS.80).aspx
>
> It turns out that clang already parses __attribute__((override)), in
> the same position as MSVC's "override", so it was pretty easy to make
> a tiny clang plugin that produces a similar error.  I'll inline the
> code below.
>
> dgregor on IRC suggested I should be adding this to Sema.  I would be
> happy to contribute this if anyone would give me a pointer on where it
> should plug in.  Today is the first time I've really looked at the
> clang code and I don't really know what I'm doing (as the following
> code likely demonstrates).

Try Sema::CheckFunctionDeclaration in SemaDecl.cpp.

> PS: I don't really follow C++0x but my vague impression is that it
> might have a different syntax or semantics for this keyword.  If
> that's will be an issue with this please let me know.

"override" isn't a keyword in C++0x; it's the name of an attribute.
And it does in fact have the same semantics as the MSVC keyword.

> PPS: This also prints out a warning to help me find places in my code
> to add the keyword.  I used "OVERRIDE" in my message because we
> #define OVERRIDE in our tree to the appropriate compiler-specific
> value.

A similar warning would be okay to put into Sema, assuming it's opt-in.

-Eli




More information about the cfe-dev mailing list