[cfe-commits] r167500 - /cfe/trunk/lib/Rewrite/Frontend/RewriteModernObjC.cpp

Fariborz Jahanian fjahanian at apple.com
Tue Nov 6 15:25:49 PST 2012


Author: fjahanian
Date: Tue Nov  6 17:25:49 2012
New Revision: 167500

URL: http://llvm.org/viewvc/llvm-project?rev=167500&view=rev
Log:
objective-C modern rewriter. Insert line numbers in
translated code where new code makes the lines be off.
This is wip.

Modified:
    cfe/trunk/lib/Rewrite/Frontend/RewriteModernObjC.cpp

Modified: cfe/trunk/lib/Rewrite/Frontend/RewriteModernObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/Frontend/RewriteModernObjC.cpp?rev=167500&r1=167499&r2=167500&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/Frontend/RewriteModernObjC.cpp (original)
+++ cfe/trunk/lib/Rewrite/Frontend/RewriteModernObjC.cpp Tue Nov  6 17:25:49 2012
@@ -279,6 +279,8 @@
     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 @@
   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 @@
   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 @@
   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 @@
         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 @@
   }
   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 @@
   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();
 





More information about the cfe-commits mailing list