]> granicus.if.org Git - clang/commitdiff
Suppress -Warray-bounds for classes (not just structs) where the last field is
authorMatt Beaumont-Gay <matthewbg@google.com>
Tue, 29 Nov 2011 22:43:53 +0000 (22:43 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Tue, 29 Nov 2011 22:43:53 +0000 (22:43 +0000)
a 1-length character array.

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

lib/Sema/SemaChecking.cpp
test/SemaCXX/array-bounds.cpp

index 9dd03133b994dd6c25f19a04e8833c25e2f65096..ec36963ed6175bfb2692acf94dee2aaf85bb31bc 100644 (file)
@@ -4202,8 +4202,11 @@ static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
     return false;
 
   const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
-  if (!RD || !RD->isStruct())
-    return false;
+  if (!RD) return false;
+  if (RD->isUnion()) return false;
+  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
+    if (!CRD->isStandardLayout()) return false;
+  }
 
   // See if this is the last field decl in the record.
   const Decl *D = FD;
index 48788f67d74e0515bd91e6ece498b1395d8a6578..c1b37011727da8a70fffb2b5ca8d7a86d88a7861 100644 (file)
@@ -190,10 +190,19 @@ namespace tailpad {
     int x;
     char c2[1];
   };
-  
-  char bar(struct foo *F) {
-    return F->c1[3]; // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
-    return F->c2[3]; // no warning, foo could have tail padding allocated.
+
+  class baz {
+   public:
+    char c1[1]; // expected-note {{declared here}}
+    int x;
+    char c2[1];
+  };
+
+  char bar(struct foo *F, baz *B) {
+    return F->c1[3] + // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
+           F->c2[3] + // no warning, foo could have tail padding allocated.
+           B->c1[3] + // expected-warning {{array index 3 is past the end of the array (which contains 1 element)}}
+           B->c2[3]; // no warning, baz could have tail padding allocated.
   }
 }