]> granicus.if.org Git - clang/commitdiff
C++11 weakens the requirement for types used with offsetof from POD to standard layou...
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 28 Apr 2012 11:14:51 +0000 (11:14 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 28 Apr 2012 11:14:51 +0000 (11:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@155757 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
test/SemaCXX/offsetof-0x.cpp [new file with mode: 0644]

index b53e250f1978db82966e696cd83d4a812c56c11a..cf7de93e0137f9f2defee3a2b587bb9aa2208249 100644 (file)
@@ -3535,6 +3535,8 @@ def ext_offsetof_extended_field_designator : Extension<
   InGroup<DiagGroup<"extended-offsetof">>;
 def warn_offsetof_non_pod_type : ExtWarn<"offset of on non-POD type %0">,
   InGroup<InvalidOffsetof>;
+def warn_offsetof_non_standardlayout_type : ExtWarn<
+  "offset of on non-standard-layout type %0">, InGroup<InvalidOffsetof>;
 def err_offsetof_bitfield : Error<"cannot compute offset of bit-field %0">;
 
 def warn_floatingpoint_eq : Warning<
index 043a6f7fde5b1c083e36886c5fa930ada60aedcd..9de08bc309627b8595948e3593a3685a95daacc0 100644 (file)
@@ -8744,10 +8744,18 @@ ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc,
     //   The macro offsetof accepts a restricted set of type arguments in this
     //   International Standard. type shall be a POD structure or a POD union
     //   (clause 9).
+    // C++11 [support.types]p4:
+    //   If type is not a standard-layout class (Clause 9), the results are
+    //   undefined.
     if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
-      if (!CRD->isPOD() && !DidWarnAboutNonPOD &&
+      bool IsSafe = LangOpts.CPlusPlus0x? CRD->isStandardLayout() : CRD->isPOD();
+      unsigned DiagID =
+        LangOpts.CPlusPlus0x? diag::warn_offsetof_non_standardlayout_type
+                            : diag::warn_offsetof_non_pod_type;
+
+      if (!IsSafe && !DidWarnAboutNonPOD &&
           DiagRuntimeBehavior(BuiltinLoc, 0,
-                              PDiag(diag::warn_offsetof_non_pod_type)
+                              PDiag(DiagID)
                               << SourceRange(CompPtr[0].LocStart, OC.LocEnd)
                               << CurrentType))
         DidWarnAboutNonPOD = true;
diff --git a/test/SemaCXX/offsetof-0x.cpp b/test/SemaCXX/offsetof-0x.cpp
new file mode 100644 (file)
index 0000000..610d919
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10.0.0 -fsyntax-only -std=c++11 -verify %s -Winvalid-offsetof
+
+struct NonPOD {
+  virtual void f();
+  int m;
+};
+
+struct P {
+  NonPOD fieldThatPointsToANonPODType;
+};
+
+void f() {
+  int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-warning{{offset of on non-standard-layout type 'P'}}
+}
+
+struct StandardLayout {
+  int x;
+  StandardLayout() {}
+};
+int o = __builtin_offsetof(StandardLayout, x); // no-warning