[cfe-commits] r126630 - in /cfe/trunk: include/clang/Driver/Options.td lib/CodeGen/CGException.cpp lib/Driver/Tools.cpp lib/Sema/SemaExprCXX.cpp lib/Sema/SemaStmt.cpp

Anders Carlsson andersca at mac.com
Sun Feb 27 18:27:16 PST 2011


Author: andersca
Date: Sun Feb 27 20:27:16 2011
New Revision: 126630

URL: http://llvm.org/viewvc/llvm-project?rev=126630&view=rev
Log:
Add a -fcxx-exceptions flag to the frontend, which can be used to enable
C++ exceptions, even when exceptions have been turned off using -fno-exceptions.
Make the -fobjc-exceptions flag do the same thing, but for Objective-C exceptions.

C++ and Objective-C exceptions can also be disabled using -fno-cxx-excptions and
-fno-objc-exceptions.

Modified:
    cfe/trunk/include/clang/Driver/Options.td
    cfe/trunk/lib/CodeGen/CGException.cpp
    cfe/trunk/lib/Driver/Tools.cpp
    cfe/trunk/lib/Sema/SemaExprCXX.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp

Modified: cfe/trunk/include/clang/Driver/Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.td?rev=126630&r1=126629&r2=126630&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/Options.td (original)
+++ cfe/trunk/include/clang/Driver/Options.td Sun Feb 27 20:27:16 2011
@@ -264,6 +264,7 @@
 def fconstant_cfstrings : Flag<"-fconstant-cfstrings">, Group<f_Group>;
 def fconstant_string_class_EQ : Joined<"-fconstant-string-class=">, Group<f_Group>;
 def fcreate_profile : Flag<"-fcreate-profile">, Group<f_Group>;
+def fcxx_exceptions: Flag<"-fcxx-exceptions">, Group<f_Group>;
 def fdebug_pass_arguments : Flag<"-fdebug-pass-arguments">, Group<f_Group>;
 def fdebug_pass_structure : Flag<"-fdebug-pass-structure">, Group<f_Group>;
 def fdiagnostics_fixit_info : Flag<"-fdiagnostics-fixit-info">, Group<f_Group>;
@@ -325,6 +326,7 @@
 def fno_color_diagnostics : Flag<"-fno-color-diagnostics">, Group<f_Group>;
 def fno_common : Flag<"-fno-common">, Group<f_Group>;
 def fno_constant_cfstrings : Flag<"-fno-constant-cfstrings">, Group<f_Group>;
+def fno_cxx_exceptions: Flag<"-fno-cxx-exceptions">, Group<f_Group>;
 def fno_diagnostics_fixit_info : Flag<"-fno-diagnostics-fixit-info">, Group<f_Group>;
 def fno_diagnostics_show_option : Flag<"-fno-diagnostics-show-option">, Group<f_Group>;
 def fno_dollars_in_identifiers : Flag<"-fno-dollars-in-identifiers">, Group<f_Group>;

Modified: cfe/trunk/lib/CodeGen/CGException.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGException.cpp?rev=126630&r1=126629&r2=126630&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGException.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGException.cpp Sun Feb 27 20:27:16 2011
@@ -439,7 +439,7 @@
 }
 
 void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
-  if (!CGM.getLangOptions().Exceptions)
+  if (!CGM.getLangOptions().CXXExceptions)
     return;
   
   const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
@@ -467,7 +467,7 @@
 }
 
 void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
-  if (!CGM.getLangOptions().Exceptions)
+  if (!CGM.getLangOptions().CXXExceptions)
     return;
   
   const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=126630&r1=126629&r2=126630&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Sun Feb 27 20:27:16 2011
@@ -780,25 +780,84 @@
            Triple.getArch() == llvm::Triple::arm));  
 }
 
