[cfe-commits] [Patch] Add -Wmissing-variable-declarations
Eli Friedman
eli.friedman at gmail.com
Fri Dec 9 18:38:05 PST 2011
On Fri, Dec 9, 2011 at 2:24 PM, Ed Schouten <ed at 80386.nl> wrote:
> Hi folks,
>
> Attached is a patch to add a new type of compiler warning to Clang,
> namely -Wmissing-variable-declarations.
>
> Basically it acts like -Wmissing-prototypes, but then for global
> variables. This warning forces people to properly mark global variables
> as static or properly add an extern declaration.
>
> I have already used this patch to make various changes to the FreeBSD
> kernel and utilities. My observation is that marking stuff static tends
> to reduce the binary size, but it even allows the compiler to place more
> stuff in read-only segments if the user forgets to mark globals const.
Looks like an interesting idea.
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp (revision 143219)
+++ lib/Sema/SemaDecl.cpp (working copy)
@@ -4273,6 +4273,11 @@
Previous.addDecl(Pos->second);
}
+ if (Previous.empty() && NewVD->getStorageClass() == SC_None &&
+ NewVD->hasGlobalStorage())
+ Diag(NewVD->getLocation(),
+ diag::warn_missing_variable_declarations) << NewVD;
+
if (T->isVoidType() && !NewVD->hasExternalStorage()) {
Diag(NewVD->getLocation(), diag::err_typecheck_decl_incomplete_type)
<< T;
The check should be in CheckCompleteVariableDeclaration, not
CheckVariableDeclaration. Also, please use
isThisDeclarationADefinition() to check for a definition, and
getLinkage() to check whether the declaration is externally visible.
(The one interesting C testcase here is "extern int x = 3;"; there are
also various C++ tests of interest, like const globals and a
definition in an anonymous namespace.)
-Eli
More information about the cfe-commits
mailing list