]> granicus.if.org Git - clang/commitdiff
Provide the correct mangling and linkage for certain unnamed nested classes.
authorDavid Blaikie <dblaikie@gmail.com>
Wed, 14 Nov 2012 01:52:05 +0000 (01:52 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Wed, 14 Nov 2012 01:52:05 +0000 (01:52 +0000)
This corrects the mangling and linkage of classes (& their member functions) in
cases like this:

  struct foo {
    struct {
      void func() { ... }
    } x;
  };

we were accidentally giving this nested unnamed struct 'no' linkage where it
should've had the linkage of the outer class. The mangling was incorrecty too,
mangling as TU-wide unnamed type mangling of $_X rather than class-scoped
mangling of UtX_.

This also fixes -Wunused-member-function which would incorrectly diagnose
'func' as unused due to it having no linkage & thus appearing to be TU-local
when in fact it might be correctly used in another TU.

Similar mangling should be applied to function local classes in similar cases
but I've deferred that for a subsequent patch.

Review/discussion by Richard Smith, John McCall, & especially Eli Friedman.

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

include/clang/AST/ASTContext.h
include/clang/Sema/DeclSpec.h
lib/AST/ASTContext.cpp
lib/AST/Decl.cpp
lib/AST/ItaniumMangle.cpp
lib/Sema/SemaDecl.cpp
test/CodeGenCXX/mangle.cpp
test/CodeGenCXX/template-anonymous-types.cpp
test/SemaCXX/warn-unused-filescoped.cpp

index d559feed171af01b288ef2f40172b7f499d6bf13..25135133d456fbadee56b32b5ce2073906823607 100644 (file)
@@ -343,7 +343,10 @@ class ASTContext : public RefCountedBase<ASTContext> {
   /// \brief Mapping from each declaration context to its corresponding lambda 
   /// mangling context.
   llvm::DenseMap<const DeclContext *, LambdaMangleContext> LambdaMangleContexts;
-  
+
+  llvm::DenseMap<const DeclContext *, unsigned> UnnamedMangleContexts;
+  llvm::DenseMap<const TagDecl *, unsigned> UnnamedMangleNumbers;
+
   /// \brief Mapping that stores parameterIndex values for ParmVarDecls when
   /// that value exceeds the bitfield size of ParmVarDeclBits.ParameterIndex.
   typedef llvm::DenseMap<const VarDecl *, unsigned> ParameterIndexTable;
@@ -1989,6 +1992,9 @@ public:
   /// it is not used.
   bool DeclMustBeEmitted(const Decl *D);
 
+  void addUnnamedTag(const TagDecl *Tag);
+  int getUnnamedTagManglingNumber(const TagDecl *Tag) const;
+
   /// \brief Retrieve the lambda mangling number for a lambda expression.
   unsigned getLambdaManglingNumber(CXXMethodDecl *CallOperator);
   
index 0728e87376387d497bc387948e430d3e01e263f0..c1cf30e45f0ec545259d4f0f682769559dd3d898 100644 (file)
@@ -377,16 +377,16 @@ private:
   static bool isExprRep(TST T) {
     return (T == TST_typeofExpr || T == TST_decltype);
   }
+
+  DeclSpec(const DeclSpec &) LLVM_DELETED_FUNCTION;
+  void operator=(const DeclSpec &) LLVM_DELETED_FUNCTION;
+public:
   static bool isDeclRep(TST T) {
     return (T == TST_enum || T == TST_struct ||
             T == TST_interface || T == TST_union ||
             T == TST_class);
   }
 
-  DeclSpec(const DeclSpec &) LLVM_DELETED_FUNCTION;
-  void operator=(const DeclSpec &) LLVM_DELETED_FUNCTION;
-public:
-
   DeclSpec(AttributeFactory &attrFactory)
     : StorageClassSpec(SCS_unspecified),
       SCS_thread_specified(false),
index 577dd0adaf2fd362f5c4548263be3c26c2c83bba..80ce66a01742e79d8a04553f2e99bb55e7394217 100644 (file)
@@ -7508,6 +7508,23 @@ size_t ASTContext::getSideTableAllocatedMemory() const {
     + llvm::capacity_in_bytes(ClassScopeSpecializationPattern);
 }
 
+void ASTContext::addUnnamedTag(const TagDecl *Tag) {
+  // FIXME: This mangling should be applied to function local classes too
+  if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl() ||
+      !isa<CXXRecordDecl>(Tag->getParent()) || Tag->getLinkage() != ExternalLinkage)
+    return;
+
+  std::pair<llvm::DenseMap<const DeclContext *, unsigned>::iterator, bool> P =
+    UnnamedMangleContexts.insert(std::make_pair(Tag->getParent(), 0));
+  UnnamedMangleNumbers.insert(std::make_pair(Tag, P.first->second++));
+}
+
+int ASTContext::getUnnamedTagManglingNumber(const TagDecl *Tag) const {
+  llvm::DenseMap<const TagDecl *, unsigned>::const_iterator I =
+    UnnamedMangleNumbers.find(Tag);
+  return I != UnnamedMangleNumbers.end() ? I->second : -1;
+}
+
 unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) {
   CXXRecordDecl *Lambda = CallOperator->getParent();
   return LambdaMangleContexts[Lambda->getDeclContext()]
index 74abbaa492de659efd50ba58700f76c74e88bc74..9569841a5688967c79484a41db8064d66ff53060 100644 (file)
@@ -480,8 +480,7 @@ static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
   if (!(isa<CXXMethodDecl>(D) ||
         isa<VarDecl>(D) ||
         isa<FieldDecl>(D) ||
-        (isa<TagDecl>(D) &&
-         (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
+        isa<TagDecl>(D)))
     return LinkageInfo::none();
 
   LinkageInfo LV;
@@ -2561,8 +2560,7 @@ void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
 void TagDecl::startDefinition() {
   IsBeingDefined = true;
 
-  if (isa<CXXRecordDecl>(this)) {
-    CXXRecordDecl *D = cast<CXXRecordDecl>(this);
+  if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
     struct CXXRecordDecl::DefinitionData *Data = 
       new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
     for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
index 851944a42b6e974c0ab9c3d759abeb0b590a0086..fc61d88bd9fca73a60bc76fcc342c045831646cb 100644 (file)
@@ -1117,6 +1117,18 @@ void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
         break;
       }
     }
+
+    int UnnamedMangle = Context.getASTContext().getUnnamedTagManglingNumber(TD);
+    if (UnnamedMangle != -1) {
+      Out << "Ut";
+      if (UnnamedMangle != 0)
+        Out << llvm::utostr(UnnamedMangle - 1);
+      Out << '_';
+      break;
+    }
+
+    //assert(cast<RecordDecl>(RD)->isAnonymousStructOrUnion() && "Don't mangle unnamed things as "
+    //  "anonymous things");
         
     // Get a unique id for the anonymous struct.
     uint64_t AnonStructId = Context.getAnonymousStructId(TD);
index 04f66e5882b98d17181297f91b2ebca0e9b23849..72516fda80c77b3b6286bc94a8f99d2c6a468cf2 100644 (file)
@@ -2655,6 +2655,7 @@ Decl *Sema::ParsedFreeStandingDeclSpec(Scope *S, AccessSpecifier AS,
   }
 
   if (Tag) {
+    getASTContext().addUnnamedTag(Tag);
     Tag->setFreeStanding();
     if (Tag->isInvalidDecl())
       return Tag;
@@ -7355,6 +7356,10 @@ Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS,
     if (Decl *D = Group[i])
       Decls.push_back(D);
 
+  if (DeclSpec::isDeclRep(DS.getTypeSpecType()))
+    if (const TagDecl *Tag = dyn_cast_or_null<TagDecl>(DS.getRepAsDecl()))
+      getASTContext().addUnnamedTag(Tag);
+
   return BuildDeclaratorGroup(Decls.data(), Decls.size(),
                               DS.getTypeSpecType() == DeclSpec::TST_auto);
 }
index ba1b3bf5acd267b69d8d09b78cfe1b3e59cfa020..5dad030d5ea2f5b1c4a905aa08d244caa1bf498a 100644 (file)
@@ -218,7 +218,7 @@ struct S7 {
 // PR5139
 // CHECK: @_ZN2S7C1Ev
 // CHECK: @_ZN2S7C2Ev
-// CHECK: @"_ZN2S73$_0C1Ev"
+// CHECK: @_ZN2S7Ut_C1Ev
 S7::S7() {}
 
 // PR5063
@@ -852,3 +852,23 @@ namespace test36 {
   // CHECK: define weak_odr {{.*}} @_ZN6test362f1IJifEEENS_1AIXsZfp_EEEDpT_
   template A<2> f1(int, float);
 }
+
+namespace test37 {
+  struct foo {
+    struct {
+    } a;
+    typedef struct { } b;
+    typedef struct { } *c;
+    struct {
+    } d;
+  };
+  template<typename T> void func(T) { }
+  void test() {
+    // CHECK: define linkonce_odr void @_ZN6test374funcINS_3fooUt_EEEvT_
+    func(foo().a);
+    // CHECK: define linkonce_odr void @_ZN6test374funcINS_3fooUt0_EEEvT_
+    func(*foo::c());
+    // CHECK: define linkonce_odr void @_ZN6test374funcINS_3fooUt1_EEEvT_
+    func(foo().d);
+  }
+}
index 72fe090ceb7fd3e2e52f6edd286babffed8d23cd..3df487a33f3c85fc54f9e4e0dc7fe5c4224705cb 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -emit-llvm -w -o - | FileCheck %s
 
 struct S {
   enum { FOO = 42 };
@@ -17,21 +17,21 @@ template <typename T> int f(T t) {
 }
 
 void test() {
-  // Look for two instantiations, entirely internal to this TU, one for FOO's
+  // Look for two instantiations, one for FOO's
   // type and one for BAR's.
-  // CHECK: define internal i32 @"_Z1fIN1S3$_0EEiT_"(i32 %t)
+  // CHECK: define linkonce_odr i32 @_Z1fIN1SUt_EEiT_(i32 %t)
   (void)f(S::FOO);
-  // CHECK: define internal i32 @"_Z1fIN1S3$_1EEiT_"(i32 %t)
+  // CHECK: define linkonce_odr i32 @_Z1fIN1SUt0_EEiT_(i32 %t)
   (void)f(S::BAR);
 
   // Now check for the class template instantiations. Annoyingly, they are in
   // reverse order.
   //
   // BAR's instantiation of X:
-  // CHECK: define internal i32 @"_ZN1XIN1S3$_1EE1fEv"(%struct.X* %this)
-  // CHECK: define internal void @"_ZN1XIN1S3$_1EEC2ES1_"(%struct.X* %this, i32 %t) unnamed_addr
+  // CHECK: define linkonce_odr i32 @_ZN1XIN1SUt0_EE1fEv(%struct.X* %this)
+  // CHECK: define linkonce_odr void @_ZN1XIN1SUt0_EEC2ES1_(%struct.X* %this, i32 %t) unnamed_addr
   //
   // FOO's instantiation of X:
-  // CHECK: define internal i32 @"_ZN1XIN1S3$_0EE1fEv"(%struct.X.0* %this)
-  // CHECK: define internal void @"_ZN1XIN1S3$_0EEC2ES1_"(%struct.X.0* %this, i32 %t) unnamed_addr
+  // CHECK: define linkonce_odr i32 @_ZN1XIN1SUt_EE1fEv(%struct.X.0* %this)
+  // CHECK: define linkonce_odr void @_ZN1XIN1SUt_EEC2ES1_(%struct.X.0* %this, i32 %t) unnamed_addr
 }
index ad896b5212044b7fcd1df76e8ed23def47762f7c..1e0802e42dc10f194e9e4693da81a74270d5ebca 100644 (file)
@@ -101,3 +101,20 @@ namespace test5 {
   static const double d = 0.0;
   int y = sizeof(d);
 }
+
+namespace unused_nested {
+  class outer {
+    void func1();
+    struct {
+      void func2() {
+      }
+    } x;
+  };
+}
+
+namespace unused {
+  struct {
+    void func() { // expected-warning {{unused member function}}
+    }
+  } x; // expected-warning {{unused variable}}
+}