diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-10-04 23:52:29 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-10-04 23:52:29 +0000 |
commit | 36ef0d54cfddf31cd48816e78ab4db73b31a6c1d (patch) | |
tree | a7b58d06715aadcfe6ff90abdf525f2ab1dfdbef /lib/CodeGen/CodeGenFunction.cpp | |
parent | c339393067438596599b142a8950f0e8694e2f1a (diff) |
If we flow off the end of a value-returning function:
- outside C++, return undef (behavior is not undefined unless the value is used)
- in C++, with -fcatch-undefined-behavior, perform an appropriate trap
- in C++, produce an 'unreachable' (behavior is undefined immediately)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@165273 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CodeGenFunction.cpp')
-rw-r--r-- | lib/CodeGen/CodeGenFunction.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp index 1d02861ed7..5c0247a33f 100644 --- a/lib/CodeGen/CodeGenFunction.cpp +++ b/lib/CodeGen/CodeGenFunction.cpp @@ -535,6 +535,20 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, else EmitFunctionBody(Args); + // C++11 [stmt.return]p2: + // Flowing off the end of a function [...] results in undefined behavior in + // a value-returning function. + // C11 6.9.1p12: + // If the '}' that terminates a function is reached, and the value of the + // function call is used by the caller, the behavior is undefined. + if (getContext().getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() && + !FD->getResultType()->isVoidType() && Builder.GetInsertBlock()) { + if (CatchUndefined) + EmitCheck(Builder.getFalse()); + Builder.CreateUnreachable(); + Builder.ClearInsertionPoint(); + } + // Emit the standard function epilogue. FinishFunction(BodyRange.getEnd()); |