[cfe-commits] r143698 - in /cfe/trunk: include/clang/ARCMigrate/ARCMT.h lib/ARCMigrate/ARCMT.cpp lib/ARCMigrate/CMakeLists.txt lib/ARCMigrate/Internals.h lib/ARCMigrate/TransGCCalls.cpp lib/ARCMigrate/Transforms.cpp lib/ARCMigrate/Transforms.h test/ARCMT/Common.h test/ARCMT/GC.m test/ARCMT/GC.m.result tools/arcmt-test/arcmt-test.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Nov 4 08:58:08 PDT 2011


Author: akirtzidis
Date: Fri Nov  4 10:58:08 2011
New Revision: 143698

URL: http://llvm.org/viewvc/llvm-project?rev=143698&view=rev
Log:
[arcmt] In GC, transform NSMakeCollectable to CFBridgingRelease.

Added:
    cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp
    cfe/trunk/test/ARCMT/GC.m
    cfe/trunk/test/ARCMT/GC.m.result
Modified:
    cfe/trunk/include/clang/ARCMigrate/ARCMT.h
    cfe/trunk/lib/ARCMigrate/ARCMT.cpp
    cfe/trunk/lib/ARCMigrate/CMakeLists.txt
    cfe/trunk/lib/ARCMigrate/Internals.h
    cfe/trunk/lib/ARCMigrate/Transforms.cpp
    cfe/trunk/lib/ARCMigrate/Transforms.h
    cfe/trunk/test/ARCMT/Common.h
    cfe/trunk/tools/arcmt-test/arcmt-test.cpp

Modified: cfe/trunk/include/clang/ARCMigrate/ARCMT.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ARCMigrate/ARCMT.h?rev=143698&r1=143697&r2=143698&view=diff
==============================================================================
--- cfe/trunk/include/clang/ARCMigrate/ARCMT.h (original)
+++ cfe/trunk/include/clang/ARCMigrate/ARCMT.h Fri Nov  4 10:58:08 2011
@@ -78,7 +78,7 @@
 
 typedef void (*TransformFn)(MigrationPass &pass);
 
-std::vector<TransformFn> getAllTransformations();
+std::vector<TransformFn> getAllTransformations(LangOptions::GCMode OrigGCMode);
 
 class MigrationProcess {
   CompilerInvocation OrigCI;

Modified: cfe/trunk/lib/ARCMigrate/ARCMT.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/ARCMT.cpp?rev=143698&r1=143697&r2=143698&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/ARCMT.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/ARCMT.cpp Fri Nov  4 10:58:08 2011
@@ -192,6 +192,7 @@
   define += '=';
   CInvok->getPreprocessorOpts().addMacroDef(define);
   CInvok->getLangOpts().ObjCAutoRefCount = true;
+  CInvok->getLangOpts().setGC(LangOptions::NonGC);
   CInvok->getDiagnosticOpts().ErrorLimit = 0;
   CInvok->getDiagnosticOpts().Warnings.push_back(
                                             "error=arc-unsafe-retained-assign");
@@ -226,7 +227,9 @@
   if (!origCI.getLangOpts().ObjC1)
     return false;
 
-  std::vector<TransformFn> transforms = arcmt::getAllTransformations();
+  LangOptions::GCMode OrigGCMode = origCI.getLangOpts().getGC();
+
+  std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode);
   assert(!transforms.empty());
 
   llvm::OwningPtr<CompilerInvocation> CInvok;
@@ -287,7 +290,7 @@
   std::vector<SourceLocation> ARCMTMacroLocs;
 
   TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
-  MigrationPass pass(Ctx, Unit->getSema(), testAct, ARCMTMacroLocs);
+  MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, ARCMTMacroLocs);
 
   for (unsigned i=0, e = transforms.size(); i != e; ++i)
     transforms[i](pass);
@@ -316,6 +319,8 @@
   if (!origCI.getLangOpts().ObjC1)
     return false;
 
+  LangOptions::GCMode OrigGCMode = origCI.getLangOpts().getGC();
+
   // Make sure checking is successful first.
   CompilerInvocation CInvokForCheck(origCI);
   if (arcmt::checkForManualIssues(CInvokForCheck, Filename, Kind, DiagClient,
@@ -328,7 +333,7 @@
   
   MigrationProcess migration(CInvok, DiagClient, outputDir);
 
-  std::vector<TransformFn> transforms = arcmt::getAllTransformations();
+  std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode);
   assert(!transforms.empty());
 
   for (unsigned i=0, e = transforms.size(); i != e; ++i) {
@@ -537,7 +542,8 @@
 
   Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions());
   TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor());
