From: Daniel Dunbar Date: Wed, 30 Jun 2010 19:16:53 +0000 (+0000) Subject: Rewriter: Use the appropriate printing context instead of the default X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fa297fb29b38991c537a0ae90ff595102dcd21a9;p=clang Rewriter: Use the appropriate printing context instead of the default constructed one -- this is necessary to ensure types get printed correctly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@107312 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Rewrite/RewriteObjC.cpp b/lib/Rewrite/RewriteObjC.cpp index 65ebf227e8..d6fe4e3b28 100644 --- a/lib/Rewrite/RewriteObjC.cpp +++ b/lib/Rewrite/RewriteObjC.cpp @@ -268,6 +268,8 @@ namespace { void RewriteMethodDeclaration(ObjCMethodDecl *Method); void RewriteProperty(ObjCPropertyDecl *prop); void RewriteFunctionDecl(FunctionDecl *FD); + void RewriteBlockPointerType(std::string& Str, QualType Type); + void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD); void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); void RewriteTypeOfDecl(VarDecl *VD); @@ -835,11 +837,12 @@ void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, Getr += ")"; // close the precedence "scope" for "*". // Now, emit the argument types (if any). - if (const FunctionProtoType *FT = dyn_cast(FPRetType)) { + if (const FunctionProtoType *FT = dyn_cast(FPRetType)){ Getr += "("; for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { if (i) Getr += ", "; - std::string ParamStr = FT->getArgType(i).getAsString(); + std::string ParamStr = FT->getArgType(i).getAsString( + Context->PrintingPolicy); Getr += ParamStr; } if (FT->isVariadic()) { @@ -1047,11 +1050,12 @@ void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, else if (const BlockPointerType *BPT = retType->getAs()) PointeeTy = BPT->getPointeeType(); if ((FPRetType = PointeeTy->getAs())) { - ResultStr += FPRetType->getResultType().getAsString(); + ResultStr += FPRetType->getResultType().getAsString( + Context->PrintingPolicy); ResultStr += "(*"; } } else - ResultStr += T.getAsString(); + ResultStr += T.getAsString(Context->PrintingPolicy); } void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, @@ -1107,10 +1111,11 @@ void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, ResultStr += " *"; } else - ResultStr += Context->getObjCClassType().getAsString(); + ResultStr += Context->getObjCClassType().getAsString( + Context->PrintingPolicy); ResultStr += " self, "; - ResultStr += Context->getObjCSelType().getAsString(); + ResultStr += Context->getObjCSelType().getAsString(Context->PrintingPolicy); ResultStr += " _cmd"; // Method arguments. @@ -1144,7 +1149,8 @@ void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, ResultStr += "("; for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { if (i) ResultStr += ", "; - std::string ParamStr = FT->getArgType(i).getAsString(); + std::string ParamStr = FT->getArgType(i).getAsString( + Context->PrintingPolicy); ResultStr += ParamStr; } if (FT->isVariadic()) { @@ -1560,7 +1566,7 @@ Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, // Simply use 'id' for all qualified types. elementTypeAsString = "id"; else - elementTypeAsString = ElementType.getAsString(); + elementTypeAsString = ElementType.getAsString(Context->PrintingPolicy); buf += elementTypeAsString; buf += " "; elementName = D->getNameAsCString(); @@ -1576,7 +1582,7 @@ Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, // Simply use 'id' for all qualified types. elementTypeAsString = "id"; else - elementTypeAsString = VD->getType().getAsString(); + elementTypeAsString = VD->getType().getAsString(Context->PrintingPolicy); } // struct __objcFastEnumerationState enumState = { 0 }; @@ -2275,7 +2281,7 @@ void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { } // FIXME. This will not work for multiple declarators; as in: // __typeof__(a) b,c,d; - std::string TypeAsString(QT.getAsString()); + std::string TypeAsString(QT.getAsString(Context->PrintingPolicy)); SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); const char *startBuf = SM->getCharacterData(DeclLoc); if (ND->getInit()) { @@ -2326,8 +2332,8 @@ void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { RewriteObjCQualifiedInterfaceTypes(FD); } -static void RewriteBlockPointerType(std::string& Str, QualType Type) { - std::string TypeString(Type.getAsString()); +void RewriteObjC::RewriteBlockPointerType(std::string& Str, QualType Type) { + std::string TypeString(Type.getAsString(Context->PrintingPolicy)); const char *argPtr = TypeString.c_str(); if (!strchr(argPtr, '^')) { Str += TypeString; @@ -2340,9 +2346,10 @@ static void RewriteBlockPointerType(std::string& Str, QualType Type) { } // FIXME. Consolidate this routine with RewriteBlockPointerType. -static void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD) { +void RewriteObjC::RewriteBlockPointerTypeVariable(std::string& Str, + ValueDecl *VD) { QualType Type = VD->getType(); - std::string TypeString(Type.getAsString()); + std::string TypeString(Type.getAsString(Context->PrintingPolicy)); const char *argPtr = TypeString.c_str(); int paren = 0; while (*argPtr) { @@ -2376,7 +2383,7 @@ void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { if (!proto) return; QualType Type = proto->getResultType(); - std::string FdStr = Type.getAsString(); + std::string FdStr = Type.getAsString(Context->PrintingPolicy); FdStr += " "; FdStr += FD->getNameAsCString(); FdStr += "("; @@ -4099,7 +4106,7 @@ std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, const FunctionType *AFT = CE->getFunctionType(); QualType RT = AFT->getResultType(); std::string StructRef = "struct " + Tag; - std::string S = "static " + RT.getAsString() + " __" + + std::string S = "static " + RT.getAsString(Context->PrintingPolicy) + " __" + funcName + "_" + "block_func_" + utostr(i); BlockDecl *BD = CE->getBlockDecl(); diff --git a/test/Rewriter/rewrite-elaborated-type.mm b/test/Rewriter/rewrite-elaborated-type.mm index 2c139647a9..9867b4d6de 100644 --- a/test/Rewriter/rewrite-elaborated-type.mm +++ b/test/Rewriter/rewrite-elaborated-type.mm @@ -2,6 +2,7 @@ // RUN: %clang_cc1 -fsyntax-only -Wno-address-of-temporary -D_Bool=bool -D"id=void*" -D"SEL=void*" -D"__declspec(X)=" %t-rw.cpp // radar 8143056 +typedef struct objc_class *Class; typedef unsigned NSPointerFunctionsOptions; extern "C" id NSClassFromObject(id object); void *sel_registerName(const char *); @@ -28,3 +29,8 @@ struct NSSlice { } @end +@implementation I1 ++ (struct s1 *) f0 { + return 0; +} +@end