From: Fariborz Jahanian Date: Wed, 9 Jan 2008 18:15:42 +0000 (+0000) Subject: Type-cast RHS of assignment to prevent warning compiling rewritten foreach code. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=88f50f31d84185827a8f765dd611134bcaface7b;p=clang Type-cast RHS of assignment to prevent warning compiling rewritten foreach code. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45777 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index 1ba0af5ae3..81f818a67b 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -808,7 +808,7 @@ void RewriteTest::SynthCountByEnumWithState(std::string &buf) { /// do { /// if (startMutations != *enumState.mutationsPtr) /// objc_enumerationMutation(l_collection); -/// elem = enumState.itemsPtr[counter++]; +/// elem = (type)enumState.itemsPtr[counter++]; /// stmts; /// } while (counter < limit); /// } while (limit = [l_collection countByEnumeratingWithState:&enumState @@ -826,19 +826,23 @@ Stmt *RewriteTest::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S) { const char *startBuf = SM->getCharacterData(startLoc); const char *startCollectionBuf = SM->getCharacterData(collectionLoc); const char *elementName; + std::string elementTypeAsString; std::string buf; buf = "\n{\n\t"; if (DeclStmt *DS = dyn_cast(S->getElement())) { // type elem; QualType ElementType = cast(DS->getDecl())->getType(); - buf += ElementType.getAsString(); + elementTypeAsString = ElementType.getAsString(); + buf += elementTypeAsString; buf += " "; elementName = DS->getDecl()->getName(); buf += elementName; buf += ";\n\t"; } - else if (DeclRefExpr *DR = dyn_cast(S->getElement())) + else if (DeclRefExpr *DR = dyn_cast(S->getElement())) { elementName = DR->getDecl()->getName(); + elementTypeAsString = DR->getDecl()->getType().getAsString(); + } else assert(false && "RewriteObjCForCollectionStmt - bad element kind"); @@ -879,7 +883,7 @@ Stmt *RewriteTest::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S) { /// do { /// if (startMutations != *enumState.mutationsPtr) /// objc_enumerationMutation(l_collection); - /// elem = enumState.itemsPtr[counter++]; + /// elem = (type)enumState.itemsPtr[counter++]; buf += "if (limit) {\n\t"; buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; buf += "do {\n\t\t"; @@ -888,7 +892,9 @@ Stmt *RewriteTest::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S) { buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; buf += elementName; - buf += " = enumState.itemsPtr[counter++];"; + buf += " = ("; + buf += elementTypeAsString; + buf += ")enumState.itemsPtr[counter++];"; // Replace ')' in for '(' type elem in collection ')' with all of these. Rewrite.ReplaceText(lparenLoc, 1, buf.c_str(), buf.size()); diff --git a/test/Sema/rewrite-foreach-3.m b/test/Sema/rewrite-foreach-3.m new file mode 100644 index 0000000000..a96a8600ea --- /dev/null +++ b/test/Sema/rewrite-foreach-3.m @@ -0,0 +1,29 @@ +// RUN: clang -rewrite-test %s + +@protocol P @end + +@interface MyList +@end + +@implementation MyList +- (unsigned int)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state objects: (id *)items count:(unsigned int)stackcount +{ + return 0; +} +@end + +@interface MyList (BasicTest) +- (void)compilerTestAgainst; +@end + +int LOOP(); +@implementation MyList (BasicTest) +- (void)compilerTestAgainst { + MyList * el; + for (el in self) + { LOOP(); } + for (MyList * el1 in self) + LOOP(); +} +@end +