-  MigrationPass pass(Ctx, Unit->getSema(), TA, ARCMTMacroLocs);
+  MigrationPass pass(Ctx, OrigCI.getLangOpts().getGC(),
+                     Unit->getSema(), TA, ARCMTMacroLocs);
 
   trans(pass);
 

Modified: cfe/trunk/lib/ARCMigrate/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/CMakeLists.txt?rev=143698&r1=143697&r2=143698&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/CMakeLists.txt (original)
+++ cfe/trunk/lib/ARCMigrate/CMakeLists.txt Fri Nov  4 10:58:08 2011
@@ -12,6 +12,7 @@
   TransEmptyStatementsAndDealloc.cpp
   TransformActions.cpp
   Transforms.cpp
+  TransGCCalls.cpp
   TransProperties.cpp
   TransRetainReleaseDealloc.cpp
   TransUnbridgedCasts.cpp

Modified: cfe/trunk/lib/ARCMigrate/Internals.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Internals.h?rev=143698&r1=143697&r2=143698&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Internals.h (original)
+++ cfe/trunk/lib/ARCMigrate/Internals.h Fri Nov  4 10:58:08 2011
@@ -137,13 +137,18 @@
 class MigrationPass {
 public:
   ASTContext &Ctx;
+  LangOptions::GCMode OrigGCMode;
   Sema &SemaRef;
   TransformActions &TA;
   std::vector<SourceLocation> &ARCMTMacroLocs;
 
-  MigrationPass(ASTContext &Ctx, Sema &sema, TransformActions &TA,
+  MigrationPass(ASTContext &Ctx, LangOptions::GCMode OrigGCMode,
+                Sema &sema, TransformActions &TA,
                 std::vector<SourceLocation> &ARCMTMacroLocs)
-    : Ctx(Ctx), SemaRef(sema), TA(TA), ARCMTMacroLocs(ARCMTMacroLocs) { }
+    : Ctx(Ctx), OrigGCMode(OrigGCMode), SemaRef(sema), TA(TA),
+      ARCMTMacroLocs(ARCMTMacroLocs) { }
+
+  bool isGCMigration() const { return OrigGCMode != LangOptions::NonGC; }
 };
 
 static inline StringRef getARCMTMacroName() {

Added: cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp?rev=143698&view=auto
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp (added)
+++ cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp Fri Nov  4 10:58:08 2011
@@ -0,0 +1,59 @@
+//===--- TransGCCalls.cpp - Tranformations to ARC mode --------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Transforms.h"
+#include "Internals.h"
+#include "clang/Sema/SemaDiagnostic.h"
+
+using namespace clang;
+using namespace arcmt;
+using namespace trans;
+
+namespace {
+
+class GCCollectableCallsChecker :
+                         public RecursiveASTVisitor<GCCollectableCallsChecker> {
+  MigrationContext &MigrateCtx;
+  ParentMap &PMap;
+  IdentifierInfo *NSMakeCollectableII;
+
+public:
+  GCCollectableCallsChecker(MigrationContext &ctx, ParentMap &map)
+    : MigrateCtx(ctx), PMap(map) {
+    NSMakeCollectableII =
+        &MigrateCtx.getPass().Ctx.Idents.get("NSMakeCollectable");
+  }
+
+  bool VisitCallExpr(CallExpr *E) {
+    Expr *CEE = E->getCallee()->IgnoreParenImpCasts();
+    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) {
+      if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl())) {
+        if (FD->getDeclContext()->getRedeclContext()->isFileContext() &&
+            FD->getIdentifier() == NSMakeCollectableII) {
+          TransformActions &TA = MigrateCtx.getPass().TA;
+          Transaction Trans(TA);
+          TA.clearDiagnostic(diag::err_unavailable,
+                             diag::err_unavailable_message,
+                             DRE->getSourceRange());
+          TA.replace(DRE->getSourceRange(), "CFBridgingRelease");
+        }
+      }
+    }
+
+    return true;
+  }
+};
+
+} // anonymous namespace
+
+void GCCollectableCallsTraverser::traverseBody(BodyContext &BodyCtx) {
+  GCCollectableCallsChecker(BodyCtx.getMigrationContext(),
+                            BodyCtx.getParentMap())
+                                            .TraverseStmt(BodyCtx.getTopStmt());
+}

Modified: cfe/trunk/lib/ARCMigrate/Transforms.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Transforms.cpp?rev=143698&r1=143697&r2=143698&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Transforms.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/Transforms.cpp Fri Nov  4 10:58:08 2011
@@ -12,7 +12,6 @@
 #include "clang/Sema/SemaDiagnostic.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/AST/StmtVisitor.h"
-#include "clang/AST/ParentMap.h"
 #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Basic/SourceManager.h"
@@ -24,6 +23,8 @@
 using namespace arcmt;
 using namespace trans;
 
+ASTTraverser::~ASTTraverser() { }
+
 //===----------------------------------------------------------------------===//
 // Helpers.
 //===----------------------------------------------------------------------===//
@@ -290,9 +291,57 @@
 }
 
 //===----------------------------------------------------------------------===//
+// MigrationContext
+//===----------------------------------------------------------------------===//
+
+namespace {
+
+class ASTTransform : public RecursiveASTVisitor<ASTTransform> {
+  MigrationContext &MigrateCtx;
+
+public:
+  ASTTransform(MigrationContext &MigrateCtx) : MigrateCtx(MigrateCtx) { }
+
+  bool TraverseStmt(Stmt *rootS) {
+    if (!rootS)
+      return true;
+
+    BodyContext BodyCtx(MigrateCtx, rootS);
+    for (MigrationContext::traverser_iterator
+           I = MigrateCtx.traversers_begin(),
+           E = MigrateCtx.traversers_end(); I != E; ++I)
+      (*I)->traverseBody(BodyCtx);
+
+    return true;
+  }
+};
+
+}
+
+MigrationContext::~MigrationContext() {
+  for (traverser_iterator
+         I = traversers_begin(), E = traversers_end(); I != E; ++I)
+    delete *I;
+}
+
+void MigrationContext::traverse(TranslationUnitDecl *TU) {
+  ASTTransform(*this).TraverseDecl(TU);
+}
+
+//===----------------------------------------------------------------------===//
 // getAllTransformations.
 //===----------------------------------------------------------------------===//
 
+static void traverseAST(MigrationPass &pass) {
+  MigrationContext MigrateCtx(pass);
+
+  if (pass.isGCMigration()) {
+    MigrateCtx.addTraverser(new GCCollectableCallsTraverser);
+  }
+
+  MigrateCtx.traverse(pass.Ctx.getTranslationUnitDecl());
+}
+
 static void independentTransforms(MigrationPass &pass) {
   rewriteAutoreleasePool(pass);
   rewriteProperties(pass);
@@ -303,9 +352,11 @@
   rewriteUnbridgedCasts(pass);
   rewriteBlockObjCVariable(pass);
   checkAPIUses(pass);
+  traverseAST(pass);
 }
 
