diff options
author | Reid Kleckner <rnk@google.com> | 2016-08-18 18:45:07 +0000 |
---|---|---|
committer | Reid Kleckner <rnk@google.com> | 2016-08-18 18:45:07 +0000 |
commit | 24c86990575fd7c780f944a60d345565208067b0 (patch) | |
tree | 31eb2c3910d627209462456474457296b86b514f /clang/lib/Sema/SemaDecl.cpp | |
parent | 0f85498a0b6cf1e4a4c8ce3d49b2f6f1a83c361e (diff) | |
download | bcm5719-llvm-24c86990575fd7c780f944a60d345565208067b0.tar.gz bcm5719-llvm-24c86990575fd7c780f944a60d345565208067b0.zip |
[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.
llvm-svn: 279116
Diffstat (limited to 'clang/lib/Sema/SemaDecl.cpp')
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 55b044d1c59..42f8dea6f6b 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -9938,10 +9938,17 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, VDecl->setInvalidDecl(); } } 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 && - (!getLangOpts().CPlusPlus || - !(Context.getBaseElementType(VDecl->getType()).isConstQualified() || - VDecl->isExternC())) && + ((!getLangOpts().CPlusPlus && !VDecl->hasAttr<SelectAnyAttr>()) || + !Context.getBaseElementType(VDecl->getType()).isConstQualified()) && + !(getLangOpts().CPlusPlus && VDecl->isExternC()) && !isTemplateInstantiation(VDecl->getTemplateSpecializationKind())) Diag(VDecl->getLocation(), diag::warn_extern_init); |