[cfe-commits] [Patch] Add -Wmissing-variable-declarations

Eli Friedman eli.friedman at gmail.com
Wed Jun 20 16:51:48 PDT 2012


On Thu, Jun 14, 2012 at 3:00 PM, Ed Schouten <ed at 80386.nl> wrote:
> Hi Eli,
>
> Sorry for the very late response, but the last months were a bit busy
> for me (graduating, migrating).
>
> 2011/12/15 Eli Friedman <eli.friedman at gmail.com>:
>> I'm pretty sure we call CheckCompleteVariableDeclaration for both
>> those declarations; I could be mistaken, though.
>
> I just checked and it seems CheckCompleteVariableDeclaration is not
> called for those declarations. Maybe it's only called for variable
> declarations with an initializer?

Ah... looks like we don't call it for tentative definitions (which we
handle in Sema::ActOnEndOfTranslationUnit).  This has no effect for
the existing checks in CheckCompleteVariableDeclaration because none
of them apply to global variables in C.  That could easily be changed,
though.

Actually, you might want some special handling for tentative
definitions.  What do you expect the warning to do for a testcase like
"int x; int x = 10;"?

>>> I'm not a very good compiler hacker so I could use some help here. I
>>> suspect you mean something like this?
>>>
>>>  if (var->isThisDeclarationADefinition() &&
>>>      var->getLinkage() == ExternalLinkage &&
>>>      var->getPreviousDeclaration() == NULL)
>>>    Diag(var->getLocation(),
>>>      diag::warn_missing_variable_declarations) << var;
>>
>> Yes.
>
> It seems getPreviousDeclaration() has been removed, so I've fallen
> back to simply checking against `Previous.empty()'. If anyone knows of
> a more elegant solution, please let me know.

I think it just got renamed to getPreviousDecl().

> C (maybe just ELF?) has this `awesome' feature that it merges global
> variables if they have the same size (and are not initialized multiple
> times). If one C file contains `long x;' and the other one contains
> `double x;', all sorts of funny things might happen. This warning flag
> could spot issues like this.

It's a general C thing... most implementations allow a tentative
definition of a global in multiple translation units, and the linker
doesn't know anything about the type of a variable.

-Eli




More information about the cfe-commits mailing list