-std::vector<TransformFn> arcmt::getAllTransformations() {
+std::vector<TransformFn> arcmt::getAllTransformations(
+                                               LangOptions::GCMode OrigGCMode) {
   std::vector<TransformFn> transforms;
 
   transforms.push_back(independentTransforms);

Modified: cfe/trunk/lib/ARCMigrate/Transforms.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Transforms.h?rev=143698&r1=143697&r2=143698&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Transforms.h (original)
+++ cfe/trunk/lib/ARCMigrate/Transforms.h Fri Nov  4 10:58:08 2011
@@ -11,6 +11,7 @@
 #define LLVM_CLANG_LIB_ARCMIGRATE_TRANSFORMS_H
 
 #include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/ParentMap.h"
 #include "llvm/ADT/DenseSet.h"
 
 namespace clang {
@@ -25,6 +26,8 @@
 
 namespace trans {
 
+  class MigrationContext;
+
 //===----------------------------------------------------------------------===//
 // Transformations.
 //===----------------------------------------------------------------------===//
@@ -41,6 +44,54 @@
 
 void removeEmptyStatementsAndDealloc(MigrationPass &pass);
 
+class BodyContext {
+  MigrationContext &MigrateCtx;
+  ParentMap PMap;
+  Stmt *TopStmt;
+
+public:
+  BodyContext(MigrationContext &MigrateCtx, Stmt *S)
+    : MigrateCtx(MigrateCtx), PMap(S), TopStmt(S) {}
+
+  MigrationContext &getMigrationContext() { return MigrateCtx; }
+  ParentMap &getParentMap() { return PMap; }
+  Stmt *getTopStmt() { return TopStmt; }
+};
+
+class ASTTraverser {
+public:
+  virtual ~ASTTraverser();
+  virtual void traverseBody(BodyContext &BodyCtx) { }
+};
+
+class MigrationContext {
+  MigrationPass &Pass;
+  std::vector<ASTTraverser *> Traversers;
+
+public:
+  explicit MigrationContext(MigrationPass &pass) : Pass(pass) {}
+  ~MigrationContext();
+
+  MigrationPass &getPass() { return Pass; }
+  
+  typedef std::vector<ASTTraverser *>::iterator traverser_iterator;
+  traverser_iterator traversers_begin() { return Traversers.begin(); }
+  traverser_iterator traversers_end() { return Traversers.end(); }
+
+  void addTraverser(ASTTraverser *traverser) {
+    Traversers.push_back(traverser);
+  }
+
+  void traverse(TranslationUnitDecl *TU);
+};
+
+// GC transformations
+
+class GCCollectableCallsTraverser : public ASTTraverser {
+public:
+  virtual void traverseBody(BodyContext &BodyCtx);
+};
+
 //===----------------------------------------------------------------------===//
 // Helpers.
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/test/ARCMT/Common.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/Common.h?rev=143698&r1=143697&r2=143698&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/Common.h (original)
+++ cfe/trunk/test/ARCMT/Common.h Fri Nov  4 10:58:08 2011
@@ -4,8 +4,10 @@
 #define NS_AUTOMATED_REFCOUNT_UNAVAILABLE
 #endif
 
+#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
 #define CF_CONSUMED __attribute__((cf_consumed))
 
+#define NS_INLINE static __inline__ __attribute__((always_inline))
 #define nil ((void*) 0)
 
 typedef int BOOL;
@@ -19,6 +21,9 @@
 
 typedef const void * CFTypeRef;
 CFTypeRef CFRetain(CFTypeRef cf);
+id CFBridgingRelease(CFTypeRef CF_CONSUMED X);
+
+NS_INLINE NS_RETURNS_RETAINED id NSMakeCollectable(CFTypeRef CF_CONSUMED cf) NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
 
 @protocol NSObject
 - (BOOL)isEqual:(id)object;

Added: cfe/trunk/test/ARCMT/GC.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/GC.m?rev=143698&view=auto
==============================================================================
--- cfe/trunk/test/ARCMT/GC.m (added)
+++ cfe/trunk/test/ARCMT/GC.m Fri Nov  4 10:58:08 2011
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-arc -x objective-c %s.result
+// RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-gc-only -x objective-c %s > %t
+// RUN: diff %t %s.result
+
+#include "Common.h"
+
+void test1(CFTypeRef *cft) {
+  id x = NSMakeCollectable(cft);
+}

Added: cfe/trunk/test/ARCMT/GC.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/GC.m.result?rev=143698&view=auto
==============================================================================
--- cfe/trunk/test/ARCMT/GC.m.result (added)
+++ cfe/trunk/test/ARCMT/GC.m.result Fri Nov  4 10:58:08 2011
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-arc -x objective-c %s.result
+// RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-gc-only -x objective-c %s > %t
+// RUN: diff %t %s.result
+
+#include "Common.h"
+
+void test1(CFTypeRef *cft) {
+  id x = CFBridgingRelease(cft);
+}

Modified: cfe/trunk/tools/arcmt-test/arcmt-test.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/arcmt-test/arcmt-test.cpp?rev=143698&r1=143697&r2=143698&view=diff
==============================================================================
--- cfe/trunk/tools/arcmt-test/arcmt-test.cpp (original)
+++ cfe/trunk/tools/arcmt-test/arcmt-test.cpp Fri Nov  4 10:58:08 2011
@@ -172,7 +172,8 @@
 
   MigrationProcess migration(origCI, DiagClient);
 
-  std::vector<TransformFn> transforms = arcmt::getAllTransformations();
+  std::vector<TransformFn>
+    transforms = arcmt::getAllTransformations(origCI.getLangOpts().getGC());
   assert(!transforms.empty());
 
   llvm::OwningPtr<PrintTransforms> transformPrinter;





More information about the cfe-commits mailing list