1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 11:42:21 +00:00

Port illumos' shell linter improvements (#353)

This commit ports over two improvements to the shell linter from
illumos (original patch written by Andy Fiddaman). Links to the
relevant bug reports and the original patch:
https://www.illumos.org/issues/13601
https://www.illumos.org/issues/13631
c7b656fc71

The first improvement is to the lint warning for arithmetic
operators in [[ ... ]]. The ksh linter now suggests the correct
equivalent operator to use in ((...)). Example:
  $ ksh -nc '[[ 30 -gt 25 ]]'
  # Original warning
  warning: line 1: -gt within [[ ... ]] obsolete, use ((...))
  # New warning
  warning: line 1: [[ ... -gt ... ]] obsolete, use ((... > ...))

The second improvement pertains to variable expansion in arithmetic
expressions. The ksh linter now suggests referencing variable names
directly:
  $ ksh -nc 'integer foo=40; (($foo < 50 ))'
  # Old warning
  warning: line 1: variable expansion makes arithmetic evaluation less efficient
  # New warning
  warning: line 1: in '(($foo < 50))', using '$' is slower and can introduce rounding errors

src/cmd/ksh93/{data/lexstates,sh/lex,sh/parse}.c:
- Port the improved shell lint warnings from illumos to ksh93u+m.
- The original checks for arithmetic operators involved a bunch of
  if statements with inefficient calls to strcmp(3). These were
  replaced with a more efficient switch statement that avoids
  strcmp.
This commit is contained in:
Johnothan King 2021-11-30 12:26:17 -08:00 committed by Martijn Dekker
parent 370440473e
commit 6904585f49
4 changed files with 32 additions and 4 deletions

3
NEWS
View file

@ -13,6 +13,9 @@ Any uppercase BUG_* names are modernish shell bug IDs.
- Fixed a crash that could occur when a KEYBD trap was set and a multi-line
command substitution was input in an interactive shell.
- The shell linter's warnings for obsolete arithmetic operators in [[ ... ]]
and unnecessary variable expansion in ((...)) have been improved.
2021-11-24:
- The --posix mode was amended to stop the '.' command (but not 'source') from

View file

@ -430,13 +430,13 @@ const char e_lexsyntax2[] = "syntax error: `%s' %s";
const char e_lexsyntax3[] = "syntax error at line %d: duplicate label %s";
const char e_lexsyntax4[] = "syntax error at line %d: invalid reference list";
const char e_lexsyntax5[] = "syntax error at line %d: `<<%s' here-document not contained within command substitution";
const char e_lexwarnvar[] = "line %d: variable expansion makes arithmetic evaluation less efficient";
const char e_lexwarnvar[] = "line %d: in '((%s))', using '$' is slower and can introduce rounding errors";
const char e_lexlabignore[] = "line %d: label %s ignored";
const char e_lexlabunknown[] = "line %d: %s unknown label";
const char e_lexobsolete1[] = "line %d: `...` obsolete, use $(...)";
const char e_lexobsolete2[] = "line %d: -a obsolete, use -e";
const char e_lexobsolete3[] = "line %d: '=' obsolete, use '=='";
const char e_lexobsolete4[] = "line %d: %s within [[ ... ]] obsolete, use ((...))";
const char e_lexobsolete4[] = "line %d: [[ ... %s ... ]] obsolete, use ((... %s ...))";
const char e_lexobsolete5[] = "line %d: set %s obsolete";
const char e_lexobsolete6[] = "line %d: `{' instead of `in' is obsolete";
const char e_lexusebrace[] = "line %d: use braces to avoid ambiguities with $id[...]";

View file

@ -1389,7 +1389,32 @@ breakloop:
if(lp->lex.testop2)
{
if(lp->lexd.warn && (c&TEST_ARITH))
errormsg(SH_DICT,ERROR_warn(0),e_lexobsolete4,shp->inlineno,state);
{
char *alt;
switch(c)
{
case TEST_EQ:
alt = "=="; /* '-eq' --> '==' */
break;
case TEST_NE:
alt = "!="; /* '-ne' --> '!=' */
break;
case TEST_LT:
alt = "<"; /* '-lt' --> '<' */
break;
case TEST_GT:
alt = ">"; /* '-gt' --> '>' */
break;
case TEST_LE:
alt = "<="; /* '-le' --> '<=' */
break;
case TEST_GE:
alt = ">="; /* '-ge' --> '>=' */
break;
}
errormsg(SH_DICT, ERROR_warn(0), e_lexobsolete4,
shp->inlineno, state, alt);
}
if(c&TEST_STRCMP)
lp->lex.incase = 1;
else if(c==TEST_REP)

View file

@ -303,7 +303,7 @@ static Shnode_t *getanode(Lex_t *lp, struct argnod *ap)
else
{
if(sh_isoption(SH_NOEXEC) && (ap->argflag&ARG_MAC) && paramsub(ap->argval))
errormsg(SH_DICT,ERROR_warn(0),e_lexwarnvar,lp->sh->inlineno);
errormsg(SH_DICT,ERROR_warn(0),e_lexwarnvar,lp->sh->inlineno,ap->argval);
t->ar.arcomp = 0;
}
return(t);