-static bool needsExceptions(const ArgList &Args,  types::ID InputType,
-                            const llvm::Triple &Triple) {
-  // Handle -fno-exceptions.
+/// addExceptionArgs - Adds exception related arguments to the driver command
+/// arguments. There's a master flag, -fexceptions and also language specific
+/// flags to enable/disable C++ and Objective-C exceptions.
+/// This makes it possible to for example disable C++ exceptions but enable
+/// Objective-C exceptions.
+static void addExceptionArgs(const ArgList &Args, types::ID InputType,
+                             const llvm::Triple &Triple,
+                             bool KernelOrKext, bool IsRewriter,
+                             ArgStringList &CmdArgs) {
+  if (KernelOrKext)
+    return;
+
+  // Exceptions are enabled by default.
+  bool ExceptionsEnabled = true;
+
+  // This keeps track of whether exceptions were explicitly turned on or off.
+  bool DidHaveExplicitExceptionFlag = false;
+
   if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
                                options::OPT_fno_exceptions)) {
     if (A->getOption().matches(options::OPT_fexceptions))
-      return true;
-    else
-      return false;
+      ExceptionsEnabled = true;
+    else 
+      ExceptionsEnabled = false;
+
+    DidHaveExplicitExceptionFlag = true;
+  }
+
+  bool ShouldUseExceptionTables = false;
+
+  // Exception tables and cleanups can be enabled with -fexceptions even if the
+  // language itself doesn't support exceptions.
+  if (ExceptionsEnabled && DidHaveExplicitExceptionFlag)
+    ShouldUseExceptionTables = true;
+
+  if (types::isObjC(InputType)) {
+    bool ObjCExceptionsEnabled = ExceptionsEnabled;
+
+    if (Arg *A = Args.getLastArg(options::OPT_fobjc_exceptions, 
+                                 options::OPT_fno_objc_exceptions,
+                                 options::OPT_fexceptions,
+                                 options::OPT_fno_exceptions)) {
+      if (A->getOption().matches(options::OPT_fobjc_exceptions))
+        ObjCExceptionsEnabled = true;
+      else if (A->getOption().matches(options::OPT_fno_objc_exceptions))
+        ObjCExceptionsEnabled = false;
+    }
+
+    if (ObjCExceptionsEnabled) {
+      CmdArgs.push_back("-fobjc-exceptions");
+
+      ShouldUseExceptionTables |= 
+        shouldUseExceptionTablesForObjCExceptions(Args, Triple);
+    }
   }
 
-  // Otherwise, C++ inputs use exceptions.
-  if (types::isCXX(InputType))
-    return true;
+  if (types::isCXX(InputType)) {
+    bool CXXExceptionsEnabled = ExceptionsEnabled;
 
-  if (types::isObjC(InputType))
-    return shouldUseExceptionTablesForObjCExceptions(Args, Triple);
+    if (Arg *A = Args.getLastArg(options::OPT_fcxx_exceptions, 
+                                 options::OPT_fno_cxx_exceptions, 
+                                 options::OPT_fexceptions,
+                                 options::OPT_fno_exceptions)) {
+      if (A->getOption().matches(options::OPT_fcxx_exceptions))
+        CXXExceptionsEnabled = true;
+      else
+        CXXExceptionsEnabled = false;
+    }
+
+    if (CXXExceptionsEnabled) {
+      CmdArgs.push_back("-fcxx-exceptions");
 
-  return false;
+      ShouldUseExceptionTables = true;
+    }
+  }
+
+  if (ShouldUseExceptionTables)
+    CmdArgs.push_back("-fexceptions");
 }
 
 void Clang::ConstructJob(Compilation &C, const JobAction &JA,
@@ -1416,10 +1475,9 @@
                    false))
     CmdArgs.push_back("-fno-elide-constructors");
 
-  // -fexceptions=0 is default.
-  if (!KernelOrKext &&
-      needsExceptions(Args, InputType, getToolChain().getTriple()))
-    CmdArgs.push_back("-fexceptions");
+  // Add exception args.
+  addExceptionArgs(Args, InputType, getToolChain().getTriple(),
+                   KernelOrKext, IsRewriter, CmdArgs);
 
   if (getToolChain().UseSjLjExceptions())
     CmdArgs.push_back("-fsjlj-exceptions");
@@ -1556,11 +1614,6 @@
                      getToolChain().IsObjCDefaultSynthPropertiesDefault())) {
       CmdArgs.push_back("-fobjc-default-synthesize-properties");
     }
-
-    // -fno-objc-exceptions is default.
-    if (IsRewriter || Args.hasFlag(options::OPT_fobjc_exceptions, 
-                                   options::OPT_fno_objc_exceptions))
-      CmdArgs.push_back("-fobjc-exceptions");
   }
 
   if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=126630&r1=126629&r2=126630&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Sun Feb 27 20:27:16 2011
@@ -477,7 +477,7 @@
 ExprResult
 Sema::ActOnCXXThrow(SourceLocation OpLoc, Expr *Ex) {
   // Don't report an error if 'throw' is used in system headers.
-  if (!getLangOptions().Exceptions &&
+  if (!getLangOptions().CXXExceptions &&
       !getSourceManager().isInSystemHeader(OpLoc))
     Diag(OpLoc, diag::err_exceptions_disabled) << "throw";
 

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=126630&r1=126629&r2=126630&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Sun Feb 27 20:27:16 2011
@@ -1766,7 +1766,7 @@
 Sema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock,
                        MultiStmtArg RawHandlers) {
   // Don't report an error if 'try' is used in system headers.
-  if (!getLangOptions().Exceptions &&
+  if (!getLangOptions().CXXExceptions &&
       !getSourceManager().isInSystemHeader(TryLoc))
       Diag(TryLoc, diag::err_exceptions_disabled) << "try";
 





More information about the cfe-commits mailing list