[PATCH] D16843: [Sema] Fix bug in TypeLocBuilder::pushImpl

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 18 13:09:38 PST 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL261260: [Sema] Fix bug in TypeLocBuilder::pushImpl (authored by ahatanak).

Changed prior to commit:
  http://reviews.llvm.org/D16843?vs=48387&id=48392#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D16843

Files:
  cfe/trunk/lib/Sema/TypeLocBuilder.cpp
  cfe/trunk/test/SemaObjCXX/typeloc-data-alignment.mm

Index: cfe/trunk/test/SemaObjCXX/typeloc-data-alignment.mm
===================================================================
--- cfe/trunk/test/SemaObjCXX/typeloc-data-alignment.mm
+++ cfe/trunk/test/SemaObjCXX/typeloc-data-alignment.mm
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+// Make sure this doesn't crash.
+
+ at protocol P
+ at end
+template <class T>
+id<P> foo(T) {
+  int i;
+  foo(i);
+}
Index: cfe/trunk/lib/Sema/TypeLocBuilder.cpp
===================================================================
--- cfe/trunk/lib/Sema/TypeLocBuilder.cpp
+++ cfe/trunk/lib/Sema/TypeLocBuilder.cpp
@@ -115,11 +115,39 @@
       NumBytesAtAlign4 += LocalSize;
     }
   } else if (LocalAlignment == 8) {
-    if (!NumBytesAtAlign8 && NumBytesAtAlign4 % 8 != 0) {
-      // No existing padding and misaligned members; add in 4 bytes padding
-      memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
-      Index -= 4;
+    if (NumBytesAtAlign8 == 0) {
+      // We have not seen any 8-byte aligned element yet. We insert a padding
+      // only if the new Index is not 8-byte-aligned.
+      if ((Index - LocalSize) % 8 != 0) {
+        memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
+        Index -= 4;
+      }
+    } else {
+      unsigned Padding = NumBytesAtAlign4 % 8;
+      if (Padding == 0) {
+        if (LocalSize % 8 == 0) {
+          // Everything is set: there's no padding and we don't need to add
+          // any.
+        } else {
+          assert(LocalSize % 8 == 4);
+          // No existing padding; add in 4 bytes padding
+          memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
+          Index -= 4;
+        }
+      } else {
+        assert(Padding == 4);
+        if (LocalSize % 8 == 0) {
+          // Everything is set: there's 4 bytes padding and we don't need
+          // to add any.
+        } else {
+          assert(LocalSize % 8 == 4);
+          // There are 4 bytes padding, but we don't need any; remove it.
+          memmove(&Buffer[Index + 4], &Buffer[Index], NumBytesAtAlign4);
+          Index += 4;
+        }
+      }
     }
+
     // Forget about any padding.
     NumBytesAtAlign4 = 0;
     NumBytesAtAlign8 += LocalSize;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16843.48392.patch
Type: text/x-patch
Size: 2266 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160218/5248bf9c/attachment.bin>


More information about the cfe-commits mailing list