[PATCH] Add a mangler entry point for TBAA rather than using RTTI directly

Reid Kleckner rnk at google.com
Tue Nov 12 11:12:30 PST 2013


rnk added you to the CC list for the revision "Add a mangler entry point for TBAA rather than using RTTI directly".

RTTI is not yet implemented for the Microsoft C++ ABI and isn't expected
soon.  We could easily add the mangling, but the error is what prevents
us from silently miscompiling code that expects RTTI.

Instead, add a new mangleTBAAName entry point that simply forwards to
mangleType to produce a string that isn't part of the ABI.  Itanium can
continue to use RTTI names to avoid unecessary test breakage.

This also seems like the right design.  The fact that TBAA names happen
to be RTTI names is now an implementation detail of the mangler, rather
than part of TBAA.

http://llvm-reviews.chandlerc.com/D2153

Files:
  include/clang/AST/Mangle.h
  lib/AST/ItaniumMangle.cpp
  lib/AST/MicrosoftMangle.cpp
  lib/CodeGen/CodeGenTBAA.cpp
  test/CodeGen/tbaa-ms-abi.cpp

Index: include/clang/AST/Mangle.h
===================================================================
--- include/clang/AST/Mangle.h
+++ include/clang/AST/Mangle.h
@@ -148,6 +148,10 @@
   virtual void mangleDynamicAtExitDestructor(const VarDecl *D,
                                              raw_ostream &) = 0;
 
+  /// Generates a unique string for a type for use with TBAA.  This is an
+  /// implementation detail of clang, and not part of the ABI.
+  virtual void mangleTBAAName(QualType T, raw_ostream &) = 0;
+
   /// @}
 };
 
Index: lib/AST/ItaniumMangle.cpp
===================================================================
--- lib/AST/ItaniumMangle.cpp
+++ lib/AST/ItaniumMangle.cpp
@@ -144,6 +144,7 @@
                            raw_ostream &);
   void mangleCXXRTTI(QualType T, raw_ostream &);
   void mangleCXXRTTIName(QualType T, raw_ostream &);
+  void mangleTBAAName(QualType T, raw_ostream &);
   void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
                      raw_ostream &);
   void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
@@ -3784,6 +3785,10 @@
   Mangler.mangleType(Ty);
 }
 
+void ItaniumMangleContextImpl::mangleTBAAName(QualType Ty, raw_ostream &Out) {
+  mangleCXXRTTIName(Ty, Out);
+}
+
 ItaniumMangleContext *
 ItaniumMangleContext::create(ASTContext &Context, DiagnosticsEngine &Diags) {
   return new ItaniumMangleContextImpl(Context, Diags);
Index: lib/AST/MicrosoftMangle.cpp
===================================================================
--- lib/AST/MicrosoftMangle.cpp
+++ lib/AST/MicrosoftMangle.cpp
@@ -195,6 +195,7 @@
                                 raw_ostream &Out);
   virtual void mangleCXXRTTI(QualType T, raw_ostream &);
   virtual void mangleCXXRTTIName(QualType T, raw_ostream &);
+  virtual void mangleTBAAName(QualType T, raw_ostream &);
   virtual void mangleCXXCtor(const CXXConstructorDecl *D, CXXCtorType Type,
                              raw_ostream &);
   virtual void mangleCXXDtor(const CXXDestructorDecl *D, CXXDtorType Type,
@@ -2004,6 +2005,14 @@
     << T.getBaseTypeIdentifier();
 }
 
+void MicrosoftMangleContextImpl::mangleTBAAName(QualType T, raw_ostream &Out) {
+  // This is just a made up unique string for the purposes of tbaa.  undname
+  // does *not* know how to demangle it.
+  MicrosoftCXXNameMangler Mangler(*this, Out);
+  Mangler.getStream() << '?';
+  Mangler.mangleType(T, SourceRange());
+}
+
 void MicrosoftMangleContextImpl::mangleCXXCtor(const CXXConstructorDecl *D,
                                                CXXCtorType Type,
                                                raw_ostream &Out) {
Index: lib/CodeGen/CodeGenTBAA.cpp
===================================================================
--- lib/CodeGen/CodeGenTBAA.cpp
+++ lib/CodeGen/CodeGenTBAA.cpp
@@ -152,11 +152,9 @@
     if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
       return MetadataCache[Ty] = getChar();
 
-    // TODO: This is using the RTTI name. Is there a better way to get
-    // a unique string for a type?
     SmallString<256> OutName;
     llvm::raw_svector_ostream Out(OutName);
-    MContext.mangleCXXRTTIName(QualType(ETy, 0), Out);
+    MContext.mangleTBAAName(QualType(ETy, 0), Out);
     Out.flush();
     return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
   }
@@ -268,13 +266,11 @@
           FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth()));
     }
 
-    // TODO: This is using the RTTI name. Is there a better way to get
-    // a unique string for a type?
     SmallString<256> OutName;
     if (Features.CPlusPlus) {
-      // Don't use mangleCXXRTTIName for C code.
+      // Don't use the mangler for C code.
       llvm::raw_svector_ostream Out(OutName);
-      MContext.mangleCXXRTTIName(QualType(Ty, 0), Out);
+      MContext.mangleTBAAName(QualType(Ty, 0), Out);
       Out.flush();
     } else {
       OutName = RD->getName();
Index: test/CodeGen/tbaa-ms-abi.cpp
===================================================================
--- /dev/null
+++ test/CodeGen/tbaa-ms-abi.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -cxx-abi microsoft -triple i686-pc-win32 -disable-llvm-optzns -emit-llvm -o - -O1 %s | FileCheck %s
+//
+// Test that TBAA works in the Microsoft C++ ABI.  We used to error out while
+// attempting to mangle RTTI.
+
+struct StructA {
+  int a;
+};
+
+struct StructB : virtual StructA {
+  StructB();
+};
+
+StructB::StructB() {
+  a = 42;
+}
+
+// CHECK: store {{.*}} !tbaa [[TAG_A_i32:!.*]]
+// CHECK: [[TAG_A_i32]] = metadata !{metadata [[TYPE_A:!.*]], metadata [[TYPE_INT:!.*]], i64 0}
+// CHECK: [[TYPE_A]] = metadata !{metadata !"?AUStructA@@", metadata [[TYPE_INT:!.*]], i64 0}
+// CHECK: [[TYPE_INT]] = metadata !{metadata !"int", metadata [[TYPE_CHAR:!.*]],
+// CHECK: [[TYPE_CHAR]] = metadata !{metadata !"omnipotent char", metadata
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2153.1.patch
Type: text/x-patch
Size: 4862 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131112/3224061e/attachment.bin>


More information about the cfe-commits mailing list