[prev in list] [next in list] [prev in thread] [next in thread]
List: cfe-commits
Subject: RE: [PATCH] RE: [cfe-dev] missing return statement for non-void functions in C++
From: Sjoerd Meijer via cfe-commits <cfe-commits () lists ! llvm ! org>
Date: 2015-08-18 11:26:36
Message-ID: 000501d0d9a8$be24a4b0$3a6dee10$ () arm ! com
[Download RAW message or body]
This is a multipart message in MIME format.
[Attachment #2 (multipart/alternative)]
Please see updated patch file attached, which now includes a fixed/added regression \
test.
Sjoerd.
From: Sjoerd Meijer
Sent: 17 August 2015 13:34
To: 'Gabriel Dos Reis'; Richard Smith
Cc: Marshall Clow; cfe-commits
Subject: RE: [PATCH] RE: [cfe-dev] missing return statement for non-void functions in \
C++
Agreed, let's get that right first.
The attached patch causes the program to trap for all optimizations levels, except \
when optimizing for size.
Sjoerd.
From: dosreis@gmail.com [mailto:dosreis@gmail.com] On Behalf Of Gabriel Dos Reis
Sent: 13 August 2015 21:51
To: Richard Smith
Cc: Sjoerd Meijer; Marshall Clow; cfe-commits
Subject: Re: [PATCH] RE: [cfe-dev] missing return statement for non-void functions in \
C++
Please make such programs crash early and often. They are a nightmare to maintain. \
Make them blow in the face of the original authors; not after they are gone.
-- Gaby
[Attachment #5 (text/html)]
<html xmlns:v="urn:schemas-microsoft-com:vml" \
xmlns:o="urn:schemas-microsoft-com:office:office" \
xmlns:w="urn:schemas-microsoft-com:office:word" \
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" \
xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type \
content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 14 \
(filtered medium)"><style><!-- /* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p
{mso-style-priority:99;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
{mso-style-priority:99;
mso-style-link:"Balloon Text Char";
margin:0cm;
margin-bottom:.0001pt;
font-size:8.0pt;
font-family:"Tahoma","sans-serif";}
span.BalloonTextChar
{mso-style-name:"Balloon Text Char";
mso-style-priority:99;
mso-style-link:"Balloon Text";
font-family:"Tahoma","sans-serif";
mso-fareast-language:EN-GB;}
span.EmailStyle20
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:#1F497D;}
span.EmailStyle21
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-GB link=blue vlink=purple><div \
class=WordSection1><p class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Please see \
updated patch file attached, which now includes a fixed/added regression \
test.<o:p></o:p></span></p><p class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Sjoerd.<o:p></o:p></span></p><p \
class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><div><div \
style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p \
class=MsoNormal><b><span lang=EN-US \
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span \
lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> Sjoerd Meijer \
<br><b>Sent:</b> 17 August 2015 13:34<br><b>To:</b> 'Gabriel Dos Reis'; Richard \
Smith<br><b>Cc:</b> Marshall Clow; cfe-commits<br><b>Subject:</b> RE: [PATCH] RE: \
[cfe-dev] missing return statement for non-void functions in \
C++<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p \
class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Agreed, \
let's get that right first.<o:p></o:p></span></p><p class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>The \
attached patch causes the program to trap for all optimizations levels, except when \
optimizing for size.<o:p></o:p></span></p><p class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Sjoerd.<o:p></o:p></span></p><p \
class=MsoNormal><span \
style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p \
class=MsoNormal><b><span lang=EN-US \
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span \
lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> <a \
href="mailto:dosreis@gmail.com">dosreis@gmail.com</a> [<a \
href="mailto:dosreis@gmail.com">mailto:dosreis@gmail.com</a>] <b>On Behalf Of \
</b>Gabriel Dos Reis<br><b>Sent:</b> 13 August 2015 21:51<br><b>To:</b> Richard \
Smith<br><b>Cc:</b> Sjoerd Meijer; Marshall Clow; cfe-commits<br><b>Subject:</b> Re: \
[PATCH] RE: [cfe-dev] missing return statement for non-void functions in \
C++<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><div><p \
class=MsoNormal>Please make such programs crash early and often. They are a \
nightmare to maintain. Make them blow in the face of the original authors; not \
after they are gone.<o:p></o:p></p><div><p \
class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>-- \
Gaby<o:p></o:p></p><div><p \
class=MsoNormal><o:p> </o:p></p></div></div></div></div></body></html>
["0001-Always-trap-for-missing-return-statements-except-whe.patch" (application/octet-stream)]
From 64fb3ce15a22d100d79847a074b31c64f24cd75d Mon Sep 17 00:00:00 2001
From: Sjoerd Meijer <sjoerd.meijer@arm.com>
Date: Mon, 17 Aug 2015 11:38:35 +0100
Subject: [PATCH] Always trap for missing return statements, except when
optimizing for size.
---
lib/CodeGen/CodeGenFunction.cpp | 9 ++++++++-
test/CodeGenCXX/return.cpp | 6 +++++-
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp
index 9bf4161..3135c1a 100644
--- a/lib/CodeGen/CodeGenFunction.cpp
+++ b/lib/CodeGen/CodeGenFunction.cpp
@@ -926,7 +926,14 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function \
*Fn, EmitCheck(std::make_pair(IsFalse, SanitizerKind::Return),
"missing_return", EmitCheckSourceLocation(FD->getLocation()),
None);
- } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+ }
+
+ // A missing return statement provides opportunities for optimizing away
+ // unreachable control flow, i.e it is a code-size optimization opportunity.
+ // However, not returning from a function is potentially unsafe and also not
+ // so user-friendly. Therefore, we trap for all optimizations levels, except
+ // when optimizing for size.
+ if (!CGM.getCodeGenOpts().OptimizeSize) {
EmitTrapCall(llvm::Intrinsic::trap);
}
Builder.CreateUnreachable();
diff --git a/test/CodeGenCXX/return.cpp b/test/CodeGenCXX/return.cpp
index 5c1cfda..ff4fbee 100644
--- a/test/CodeGenCXX/return.cpp
+++ b/test/CodeGenCXX/return.cpp
@@ -1,5 +1,6 @@
// RUN: %clang_cc1 -emit-llvm -triple %itanium_abi_triple -o - %s | FileCheck %s
// RUN: %clang_cc1 -emit-llvm -triple %itanium_abi_triple -O -o - %s | FileCheck %s \
--check-prefix=CHECK-OPT +// RUN: %clang_cc1 -emit-llvm -triple %itanium_abi_triple \
-Oz -o - %s | FileCheck %s --check-prefix=CHECK-OPT-SIZE
// CHECK: @_Z9no_return
// CHECK-OPT: @_Z9no_return
@@ -7,6 +8,9 @@ int no_return() {
// CHECK: call void @llvm.trap
// CHECK-NEXT: unreachable
- // CHECK-OPT-NOT: call void @llvm.trap
+ // CHECK-OPT: call void @llvm.trap
// CHECK-OPT: unreachable
+
+ // CHECK-OPT-SIZE-NOT: call void @llvm.trap
+ // CHECK-OPT-SIZE: unreachable
}
--
1.9.1
[Attachment #7 (text/plain)]
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic