1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Introduce usage of __builtin_unreachable() and noreturn (#248)

This commit adds an UNREACHABLE() macro that expands to either the
__builtin_unreachable() compiler builtin (for release builds) or
abort(3) (for development builds). This is used to mark code paths
that are never to be reached.

It also adds the 'noreturn' attribute to functions that never
return: path_exec(), sh_done() and sh_syntax(). The UNREACHABLE()
macro is not added after calling these.

The purpose of these is:
* to slightly improve GCC/Clang compiler optimizations;
* to fix a few compiler warnings;
* to add code clarity.

Changes of note:

src/cmd/ksh93/sh/io.c: outexcept():
- Avoid using __builtin_unreachable() here since errormsg can
  return despite using ERROR_system(1), as shp->jmplist->mode is
  temporarily set to 0. See: https://github.com/att/ast/issues/1336

src/cmd/ksh93/tests/io.sh:
- Add a regression test for the ksh2020 bug referenced above.

src/lib/libast/features/common:
- Detect the existence of either the C11 stdnoreturn.h header or
  the GCC noreturn attribute, preferring the former when available.
- Test for the existence of __builtin_unreachable(). Use it for
  release builds. On development builds, use abort() instead, which
  crahses reliably for debugging when unreachable code is reached.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
Johnothan King 2021-04-04 16:28:24 -07:00 committed by GitHub
parent 56913f8c2a
commit c4f980eb29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
87 changed files with 1170 additions and 122 deletions

View file

@ -98,6 +98,7 @@ typedef struct _mac_
#define M_NAMECOUNT 7 /* ${#var*} */
#define M_TYPE 8 /* ${@var} */
static noreturn void mac_error(Namval_t*);
static int substring(const char*, const char*, int[], int);
static void copyto(Mac_t*, int, int);
static void comsubst(Mac_t*, Shnode_t*, int);
@ -107,7 +108,6 @@ static void tilde_expand2(Shell_t*,int);
static char *sh_tilde(Shell_t*,const char*);
static char *special(Shell_t *,int);
static void endfield(Mac_t*,int);
static void mac_error(Namval_t*);
static char *mac_getstring(char*);
static int charlen(const char*,int);
#if SHOPT_MULTIBYTE
@ -184,7 +184,10 @@ char *sh_mactrim(Shell_t *shp, char *str, register int mode)
if((mode=path_expand(shp,str,&arglist))==1)
str = arglist->argval;
else if(mode>1)
{
errormsg(SH_DICT,ERROR_exit(1),e_ambiguous,str);
UNREACHABLE();
}
sh_trim(str);
}
*mp = savemac;
@ -712,7 +715,10 @@ static void copyto(register Mac_t *mp,int endch, int newquote)
if(first[c-2]=='.')
offset = stktell(stkp);
if(isastchar(*cp) && cp[1]==']')
{
errormsg(SH_DICT,ERROR_exit(1),e_badsubscript,*cp);
UNREACHABLE();
}
}
first = fcseek(c);
mp->pattern = 4;
@ -1198,7 +1204,10 @@ retry1:
d=fcget();
fcseek(-1);
if(!(d && strchr(":+-?=",d)))
{
errormsg(SH_DICT,ERROR_exit(1),e_notset,ltos(c));
UNREACHABLE();
}
}
break;
case S_ALP:
@ -1469,7 +1478,10 @@ retry1:
{
/* The parameter is unset. */
if(sh_isoption(SH_NOUNSET) && !isastchar(mode) && (type==M_VNAME || type==M_SIZE))
{
errormsg(SH_DICT,ERROR_exit(1),e_notset,id);
UNREACHABLE();
}
v = 0;
if(type==M_VNAME)
{
@ -1598,7 +1610,10 @@ retry1:
int newops = (c=='#' || c == '%' || c=='/');
offset = stktell(stkp);
if(newops && sh_isoption(SH_NOUNSET) && *id && id!=idbuff && (!np || nv_isnull(np)))
{
errormsg(SH_DICT,ERROR_exit(1),e_notset,id);
UNREACHABLE();
}
if(c=='/' ||c==':' || ((!v || (nulflg && *v==0)) ^ (c=='+'||c=='#'||c=='%')))
{
int newquote = mp->quote;
@ -1999,6 +2014,7 @@ retry2:
errormsg(SH_DICT,ERROR_exit(1),e_nullset,id);
else
errormsg(SH_DICT,ERROR_exit(1),e_notset,id);
UNREACHABLE();
}
else if(c=='=')
{
@ -2030,6 +2046,7 @@ retry2:
nv_close(np);
}
errormsg(SH_DICT,ERROR_exit(1),e_notset,id);
UNREACHABLE();
}
if(np)
nv_close(np);
@ -2799,6 +2816,7 @@ static char *special(Shell_t *shp,register int c)
c_str[0]=(char)c;
c_str[1]='\0';
errormsg(SH_DICT,ERROR_exit(1),e_notset,c_str);
UNREACHABLE();
}
}
return(NIL(char*));
@ -2807,11 +2825,12 @@ static char *special(Shell_t *shp,register int c)
/*
* Handle macro expansion errors
*/
static void mac_error(Namval_t *np)
static noreturn void mac_error(Namval_t *np)
{
if(np)
nv_close(np);
errormsg(SH_DICT,ERROR_exit(1),e_subst,fcfirst());
UNREACHABLE();
}
/*