Skip to content

Commit c03ade1

Browse files
Merge pull request #129 from just-max/add-return-on-noreturn-fallthrough
Add `return` statements in `noreturn` functions
2 parents 3edded1 + 2a715ba commit c03ade1

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/cil.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ let useComputedGoto = ref false
8888

8989
let useCaseRange = ref false
9090

91+
let addReturnOnNoreturnFallthrough = ref false
92+
9193
module M = Machdep
9294
(* Cil.initCil will set this to the current machine description.
9395
Makefile.cil generates the file src/machdep.ml,

src/cil.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type cstd = C90 | C99 | C11
5757
val cstd_of_string: string -> cstd
5858
val cstd: cstd ref
5959
val gnu89inline: bool ref
60+
val addReturnOnNoreturnFallthrough: bool ref
6061

6162
(** This module defines the abstract syntax of CIL. It also provides utility
6263
functions for traversing the CIL data structures, and pretty-printing

src/frontc/cabs2cil.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6437,8 +6437,14 @@ and doDecl (isglobal: bool) : A.definition -> chunk = function
64376437
ignore (warn "Body of function %s falls-through and cannot find an appropriate return value" !currentFunctionFDEC.svar.vname);
64386438
None
64396439
in
6440-
if not (hasAttribute "noreturn"
6441-
!currentFunctionFDEC.svar.vattr) then
6440+
if
6441+
(* By default, (addReturnOnNoreturnFallthrough = false), CIL does not add a return statement
6442+
in functions marked noreturn. However, unlike functions that are detected to never fall through,
6443+
the end of these functions can possibly be reached (perhaps in buggy program code), so with
6444+
the option set to true, always add the return statement. *)
6445+
!addReturnOnNoreturnFallthrough
6446+
|| not (hasAttribute "noreturn" !currentFunctionFDEC.svar.vattr)
6447+
then
64426448
!currentFunctionFDEC.sbody.bstmts <-
64436449
!currentFunctionFDEC.sbody.bstmts
64446450
@ [mkStmt (Return(retval, endloc))]

0 commit comments

Comments
 (0)