objective-C modern rewriter. Insert line numbers in

translated code where new code makes the lines be off.
This is wip.

llvm-svn: 167500
This commit is contained in:
Fariborz Jahanian 2012-11-06 23:25:49 +00:00
parent 1ec8e404fc
commit b6933bc1d0
1 changed files with 44 additions and 11 deletions

View File

@ -279,6 +279,8 @@ namespace {
void RewriteRecordBody(RecordDecl *RD); void RewriteRecordBody(RecordDecl *RD);
void RewriteInclude(); void RewriteInclude();
void RewriteLineDirective(const Decl *D); void RewriteLineDirective(const Decl *D);
void ConvertSourceLocationToLineDirective(SourceLocation Loc,
std::string &LineString);
void RewriteForwardClassDecl(DeclGroupRef D); void RewriteForwardClassDecl(DeclGroupRef D);
void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG); void RewriteForwardClassDecl(const llvm::SmallVector<Decl*, 8> &DG);
void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl, void RewriteForwardClassEpilogue(ObjCInterfaceDecl *ClassDecl,
@ -1603,6 +1605,19 @@ Stmt *RewriteModernObjC::RewriteBreakStmt(BreakStmt *S) {
return 0; 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 /// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach
/// statement to continue with its inner synthesized loop. /// statement to continue with its inner synthesized loop.
/// ///
@ -1665,7 +1680,10 @@ Stmt *RewriteModernObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S,
StringRef elementName; StringRef elementName;
std::string elementTypeAsString; std::string elementTypeAsString;
std::string buf; 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())) { if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) {
// type elem; // type elem;
NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl());
@ -1903,12 +1921,14 @@ Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt(); ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt();
bool noCatch = S->getNumCatchStmts() == 0; bool noCatch = S->getNumCatchStmts() == 0;
std::string buf; std::string buf;
SourceLocation TryLocation = S->getAtTryLoc();
ConvertSourceLocationToLineDirective(TryLocation, buf);
if (finalStmt) { if (finalStmt) {
if (noCatch) if (noCatch)
buf = "{ id volatile _rethrow = 0;\n"; buf += "{ id volatile _rethrow = 0;\n";
else { else {
buf = "{ id volatile _rethrow = 0;\ntry {\n"; buf += "{ id volatile _rethrow = 0;\ntry {\n";
} }
} }
// Get the start location and compute the semi location. // Get the start location and compute the semi location.
@ -1935,13 +1955,15 @@ Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface();
if (IDecl) { if (IDecl) {
std::string Result; std::string Result;
ConvertSourceLocationToLineDirective(Catch->getLocStart(), Result);
startBuf = SM->getCharacterData(startLoc); startBuf = SM->getCharacterData(startLoc);
assert((*startBuf == '@') && "bogus @catch location"); assert((*startBuf == '@') && "bogus @catch location");
SourceLocation rParenLoc = Catch->getRParenLoc(); SourceLocation rParenLoc = Catch->getRParenLoc();
const char *rParenBuf = SM->getCharacterData(rParenLoc); const char *rParenBuf = SM->getCharacterData(rParenLoc);
// _objc_exc_Foo *_e as argument to catch. // _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 += " *_"; Result += catchDecl->getNameAsString();
Result += ")"; Result += ")";
ReplaceText(startLoc, rParenBuf-startBuf+1, Result); ReplaceText(startLoc, rParenBuf-startBuf+1, Result);
@ -1967,10 +1989,17 @@ Stmt *RewriteModernObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) {
} }
if (finalStmt) { if (finalStmt) {
buf.clear(); buf.clear();
if (noCatch) SourceLocation FinallyLoc = finalStmt->getLocStart();
buf = "catch (id e) {_rethrow = e;}\n";
else if (noCatch) {
buf = "}\ncatch (id e) {_rethrow = e;}\n"; 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(); SourceLocation startFinalLoc = finalStmt->getLocStart();
ReplaceText(startFinalLoc, 8, buf); ReplaceText(startFinalLoc, 8, buf);
@ -3996,7 +4025,11 @@ std::string RewriteModernObjC::SynthesizeBlockFunc(BlockExpr *CE, int i,
const FunctionType *AFT = CE->getFunctionType(); const FunctionType *AFT = CE->getFunctionType();
QualType RT = AFT->getResultType(); QualType RT = AFT->getResultType();
std::string StructRef = "struct " + Tag; std::string StructRef = "struct " + Tag;
std::string S = "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" + SourceLocation BlockLoc = CE->getExprLoc();
std::string S;
ConvertSourceLocationToLineDirective(BlockLoc, S);
S += "static " + RT.getAsString(Context->getPrintingPolicy()) + " __" +
funcName.str() + "_block_func_" + utostr(i); funcName.str() + "_block_func_" + utostr(i);
BlockDecl *BD = CE->getBlockDecl(); BlockDecl *BD = CE->getBlockDecl();