r371749 - Don't warn about selectany on implicitly inline variables

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 12 10:55:48 PDT 2019


Author: rnk
Date: Thu Sep 12 10:55:48 2019
New Revision: 371749

URL: http://llvm.org/viewvc/llvm-project?rev=371749&view=rev
Log:
Don't warn about selectany on implicitly inline variables

Summary:
This avoids a -Wignored-attribute warning on the code pattern Microsoft
recommends for integral const static data members defined in headers
here:
https://docs.microsoft.com/en-us/cpp/build/reference/microsoft-extensions-to-c-and-cpp?view=vs-2019

The attribute is redundant, but it is necessary when compiling in C++14
modes with /Za, which disables MSVC's extension that treats such
variables as implicitly inline.

Fixes PR43270

Reviewers: epastor, thakis, hans

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D67426

Added:
    cfe/trunk/test/SemaCXX/declspec-selectany.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=371749&r1=371748&r2=371749&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Sep 12 10:55:48 2019
@@ -2652,6 +2652,15 @@ static void checkNewAttributesAfterDef(S
         --E;
         continue;
       }
+    } else if (isa<SelectAnyAttr>(NewAttribute) &&
+               cast<VarDecl>(New)->isInline() &&
+               !cast<VarDecl>(New)->isInlineSpecified()) {
+      // Don't warn about applying selectany to implicitly inline variables.
+      // Older compilers and language modes would require the use of selectany
+      // to make such variables inline, and it would have no effect if we
+      // honored it.
+      ++I;
+      continue;
     }
 
     S.Diag(NewAttribute->getLocation(),

Added: cfe/trunk/test/SemaCXX/declspec-selectany.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/declspec-selectany.cpp?rev=371749&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/declspec-selectany.cpp (added)
+++ cfe/trunk/test/SemaCXX/declspec-selectany.cpp Thu Sep 12 10:55:48 2019
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -std=c++14 %s -triple x86_64-windows-msvc -fdeclspec -verify
+// RUN: %clang_cc1 -std=c++17 %s -triple x86_64-windows-msvc -fdeclspec -verify
+// RUN: %clang_cc1 -std=c++14 %s -triple x86_64-scei-ps4 -fdeclspec -verify
+
+// MSVC emits this error too.
+const int __declspec(selectany) test1 = 0; // expected-error {{'selectany' can only be applied to data items with external linkage}}
+
+extern const int test2;
+const int test2 = 42; // expected-note {{previous definition is here}}
+extern __declspec(selectany) const int test2; // expected-warning {{attribute declaration must precede definition}}
+
+extern const int test3;
+const int __declspec(selectany) test3 = 42; // Standard usage.
+
+struct Test4 {
+  static constexpr int sdm = 0;
+};
+__declspec(selectany) constexpr int Test4::sdm; // no warning




More information about the cfe-commits mailing list