From: Fariborz Jahanian Date: Tue, 6 Nov 2012 23:25:49 +0000 (+0000) Subject: objective-C modern rewriter. Insert line numbers in X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b6933bc1d075bb8104dd33cdec7547b857c0260d;p=platform%2Fupstream%2Fllvm.git objective-C modern rewriter. Insert line numbers in translated code where new code makes the lines be off. This is wip. llvm-svn: 167500 --- diff --git a/clang/lib/Rewrite/Frontend/RewriteModernObjC.cpp b/clang/lib/Rewrite/Frontend/RewriteModernObjC.cpp index 76fe5efb..2f2f005 100644 --- a/clang/lib/Rewrite/Frontend/RewriteModernObjC.cpp +++ b/clang/lib/Rewrite/Frontend/RewriteModernObjC.cpp @@ -279,6 +279,8 @@ namespace { void RewriteRecordBody(RecordDecl *RD); void RewriteInclude(); void RewriteLineDirective(const Decl *D); + void ConvertSourceLocationToLineDirective(SourceLocation Loc, + std::string &LineString); void RewriteForwardClassDecl(DeclGroupRef D); void RewriteForwardClassDecl(const llvm::SmallVector &DG); void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, @@ -1603,6 +1605,19 @@ Stmt *RewriteModernObjC::RewriteBreakStmt(BreakStmt *S) { return 0; } +void RewriteModernObjC::ConvertSourceLocationToLineDirective( + SourceLocation Loc, + std::string &LineString) { + if (Loc.isFileID()) { + LineString += "\n#line "; + PresumedLoc PLoc = SM->getPresumedLoc(Loc); + LineString += utostr(PLoc.getLine()); + LineString += " \""; + LineString += Lexer::Stringify(PLoc.getFilename()); + LineString += "\"\n"; + } +} + /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach /// statement to continue with its inner synthesized loop. /// @@ -1665,7 +1680,10 @@ Stmt *RewriteModernObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, StringRef elementName; std::string elementTypeAsString; std::string buf; - buf = "\n{\n\t"; + // line directive first. + SourceLocation ForEachLoc = S->getForLoc(); + ConvertSourceLocationToLineDirective(ForEachLoc, buf); + buf += "{\n\t"; if (DeclStmt *DS = dyn_cast(S->getElement())) { // type elem; NamedDecl* D = cast(DS->getSingleDecl()); @@ -1903,12 +1921,14 @@ Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt(); bool noCatch = S->getNumCatchStmts() == 0; std::string buf; + SourceLocation TryLocation = S->getAtTryLoc(); + ConvertSourceLocationToLineDirective(TryLocation, buf); if (finalStmt) { if (noCatch) - buf = "{ id volatile _rethrow = 0;\n"; + buf += "{ id volatile _rethrow = 0;\n"; else { - buf = "{ id volatile _rethrow = 0;\ntry {\n"; + buf += "{ id volatile _rethrow = 0;\ntry {\n"; } } // Get the start location and compute the semi location. @@ -1935,13 +1955,15 @@ Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); if (IDecl) { std::string Result; + ConvertSourceLocationToLineDirective(Catch->getLocStart(), Result); + startBuf = SM->getCharacterData(startLoc); assert((*startBuf == '@') && "bogus @catch location"); SourceLocation rParenLoc = Catch->getRParenLoc(); const char *rParenBuf = SM->getCharacterData(rParenLoc); // _objc_exc_Foo *_e as argument to catch. - Result = "catch (_objc_exc_"; Result += IDecl->getNameAsString(); + Result += "catch (_objc_exc_"; Result += IDecl->getNameAsString(); Result += " *_"; Result += catchDecl->getNameAsString(); Result += ")"; ReplaceText(startLoc, rParenBuf-startBuf+1, Result); @@ -1967,11 +1989,18 @@ Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { } if (finalStmt) { buf.clear(); - if (noCatch) - buf = "catch (id e) {_rethrow = e;}\n"; - else - buf = "}\ncatch (id e) {_rethrow = e;}\n"; - + SourceLocation FinallyLoc = finalStmt->getLocStart(); + + if (noCatch) { + ConvertSourceLocationToLineDirective(FinallyLoc, buf); + buf += "catch (id e) {_rethrow = e;}\n"; + } + else { + buf += "}\n"; + ConvertSourceLocationToLineDirective(FinallyLoc, buf); + buf += "catch (id e) {_rethrow = e;}\n"; + } + SourceLocation startFinalLoc = finalStmt->getLocStart(); ReplaceText(startFinalLoc, 8, buf); Stmt *body = finalStmt->getFinallyBody(); @@ -3996,8 +4025,12 @@ std::string RewriteModernObjC::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(Context->getPrintingPolicy()) + " __" + - funcName.str() + "_block_func_" + utostr(i); + SourceLocation BlockLoc = CE->getExprLoc(); + std::string S; + ConvertSourceLocationToLineDirective(BlockLoc, S); + + S += "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + + funcName.str() + "_block_func_" + utostr(i); BlockDecl *BD = CE->getBlockDecl();