📄 error.c
字号:
/*
Copyright 1994-2003 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
This program is derived from the cc68k complier by
Matthew Brandt (mattb@walkingdog.net)
You may contact the author of this derivative at:
mailto::camille@bluegrass.net
or by snail mail at:
David Lindauer
850 Washburn Ave Apt 99
Louisville, KY 40222
*/
/*
* error handler
*/
#include <stdio.h>
#include <string.h>
#include "lists.h"
#include "expr.h"
#include "c.h"
#include "ccerr.h"
#include "diag.h"
extern int prm_cmangle;
extern int prm_errfile;
extern FILE *errFile;
extern int global_flag;
extern FILE *listFile;
extern int errlineno;
extern char *errfile;
extern int prm_asmfile;
extern int outputFile;
extern int prm_maxerr;
extern int prm_diag;
extern int prm_listfile;
extern int lastch;
extern enum e_sym lastst;
extern char lastid[];
extern FILE *inclfile[10]; /* shared with preproc */
extern int incldepth; /* shared with preproc */
extern char *infile;
extern SYM *currentfunc;
extern int prm_warning, prm_cplusplus, prm_extwarning, prm_quieterrors, prm_c99;
extern int prm_ansi;
int diagcount = 0;
int referrorlvl = 3;
ERRORS *errlist = 0;
static ERRORS *errtail = 0;
static ERRORS *curerr = 0;
static char expectlist[] =
{
"#####################################################:{}.#])#,;"
};
static int errline;
/* table of which warnings are enabled */
char nowarn[ERR_MAX];
/* table of warning keywords for the -w command line option
*/
char warnarray[ERR_MAX][4] =
{
"all", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"cln", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"ret", "sun", "sud", "sas", "npo", "urc", "fun", "cno", "ieq", "",
"nco", "lad", "", "", "zer", "dpc", "nsf", "lun", "pro", "cnv", "",
"irg", "san", "ssu", "", "", "", "", "", "tua", "", "tui", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "suz", "fsu", "lli",
"", "", "", "", "", "spc", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "IPR", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"",
};
int total_errors = 0;
void initerr(void)
{
errlist = errtail = curerr = 0;
total_errors = 0;
diagcount = 0;
errline = 0;
}
/*
* handling for warnings on the command line
*/
void warning_setup(char select, char *string)
{
int bool = FALSE;
while (*string)
{
int i;
if (string[0] == '-')
{
bool = TRUE;
string++;
}
else
if (string[0] == '+')
string++;
for (i = 0; i < ERR_MAX; i++)
if (!strncmp(warnarray[i], string, 3))
{
if (i == 0)
{
int j;
for (j = 0; j < ERR_MAX; j++)
nowarn[j] = (unsigned char)bool;
}
else
nowarn[i] = (unsigned char)bool;
string += 3;
break;
}
if (i == ERR_MAX)
{
fatal("Invalid warning");
}
}
}
//-------------------------------------------------------------------------
#ifdef DIAGNOSTICS
void diag(char *s)
/*
* internal diags come here
*/
{
diagcount++;
if (prm_diag)
{
char buf[256];
if (currentfunc)
sprintf(buf, "%s(%s)", s, currentfunc->name);
else
strcpy(buf, s);
printf("DIAG - %s\n", buf);
if (prm_errfile && errFile)
fprintf(errFile, "/*DIAG - %s*/", buf);
if (prm_listfile && listFile)
fprintf(listFile, "/*DIAG - %s*/", buf);
if (prm_asmfile)
oprintf(outputFile, "DIAG - %s\n", buf);
}
}
#endif
int printerr(char *buf, ERRORS *err)
/*
* subroutine gets the error code and returns whether it is an error or
* warning
*/
{
int errlvl = 0;
switch (err->errornumber)
{
case ERR_NOCASE:
sprintf(buf, "Expected 'case' or 'default'");
break;
case ERR_PUNCT:
sprintf(buf, "Expected '%c'", expectlist[(int)err->data]);
break;
case ERR_INSERT:
sprintf(buf, "Inserted '%c'", expectlist[(int)err->data]);
break;
case ERR_NEEDCHAR:
sprintf(buf, "Expected '%c'", (char)err->data);
break;
case ERR_ILLCHAR:
sprintf(buf, "Illegal character '%c'", (char)err->data);
break;
case ERR_NEEDCONST:
sprintf(buf, "Constant value expected");
break;
case ERR_UNDEFINED:
sprintf(buf, "Undefined symbol %s", (char*)err->data);
break;
case ERR_DUPSYM:
sprintf(buf, "Duplicate symbol '%s'", (char*)err->data);
break;
case ERR_IDENTEXPECT:
sprintf(buf, "Expected '%s'", (char*)err->data);
break;
case ERR_IDEXPECT:
sprintf(buf, "Identifier expected");
break;
case ERR_EXTRAIDENT:
sprintf(buf,"Too many identifiers");
break;
case ERR_INITSIZE:
sprintf(buf, "Too many initializers");
break;
case ERR_NOINIT:
sprintf(buf, "Cannot initialize '%s' here", (char*)err->data);
break;
case ERR_PREPROCID:
sprintf(buf, "Invalid preprocessor directive '%s'", (char*)err
->data);
break;
case ERR_INCLFILE:
sprintf(buf, "File name expected in #include directive");
break;
case ERR_CANTOPEN:
sprintf(buf, "Cannot open file \"%s\" for read access", (char*)err
->data);
break;
case ERR_ILLCLASS:
sprintf(buf, "Illegal storage class specifier '%s'", (char*)err
->data);
break;
case ERR_ILLCLASS2:
sprintf(buf, "Illegal storage class specifier on '%s'", (char*)err
->data);
break;
case ERR_DUPCASE:
sprintf(buf, "Duplicate case %d", (int)err->data);
break;
case ERR_RETMISMATCH:
sprintf(buf, "Type mismatch in return");
break;
case ERR_ARGMISMATCH:
sprintf(buf, "Type mismatch in arg '%s'", (char*)err->data);
break;
case ERR_ARGLENSHORT:
sprintf(buf,
"parameter list too short in redeclaration of function '%s'",
(char*)err->data);
break;
case ERR_ARGLENLONG:
sprintf(buf,
"parameter list too long in redeclaration of function '%s'",
(char*)err->data);
break;
case ERR_DECLMISMATCH:
sprintf(buf, "Type mismatch in redeclaration of '%s'", (char*)err
->data);
break;
case ERR_CALLMISMATCH:
sprintf(buf, "Type mismatch in arg '%s'", (char*)err->data);
break;
case ERR_CALLLENSHORT:
sprintf(buf, "parameter list too short '%s'", (char*)err->data);
break;
case ERR_CALLLENLONG:
sprintf(buf, "parameter list too long '%s'", (char*)err->data);
errlvl = 1;
break;
case ERR_LABEL:
sprintf(buf, "'%s' is not a label", (char*)err->data);
break;
case ERR_NOPOINTER:
sprintf(buf, "Pointer type expected");
break;
case ERR_LVALUE:
sprintf(buf, "Lvalue expected");
break;
case ERR_NOFUNC:
sprintf(buf, "'%s' is not a function", (char*)err->data);
break;
case ERR_MISMATCH:
sprintf(buf, "Type mismatch");
break;
case ERR_ELSE:
sprintf(buf, "Misplaced else");
break;
case ERR_EXPREXPECT:
sprintf(buf, "Expression expected");
break;
case ERR_DEREF:
sprintf(buf, "Illegal pointer");
break;
case ERR_UNEXPECT:
if (lastst == id)
sprintf(buf, "Unexpected '%s'", lastid);
else
sprintf(buf, "Unexpected '%c'", lastch);
break;
case ERR_ILLTYPE:
sprintf(buf, "Illegal typedef of '%s'", (char*)err->data);
break;
case ERR_ARRAYMISMATCH:
sprintf(buf, "Non-scalar or negative array index");
break;
case ERR_PREPROCMATCH:
sprintf(buf, "Unbalanced preprocessor directives");
break;
case ERR_MACROSUBS:
sprintf(buf, "Macro substitution error");
break;
case ERR_DECLEXPECT:
sprintf(buf, "Declaration expected");
break;
case ERR_INVFLOAT:
sprintf(buf, "Invalid floating point");
break;
case ERR_INVTRAP:
sprintf(buf, "Invalid trap id");
break;
case ERR_BFILLEGAL:
sprintf(buf, "Cannot use bit field as a non-member");
break;
case ERR_BFTOOBIG:
sprintf(buf, "Bit field too big");
break;
case ERR_BFTYPE:
sprintf(buf, "Bit field only allowed on scalar types");
break;
case ERR_ERROR:
sprintf(buf, "Error Directive: %s", (char*)err->data);
break;
case ERR_INTERP:
sprintf(buf, "%s", (char*)err->data);
break;
case ERR_BFADDR:
sprintf(buf, "Cannot take address of bit field");
break;
case ERR_MODCONS:
sprintf(buf, "Cannot modify a const object");
break;
case ERR_SUSPICIOUSCONST:
sprintf(buf, "Suspicious use of const pointer");
errlvl = 1;
break;
case ERR_SZTYPE:
sprintf(buf, "Type expected in sizeof");
break;
case ERR_FUNCRETVAL2:
if (!prm_c99 && !prm_cplusplus)
errlvl = 1;
sprintf(buf, "Function should return a value");
break;
/* fall through */
case ERR_FUNCRETVAL:
errlvl = 1 ;
sprintf(buf, "Function should return a value");
break;
case ERR_STATICSYMUNUSED:
sprintf(buf, "Static variable '%s' is never used", (char*)err->data)
;
errlvl = 3;
break;
case ERR_SYMUNUSED:
sprintf(buf, "Variable '%s' is never used", (char*)err->data);
errlvl = 3;
break;
case ERR_PARAMUNUSED:
sprintf(buf, "Parameter '%s' is never used", (char*)err->data);
errlvl = 3;
break;
case ERR_FUNCUNUSED:
sprintf(buf, "Static function '%s' is never used", (char*)err->data)
;
errlvl = 3;
break;
case ERR_SYMUNDEF:
sprintf(buf, "Possible use of '%s' before assignment", (char*)err
->data);
errlvl = 3;
break;
case ERR_SYMASSIGNED:
sprintf(buf,
"Variable '%s' is possibly assigned a value which is never used", (char*)err->data);
errlvl = 3;
break;
case ERR_NONPORT:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -