ack/lang/cem/cemcom.ansi/l_states.c

1309 lines
27 KiB
C
Raw Normal View History

1989-02-07 11:04:05 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Header$ */
/* Lint status checking */
#include "lint.h"
#ifdef LINT
#include <alloc.h> /* for st_free */
#include "interface.h"
#include "assert.h"
1990-12-07 14:42:26 +00:00
#include "debug.h"
#ifdef ANSI
#include <flt_arith.h>
1990-12-07 14:42:26 +00:00
#endif ANSI
#include "arith.h"
#include "label.h"
1989-02-07 11:04:05 +00:00
#include "expr.h"
#include "idf.h"
#include "def.h"
#include "code.h" /* RVAL etc */
#include "LLlex.h"
#include "Lpars.h"
#include "stack.h"
#include "type.h"
#include "level.h"
#include "l_lint.h"
#include "l_brace.h"
#include "l_state.h"
#include "l_comment.h"
#include "l_outdef.h"
1990-12-07 14:42:26 +00:00
#ifdef DEBUG
#define dbg_lint_stack(m) /*print_lint_stack(m) /* or not */
#else
#define dbg_lint_stack(m)
#endif DEBUG
1989-02-07 11:04:05 +00:00
extern char *symbol2str();
extern char *func_name;
extern struct type *func_type;
extern int func_notypegiven;
extern char loptions[];
1990-12-07 14:42:26 +00:00
extern struct stack_level *local_level;
1989-02-07 11:04:05 +00:00
/* global variables for the lint_stack */
1990-12-07 14:42:26 +00:00
PRIVATE struct lint_stack_entry *top_ls;
1989-02-07 11:04:05 +00:00
/* global variables for the brace stack */
PRIVATE int brace_count;
1990-12-07 14:42:26 +00:00
PRIVATE struct brace *top_br;
/* global variables for the function return */
PRIVATE int valreturned; /* see l_lint.h */
PRIVATE int return_warned;
1989-02-07 11:04:05 +00:00
PRIVATE end_brace();
PRIVATE lint_1_local();
PRIVATE lint_1_global();
1990-12-07 14:42:26 +00:00
PRIVATE start_loop_stmt();
1989-02-07 11:04:05 +00:00
PRIVATE check_autos();
1990-12-07 14:42:26 +00:00
PRIVATE struct auto_def *copy_auto_list();
PRIVATE free_auto_list();
1989-02-07 11:04:05 +00:00
PRIVATE struct state *copy_state();
PRIVATE Free_state();
PRIVATE remove_settings();
PRIVATE struct auto_def *merge_autos();
PRIVATE merge_states();
PRIVATE struct lint_stack_entry *find_wdf(), *find_wdfc(), *find_cs();
1990-12-07 14:42:26 +00:00
PRIVATE cont_merge();
PRIVATE break_merge();
PRIVATE struct lint_stack_entry *mk_lint_stack_entry();
1989-02-07 11:04:05 +00:00
PRIVATE lint_push();
PRIVATE lint_pop();
lint_init_stack()
{
1990-12-07 14:42:26 +00:00
/* Allocate memory for the global lint-stack elements.
*/
top_ls = new_lint_stack_entry();
top_ls->ls_current = new_state();
1989-02-07 11:04:05 +00:00
}
lint_start_local()
{
register struct brace *br = new_brace();
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_start_local");
1989-02-07 11:04:05 +00:00
brace_count++;
br->br_count = brace_count;
br->br_level = level;
br->next = top_br;
top_br = br;
}
1990-12-07 14:42:26 +00:00
lint_end_local(stl)
1989-02-07 11:04:05 +00:00
struct stack_level *stl;
{
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_end_local");
1989-02-07 11:04:05 +00:00
if (s_NOTREACHED) {
top_ls->ls_current->st_notreached = 1;
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_warned = 0;
1989-02-07 11:04:05 +00:00
s_NOTREACHED = 0;
}
if (top_ls->ls_class == CASE && level == top_ls->ls_level) {
1990-12-07 14:42:26 +00:00
/* supply missing break; at end of switch */
1989-02-07 11:04:05 +00:00
lint_break_stmt();
}
check_autos();
end_brace(stl);
}
PRIVATE
end_brace(stl)
struct stack_level *stl;
{
/* Check if static variables and labels are used and/or set.
1990-12-07 14:42:26 +00:00
Automatic vars have already been checked by check_autos().
1989-02-07 11:04:05 +00:00
*/
register struct stack_entry *se = stl->sl_entry;
register struct brace *br;
while (se) {
register struct idf *idf = se->se_idf;
register struct def *def = idf->id_def;
if (def) {
lint_1_local(idf, def);
}
se = se->next;
}
1990-12-07 14:42:26 +00:00
/* remove entry from brace stack */
1989-02-07 11:04:05 +00:00
br = top_br;
top_br = br->next;
free_brace(br);
}
PRIVATE
lint_1_local(idf, def)
struct idf *idf;
struct def *def;
{
register int sc = def->df_sc;
if ( (sc == STATIC || sc == LABEL)
&& !def->df_used
1990-12-07 14:42:26 +00:00
&& !is_anon_idf(idf)
1989-02-07 11:04:05 +00:00
) {
1990-12-07 14:42:26 +00:00
def_warning(def, "%s %s not applied anywhere in function %s",
1989-02-07 11:04:05 +00:00
symbol2str(sc), idf->id_text, func_name);
}
if ( loptions['h']
&& sc == AUTO
&& !def->df_initialized
&& def->df_firstbrace != 0
&& def->df_minlevel != level
1990-12-07 14:42:26 +00:00
&& !is_anon_idf(idf)
1989-02-07 11:04:05 +00:00
) {
register int diff = def->df_minlevel - level;
def_warning(def,
"local %s could be declared %d level%s deeper",
idf->id_text, diff, (diff == 1 ? "" : "s")
);
}
}
1990-12-07 14:42:26 +00:00
lint_end_global(stl)
1989-02-07 11:04:05 +00:00
struct stack_level *stl;
{
register struct stack_entry *se = stl->sl_entry;
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_end_global");
1989-02-07 11:04:05 +00:00
ASSERT(level == L_GLOBAL);
while (se) {
register struct idf *idf = se->se_idf;
register struct def *def = idf->id_def;
if (def) {
lint_1_global(idf, def);
}
se = se->next;
}
}
PRIVATE
lint_1_global(idf, def)
struct idf *idf;
struct def *def;
{
register int sc = def->df_sc;
register int fund = def->df_type->tp_fund;
switch (sc) {
case STATIC:
case EXTERN:
case GLOBAL:
1990-12-07 14:42:26 +00:00
#ifdef IMPLICIT
1989-02-07 11:04:05 +00:00
case IMPLICIT:
1990-12-07 14:42:26 +00:00
#endif IMPLICIT
1989-02-07 11:04:05 +00:00
if (fund == ERRONEOUS)
break;
if (def->df_set || def->df_used) {
/* Output a line to the intermediate file for
* used external variables (including functions)
*/
output_use(idf);
}
1990-12-07 14:42:26 +00:00
if (sc == STATIC && !def->df_used) {
if (def->df_set) {
if (!is_anon_idf(idf) && fund != ERRONEOUS) {
1989-02-07 11:04:05 +00:00
def_warning(def,
"%s %s %s set but not used",
symbol2str(sc),
symbol2str(fund),
idf->id_text);
}
}
1990-12-07 14:42:26 +00:00
else {
if (!is_anon_idf(idf) && fund != ERRONEOUS) {
1989-02-07 11:04:05 +00:00
def_warning(def,
"%s %s %s not used anywhere",
symbol2str(sc),
symbol2str(fund),
idf->id_text);
}
}
}
1990-12-07 14:42:26 +00:00
if (loptions['x']) {
register char *fn = def->df_file;
if ( (sc == EXTERN || sc == GLOBAL)
&& def->df_alloc == 0
&& !def->df_set
&& !def->df_initialized
&& !def->df_used
&& strcmp(&fn[strlen(fn)-2], ".c") == 0
&& !is_anon_idf(idf)
&& fund != ERRONEOUS
) {
def_warning(def,
"%s %s %s not used anywhere",
symbol2str(sc),
symbol2str(fund),
idf->id_text);
}
}
1989-02-07 11:04:05 +00:00
break;
}
}
change_state(idf, to_state)
struct idf *idf;
int to_state; /* SET or USED */
{
/* Changes the state of the variable identified by idf in the current state
* on top of the stack.
1990-12-07 14:42:26 +00:00
* The fields in the def-descriptor are set too.
1989-02-07 11:04:05 +00:00
*/
register struct def *def = idf->id_def;
register struct auto_def *a = top_ls->ls_current->st_auto_list;
1990-12-07 14:42:26 +00:00
ASSERT(def);
1989-02-07 11:04:05 +00:00
1990-12-07 14:42:26 +00:00
switch (to_state) {
case SET:
def->df_set = 1;
break;
case USED:
def->df_used = 1;
break;
}
1989-02-07 11:04:05 +00:00
1990-12-07 14:42:26 +00:00
/* adjust minimum required brace level */
if (def->df_firstbrace == 0) {
def->df_firstbrace = brace_count;
def->df_minlevel = level;
}
else {
register struct brace *br = top_br;
/* find the smallest brace range from which
firstbrace is visible
*/
while (br && br->br_count > def->df_firstbrace) {
br = br->next;
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
ASSERT(br && def->df_minlevel >= br->br_level);
def->df_minlevel = br->br_level;
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
/* search auto_list */
1989-02-07 11:04:05 +00:00
while(a && a->ad_idf != idf)
a = a->next;
1990-12-07 14:42:26 +00:00
if (a == 0) /* identifier not in list, global definition */
1989-02-07 11:04:05 +00:00
return;
switch (to_state) {
case SET:
a->ad_maybe_set = 0;
a->ad_set = 1;
break;
case USED:
if (!a->ad_set) {
1990-12-07 14:42:26 +00:00
if (!is_anon_idf(idf)) {
warning("variable %s%s uninitialized",
idf->id_text,
(a->ad_maybe_set ? " possibly" : "")
);
}
1989-02-07 11:04:05 +00:00
a->ad_maybe_set = 0;
a->ad_set = 1; /* one warning */
}
a->ad_used = 1;
break;
}
}
add_auto(idf) /* to current state on top of lint_stack */
struct idf *idf;
{
/* Check if idf's definition is really an auto (or register).
* It could be a static or extern too.
1990-12-07 14:42:26 +00:00
* Watch out for formal parameters.
1989-02-07 11:04:05 +00:00
*/
register struct def *def = idf->id_def;
1990-12-07 14:42:26 +00:00
ASSERT(def);
1989-02-07 11:04:05 +00:00
switch (def->df_sc) {
1990-12-07 14:42:26 +00:00
register struct auto_def *a;
1989-02-07 11:04:05 +00:00
case AUTO:
case REGISTER:
if (def->df_level < L_LOCAL)
1990-12-07 14:42:26 +00:00
return; /* a formal */
1989-02-07 11:04:05 +00:00
a = new_auto_def();
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
a->ad_idf = idf;
1990-12-07 14:42:26 +00:00
a->ad_def = def;
1989-02-07 11:04:05 +00:00
a->ad_used = def->df_used;
a->ad_set = def->df_set;
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
a->next = top_ls->ls_current->st_auto_list;
top_ls->ls_current->st_auto_list = a;
}
}
PRIVATE
check_autos()
{
1990-12-07 14:42:26 +00:00
/* Before leaving a block, remove the auto_defs of the automatic
1989-02-07 11:04:05 +00:00
* variables on this level and check if they are used
*/
1990-12-07 14:42:26 +00:00
register struct auto_def *a = top_ls->ls_current->st_auto_list;
1989-02-07 11:04:05 +00:00
1990-12-07 14:42:26 +00:00
ASSERT(!(a && a->ad_def->df_level > level));
while (a && a->ad_def->df_level == level) {
struct idf *idf = a->ad_idf;
struct def *def = idf->id_def;
if (!def->df_used && !is_anon_idf(idf)) {
if (def->df_set || a->ad_maybe_set) {
def_warning(def,
1989-02-07 11:04:05 +00:00
"%s set but not used in function %s",
1990-12-07 14:42:26 +00:00
idf->id_text, func_name);
1989-02-07 11:04:05 +00:00
}
else {
1990-12-07 14:42:26 +00:00
def_warning(def,
"%s not used anywhere in function %s",
idf->id_text, func_name);
1989-02-07 11:04:05 +00:00
}
}
1990-12-07 14:42:26 +00:00
{ /* free a */
register struct auto_def *aux = a;
a = a->next;
free_auto_def(aux);
}
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_auto_list = a;
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
lint_end_formals()
1989-02-07 11:04:05 +00:00
{
register struct stack_entry *se = local_level->sl_entry;
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_end_formals");
1989-02-07 11:04:05 +00:00
ASSERT(level == L_FORMAL1);
while (se) {
register struct def *def = se->se_idf->id_def;
if ( (def && !def->df_used)
&& !(f_ARGSUSED || LINTLIB)
1990-12-07 14:42:26 +00:00
&& !is_anon_idf(se->se_idf)
1989-02-07 11:04:05 +00:00
) {
def_warning(def, "argument %s not used in function %s",
se->se_idf->id_text, func_name);
}
se = se->next;
}
}
PRIVATE struct auto_def *
1990-12-07 14:42:26 +00:00
copy_auto_list(from_al, lvl)
1989-02-07 11:04:05 +00:00
struct auto_def *from_al;
1990-12-07 14:42:26 +00:00
int lvl;
1989-02-07 11:04:05 +00:00
{
struct auto_def *start = 0;
register struct auto_def **hook = &start;
1990-12-07 14:42:26 +00:00
/* skip too high levels */
1989-02-07 11:04:05 +00:00
while (from_al && from_al->ad_def->df_level > lvl) {
from_al = from_al->next;
}
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
while (from_al) {
register struct auto_def *a = new_auto_def();
*hook = a;
*a = *from_al;
hook = &a->next;
from_al = from_al->next;
}
return start;
}
PRIVATE
1990-12-07 14:42:26 +00:00
free_auto_list(a)
1989-02-07 11:04:05 +00:00
register struct auto_def *a;
1990-12-07 14:42:26 +00:00
{
while (a) {
register struct auto_def *aux = a;
a = a->next;
free_auto_def(aux);
1989-02-07 11:04:05 +00:00
}
}
PRIVATE struct state *
copy_state(from_st, lvl)
struct state *from_st;
1990-12-07 14:42:26 +00:00
int lvl;
1989-02-07 11:04:05 +00:00
{
/* Memory for the struct state and the struct auto_defs is allocated
* by this function
*/
register struct state *st = new_state();
1990-12-07 14:42:26 +00:00
st->st_auto_list = copy_auto_list(from_st->st_auto_list, lvl);
1989-02-07 11:04:05 +00:00
st->st_notreached = from_st->st_notreached;
st->st_warned = from_st->st_warned;
return st;
}
PRIVATE
Free_state(stp)
struct state **stp;
{
/* This function also frees the list of auto_defs
*/
1990-12-07 14:42:26 +00:00
free_auto_list((*stp)->st_auto_list);
1989-02-07 11:04:05 +00:00
free_state(*stp);
*stp = 0;
}
PRIVATE
1990-12-07 14:42:26 +00:00
remove_settings(st, lvl)
struct state *st;
int lvl;
1989-02-07 11:04:05 +00:00
{
1990-12-07 14:42:26 +00:00
/* The states of all variables on this level are set to 'not set' and
1989-02-07 11:04:05 +00:00
* 'not maybe set'. (I think you have to read this twice.)
*/
1990-12-07 14:42:26 +00:00
register struct auto_def *a = st->st_auto_list;
1989-02-07 11:04:05 +00:00
while (a && a->ad_def->df_level == lvl) {
a->ad_set = a->ad_maybe_set = 0;
a = a->next;
}
}
/******** M E R G E ********/
/* modes for merging */
#define NORMAL 0
#define CASE_BREAK 1
#define USE_ONLY 2
1990-12-07 14:42:26 +00:00
PRIVATE
merge_states(st1, st2, lvl, mode)
struct state *st1, *st2;
int lvl;
int mode; /* NORMAL or CASE_BREAK */
{
/* st2 becomes the result.
* st1 is left unchanged.
* The resulting state is the state the program gets in if st1 OR st2
* becomes the state. (E.g. the states at the end of an if-part and an
* end-part are merged by this function.)
*/
if (st1->st_notreached) {
if (mode == NORMAL || st2->st_notreached) {
st2->st_auto_list =
merge_autos(st1->st_auto_list,
st2->st_auto_list, lvl, USE_ONLY);
}
}
else
if (st2->st_notreached) {
register struct auto_def *tmp = st2->st_auto_list;
st2->st_auto_list = copy_auto_list(st1->st_auto_list, lvl);
st2->st_notreached = 0;
st2->st_warned = 0;
st2->st_auto_list = merge_autos(tmp, st2->st_auto_list,
lvl, USE_ONLY);
free_auto_list(tmp);
}
else {
/* both st1 and st2 reached */
st2->st_auto_list =
merge_autos(st1->st_auto_list, st2->st_auto_list,
lvl, mode);
}
}
1989-02-07 11:04:05 +00:00
PRIVATE struct auto_def *
merge_autos(a1, a2, lvl, mode)
struct auto_def *a1, *a2;
1990-12-07 14:42:26 +00:00
int lvl;
1989-02-07 11:04:05 +00:00
int mode;
{
/* Returns a pointer to the result.
* a1 is left unchanged.
* a2 is used to create this result.
* The fields are set as follows:
* a1_set + a2_set -> set
* + a?_maybe_set -> maybe set
* ELSE -> NOT set && NOT maybe set
* * + a?_used -> used
*
* For mode == CASE_BREAK:
* First a2 is taken as the result, then
* variables NOT set in a2 and set or maybe set in a1 become 'maybe set'
*
* For mode == USE_ONLY:
* Start with a2 as the result.
* Variables used in a1 become used in a2.
* The rest of the result is not changed.
*/
register struct auto_def *a;
1990-12-07 14:42:26 +00:00
/* skip too local entries */
1989-02-07 11:04:05 +00:00
while (a1 && a1->ad_def->df_level > lvl) {
a1 = a1->next;
}
1990-12-07 14:42:26 +00:00
/* discard too local entries */
1989-02-07 11:04:05 +00:00
while (a2 && a2->ad_def->df_level > lvl) {
1990-12-07 14:42:26 +00:00
register struct auto_def *aux = a2;
1989-02-07 11:04:05 +00:00
a2 = a2->next;
1990-12-07 14:42:26 +00:00
free_auto_def(aux);
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
a = a2; /* pointer to the result */
while (a1) {
ASSERT(a2);
1990-12-07 14:42:26 +00:00
/* merge the auto_defs for one idf */
1989-02-07 11:04:05 +00:00
ASSERT(a1->ad_idf == a2->ad_idf);
if (a1->ad_used)
a2->ad_used = 1;
if (mode != USE_ONLY) {
if ( ( !a2->ad_set
&& (a1->ad_set || a1->ad_maybe_set)
)
|| ( mode == NORMAL
&& !a1->ad_set
&& (a2->ad_set || a2->ad_maybe_set)
)
) {
a2->ad_set = 0;
a2->ad_maybe_set = 1;
}
}
a1 = a1->next;
a2 = a2->next;
}
ASSERT(!a2);
return a;
}
/******** L I N T S T A C K S E A R C H I N G ********/
/* The next four find-functions search the lint_stack for an entry.
* The letters mean : w: WHILE; d: DO; f: FOR; s: SWITCH; c: CASE.
*/
PRIVATE struct lint_stack_entry *
find_wdf()
{
register struct lint_stack_entry *lse = top_ls;
1990-12-07 14:42:26 +00:00
while (lse) {
1989-02-07 11:04:05 +00:00
switch (lse->ls_class) {
case WHILE:
case DO:
case FOR:
return lse;
}
lse = lse->ls_previous;
}
return 0;
}
PRIVATE struct lint_stack_entry *
find_wdfc()
{
register struct lint_stack_entry *lse = top_ls;
1990-12-07 14:42:26 +00:00
while (lse) {
1989-02-07 11:04:05 +00:00
switch (lse->ls_class) {
case WHILE:
case DO:
case FOR:
case CASE:
return lse;
}
lse = lse->ls_previous;
}
return 0;
}
PRIVATE struct lint_stack_entry *
find_cs()
{
register struct lint_stack_entry *lse = top_ls;
1990-12-07 14:42:26 +00:00
while (lse) {
1989-02-07 11:04:05 +00:00
switch (lse->ls_class) {
case CASE:
case SWITCH:
return lse;
}
lse = lse->ls_previous;
}
return 0;
}
1990-12-07 14:42:26 +00:00
/******** A C T I O N S : I F ********/
1989-02-07 11:04:05 +00:00
start_if_part(const)
{
1990-12-07 14:42:26 +00:00
register struct lint_stack_entry *new = mk_lint_stack_entry(IF);
1989-02-07 11:04:05 +00:00
1990-12-07 14:42:26 +00:00
dbg_lint_stack("start_if_part");
1989-02-07 11:04:05 +00:00
if (const)
hwarning("condition in if statement is constant");
1990-12-07 14:42:26 +00:00
lint_push(new);
/* ls_current: the state at the start of the if-part
*/
1989-02-07 11:04:05 +00:00
}
start_else_part()
{
1990-12-07 14:42:26 +00:00
/* ls_current: the state at the end of the if-part
ls_previous->ls_current: the state before the if-part
*/
dbg_lint_stack("start_else_part");
1989-02-07 11:04:05 +00:00
if (s_NOTREACHED) {
top_ls->ls_current->st_notreached = 1;
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_warned = 0;
1989-02-07 11:04:05 +00:00
s_NOTREACHED = 0;
}
1990-12-07 14:42:26 +00:00
top_ls->LS_IF = top_ls->ls_current;
1989-02-07 11:04:05 +00:00
/* this is the reason why ls_current is a pointer */
1990-12-07 14:42:26 +00:00
top_ls->ls_current =
copy_state(top_ls->ls_previous->ls_current, level);
1989-02-07 11:04:05 +00:00
top_ls->ls_level = level;
1990-12-07 14:42:26 +00:00
/* ls_current: the state before the if-part and the else-part
LS_IF: the state at the end of the if-part
*/
1989-02-07 11:04:05 +00:00
}
end_if_else_stmt()
{
1990-12-07 14:42:26 +00:00
/* ls_current: state at the end of the else-part
LS_IF: state at the end of the if-part
*/
dbg_lint_stack("end_if_else_stmt");
merge_states(top_ls->LS_IF, top_ls->ls_current,
1989-02-07 11:04:05 +00:00
top_ls->ls_level, NORMAL);
1990-12-07 14:42:26 +00:00
Free_state(&top_ls->LS_IF);
Free_state(&top_ls->ls_previous->ls_current);
1989-02-07 11:04:05 +00:00
top_ls->ls_previous->ls_current = top_ls->ls_current;
lint_pop();
}
end_if_stmt()
{
1990-12-07 14:42:26 +00:00
/* No else-part met.
ls_current: state at the end of the if-part
*/
dbg_lint_stack("end_if_stmt");
1989-02-07 11:04:05 +00:00
merge_states(top_ls->ls_current, top_ls->ls_previous->ls_current,
top_ls->ls_level, NORMAL);
Free_state(&top_ls->ls_current);
lint_pop();
}
1990-12-07 14:42:26 +00:00
/******** A C T I O N S : L O O P S ********/
start_while_stmt(expr)
struct expr *expr;
{
if (is_cp_cst(expr)) {
start_loop_stmt(WHILE, 1, expr->VL_VALUE != (arith)0);
}
else {
start_loop_stmt(WHILE, 0, 0);
}
}
start_do_stmt()
{
start_loop_stmt(DO, 1, 1);
}
start_for_stmt(expr)
struct expr *expr;
{
if (!expr) {
start_loop_stmt(FOR, 1, 1);
}
else
if (is_cp_cst(expr)) {
start_loop_stmt(FOR, 1, expr->VL_VALUE != (arith)0);
}
else {
start_loop_stmt(FOR, 0, 0);
}
}
PRIVATE
1989-02-07 11:04:05 +00:00
start_loop_stmt(looptype, const, cond)
{
1990-12-07 14:42:26 +00:00
/* If const, the condition is a constant and its value is cond
*/
register struct lint_stack_entry *new = mk_lint_stack_entry(looptype);
1989-02-07 11:04:05 +00:00
1990-12-07 14:42:26 +00:00
dbg_lint_stack("start_loop_stmt");
1989-02-07 11:04:05 +00:00
if (const && !cond) {
/* while (0) | for (;0;) */
1990-12-07 14:42:26 +00:00
hwarning("condition in %s statement is always false",
1989-02-07 11:04:05 +00:00
symbol2str(looptype));
1990-12-07 14:42:26 +00:00
new->ls_current->st_notreached = 1;
1989-02-07 11:04:05 +00:00
}
if (const && cond) {
/* while (1) | for (;;) | do */
1990-12-07 14:42:26 +00:00
/* omitting the copy for LS_LOOP will force this loop
1989-02-07 11:04:05 +00:00
to be treated as a do loop
*/
top_ls->ls_current->st_notreached = 1;
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_warned = 0;
1989-02-07 11:04:05 +00:00
}
else {
1990-12-07 14:42:26 +00:00
new->LS_LOOP = copy_state(top_ls->ls_current, level);
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
new->LS_TEST = (!const ? TEST_VAR : cond ? TEST_TRUE : TEST_FALSE);
lint_push(new);
/* ls_current: the state at the start of the body
LS_TEST: info about the loop test
LS_BODY: 0, the state at the end of the body
LS_LOOP: the state at the end of the loop, or 0 if the loop
does not end
*/
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
end_loop_body()
1989-02-07 11:04:05 +00:00
{
1990-12-07 14:42:26 +00:00
register struct lint_stack_entry *lse = find_wdf();
1989-02-07 11:04:05 +00:00
1990-12-07 14:42:26 +00:00
dbg_lint_stack("end_loop_body");
ASSERT(lse == top_ls);
if (!lse->ls_current->st_notreached)
cont_merge(lse);
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
end_loop_stmt()
1989-02-07 11:04:05 +00:00
{
1990-12-07 14:42:26 +00:00
register struct lint_stack_entry *lse = find_wdf();
dbg_lint_stack("end_loop_stmt");
ASSERT(lse == top_ls);
if (lse->LS_TEST != TEST_TRUE)
break_merge(lse);
dbg_lint_stack("end_loop_stmt after break_merge");
if (!top_ls->LS_LOOP) {
1989-02-07 11:04:05 +00:00
/* no break met; this is really an endless loop */
1990-12-07 14:42:26 +00:00
hwarning("endless %s loop", symbol2str(top_ls->ls_class));
Free_state(&top_ls->ls_current);
1989-02-07 11:04:05 +00:00
}
else {
1990-12-07 14:42:26 +00:00
Free_state(&top_ls->ls_current);
Free_state(&top_ls->ls_previous->ls_current);
top_ls->ls_previous->ls_current = top_ls->LS_LOOP;
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
lint_pop();
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
end_do_stmt(const, cond)
1989-02-07 11:04:05 +00:00
{
1990-12-07 14:42:26 +00:00
register struct lint_stack_entry *lse = find_wdf();
dbg_lint_stack("end_do_stmt");
if (const && !cond) {
/* do ... while (0) */
hwarning("condition in do statement is always false");
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
lse->LS_TEST = (!const ? TEST_VAR : cond ? TEST_TRUE : TEST_FALSE);
end_loop_stmt();
1989-02-07 11:04:05 +00:00
}
lint_continue_stmt()
{
register struct lint_stack_entry *lse = find_wdf();
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_continue_stmt");
1989-02-07 11:04:05 +00:00
if (!lse)
return; /* not inside a loop statement */
1990-12-07 14:42:26 +00:00
cont_merge(lse);
1989-02-07 11:04:05 +00:00
top_ls->ls_current->st_notreached = 1;
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_warned = 0;
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
/******** A C T I O N S : S W I T C H ********/
start_switch_part(const)
1989-02-07 11:04:05 +00:00
{
/* ls_current of a SWITCH entry has different meaning from ls_current of
* other entries. It keeps track of which variables are used in all
* following case parts. (Needed for variables declared in a compound
* switch-block.)
*/
1990-12-07 14:42:26 +00:00
register struct lint_stack_entry *new = mk_lint_stack_entry(SWITCH);
1989-02-07 11:04:05 +00:00
1990-12-07 14:42:26 +00:00
dbg_lint_stack("start_switch_part");
if (const)
1989-02-07 11:04:05 +00:00
hwarning("value in switch statement is constant");
1990-12-07 14:42:26 +00:00
new->LS_CASE = copy_state(top_ls->ls_current, level);
new->ls_current->st_notreached = 1;
new->ls_current->st_warned = 0;
1989-02-07 11:04:05 +00:00
top_ls->ls_current->st_notreached = 1;
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_warned = 0;
lint_push(new);
1989-02-07 11:04:05 +00:00
}
end_switch_stmt()
{
1990-12-07 14:42:26 +00:00
dbg_lint_stack("end_switch_stmt");
1989-02-07 11:04:05 +00:00
if (top_ls->ls_class == CASE) {
/* no break after last case or default */
lint_break_stmt(); /* introduce break */
}
if (!top_ls->LS_DEFAULT_MET) {
top_ls->ls_current->st_notreached = 0;
if (top_ls->LS_BREAK) {
merge_states(top_ls->ls_current, top_ls->LS_BREAK,
top_ls->ls_level, NORMAL);
Free_state(&top_ls->ls_current);
}
else {
top_ls->LS_BREAK = top_ls->ls_current;
}
}
else {
Free_state(&top_ls->ls_current);
}
if (top_ls->LS_BREAK) {
merge_states(top_ls->LS_CASE, top_ls->LS_BREAK,
top_ls->ls_level, CASE_BREAK);
Free_state(&top_ls->LS_CASE);
}
else {
top_ls->LS_BREAK = top_ls->LS_CASE;
}
top_ls->LS_BREAK->st_notreached =
top_ls->ls_previous->ls_current->st_notreached;
/* yack */
Free_state(&top_ls->ls_previous->ls_current);
if (!top_ls->LS_DEFAULT_MET)
top_ls->LS_BREAK->st_notreached = 0;
top_ls->ls_previous->ls_current = top_ls->LS_BREAK;
lint_pop();
}
lint_case_stmt(dflt)
{
/* A default statement is just a special case statement */
register struct lint_stack_entry *cs_entry = find_cs();
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_case_stmt");
1989-02-07 11:04:05 +00:00
if (!cs_entry)
return; /* not inside switch */
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
if (cs_entry != top_ls) {
warning("%s statement in strange context",
dflt ? "default" : "case");
return;
}
1990-12-07 14:42:26 +00:00
switch (cs_entry->ls_class) {
register struct lint_stack_entry *new;
case SWITCH:
1989-02-07 11:04:05 +00:00
if (dflt) {
cs_entry->LS_DEFAULT_MET = 1;
}
1990-12-07 14:42:26 +00:00
new = mk_lint_stack_entry(CASE);
remove_settings(new->ls_current, level);
lint_push(new);
break;
case CASE:
ASSERT(top_ls->ls_previous->ls_class == SWITCH);
1989-02-07 11:04:05 +00:00
if (dflt) {
cs_entry->ls_previous->LS_DEFAULT_MET = 1;
}
merge_states(top_ls->ls_current, top_ls->ls_previous->LS_CASE,
top_ls->ls_previous->ls_level, NORMAL);
merge_states(top_ls->ls_current,
top_ls->ls_previous->ls_current,
top_ls->ls_previous->ls_level, NORMAL);
Free_state(&top_ls->ls_current);
top_ls->ls_current =
copy_state(top_ls->ls_previous->ls_current,
top_ls->ls_previous->ls_level);
remove_settings(top_ls->ls_current, top_ls->ls_level);
1990-12-07 14:42:26 +00:00
break;
default:
NOTREACHED();
/*NOTREACHED*/
1989-02-07 11:04:05 +00:00
}
}
lint_break_stmt()
{
register struct lint_stack_entry *lse = find_wdfc();
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_break_stmt");
1989-02-07 11:04:05 +00:00
if (!lse)
return;
switch (lse->ls_class) {
case WHILE:
case FOR:
case DO:
/* loop break */
lse->ls_previous->ls_current->st_notreached = 0;
1990-12-07 14:42:26 +00:00
break_merge(lse);
1989-02-07 11:04:05 +00:00
break;
case CASE:
/* case break */
if (!top_ls->ls_current->st_notreached) {
lse->ls_previous->ls_previous->ls_current->st_notreached = 0;
}
merge_states(lse->ls_current, lse->ls_previous->ls_current,
lse->ls_previous->ls_level, NORMAL);
if (lse->ls_previous->LS_BREAK) {
1990-12-07 14:42:26 +00:00
merge_states(top_ls->ls_current,
lse->ls_previous->LS_BREAK,
lse->ls_previous->ls_level, NORMAL);
1989-02-07 11:04:05 +00:00
}
else {
1990-12-07 14:42:26 +00:00
lse->ls_previous->LS_BREAK =
copy_state(top_ls->ls_current,
lse->ls_previous->ls_level);
1989-02-07 11:04:05 +00:00
}
if (lse == top_ls) {
Free_state(&lse->ls_current);
lint_pop();
}
break;
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
default:
NOTREACHED();
/*NOTREACHED*/
}
top_ls->ls_current->st_notreached = 1;
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_warned = 0;
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
PRIVATE
cont_merge(lse)
struct lint_stack_entry *lse;
{
/* merge for continue statements */
if (lse->LS_BODY) {
merge_states(top_ls->ls_current, lse->LS_BODY,
lse->ls_level, NORMAL);
}
else {
lse->LS_BODY = copy_state(top_ls->ls_current, lse->ls_level);
}
}
PRIVATE
break_merge(lse)
struct lint_stack_entry *lse;
{
/* merge for break statements */
if (lse->LS_LOOP) {
merge_states(top_ls->ls_current, lse->LS_LOOP,
lse->ls_level, NORMAL);
}
else {
lse->LS_LOOP = copy_state(top_ls->ls_current, lse->ls_level);
}
}
/******** A C T I O N S : R E T U R N ********/
1989-02-07 11:04:05 +00:00
lint_start_function()
{
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_start_function");
valreturned = NORETURN; /* initialization */
return_warned = 0;
1989-02-07 11:04:05 +00:00
lint_comment_function();
}
lint_end_function()
{
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_end_function");
1989-02-07 11:04:05 +00:00
/* write the function definition record */
outdef();
1990-12-07 14:42:26 +00:00
/* At this stage it is possible that top_ls->ls_current is
1989-02-07 11:04:05 +00:00
* pointing to a state with a list of auto_defs.
* These auto_defs must be freed and the state must be filled
* with zeros.
*/
1990-12-07 14:42:26 +00:00
ASSERT(!top_ls->ls_previous);
free_auto_list(top_ls->ls_current->st_auto_list);
1989-02-07 11:04:05 +00:00
top_ls->ls_current->st_auto_list = 0;
top_ls->ls_current->st_notreached = 0;
top_ls->ls_current->st_warned = 0;
}
1990-12-07 14:42:26 +00:00
lint_implicit_return()
{
dbg_lint_stack("lint_implicit_return");
if (!top_ls->ls_current->st_notreached) {
lint_return_stmt(NOVALRETURNED);
}
}
1989-02-07 11:04:05 +00:00
lint_return_stmt(e)
int e;
{
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_return_stmt");
if (valreturned == NORETURN) {
/* first return met */
register int fund = func_type->tp_fund;
if ( e == NOVALRETURNED
&& !func_notypegiven
&& fund != VOID
&& fund != ERRONEOUS
) {
warning("function %s declared %s%s but no value returned",
func_name,
(func_type->tp_unsigned && fund != POINTER) ?
"unsigned " : "",
symbol2str(fund)
);
/* adjust */
e = VALRETURNED;
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
valreturned = e;
}
else
if (valreturned != e && !return_warned) {
warning("function %s does not always return a value",
func_name);
return_warned = 1;
}
if (!top_ls->ls_current->st_notreached) {
set_od_valreturned(valreturned);
1989-02-07 11:04:05 +00:00
}
top_ls->ls_current->st_notreached = 1;
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_warned = 0;
1989-02-07 11:04:05 +00:00
}
1990-12-07 14:42:26 +00:00
/******** A C T I O N S : J U M P ********/
1989-02-07 11:04:05 +00:00
lint_jump_stmt(idf)
struct idf *idf;
{
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_jump_stmt");
1989-02-07 11:04:05 +00:00
top_ls->ls_current->st_notreached = 1;
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_warned = 0;
if (idf->id_def)
idf->id_def->df_used = 1;
1989-02-07 11:04:05 +00:00
}
lint_label()
{
/* When meeting a label, we should take the intersection of all
settings at all goto's leading this way, but this cannot reasonably
be done. So we assume that the user knows what he is doing and set
all automatic variables to set.
*/
register struct auto_def *a = top_ls->ls_current->st_auto_list;
1990-12-07 14:42:26 +00:00
dbg_lint_stack("lint_label");
1989-02-07 11:04:05 +00:00
while (a) {
a->ad_maybe_set = 0;
a->ad_set = 1;
a = a->next;
}
}
1990-12-07 14:42:26 +00:00
/******** A C T I O N S : S T A T E M E N T ********/
1989-02-07 11:04:05 +00:00
lint_statement()
{
1990-12-07 14:42:26 +00:00
/* Check if this statement can be reached
*/
dbg_lint_stack("lint_statement");
1989-02-07 11:04:05 +00:00
if (s_NOTREACHED) {
top_ls->ls_current->st_notreached = 1;
1990-12-07 14:42:26 +00:00
top_ls->ls_current->st_warned = 0;
1989-02-07 11:04:05 +00:00
s_NOTREACHED = 0;
}
if (DOT == '{' || DOT == ';')
return;
if (top_ls->ls_current->st_warned)
return;
if (top_ls->ls_current->st_notreached) {
if (DOT != CASE && DOT != DEFAULT && AHEAD != ':') {
if (DOT != BREAK || loptions['b'])
warning("statement cannot be reached");
top_ls->ls_current->st_warned = 1;
}
else {
top_ls->ls_current->st_notreached = 0;
top_ls->ls_current->st_warned = 0;
}
}
}
1990-12-07 14:42:26 +00:00
PRIVATE struct lint_stack_entry *
mk_lint_stack_entry(cl)
int cl;
{
/* Prepare a new stack entry for the lint_stack with class cl.
Copy the top ls_current to this entry and set its level.
*/
register struct lint_stack_entry *new = new_lint_stack_entry();
new->ls_class = cl;
new->ls_current = copy_state(top_ls->ls_current, level);
new->ls_level = level;
return new;
}
1989-02-07 11:04:05 +00:00
PRIVATE
lint_push(lse)
struct lint_stack_entry *lse;
{
lse->ls_previous = top_ls;
top_ls->next = lse;
top_ls = lse;
}
PRIVATE
lint_pop()
{
top_ls = top_ls->ls_previous;
free_lint_stack_entry(top_ls->next);
}
#ifdef DEBUG
/* FOR DEBUGGING */
1990-12-07 14:42:26 +00:00
PRIVATE
print_autos(a)
struct auto_def *a;
{
while (a) {
struct idf *idf = a->ad_idf;
struct def *def = idf->id_def;
print("%s", idf->id_text);
print("(lvl=%d)", a->ad_def->df_level);
print("(u%ds%dm%d U%dS%d) ",
a->ad_used, a->ad_set, a->ad_maybe_set,
def->df_used, def->df_set
);
a = a->next;
}
}
PRIVATE
pr_lint_state(nm, st)
char *nm;
struct state *st;
{
print("%s: ", nm);
if (st) {
print("notreached == %d ", st->st_notreached);
print_autos(st->st_auto_list);
}
else {
print("NULL");
}
print("\n");
}
print_lint_stack(msg)
char *msg;
1989-02-07 11:04:05 +00:00
{
register struct lint_stack_entry *lse = top_ls;
1990-12-07 14:42:26 +00:00
print("Lint stack: %s(level=%d)\n", msg, level);
1989-02-07 11:04:05 +00:00
while (lse) {
print(" |-------------- level %d ------------\n",
lse->ls_level);
1990-12-07 14:42:26 +00:00
pr_lint_state(" |current", lse->ls_current);
1989-02-07 11:04:05 +00:00
print(" |class == %s\n",
lse->ls_class ? symbol2str(lse->ls_class) : "{");
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
switch (lse->ls_class) {
case SWITCH:
1990-12-07 14:42:26 +00:00
pr_lint_state(" |LS_BREAK", lse->LS_BREAK);
pr_lint_state(" |LS_CASE", lse->LS_CASE);
1989-02-07 11:04:05 +00:00
break;
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
case DO:
case WHILE:
case FOR:
1990-12-07 14:42:26 +00:00
print(" |LS_TEST == %s\n",
lse->LS_TEST == TEST_VAR ? "TEST_VAR" :
lse->LS_TEST == TEST_TRUE ? "TEST_TRUE" :
lse->LS_TEST == TEST_FALSE ? "TEST_FALSE" :
"<<BAD VALUE>>"
);
pr_lint_state(" |LS_BODY", lse->LS_BODY);
pr_lint_state(" |LS_LOOP", lse->LS_LOOP);
1989-02-07 11:04:05 +00:00
break;
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
case IF:
1990-12-07 14:42:26 +00:00
pr_lint_state(" |LS_IF", lse->LS_IF);
1989-02-07 11:04:05 +00:00
break;
1990-12-07 14:42:26 +00:00
1989-02-07 11:04:05 +00:00
default:
break;
}
lse = lse->ls_previous;
}
print(" |--------------\n\n");
}
#endif DEBUG
#endif LINT