[cfe-dev] How to add a new rewriting option

Vincent R. forumer at smartmobili.com
Sun Jul 28 06:44:28 PDT 2013


I have attached my latest patch(clang 3.3) below and I am wondering if 
it wouldn't be easier to only modify the
RewriteObjc.cpp instead of trying to add a new rewriting option ...




Index: tools/clang/lib/Frontend/InitPreprocessor.cpp
===================================================================
--- tools/clang/lib/Frontend/InitPreprocessor.cpp	(revision 186145)
+++ tools/clang/lib/Frontend/InitPreprocessor.cpp	(working copy)
@@ -624,7 +624,8 @@
    else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
      Builder.defineMacro("__SSP_ALL__", "2");

-  if (FEOpts.ProgramAction == frontend::RewriteObjC)
+  if ((FEOpts.ProgramAction == frontend::RewriteObjC) ||
+      (FEOpts.ProgramAction == frontend::RewriteObjC2Cs))
      Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");

    // Define a macro that exists only when using the static analyzer.
Index: tools/clang/lib/Frontend/CompilerInstance.cpp
===================================================================
--- tools/clang/lib/Frontend/CompilerInstance.cpp	(revision 186145)
+++ tools/clang/lib/Frontend/CompilerInstance.cpp	(working copy)
@@ -661,7 +661,8 @@
    getTarget().setForcedLangOptions(getLangOpts());

    // rewriter project will change target built-in bool type from its 
default.
-  if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
+  if ((getFrontendOpts().ProgramAction == frontend::RewriteObjC) ||
+      (getFrontendOpts().ProgramAction == frontend::RewriteObjC2Cs))
      getTarget().noSignedCharForObjCBool();

    // Validate/process some options.
Index: tools/clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- tools/clang/lib/Frontend/CompilerInvocation.cpp	(revision 186145)
+++ tools/clang/lib/Frontend/CompilerInvocation.cpp	(working copy)
@@ -661,6 +661,8 @@
        Opts.ProgramAction = frontend::RewriteMacros; break;
      case OPT_rewrite_objc:
        Opts.ProgramAction = frontend::RewriteObjC; break;
+    case OPT_rewrite_objc2cs:
+      Opts.ProgramAction = frontend::RewriteObjC2Cs; break;
      case OPT_rewrite_test:
        Opts.ProgramAction = frontend::RewriteTest; break;
      case OPT_analyze:
@@ -1473,6 +1475,7 @@
    case frontend::PluginAction:
    case frontend::PrintDeclContext:
    case frontend::RewriteObjC:
+  case frontend::RewriteObjC2Cs:
    case frontend::RewriteTest:
    case frontend::RunAnalysis:
    case frontend::MigrateSource:
@@ -1562,7 +1565,8 @@
    ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), *Args);
    if (DashX != IK_AST && DashX != IK_LLVM_IR) {
      ParseLangArgs(*Res.getLangOpts(), *Args, DashX, Diags);
-    if (Res.getFrontendOpts().ProgramAction == frontend::RewriteObjC)
+    if ((Res.getFrontendOpts().ProgramAction == frontend::RewriteObjC) 
||
+        (Res.getFrontendOpts().ProgramAction == 
frontend::RewriteObjC2Cs))
        Res.getLangOpts()->ObjCExceptions = 1;
    }
    // FIXME: ParsePreprocessorArgs uses the FileManager to read the 
contents of
Index: tools/clang/lib/Rewrite/Frontend/CMakeLists.txt
===================================================================
--- tools/clang/lib/Rewrite/Frontend/CMakeLists.txt	(revision 186145)
+++ tools/clang/lib/Rewrite/Frontend/CMakeLists.txt	(working copy)
@@ -6,6 +6,7 @@
    RewriteMacros.cpp
    RewriteModernObjC.cpp
    RewriteObjC.cpp
+  RewriteObjC2Cs.cpp
    RewriteTest.cpp
    )

Index: tools/clang/lib/Driver/Driver.cpp
===================================================================
--- tools/clang/lib/Driver/Driver.cpp	(revision 186145)
+++ tools/clang/lib/Driver/Driver.cpp	(working copy)
@@ -129,6 +129,7 @@
               (PhaseArg = 
DAL.getLastArg(options::OPT_module_file_info)) ||
               (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) ||
               (PhaseArg = 
DAL.getLastArg(options::OPT_rewrite_legacy_objc)) ||
+             (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc2cs)) 
||
               (PhaseArg = DAL.getLastArg(options::OPT__migrate)) ||
               (PhaseArg = DAL.getLastArg(options::OPT__analyze,
                                          options::OPT__analyze_auto)) 
