]> granicus.if.org Git - clang/commitdiff
Fix a rewriting crash and correct rewriting of __block
authorFariborz Jahanian <fjahanian@apple.com>
Sat, 16 Jan 2010 19:36:43 +0000 (19:36 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Sat, 16 Jan 2010 19:36:43 +0000 (19:36 +0000)
declaration where its initializer has a type-cast.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93650 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Frontend/RewriteObjC.cpp
test/Rewriter/rewrite-byref-vars.mm

index 44a770a3edf6513e90996e0aad9308df60b62302..68c23c708aa01d650321ba5aff8649f8f2f86e07 100644 (file)
@@ -4536,8 +4536,13 @@ void RewriteObjC::RewriteByRefVar(VarDecl *ND) {
   ByrefType += " " + Name + ";\n";
   ByrefType += "};\n";
   // Insert this type in global scope. It is needed by helper function.
-  assert(CurFunctionDef && "RewriteByRefVar - CurFunctionDef is null");
-  SourceLocation FunLocStart = CurFunctionDef->getTypeSpecStartLoc();
+  SourceLocation FunLocStart;
+  if (CurFunctionDef)
+     FunLocStart = CurFunctionDef->getTypeSpecStartLoc();
+  else {
+    assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null");
+    FunLocStart = CurMethodDef->getLocStart();
+  }
   InsertText(FunLocStart, ByrefType.c_str(), ByrefType.size());
   if (Ty.isObjCGCWeak()) {
     flag |= BLOCK_FIELD_IS_WEAK;
@@ -4587,12 +4592,17 @@ void RewriteObjC::RewriteByRefVar(VarDecl *ND) {
                 ByrefType.c_str(), ByrefType.size());
   }
   else {
-    SourceLocation startLoc = ND->getInit()->getLocStart();
+    SourceLocation startLoc;
+    Expr *E = ND->getInit();
+    if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E))
+      startLoc = ECE->getLParenLoc();
+    else
+      startLoc = E->getLocStart();
     startLoc = SM->getInstantiationLoc(startLoc);
+    endBuf = SM->getCharacterData(startLoc);
+   
     ByrefType += " " + Name;
-    ReplaceText(DeclLoc, endBuf-startBuf, 
-                ByrefType.c_str(), ByrefType.size());
-    ByrefType = " = {(void*)";
+    ByrefType += " = {(void*)";
     ByrefType += utostr(isa);
     ByrefType += ", &" + Name + ", ";
     ByrefType += utostr(flags);
@@ -4607,7 +4617,8 @@ void RewriteObjC::RewriteByRefVar(VarDecl *ND) {
       ByrefType += utostr(flag);
       ByrefType += ", ";
     }
-    InsertText(startLoc, ByrefType.c_str(), ByrefType.size());
+    ReplaceText(DeclLoc, endBuf-startBuf, 
+                ByrefType.c_str(), ByrefType.size());
     
     // Complete the newly synthesized compound expression by inserting a right
     // curly brace before the end of the declaration.
index 581437b5a5df9e6f61b228d18a4078bf122aa219..1489c5947293ebf26587ce90a43c604028621486 100644 (file)
@@ -31,5 +31,16 @@ __declspec(dllexport) extern "C" __declspec(dllexport) void XXXXBreakTheRewriter
 
 }
 
+@interface I
+{
+   id list;
+}
+- (void) Meth;
+@end
+
+@implementation I
+- (void) Meth { __attribute__((__blocks__(byref))) void ** listp = (void **)list; }
+@end
+
 // $CLANG -cc1 -fms-extensions -rewrite-objc -x objective-c++ -fblocks bug.mm
 // g++ -c -D"__declspec(X)=" bug.cpp