[PATCH] D71650: [AST] Fix compilation with GCC < 8 for MinGW

Martin Storsjö via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 18 01:04:59 PST 2019


mstorsjo created this revision.
mstorsjo added reviewers: rsmith, rjmccall, rnk, Mordante.
Herald added a project: clang.

GCC 7 and earlier, when targeting MinGW, seems to have a bug in layout/size of bitfield structs if they contain a nested enum, making the size of the struct 8 bytes, while we have a static assert requiring it to be 4 bytes or less.

While this clearly is a GCC bug, the workaround (moving the enum out of the bitfield) also is very nonintrusive and matches other existing enums there.

Testcase:

  $ cat bitfield.cpp
  struct MyStruct {
    unsigned a : 1;
    enum { SomeValue = 42 };
    unsigned b : 1;
  };      
  int StructSize = sizeof(struct MyStruct);
  $ x86_64-w64-mingw32-g++ -S -o - bitfield.cpp | grep -C 2 StructSize 
          .file   "bitfield.cpp"
          .text
          .globl  StructSize
          .data
          .align 4
  StructSize:
          .long   8 
          .ident  "GCC: (GNU) 7.3-win32 20180312"


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71650

Files:
  clang/include/clang/AST/Decl.h


Index: clang/include/clang/AST/Decl.h
===================================================================
--- clang/include/clang/AST/Decl.h
+++ clang/include/clang/AST/Decl.h
@@ -900,6 +900,8 @@
     DAK_Normal
   };
 
+  enum { NumScopeDepthOrObjCQualsBits = 7 };
+
   class ParmVarDeclBitfields {
     friend class ASTDeclReader;
     friend class ParmVarDecl;
@@ -922,8 +924,6 @@
     /// Whether this parameter is an ObjC method parameter or not.
     unsigned IsObjCMethodParam : 1;
 
-    enum { NumScopeDepthOrObjCQualsBits = 7 };
-
     /// If IsObjCMethodParam, a Decl::ObjCDeclQualifier.
     /// Otherwise, the number of function parameter scopes enclosing
     /// the function parameter scope in which this parameter was
@@ -1654,7 +1654,7 @@
   }
 
   static constexpr unsigned getMaxFunctionScopeDepth() {
-    return (1u << ParmVarDeclBitfields::NumScopeDepthOrObjCQualsBits) - 1;
+    return (1u << NumScopeDepthOrObjCQualsBits) - 1;
   }
 
   /// Returns the index of this parameter in its prototype or method scope.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71650.234476.patch
Type: text/x-patch
Size: 1036 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191218/0cfea919/attachment.bin>


More information about the cfe-commits mailing list