[cfe-commits] r127854 - in /cfe/trunk: include/clang/Driver/CC1Options.td include/clang/Frontend/CodeGenOptions.h lib/CodeGen/CGDeclCXX.cpp lib/CodeGen/CodeGenModule.cpp lib/CodeGen/CodeGenModule.h lib/Driver/Tools.cpp lib/Frontend/CompilerInvocation.cpp test/CodeGenCXX/apple-kext-guard-variable.cpp

John McCall rjmccall at apple.com
Thu Mar 17 19:56:14 PDT 2011


Author: rjmccall
Date: Thu Mar 17 21:56:14 2011
New Revision: 127854

URL: http://llvm.org/viewvc/llvm-project?rev=127854&view=rev
Log:
The Darwin kernel does not provide useful guard variable support.
Issue this as an IR-gen error;  it's not really worthwhile doing this
"right", i.e. in Sema, because IR gen knows a lot of tricks beyond
what the constant evaluator knows.


Added:
    cfe/trunk/test/CodeGenCXX/apple-kext-guard-variable.cpp
Modified:
    cfe/trunk/include/clang/Driver/CC1Options.td
    cfe/trunk/include/clang/Frontend/CodeGenOptions.h
    cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h
    cfe/trunk/lib/Driver/Tools.cpp
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp

Modified: cfe/trunk/include/clang/Driver/CC1Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=127854&r1=127853&r2=127854&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/CC1Options.td (original)
+++ cfe/trunk/include/clang/Driver/CC1Options.td Thu Mar 17 21:56:14 2011
@@ -110,6 +110,8 @@
   HelpText<"Do not emit code that uses the red zone.">;
 def dwarf_debug_flags : Separate<"-dwarf-debug-flags">,
   HelpText<"The string to embed in the Dwarf debug flags record.">;
+def fforbid_guard_variables : Flag<"-fforbid-guard-variables">,
+  HelpText<"Emit an error if a C++ static local initializer would need a guard variable">;
 def g : Flag<"-g">, HelpText<"Generate source level debug information">;
 def fcatch_undefined_behavior : Flag<"-fcatch-undefined-behavior">,
     HelpText<"Generate runtime checks for undefined behavior.">;

Modified: cfe/trunk/include/clang/Frontend/CodeGenOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CodeGenOptions.h?rev=127854&r1=127853&r2=127854&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/CodeGenOptions.h (original)
+++ cfe/trunk/include/clang/Frontend/CodeGenOptions.h Thu Mar 17 21:56:14 2011
@@ -51,6 +51,8 @@
                                   /// Decl* various IR entities came from.  Only
                                   /// useful when running CodeGen as a
                                   /// subroutine.
+  unsigned ForbidGuardVariables : 1; /// Issue errors if C++ guard variables
+                                  /// are required
   unsigned FunctionSections  : 1; /// Set when -ffunction-sections is enabled
   unsigned HiddenWeakTemplateVTables : 1; /// Emit weak vtables and RTTI for
                                   /// template classes with hidden visibility
@@ -128,6 +130,7 @@
     DisableLLVMOpts = 0;
     DisableRedZone = 0;
     EmitDeclMetadata = 0;
+    ForbidGuardVariables = 0;
     FunctionSections = 0;
     HiddenWeakTemplateVTables = 0;
     HiddenWeakVTables = 0;

Modified: cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDeclCXX.cpp?rev=127854&r1=127853&r2=127854&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDeclCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDeclCXX.cpp Thu Mar 17 21:56:14 2011
@@ -149,6 +149,14 @@
 
 void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
                                          llvm::GlobalVariable *DeclPtr) {
+  // If we've been asked to forbid guard variables, emit an error now.
+  // This diagnostic is hard-coded for Darwin's use case;  we can find
+  // better phrasing if someone else needs it.
+  if (CGM.getCodeGenOpts().ForbidGuardVariables)
+    CGM.Error(D.getLocation(),
+              "this initialization requires a guard variable, which "
+              "the kernel does not support");
+
   CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr);
 }
 

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=127854&r1=127853&r2=127854&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Thu Mar 17 21:56:14 2011
@@ -154,6 +154,11 @@
   return getContext().Target.getTriple().getOS() == llvm::Triple::Darwin;
 }
 
+void CodeGenModule::Error(SourceLocation loc, llvm::StringRef error) {
+  unsigned diagID = getDiags().getCustomDiagID(Diagnostic::Error, error);
+  getDiags().Report(Context.getFullLoc(loc), diagID);
+}
+
 /// ErrorUnsupported - Print out an error that codegen doesn't support the
 /// specified stmt yet.
 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=127854&r1=127853&r2=127854&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Thu Mar 17 21:56:14 2011
@@ -527,6 +527,9 @@
   llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
                                    const AnnotateAttr *AA, unsigned LineNo);
 
+  /// Error - Emit a general error that something can't be done.
+  void Error(SourceLocation loc, llvm::StringRef error);
+
   /// ErrorUnsupported - Print out an error that codegen doesn't support the
   /// specified stmt yet.
   /// \param OmitOnError - If true, then this error should only be emitted if no

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=127854&r1=127853&r2=127854&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Thu Mar 17 21:56:14 2011
@@ -1138,6 +1138,12 @@
   if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
     CmdArgs.push_back("-mconstructor-aliases");
 
+  // Darwin's kernel doesn't support guard variables; just die if we
+  // try to use them.
+  if (KernelOrKext &&
+      getToolChain().getTriple().getOS() == llvm::Triple::Darwin)
+    CmdArgs.push_back("-fforbid-guard-variables");
+
   if (Args.hasArg(options::OPT_mms_bitfields)) {
     CmdArgs.push_back("-mms-bitfields");
   }

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=127854&r1=127853&r2=127854&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Thu Mar 17 21:56:14 2011
@@ -127,6 +127,8 @@
     Res.push_back("-fno-merge-all-constants");
   if (Opts.NoCommon)
     Res.push_back("-fno-common");
+  if (Opts.ForbidGuardVariables)
+    Res.push_back("-fforbid-guard-variables");
   if (Opts.NoImplicitFloat)
     Res.push_back("-no-implicit-float");
   if (Opts.OmitLeafFramePointer)
@@ -896,6 +898,7 @@
   Opts.LimitDebugInfo = Args.hasArg(OPT_flimit_debug_info);
   Opts.DisableLLVMOpts = Args.hasArg(OPT_disable_llvm_optzns);
   Opts.DisableRedZone = Args.hasArg(OPT_disable_red_zone);
+  Opts.ForbidGuardVariables = Args.hasArg(OPT_fforbid_guard_variables);
   Opts.RelaxedAliasing = Args.hasArg(OPT_relaxed_aliasing);
   Opts.DwarfDebugFlags = Args.getLastArgValue(OPT_dwarf_debug_flags);
   Opts.MergeAllConstants = !Args.hasArg(OPT_fno_merge_all_constants);

Added: cfe/trunk/test/CodeGenCXX/apple-kext-guard-variable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/apple-kext-guard-variable.cpp?rev=127854&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/apple-kext-guard-variable.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/apple-kext-guard-variable.cpp Thu Mar 17 21:56:14 2011
@@ -0,0 +1,9 @@
+// RUN: %clang -mtriple=x86_64-apple-darwin10 -S -mkernel -Xclang -verify %s
+
+// rdar://problem/9143356
+
+int foo();
+void test() {
+  static int y = 0;
+  static int x = foo(); // expected-error {{this initialization requires a guard variable, which the kernel does not support}}
+}





More information about the cfe-commits mailing list