Skip to content
Snippets Groups Projects
Commit b2df6a41 authored by Reid Kleckner's avatar Reid Kleckner
Browse files

[MS] Silence -Wextern-init on const selectany variables

In C, 'extern' is typically used to avoid tentative definitions when
declaring variables in headers, but adding an intializer makes it a
defintion. This is somewhat confusing, so GCC and Clang both warn on it.
In C++, 'extern' is often used to give implictly static 'const'
variables external linkage, so don't warn in that case. If selectany is
present, this might be header code intended for C and C++ inclusion, so
apply the C++ rules.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@279116 91177308-0d34-0410-b5e6-96231b3b80d8
parent c62bf7ad
No related branches found
No related tags found
No related merge requests found
...@@ -9938,10 +9938,17 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, ...@@ -9938,10 +9938,17 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init,
VDecl->setInvalidDecl(); VDecl->setInvalidDecl();
} }
} else if (VDecl->isFileVarDecl()) { } else if (VDecl->isFileVarDecl()) {
// In C, extern is typically used to avoid tentative definitions when
// declaring variables in headers, but adding an intializer makes it a
// defintion. This is somewhat confusing, so GCC and Clang both warn on it.
// In C++, extern is often used to give implictly static const variables
// external linkage, so don't warn in that case. If selectany is present,
// this might be header code intended for C and C++ inclusion, so apply the
// C++ rules.
if (VDecl->getStorageClass() == SC_Extern && if (VDecl->getStorageClass() == SC_Extern &&
(!getLangOpts().CPlusPlus || ((!getLangOpts().CPlusPlus && !VDecl->hasAttr<SelectAnyAttr>()) ||
!(Context.getBaseElementType(VDecl->getType()).isConstQualified() || !Context.getBaseElementType(VDecl->getType()).isConstQualified()) &&
VDecl->isExternC())) && !(getLangOpts().CPlusPlus && VDecl->isExternC()) &&
!isTemplateInstantiation(VDecl->getTemplateSpecializationKind())) !isTemplateInstantiation(VDecl->getTemplateSpecializationKind()))
Diag(VDecl->getLocation(), diag::warn_extern_init); Diag(VDecl->getLocation(), diag::warn_extern_init);
   
......
// RUN: %clang_cc1 -fms-compatibility -fms-extensions -verify %s
extern __declspec(selectany) const int x1 = 1; // no warning, const means we need extern in C++
// Should we really warn on this?
extern __declspec(selectany) int x2 = 1; // expected-warning {{'extern' variable has an initializer}}
__declspec(selectany) void foo() { } // expected-error{{'selectany' can only be applied to data items with external linkage}}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment