[cfe-commits] r165849 - in /cfe/trunk: include/clang/AST/Decl.h lib/AST/Decl.cpp lib/AST/RecordLayoutBuilder.cpp lib/CodeGen/CGDebugInfo.cpp lib/CodeGen/CGExprConstant.cpp lib/CodeGen/CGRecordLayoutBuilder.cpp lib/Sema/SemaDeclAttr.cpp test/Sema/mms-bitfields.c test/Sema/pragma-ms_struct.c

Eli Friedman eli.friedman at gmail.com
Fri Oct 12 16:29:21 PDT 2012


Author: efriedma
Date: Fri Oct 12 18:29:20 2012
New Revision: 165849

URL: http://llvm.org/viewvc/llvm-project?rev=165849&view=rev
Log:
Make -mms-bitfields behave consistently.

Patch by Jeremiah Zanin.


Added:
    cfe/trunk/test/Sema/mms-bitfields.c
Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
    cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp
    cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
    cfe/trunk/test/Sema/pragma-ms_struct.c

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=165849&r1=165848&r2=165849&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Fri Oct 12 18:29:20 2012
@@ -3015,6 +3015,11 @@
     return K >= firstRecord && K <= lastRecord;
   }
 
+  /// isMsStrust - Get whether or not this is an ms_struct which can
+  /// be turned on with an attribute, pragma, or -mms-bitfields
+  /// commandline option.
+  bool isMsStruct(const ASTContext &C) const;
+
 private:
   /// \brief Deserialize just the fields.
   void LoadFieldsFromExternalStorage() const;

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=165849&r1=165848&r2=165849&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Fri Oct 12 18:29:20 2012
@@ -2517,7 +2517,7 @@
   unsigned Index = 0;
   const RecordDecl *RD = getParent();
   const FieldDecl *LastFD = 0;
-  bool IsMsStruct = RD->hasAttr<MsStructAttr>();
+  bool IsMsStruct = RD->isMsStruct(getASTContext());
 
   for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
        I != E; ++I, ++Index) {
@@ -2765,6 +2765,13 @@
   TagDecl::completeDefinition();
 }
 
+/// isMsStruct - Get whether or not this record uses ms_struct layout.
+/// This which can be turned on with an attribute, pragma, or the
+/// -mms-bitfields command-line option.
+bool RecordDecl::isMsStruct(const ASTContext &C) const {
+  return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
+}
+
 static bool isFieldOrIndirectField(Decl::Kind K) {
   return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
 }

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=165849&r1=165848&r2=165849&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Fri Oct 12 18:29:20 2012
@@ -1574,12 +1574,12 @@
 }
 
 void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
-  if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
+  if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
     IsUnion = RD->isUnion();
+    IsMsStruct = RD->isMsStruct(Context);
+  }
 
-  Packed = D->hasAttr<PackedAttr>();
-  
-  IsMsStruct = D->hasAttr<MsStructAttr>();
+  Packed = D->hasAttr<PackedAttr>();  
 
   // Honor the default struct packing maximum alignment flag.
   if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) {
@@ -2085,7 +2085,7 @@
       ZeroLengthBitfield = 0;
     }
 
-    if (Context.getLangOpts().MSBitfields || IsMsStruct) {
+    if (IsMsStruct) {
       // If MS bitfield layout is required, figure out what type is being
       // laid out and align the field to the width of that type.
       

Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=165849&r1=165848&r2=165849&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp Fri Oct 12 18:29:20 2012
@@ -824,7 +824,7 @@
       }
     }
   } else {
-    bool IsMsStruct = record->hasAttr<MsStructAttr>();
+    bool IsMsStruct = record->isMsStruct(CGM.getContext());
     const FieldDecl *LastFD = 0;
     for (RecordDecl::field_iterator I = record->field_begin(),
            E = record->field_end();

Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=165849&r1=165848&r2=165849&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Fri Oct 12 18:29:20 2012
@@ -379,7 +379,7 @@
   unsigned FieldNo = 0;
   unsigned ElementNo = 0;
   const FieldDecl *LastFD = 0;
-  bool IsMsStruct = RD->hasAttr<MsStructAttr>();
+  bool IsMsStruct = RD->isMsStruct(CGM.getContext());
   
   for (RecordDecl::field_iterator Field = RD->field_begin(),
        FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
@@ -478,7 +478,7 @@
 
   unsigned FieldNo = 0;
   const FieldDecl *LastFD = 0;
-  bool IsMsStruct = RD->hasAttr<MsStructAttr>();
+  bool IsMsStruct = RD->isMsStruct(CGM.getContext());
   uint64_t OffsetBits = CGM.getContext().toBits(Offset);
 
   for (RecordDecl::field_iterator Field = RD->field_begin(),

Modified: cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp?rev=165849&r1=165848&r2=165849&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp Fri Oct 12 18:29:20 2012
@@ -206,7 +206,7 @@
   Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
   Packed = D->hasAttr<PackedAttr>();
   
-  IsMsStruct = D->hasAttr<MsStructAttr>();
+  IsMsStruct = D->isMsStruct(Types.getContext());
 
   if (D->isUnion()) {
     LayoutUnion(D);
@@ -1061,7 +1061,7 @@
   const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
   RecordDecl::field_iterator it = D->field_begin();
   const FieldDecl *LastFD = 0;
-  bool IsMsStruct = D->hasAttr<MsStructAttr>();
+  bool IsMsStruct = D->isMsStruct(getContext());
   for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
     const FieldDecl *FD = *it;
 

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=165849&r1=165848&r2=165849&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Fri Oct 12 18:29:20 2012
@@ -974,8 +974,8 @@
 }
 
 static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
-  if (TagDecl *TD = dyn_cast<TagDecl>(D))
-    TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
+  if (RecordDecl *RD = dyn_cast<RecordDecl>(D))
+    RD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
   else
     S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
 }

Added: cfe/trunk/test/Sema/mms-bitfields.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/mms-bitfields.c?rev=165849&view=auto
==============================================================================
--- cfe/trunk/test/Sema/mms-bitfields.c (added)
+++ cfe/trunk/test/Sema/mms-bitfields.c Fri Oct 12 18:29:20 2012
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -mms-bitfields -fsyntax-only -verify -triple x86_64-apple-darwin9 %s
+
+// The -mms-bitfields commandline parameter should behave the same
+// as the ms_struct attribute.
+struct
+{
+   int a : 1;
+   short b : 1;
+} t;
+
+// MS pads out bitfields between different types.
+static int arr[(sizeof(t) == 8) ? 1 : -1];

Modified: cfe/trunk/test/Sema/pragma-ms_struct.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/pragma-ms_struct.c?rev=165849&r1=165848&r2=165849&view=diff
==============================================================================
--- cfe/trunk/test/Sema/pragma-ms_struct.c (original)
+++ cfe/trunk/test/Sema/pragma-ms_struct.c Fri Oct 12 18:29:20 2012
@@ -31,6 +31,12 @@
                    unsigned long bf_2 : 12;
 } __attribute__((ms_struct)) t2;
 
+enum
+{
+  A = 0,
+  B,
+  C
+} __attribute__((ms_struct)) e1; // expected-warning {{'ms_struct' attribute ignored}}
 
 // rdar://10513599
 #pragma ms_struct on





More information about the cfe-commits mailing list