[PATCH] [-cxx-abi microsoft] Canonicalize array parameters better

David Majnemer david.majnemer at gmail.com
Sat Sep 7 20:14:42 PDT 2013


Hi rnk, timurrrr, whunt, cdavis5x,

More accurately characterize the nature of array parameters. Doing this
removes false back-reference opportunities.  Remove some hacks now that
we characterize these better.

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

Files:
  lib/AST/MicrosoftMangle.cpp
  test/CodeGenCXX/mangle-ms-arg-qualifiers.cpp

Index: lib/AST/MicrosoftMangle.cpp
===================================================================
--- lib/AST/MicrosoftMangle.cpp
+++ lib/AST/MicrosoftMangle.cpp
@@ -125,7 +125,7 @@
 #undef TYPE
   
   void mangleType(const TagDecl *TD);
-  void mangleDecayedArrayType(const ArrayType *T, bool IsGlobal);
+  void mangleDecayedArrayType(const ArrayType *T);
   void mangleArrayType(const ArrayType *T);
   void mangleFunctionClass(const FunctionDecl *FD);
   void mangleCallingConvention(const FunctionType *T, bool IsInstMethod = false);
@@ -331,7 +331,7 @@
       mangleQualifiers(Ty->getPointeeType().getQualifiers(), false);
   } else if (const ArrayType *AT = getASTContext().getAsArrayType(Ty)) {
     // Global arrays are funny, too.
-    mangleDecayedArrayType(AT, true);
+    mangleDecayedArrayType(AT);
     if (AT->getElementType()->isArrayType())
       Out << 'A';
     else
@@ -1051,25 +1051,24 @@
                                                  SourceRange Range) {
   // MSVC will backreference two canonically equivalent types that have slightly
   // different manglings when mangled alone.
+
+  // If the original parameter was textually written as an array,
+  // instead treat the decayed parameter like it's const.
+  //
+  // e.g.
+  // int [] -> int * const
+  if (const DecayedType *DT = T->getAs<DecayedType>()) {
+    if (DT->getOriginalType()->isArrayType())
+      T = T.withConst();
+  }
+
   void *TypePtr = getASTContext().getCanonicalType(T).getAsOpaquePtr();
   ArgBackRefMap::iterator Found = TypeBackReferences.find(TypePtr);
 
   if (Found == TypeBackReferences.end()) {
     size_t OutSizeBefore = Out.GetNumBytesInBuffer();
 
-    if (const DecayedType *DT = T->getAs<DecayedType>()) {
-      QualType OT = DT->getOriginalType();
-      if (const ArrayType *AT = getASTContext().getAsArrayType(OT)) {
-        mangleDecayedArrayType(AT, false);
-      } else if (const FunctionType *FT = OT->getAs<FunctionType>()) {
-        Out << "P6";
-        mangleFunctionType(FT, 0, false, false);
-      } else {
-        llvm_unreachable("unexpected decayed type");
-      }
-    } else {
-      mangleType(T, Range, QMM_Drop);
-    }
+    mangleType(T, Range, QMM_Drop);
 
     // See if it's worth creating a back reference.
     // Only types longer than 1 character are considered
@@ -1461,22 +1460,13 @@
 // <array-type> ::= <pointer-cvr-qualifiers> <cvr-qualifiers>
 //                  [Y <dimension-count> <dimension>+]
 //                  <element-type> # as global, E is never required
-//              ::= Q E? <cvr-qualifiers> [Y <dimension-count> <dimension>+]
-//                  <element-type> # as param, E is required for 64-bit
 // It's supposed to be the other way around, but for some strange reason, it
 // isn't. Today this behavior is retained for the sole purpose of backwards
 // compatibility.
-void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T,
-                                                     bool IsGlobal) {
+void MicrosoftCXXNameMangler::mangleDecayedArrayType(const ArrayType *T) {
   // This isn't a recursive mangling, so now we have to do it all in this
   // one call.
-  if (IsGlobal) {
-    manglePointerQualifiers(T->getElementType().getQualifiers());
-  } else {
-    Out << 'Q';
-    if (PointersAre64Bit)
-      Out << 'E';
-  }
+  manglePointerQualifiers(T->getElementType().getQualifiers());
   mangleType(T->getElementType(), SourceRange());
 }
 void MicrosoftCXXNameMangler::mangleType(const ConstantArrayType *T,
Index: test/CodeGenCXX/mangle-ms-arg-qualifiers.cpp
===================================================================
--- test/CodeGenCXX/mangle-ms-arg-qualifiers.cpp
+++ test/CodeGenCXX/mangle-ms-arg-qualifiers.cpp
@@ -204,3 +204,9 @@
 void mangle_no_fwd(char * x) {}
 // CHECK: "\01?mangle_no_fwd@@YAXPAD at Z"
 // X64:   "\01?mangle_no_fwd@@YAXPEAD at Z"
+
+// The first argument gets mangled as-if it were written "int * const"
+// The second arg should not form a backref because it isn't qualified
+void mangle_no_backref(int[], int *) {}
+// CHECK: "\01?mangle_no_backref@@YAXQAHPAH at Z"
+// X64:   "\01?mangle_no_backref@@YAXQEAHPEAH at Z"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1626.1.patch
Type: text/x-patch
Size: 4166 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130907/1d81b18d/attachment.bin>


More information about the cfe-commits mailing list