||
@@ -1153,6 +1154,8 @@
        return new CompileJobAction(Input, types::TY_RewrittenObjC);
      } else if (Args.hasArg(options::OPT_rewrite_legacy_objc)) {
        return new CompileJobAction(Input, 
types::TY_RewrittenLegacyObjC);
+    } else if (Args.hasArg(options::OPT_rewrite_objc2cs)) {
+      return new CompileJobAction(Input, types::TY_RewrittenObjC2Cs);
      } else if (Args.hasArg(options::OPT__analyze, 
options::OPT__analyze_auto)) {
        return new AnalyzeJobAction(Input, types::TY_Plist);
      } else if (Args.hasArg(options::OPT__migrate)) {
@@ -1326,6 +1329,7 @@
        !C.getArgs().hasArg(options::OPT_traditional_cpp) &&
        !C.getArgs().hasArg(options::OPT_save_temps) &&
        !C.getArgs().hasArg(options::OPT_rewrite_objc) &&
+      !C.getArgs().hasArg(options::OPT_rewrite_objc2cs) &&
        ToolForJob->hasIntegratedCPP())
      Inputs = &(*Inputs)[0]->getInputs();

Index: tools/clang/lib/Driver/Tools.cpp
===================================================================
--- tools/clang/lib/Driver/Tools.cpp	(revision 186145)
+++ tools/clang/lib/Driver/Tools.cpp	(working copy)
@@ -1889,7 +1889,8 @@
        CmdArgs.push_back("-Eonly");
      else {
        CmdArgs.push_back("-E");
-      if (Args.hasArg(options::OPT_rewrite_objc) &&
+      if ((Args.hasArg(options::OPT_rewrite_objc)  ||
+           Args.hasArg(options::OPT_rewrite_objc2cs)) &&
            !Args.hasArg(options::OPT_g_Group))
          CmdArgs.push_back("-P");
      }
@@ -1961,6 +1962,9 @@
      } else if (JA.getType() == types::TY_RewrittenLegacyObjC) {
        CmdArgs.push_back("-rewrite-objc");
        rewriteKind = RK_Fragile;
+    } else if (JA.getType() == types::TY_RewrittenObjC2Cs) {
+      CmdArgs.push_back("-rewrite-objc2cs");
+      rewriteKind = RK_NonFragile;
      } else {
        assert(JA.getType() == types::TY_PP_Asm &&
               "Unexpected output type!");
@@ -3421,7 +3425,8 @@
           it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
      const InputInfo &II = *it;
      CmdArgs.push_back("-x");
-    if (Args.hasArg(options::OPT_rewrite_objc))
+    if ((Args.hasArg(options::OPT_rewrite_objc)) ||
+	(Args.hasArg(options::OPT_rewrite_objc2cs)))
        CmdArgs.push_back(types::getTypeName(types::TY_PP_ObjCXX));
      else
        CmdArgs.push_back(types::getTypeName(II.getType()));
Index: tools/clang/include/clang/Frontend/FrontendOptions.h
===================================================================
--- tools/clang/include/clang/Frontend/FrontendOptions.h	(revision 
186145)
+++ tools/clang/include/clang/Frontend/FrontendOptions.h	(working copy)
@@ -51,6 +51,7 @@
      PrintPreprocessedInput, ///< -E mode.
      RewriteMacros,          ///< Expand macros but not \#includes.
      RewriteObjC,            ///< ObjC->C Rewriter.
+    RewriteObjC2Cs,         ///< ObjC->C# Rewriter.
      RewriteTest,            ///< Rewriter playground
      RunAnalysis,            ///< Run one or more source code analyses.
      MigrateSource,          ///< Run migrator.
Index: tools/clang/include/clang/Driver/Options.td
===================================================================
--- tools/clang/include/clang/Driver/Options.td	(revision 186145)
+++ tools/clang/include/clang/Driver/Options.td	(working copy)
@@ -1050,6 +1050,8 @@
    HelpText<"Rewrite Objective-C source to C++">, Group<Action_Group>;
  def rewrite_legacy_objc : Flag<["-"], "rewrite-legacy-objc">, 
Flags<[DriverOption]>,
    HelpText<"Rewrite Legacy Objective-C source to C++">;
+def rewrite_objc2cs : Flag<["-"], "rewrite-objc2cs">, 
Flags<[DriverOption,CC1Option]>,
+  HelpText<"Rewrite Objective-C source to C#">, Group<Action_Group>;
  def rdynamic : Flag<["-"], "rdynamic">;
  def resource_dir : Separate<["-"], "resource-dir">,
    Flags<[DriverOption, CC1Option, HelpHidden]>,
Index: tools/clang/include/clang/Driver/Types.def
===================================================================
--- tools/clang/include/clang/Driver/Types.def	(revision 186145)
+++ tools/clang/include/clang/Driver/Types.def	(working copy)
@@ -84,6 +84,7 @@
  TYPE("plist",                    Plist,        INVALID,         
"plist", "")
  TYPE("rewritten-objc",           RewrittenObjC,INVALID,         "cpp", 
  "")
  TYPE("rewritten-legacy-objc",    RewrittenLegacyObjC,INVALID,   "cpp", 
  "")
+TYPE("rewritten-objc2cs",        RewrittenObjC2Cs,INVALID,      "cs",  
"")
  TYPE("remap",                    Remap,        INVALID,         
"remap", "")
  TYPE("precompiled-header",       PCH,          INVALID,         "gch", 
  "A")
  TYPE("object",                   Object,       INVALID,         "o",   
  "")



More information about the cfe-dev mailing list