[cfe-commits] r134045 - in /cfe/trunk: include/clang/Basic/LangOptions.h include/clang/Driver/CC1Options.td include/clang/Driver/Options.td lib/CodeGen/CGBlocks.cpp lib/Driver/Tools.cpp lib/Frontend/CompilerInvocation.cpp test/CodeGen/reset-local

Eli Friedman eli.friedman at gmail.com
Tue Jun 28 17:14:34 PDT 2011


On Tue, Jun 28, 2011 at 4:51 PM, Fariborz Jahanian <fjahanian at apple.com> wrote:
> Author: fjahanian
> Date: Tue Jun 28 18:51:26 2011
> New Revision: 134045
>
> URL: http://llvm.org/viewvc/llvm-project?rev=134045&view=rev
> Log:
> Under a compiler flag, -freset-local-blocks,
> wipe out stack blocks when they go out of scope.
> // rdar://9227352
>
> Added:
>    cfe/trunk/test/CodeGen/reset-local-block.c
> Modified:
>    cfe/trunk/include/clang/Basic/LangOptions.h
>    cfe/trunk/include/clang/Driver/CC1Options.td
>    cfe/trunk/include/clang/Driver/Options.td
>    cfe/trunk/lib/CodeGen/CGBlocks.cpp
>    cfe/trunk/lib/Driver/Tools.cpp
>    cfe/trunk/lib/Frontend/CompilerInvocation.cpp
>
> Modified: cfe/trunk/include/clang/Basic/LangOptions.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.h?rev=134045&r1=134044&r2=134045&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/LangOptions.h (original)
> +++ cfe/trunk/include/clang/Basic/LangOptions.h Tue Jun 28 18:51:26 2011
> @@ -72,6 +72,7 @@
>   unsigned POSIXThreads      : 1; // Compiling with POSIX thread support
>                                   // (-pthread)
>   unsigned Blocks            : 1; // block extension to C
> +  unsigned ResetLocalBlocks  : 1; // reset local blocks going out of scope
>   unsigned EmitAllDecls      : 1; // Emit all declarations, even if
>                                   // they are unused.
>   unsigned MathErrno         : 1; // Math functions must respect errno
> @@ -198,7 +199,7 @@
>
>     ThreadsafeStatics = 1;
>     POSIXThreads = 0;
> -    Blocks = 0;
> +    Blocks = ResetLocalBlocks = 0;
>     EmitAllDecls = 0;
>     MathErrno = 1;
>     SignedOverflowBehavior = SOB_Undefined;
>
> Modified: cfe/trunk/include/clang/Driver/CC1Options.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=134045&r1=134044&r2=134045&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Driver/CC1Options.td (original)
> +++ cfe/trunk/include/clang/Driver/CC1Options.td Tue Jun 28 18:51:26 2011
> @@ -444,6 +444,8 @@
>   HelpText<"Emit all declarations, even if unused">;
>  def fblocks : Flag<"-fblocks">,
>   HelpText<"enable the 'blocks' language feature">;
> +def freset_local_blocks : Flag<"-freset-local-blocks">,
> +  HelpText<"reset local blocks when they go out of scope">;
>  def fheinous_gnu_extensions : Flag<"-fheinous-gnu-extensions">;
>  def fexceptions : Flag<"-fexceptions">,
>   HelpText<"Enable support for exception handling">;
>
> Modified: cfe/trunk/include/clang/Driver/Options.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.td?rev=134045&r1=134044&r2=134045&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Driver/Options.td (original)
> +++ cfe/trunk/include/clang/Driver/Options.td Tue Jun 28 18:51:26 2011
> @@ -259,6 +259,7 @@
>  def fast : Flag<"-fast">, Group<f_Group>;
>  def fasynchronous_unwind_tables : Flag<"-fasynchronous-unwind-tables">, Group<f_Group>;
>  def fblocks : Flag<"-fblocks">, Group<f_Group>;
> +def freset_local_blocks : Flag<"-freset-local-blocks">, Group<f_Group>;
>  def fbootclasspath_EQ : Joined<"-fbootclasspath=">, Group<f_Group>;
>  def fborland_extensions : Flag<"-fborland-extensions">, Group<f_Group>;
>  def fbuiltin_strcat : Flag<"-fbuiltin-strcat">, Group<f_Group>;
>
> Modified: cfe/trunk/lib/CodeGen/CGBlocks.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBlocks.cpp?rev=134045&r1=134044&r2=134045&view=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CGBlocks.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CGBlocks.cpp Tue Jun 28 18:51:26 2011
> @@ -25,6 +25,22 @@
>  using namespace clang;
>  using namespace CodeGen;
>
> +struct CallMemsetLocalBlockObject : EHScopeStack::Cleanup {
> +  llvm::AllocaInst *BlockAddr;
> +  CharUnits BlockSize;
> +
> +  CallMemsetLocalBlockObject(llvm::AllocaInst *blockAddr,
> +                             CharUnits blocSize)
> +    : BlockAddr(blockAddr), BlockSize(blocSize) {}
> +
> +  void Emit(CodeGenFunction &CGF, bool isForEH) {
> +    CGF.Builder.CreateMemSet(BlockAddr,
> +                             llvm::ConstantInt::get(CGF.Int8Ty, 0xCD),
> +                             BlockSize.getQuantity(),
> +                             BlockAddr->getAlignment());
> +  }
> +};
> +
>  CGBlockInfo::CGBlockInfo(const BlockExpr *blockExpr, const char *N)
>   : Name(N), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false),
>     HasCXXObject(false), UsesStret(false), StructureType(0), Block(blockExpr) {
> @@ -649,6 +665,9 @@
>   llvm::Value *result =
>     Builder.CreateBitCast(blockAddr,
>                           ConvertType(blockInfo.getBlockExpr()->getType()));
> +  if (getLangOptions().ResetLocalBlocks)
> +    EHStack.pushCleanup<CallMemsetLocalBlockObject>(NormalCleanup, blockAddr,
> +                                                    blockInfo.BlockSize);
>
>   return result;
>  }

I'm not sure this bit of code works correctly in the presence of
goto's... I was doing something sort of similar in rdar://9421018 and
ran into issues.

-Eli




More information about the cfe-commits mailing list