[cfe-dev] Fixing the bitfield enums

Ted Kremenek kremenek at apple.com
Sat Feb 23 19:55:51 PST 2008


Argiris,

I just applied this and your last patch (modulo some edits to some  
comments to wrap two lines at 80 cols):

http://llvm.org/viewvc/llvm-project?rev=47536&view=rev

Thanks so much!

Ted

On Feb 23, 2008, at 4:15 PM, Argiris Kirtzidis wrote:

> Hi,
>
> The attached patch, changes enum bitfields to "unsigned", to avoid  
> getting
> negative values because MSVC treats them as signed variables.
> Index: include/clang/AST/Decl.h
> ===================================================================
> --- include/clang/AST/Decl.h	(revision 47533)
> +++ include/clang/AST/Decl.h	(working copy)
> @@ -408,7 +408,9 @@
>     : VarDecl(ParmVar, L, Id, T, S, PrevDecl),
>     objcDeclQualifier(OBJC_TQ_None) {}
>
> -  ObjCDeclQualifier getObjCDeclQualifier() const { return  
> objcDeclQualifier; }
> +  ObjCDeclQualifier getObjCDeclQualifier() const {
> +    return ObjCDeclQualifier(objcDeclQualifier);
> +  }
>   void setObjCDeclQualifier(ObjCDeclQualifier QTVal)
>   { objcDeclQualifier = QTVal; }
>
> @@ -417,9 +419,10 @@
>   static bool classof(const ParmVarDecl *D) { return true; }
>
> private:
> +  // NOTE: VC++ treats enums as signed, avoid using the  
> ObjCDeclQualifier enum
>   /// FIXME: Also can be paced into the bitfields in Decl.
>   /// in, inout, etc.
> -  ObjCDeclQualifier objcDeclQualifier : 6;
> +  unsigned objcDeclQualifier : 6;
>
> protected:
>   /// EmitImpl - Serialize this ParmVarDecl. Called by Decl::Emit.
> @@ -474,7 +477,7 @@
>   QualType getResultType() const {
>     return cast<FunctionType>(getType())->getResultType();
>   }
> -  StorageClass getStorageClass() const { return SClass; }
> +  StorageClass getStorageClass() const { return  
> StorageClass(SClass); }
>   bool isInline() const { return IsInline; }
>
>   // Implement isa/cast/dyncast/etc.
> @@ -494,7 +497,8 @@
>   /// function.
>   ScopedDecl *DeclChain;
>
> -  StorageClass SClass : 2;
> +  // NOTE: VC++ treats enums as signed, avoid using the  
> StorageClass enum
> +  unsigned SClass : 2;
>   bool IsInline : 1;
>
> protected:
> Index: include/clang/AST/DeclObjC.h
> ===================================================================
> --- include/clang/AST/DeclObjC.h	(revision 47533)
> +++ include/clang/AST/DeclObjC.h	(working copy)
> @@ -58,11 +58,13 @@
>   bool IsInstance : 1;
>   bool IsVariadic : 1;
>
> +  // NOTE: VC++ treats enums as signed, avoid using  
> ImplementationControl enum
>   /// @required/@optional
> -  ImplementationControl DeclImplementation : 2;
> +  unsigned DeclImplementation : 2;
>
> +  // NOTE: VC++ treats enums as signed, avoid using the  
> ObjCDeclQualifier enum
>   /// in, inout, etc.
> -  ObjCDeclQualifier objcDeclQualifier : 6;
> +  unsigned objcDeclQualifier : 6;
>
>   // Context this method is declared in.
>   NamedDecl *MethodContext;
> @@ -104,7 +106,9 @@
>     MethodAttrs(M), EndLoc(endLoc), Body(0), SelfDecl(0) {}
>   virtual ~ObjCMethodDecl();
>
> -  ObjCDeclQualifier getObjCDeclQualifier() const { return  
> objcDeclQualifier; }
> +  ObjCDeclQualifier getObjCDeclQualifier() const {
> +    return ObjCDeclQualifier(objcDeclQualifier);
> +  }
>   void setObjCDeclQualifier(ObjCDeclQualifier QV)  
> { objcDeclQualifier = QV; }
>
>   // Location information, modeled after the Stmt API.
> @@ -149,7 +153,7 @@
>     DeclImplementation = ic;
>   }
>   ImplementationControl getImplementationControl() const {
> -    return DeclImplementation;
> +    return ImplementationControl(DeclImplementation);
>   }
>   Stmt *const getBody() const { return Body; }
>   void setBody(Stmt *B) { Body = B; }
> @@ -365,13 +369,14 @@
>     None, Private, Protected, Public, Package
>   };
>   void setAccessControl(AccessControl ac) { DeclAccess = ac; }
> -  AccessControl getAccessControl() const { return DeclAccess; }
> +  AccessControl getAccessControl() const { return  
> AccessControl(DeclAccess); }
>
>   // Implement isa/cast/dyncast/etc.
>   static bool classof(const Decl *D) { return D->getKind() ==  
> ObjCIvar; }
>   static bool classof(const ObjCIvarDecl *D) { return true; }
> private:
> -  AccessControl DeclAccess : 3;
> +  // NOTE: VC++ treats enums as signed, avoid using the  
> AccessControl enum
> +  unsigned DeclAccess : 3;
> };
>
>
> @@ -873,9 +878,10 @@
>   // FIXME: Property is not an ivar.
>   ObjCIvarDecl **PropertyDecls;
>   int NumPropertyDecls;
> +
> +  // NOTE: VC++ treats enums as signed, avoid using  
> PropertyAttributeKind enum
> +  unsigned PropertyAttributes : 8;
>
> -  PropertyAttributeKind PropertyAttributes : 8;
> -
>   IdentifierInfo *GetterName;    // getter name of NULL if no getter
>   IdentifierInfo *SetterName;    // setter name of NULL if no setter
>
> @@ -892,7 +898,7 @@
>   void setNumPropertyDecls(int num) { NumPropertyDecls = num; }
>
>   const PropertyAttributeKind getPropertyAttributes() const
> -    { return PropertyAttributes; }
> +    { return PropertyAttributeKind(PropertyAttributes); }
>   void setPropertyAttributes(PropertyAttributeKind PRVal) {
>     PropertyAttributes =
>     (PropertyAttributeKind) (PropertyAttributes | PRVal);
> Index: include/clang/AST/Type.h
> ===================================================================
> --- include/clang/AST/Type.h	(revision 47533)
> +++ include/clang/AST/Type.h	(working copy)
> @@ -552,8 +552,9 @@
>   /// ElementType - The element type of the array.
>   QualType ElementType;
>
> +  // NOTE: VC++ treats enums as signed, avoid using the  
> ArraySizeModifier enum
>   /// NOTE: These fields are packed into the bitfields space in the  
> Type class.
> -  ArraySizeModifier SizeModifier : 2;
> +  unsigned SizeModifier : 2;
>
>   /// IndexTypeQuals - Capture qualifiers in declarations like:
>   /// 'int X[static restrict 4]'. For function parameters only.
> @@ -566,7 +567,9 @@
>   friend class ASTContext;  // ASTContext creates these.
> public:
>   QualType getElementType() const { return ElementType; }
> -  ArraySizeModifier getSizeModifier() const { return SizeModifier; }
> +  ArraySizeModifier getSizeModifier() const {
> +    return ArraySizeModifier(SizeModifier);
> +  }
>   unsigned getIndexTypeQualifier() const { return IndexTypeQuals; }
>
>   QualType getBaseType() const {
> Index: include/clang/Basic/IdentifierTable.h
> ===================================================================
> --- include/clang/Basic/IdentifierTable.h	(revision 47533)
> +++ include/clang/Basic/IdentifierTable.h	(working copy)
> @@ -40,7 +40,8 @@
>   //       signed char and TokenKinds > 127 won't be handled  
> correctly.
>   unsigned TokenID            : 8; // Front-end token ID or  
> tok::identifier.
>   unsigned BuiltinID          : 9; // ID if this is a builtin  
> (__builtin_inf).
> -  tok::ObjCKeywordKind ObjCID : 5; // ID for objc @ keyword like  
> @'protocol'.
> +  // NOTE: VC++ treats enums as signed, avoid using  
> tok::ObjCKeywordKind enum
> +  unsigned ObjCID             : 5; // ID for objc @ keyword like  
> @'protocol'.
>   bool HasMacro               : 1; // True if there is a #define for  
> this.
>   bool IsExtension            : 1; // True if identifier is a lang  
> extension.
>   bool IsPoisoned             : 1; // True if identifier is poisoned.
> @@ -91,7 +92,9 @@
>   /// getObjCKeywordID - Return the Objective-C keyword ID for the  
> this
>   /// identifier.  For example, 'class' will return tok::objc_class  
> if ObjC is
>   /// enabled.
> -  tok::ObjCKeywordKind getObjCKeywordID() const { return ObjCID; }
> +  tok::ObjCKeywordKind getObjCKeywordID() const {
> +    return tok::ObjCKeywordKind(ObjCID);
> +  }
>   void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCID = ID; }
>
>   /// getBuiltinID - Return a value indicating whether this is a  
> builtin
> Index: include/clang/Lex/DirectoryLookup.h
> ===================================================================
> --- include/clang/Lex/DirectoryLookup.h	(revision 47533)
> +++ include/clang/Lex/DirectoryLookup.h	(working copy)
> @@ -48,9 +48,10 @@
>     const HeaderMap *Map;
>   } u;
>
> +  // NOTE: VC++ treats enums as signed, avoid using the DirType enum
>   /// DirCharacteristic - The type of directory this is, one of the  
> DirType enum
>   /// values.
> -  DirType DirCharacteristic : 2;
> +  unsigned DirCharacteristic : 2;
>
>   /// UserSupplied - True if this is a user-supplied directory.
>   ///
> @@ -110,7 +111,7 @@
>
>   /// DirCharacteristic - The type of directory this is, one of the  
> DirType enum
>   /// values.
> -  DirType getDirCharacteristic() const { return DirCharacteristic; }
> +  DirType getDirCharacteristic() const { return  
> DirType(DirCharacteristic); }
>
>   /// isUserSupplied - True if this is a user-supplied directory.
>   ///
> Index: include/clang/Lex/HeaderSearch.h
> ===================================================================
> --- include/clang/Lex/HeaderSearch.h	(revision 47533)
> +++ include/clang/Lex/HeaderSearch.h	(working copy)
> @@ -45,10 +45,11 @@
>     /// isImport - True if this is a #import'd or #pragma once file.
>     bool isImport : 1;
>
> +    // NOTE: VC++ treats enums as signed, avoid using  
> DirectoryLookup::DirType
>     /// DirInfo - Keep track of whether this is a system header, and  
> if so,
>     /// whether it is C++ clean or not.  This can be set by the  
> include paths or
>     /// by #pragma gcc system_header.
> -    DirectoryLookup::DirType DirInfo : 2;
> +    unsigned DirInfo : 2;
>
>     /// NumIncludes - This is the number of times the file has been  
> included
>     /// already.
> @@ -155,7 +156,7 @@
>   /// getFileDirFlavor - Return whether the specified file is a  
> normal header,
>   /// a system header, or a C++ friendly system header.
>   DirectoryLookup::DirType getFileDirFlavor(const FileEntry *File) {
> -    return getFileInfo(File).DirInfo;
> +    return DirectoryLookup::DirType(getFileInfo(File).DirInfo);
>   }
>
>   /// MarkFileIncludeOnce - Mark the specified file as a "once only"  
> file, e.g.
> Index: include/clang/Parse/DeclSpec.h
> ===================================================================
> --- include/clang/Parse/DeclSpec.h	(revision 47533)
> +++ include/clang/Parse/DeclSpec.h	(working copy)
> @@ -330,7 +330,7 @@
>     { objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier |  
> DQVal); }
>
>   const ObjCPropertyAttributeKind getPropertyAttributes() const
> -    { return PropertyAttributes; }
> +    { return ObjCPropertyAttributeKind(PropertyAttributes); }
>   void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) {
>     PropertyAttributes =
>       (ObjCPropertyAttributeKind) (PropertyAttributes | PRVal);
> @@ -349,7 +349,8 @@
>   // (space saving is negligible).
>   ObjCDeclQualifier objcDeclQualifier : 6;
>
> -  ObjCPropertyAttributeKind PropertyAttributes : 8;
> +  // NOTE: VC++ treats enums as signed, avoid using  
> ObjCPropertyAttributeKind
> +  unsigned PropertyAttributes : 8;
>   IdentifierInfo *GetterName;    // getter name of NULL if no getter
>   IdentifierInfo *SetterName;    // setter name of NULL if no setter
> };
> <enum-bitfields- 
> fix.zip>_______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20080223/6f39fa1c/attachment.html>


More information about the cfe-dev mailing list