r199774 - Delay attribute checking until auto types are deduced

Nico Rieck nico.rieck at gmail.com
Tue Jan 21 15:54:36 PST 2014


Author: nrieck
Date: Tue Jan 21 17:54:36 2014
New Revision: 199774

URL: http://llvm.org/viewvc/llvm-project?rev=199774&view=rev
Log:
Delay attribute checking until auto types are deduced

Checking in ActOnVariableDeclarator computes and caches the linkage using
the non-deduced auto type which defaults to external linkage. Depending on
how the auto type is deduced linkage can change and conflict with the
cached linkage, hitting asserts.

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/attr-selectany.cpp
    cfe/trunk/test/SemaCXX/attr-weak.cpp
    cfe/trunk/test/SemaCXX/attr-weakref.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=199774&r1=199773&r2=199774&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Jan 21 17:54:36 2014
@@ -4816,6 +4816,10 @@ bool Sema::inferObjCARCLifetime(ValueDec
 }
 
 static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
+  // Ensure that an auto decl is deduced otherwise the checks below might cache
+  // the wrong linkage.
+  assert(S.ParsingInitForAutoVars.count(&ND) == 0);
+
   // 'weak' only applies to declarations with external linkage.
   if (WeakAttr *Attr = ND.getAttr<WeakAttr>()) {
     if (!ND.isExternallyVisible()) {
@@ -5431,7 +5435,6 @@ Sema::ActOnVariableDeclarator(Scope *S,
   }
 
   ProcessPragmaWeak(S, NewVD);
-  checkAttributesAfterMerging(*this, *NewVD);
 
   // If this is the first declaration of an extern C variable, update
   // the map of such variables.
@@ -8892,6 +8895,8 @@ Sema::FinalizeDeclaration(Decl *ThisDecl
   if (!VD)
     return;
 
+  checkAttributesAfterMerging(*this, *VD);
+
   if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) {
     if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) {
       Diag(Attr->getLocation(), diag::warn_attribute_ignored) << Attr;

Modified: cfe/trunk/test/SemaCXX/attr-selectany.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/attr-selectany.cpp?rev=199774&r1=199773&r2=199774&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/attr-selectany.cpp (original)
+++ cfe/trunk/test/SemaCXX/attr-selectany.cpp Tue Jan 21 17:54:36 2014
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -std=c++11 %s
 // MSVC produces similar diagnostics.
 
 __declspec(selectany) void foo() { } // expected-error{{'selectany' can only be applied to data items with external linkage}}
@@ -31,3 +31,6 @@ class X {
 };
 
 __declspec(selectany) X x(1);
+
+namespace { class Internal {}; }
+__declspec(selectany) auto x8 = Internal(); // expected-error {{'selectany' can only be applied to data items with external linkage}}

Modified: cfe/trunk/test/SemaCXX/attr-weak.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/attr-weak.cpp?rev=199774&r1=199773&r2=199774&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/attr-weak.cpp (original)
+++ cfe/trunk/test/SemaCXX/attr-weak.cpp Tue Jan 21 17:54:36 2014
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify -std=c++11 %s
 
 static int test0 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
 static void test1() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
@@ -31,8 +31,10 @@ template <class T> struct Test7 {
 };
 template <class T>
 int Test7<T>::var;
-namespace { class Internal; }
+namespace { class Internal {}; }
 template struct Test7<Internal>;
 template struct Test7<int>;
 
 class __attribute__((weak)) Test8 {}; // OK
+
+__attribute__((weak)) auto Test9 = Internal(); // expected-error {{weak declaration cannot have internal linkage}}

Modified: cfe/trunk/test/SemaCXX/attr-weakref.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/attr-weakref.cpp?rev=199774&r1=199773&r2=199774&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/attr-weakref.cpp (original)
+++ cfe/trunk/test/SemaCXX/attr-weakref.cpp Tue Jan 21 17:54:36 2014
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify -std=c++11 %s
 
 // GCC will accept anything as the argument of weakref. Should we
 // check for an existing decl?
@@ -34,3 +34,5 @@ static int a10();
 int a10() __attribute__((weakref ("foo")));
 
 static int v __attribute__((weakref(a1), alias("foo"))); // expected-error {{'weakref' attribute requires a string}}
+
+__attribute__((weakref ("foo"))) auto a11 = 1; // expected-error {{weakref declaration must have internal linkage}}





More information about the cfe-commits mailing list