diff options
author | Fariborz Jahanian <fjahanian@apple.com> | 2012-11-06 23:25:49 +0000 |
---|---|---|
committer | Fariborz Jahanian <fjahanian@apple.com> | 2012-11-06 23:25:49 +0000 |
commit | f616ae2d1356dadde26ff553eb689e5455d65808 (patch) | |
tree | 7fdcc65b9e62acf4a92bca9b6144969815070331 | |
parent | ba529a9bf9acbfc718970a030017b73715108b8e (diff) |
objective-C modern rewriter. Insert line numbers in
translated code where new code makes the lines be off.
This is wip.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@167500 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Rewrite/Frontend/RewriteModernObjC.cpp | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/lib/Rewrite/Frontend/RewriteModernObjC.cpp b/lib/Rewrite/Frontend/RewriteModernObjC.cpp index 76fe5efb22..2f2f005db9 100644 --- a/lib/Rewrite/Frontend/RewriteModernObjC.cpp +++ b/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<Decl*, 8> &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<DeclStmt>(S->getElement())) { // type elem; NamedDecl* D = cast<NamedDecl>(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(); |