ObjectiveC migrator: In deciding NS_OPTION over

NS_ENUM, at least one power of 2 enumerator 
must be greater than two.

llvm-svn: 188470
This commit is contained in:
Fariborz Jahanian 2013-08-15 18:46:37 +00:00
parent 2851907cdb
commit be7bc11f94
3 changed files with 20 additions and 3 deletions

View File

@ -493,6 +493,7 @@ static bool rewriteToNSMacroDecl(const EnumDecl *EnumDcl,
static bool UseNSOptionsMacro(ASTContext &Ctx, static bool UseNSOptionsMacro(ASTContext &Ctx,
const EnumDecl *EnumDcl) { const EnumDecl *EnumDcl) {
bool PowerOfTwo = true; bool PowerOfTwo = true;
uint64_t MaxPowerOfTwoVal = 0;
for (EnumDecl::enumerator_iterator EI = EnumDcl->enumerator_begin(), for (EnumDecl::enumerator_iterator EI = EnumDcl->enumerator_begin(),
EE = EnumDcl->enumerator_end(); EI != EE; ++EI) { EE = EnumDcl->enumerator_end(); EI != EE; ++EI) {
EnumConstantDecl *Enumerator = (*EI); EnumConstantDecl *Enumerator = (*EI);
@ -507,10 +508,14 @@ static bool UseNSOptionsMacro(ASTContext &Ctx,
return true; return true;
uint64_t EnumVal = Enumerator->getInitVal().getZExtValue(); uint64_t EnumVal = Enumerator->getInitVal().getZExtValue();
if (PowerOfTwo && EnumVal && !llvm::isPowerOf2_64(EnumVal)) if (PowerOfTwo && EnumVal) {
if (!llvm::isPowerOf2_64(EnumVal))
PowerOfTwo = false; PowerOfTwo = false;
else if (EnumVal > MaxPowerOfTwoVal)
MaxPowerOfTwoVal = EnumVal;
} }
return PowerOfTwo; }
return PowerOfTwo ? ((MaxPowerOfTwoVal > 2) ? true : false) : false;
} }
void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx, void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx,

View File

@ -64,3 +64,9 @@ enum {
UNTwo UNTwo
}; };
// Should use NS_ENUM even though it is all power of 2.
enum {
UIKOne = 0x1,
UIKTwo = 0x2,
};
typedef NSInteger UIK;

View File

@ -64,3 +64,9 @@ enum {
UNTwo UNTwo
}; };
// Should use NS_ENUM even though it is all power of 2.
typedef NS_ENUM(NSInteger, UIK) {
UIKOne = 0x1,
UIKTwo = 0x2,
};