[cfe-commits] r70595 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/SemaCXX/offsetof.cpp

Anders Carlsson andersca at mac.com
Fri May 1 16:20:30 PDT 2009


Author: andersca
Date: Fri May  1 18:20:30 2009
New Revision: 70595

URL: http://llvm.org/viewvc/llvm-project?rev=70595&view=rev
Log:
It's an error to call offsetof on a non-POD type.

Added:
    cfe/trunk/test/SemaCXX/offsetof.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=70595&r1=70594&r2=70595&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri May  1 18:20:30 2009
@@ -936,6 +936,8 @@
 def err_offsetof_array_type : Error<"offsetof requires array type, %0 invalid">;
 def ext_offsetof_extended_field_designator : Extension<
   "using extended field designator is an extension">;
+def err_offsetof_non_pod_type : Error<"offset of on non-POD type %0">;
+
 def warn_floatingpoint_eq : Warning<
   "comparing floating point with == or != is unsafe">,
   InGroup<DiagGroup<"float-equal">>, DefaultIgnore;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=70595&r1=70594&r2=70595&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri May  1 18:20:30 2009
@@ -4761,6 +4761,13 @@
 
       // Get the decl corresponding to this.
       RecordDecl *RD = RC->getDecl();
+      if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
+        if (!CRD->isPOD())
+          return ExprError(Diag(BuiltinLoc, diag::err_offsetof_non_pod_type)
+                   << SourceRange(CompPtr[0].LocStart, OC.LocEnd)
+                   << Res->getType());
+      }
+      
       FieldDecl *MemberDecl
         = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo,
                                                           LookupMemberName)

Added: cfe/trunk/test/SemaCXX/offsetof.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/offsetof.cpp?rev=70595&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/offsetof.cpp (added)
+++ cfe/trunk/test/SemaCXX/offsetof.cpp Fri May  1 18:20:30 2009
@@ -0,0 +1,15 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+struct NonPOD {
+  virtual void f();
+  int m;
+};
+
+struct P {
+  NonPOD fieldThatPointsToANonPODType;
+};
+
+void f() {
+  int i = __builtin_offsetof(P, fieldThatPointsToANonPODType.m); // expected-error{{offset of on non-POD type 'struct P'}}
+}
+





More information about the cfe-commits mailing list