]> granicus.if.org Git - clang/commitdiff
c++: support gcc's application of weak attribute on
authorFariborz Jahanian <fjahanian@apple.com>
Fri, 21 Oct 2011 22:27:12 +0000 (22:27 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Fri, 21 Oct 2011 22:27:12 +0000 (22:27 +0000)
class declaration which forces any such class and any
class that inherits from such a class to have their
typeinfo symbols be marked as weak.
// rdar://10246395
A    test/CodeGenCXX/weak-extern-typeinfo.cpp
M    lib/Sema/SemaDeclCXX.cpp
M    lib/Sema/SemaDeclAttr.cpp
M    lib/CodeGen/CGRTTI.cpp

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@142693 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGRTTI.cpp
lib/Sema/SemaDeclAttr.cpp
lib/Sema/SemaDeclCXX.cpp
test/CodeGenCXX/weak-extern-typeinfo.cpp [new file with mode: 0644]

index 1b3cb08284f5f331f93e9d2bad7fb1b52de20753..d9790d0b9e759a59b2c0d791a310ae31bfde37c2 100644 (file)
@@ -336,6 +336,8 @@ getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
 
     if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
       const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
+      if (RD->hasAttr<WeakAttr>())
+        return llvm::GlobalValue::WeakODRLinkage;
       if (RD->isDynamicClass())
         return CGM.getVTableLinkage(RD);
     }
index b83b716fdf01f38b1a55de3aeb2e9d6eb4fcc2cf..d801664ad63ee0636fcb7fa5ffdfb8752a9362d5 100644 (file)
@@ -1914,6 +1914,10 @@ static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
   }
 
   if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
+    if (isa<CXXRecordDecl>(D)) {
+      D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
+      return;
+    }
     S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
       << Attr.getName() << ExpectedVariableOrFunction;
     return;
index f445e9ff5b104cad68fab0e1f523b4a862b7a09b..2896e9f585e245870a113c84b02702867bc554fe 100644 (file)
@@ -1151,6 +1151,10 @@ bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases,
       // Okay, add this new base class.
       KnownBaseTypes[NewBaseType] = Bases[idx];
       Bases[NumGoodBases++] = Bases[idx];
+      if (const RecordType *Record = dyn_cast<RecordType>(NewBaseType))
+        if (const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()))
+          if (RD->hasAttr<WeakAttr>())
+            Class->addAttr(::new (Context) WeakAttr(SourceRange(), Context));
     }
   }
 
diff --git a/test/CodeGenCXX/weak-extern-typeinfo.cpp b/test/CodeGenCXX/weak-extern-typeinfo.cpp
new file mode 100644 (file)
index 0000000..3c3406e
--- /dev/null
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+// rdar://10246395
+
+#define WEAK __attribute__ ((weak)) 
+
+class WEAK A {
+  virtual void foo();
+};
+
+class B : public A {
+  virtual void foo();
+};
+void A::foo() { }
+void B::foo() { }
+
+class T {};
+class T1 {};
+
+class C : public T1, public B, public T {
+  virtual void foo();
+};
+void C::foo() { }
+
+class V1 : public virtual A {
+  virtual void foo();
+};
+
+class V2 : public virtual V1 {
+  virtual void foo();
+};
+void V1::foo() { }
+void V2::foo() { }
+
+// CHECK: @_ZTS1A = weak_odr constant
+// CHECK: @_ZTI1A = weak_odr unnamed_addr constant
+// CHECK: @_ZTS1B = weak_odr constant
+// CHECK: @_ZTI1B = weak_odr unnamed_addr constant
+// CHECK: @_ZTS1C = weak_odr constant
+// CHECK: @_ZTS2T1 = linkonce_odr constant
+// CHECK: @_ZTI2T1 = linkonce_odr unnamed_addr constant
+// CHECK: @_ZTS1T = linkonce_odr constant
+// CHECK: @_ZTI1T = linkonce_odr unnamed_addr constant
+// CHECK: @_ZTI1C = weak_odr unnamed_addr constant
+// CHECK: @_ZTS2V1 = weak_odr constant
+// CHECK: @_ZTI2V1 = weak_odr unnamed_addr constant
+// CHECK: @_ZTS2V2 = weak_odr constant
+// CHECK: @_ZTI2V2 = weak_odr unnamed_addr constant