From a68a3545ffb003350ccc5bc933172516b54f3661 Mon Sep 17 00:00:00 2001 From: johanknol Date: Wed, 26 Sep 2001 16:51:03 +0000 Subject: [PATCH] pointer issues git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@1314 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- src/SDCCast.c | 25 +++++++++------------ src/SDCCicode.c | 51 +++++++++++++++++++++++++++++++----------- support/Util/SDCCerr.c | 19 ++++++++++------ support/Util/SDCCerr.h | 10 +++++---- 4 files changed, 67 insertions(+), 38 deletions(-) diff --git a/src/SDCCast.c b/src/SDCCast.c index 09ad2b53..94aa8e13 100644 --- a/src/SDCCast.c +++ b/src/SDCCast.c @@ -745,7 +745,7 @@ processParms (ast * func, /* the parameter type must be at least castable */ if (compareType (defParm->type, actParm->ftype) == 0) { - werror (W_INCOMPAT_CAST); + werror (E_INCOMPAT_TYPES); fprintf (stderr, "type --> '"); printTypeChain (actParm->ftype, stderr); fprintf (stderr, "' "); @@ -2082,8 +2082,12 @@ decorateType (ast * tree) { sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree)); COPYTYPE (TTYPE (tree), TETYPE (tree), ltc); - if (!tree->initMode && IS_CONSTANT (TETYPE (tree))) + if (!tree->initMode) { + if ((IS_SPEC(TETYPE(tree)) && IS_CONSTANT (TETYPE (tree))) || + (IS_PTR(TTYPE(tree)) && DCL_PTR_CONST(TTYPE(tree)))) { werror (E_CODE_WRITE, "++/--"); + } + } if (tree->right) RLVAL (tree) = 1; @@ -2732,7 +2736,7 @@ decorateType (ast * tree) /* make sure the type is complete and sane */ checkTypeSanity(LETYPE(tree), "(cast)"); -#if 1 +#if 0 /* if the right is a literal replace the tree */ if (IS_LITERAL (RETYPE (tree))) { if (!IS_PTR (LTYPE (tree))) { @@ -3106,22 +3110,15 @@ decorateType (ast * tree) fprintf (stderr, "'\n"); } - /* extra checks for pointer types */ - if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)) && - !IS_GENPTR (LTYPE (tree))) - { - if (DCL_TYPE (LTYPE (tree)) != DCL_TYPE (RTYPE (tree))) - werror (W_PTR_ASSIGN); - } - TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree)); RRVAL (tree) = 1; LLVAL (tree) = 1; if (!tree->initMode ) { - if (IS_CONSTANT (LETYPE (tree))) { - werror (E_CODE_WRITE, " "); - } + if ((IS_SPEC(LETYPE(tree)) && IS_CONSTANT (LETYPE (tree))) || + (IS_PTR(LTYPE(tree)) && DCL_PTR_CONST(LTYPE(tree)))) { + werror (E_CODE_WRITE, " "); + } } if (LRVAL (tree)) { diff --git a/src/SDCCicode.c b/src/SDCCicode.c index 2c95c640..628a1dba 100644 --- a/src/SDCCicode.c +++ b/src/SDCCicode.c @@ -1595,20 +1595,45 @@ geniCodeCast (sym_link * type, operand * op, bool implicit) return operandFromValue (valCastLiteral (type, operandLitValue (op))); - /* if casting to some pointer type && - the destination is not a generic pointer - then give a warning : (only for implicit casts) */ - if (IS_PTR (optype) && implicit && - (DCL_TYPE (optype) != DCL_TYPE (type)) && - !IS_GENPTR (type)) - { - werror (W_INCOMPAT_CAST); - fprintf (stderr, "from type '"); - printTypeChain (optype, stderr); - fprintf (stderr, "' to type '"); - printTypeChain (type, stderr); - fprintf (stderr, "'\n"); + + /* if casting to/from pointers, do some checking */ + if (IS_PTR(type)) { // to a pointer + if (!IS_PTR(optype)) { // from a non pointer + if (IS_INTEGRAL(optype)) { + // maybe this is NULL, than it's ok. + if (!(IS_LITERAL(optype) && (SPEC_CVAL(optype).v_ulong ==0))) { + if (IS_GENPTR(type)) { + // no way to set the storage + if (IS_LITERAL(optype)) { + werror(E_LITERAL_GENERIC); + } else { + werror(E_NONPTR2_GENPTR); + } + } else if (implicit) { + werror(W_INTEGRAL2PTR_NOCAST); + } + } + } else { // shouldn't do that with float, array or structure + werror(E_INCOMPAT_TYPES); + } + } else { // from a pointer to a pointer + if (implicit) { // if not to generic, they have to match + if ((!IS_GENPTR(type) && (DCL_TYPE(optype) != DCL_TYPE(type)))) { + werror(E_INCOMPAT_PTYPES); + } + } } + } else { // to a non pointer + if (IS_PTR(optype)) { // from a pointer + if (implicit) { // sneaky + if (IS_INTEGRAL(type)) { + werror(W_PTR2INTEGRAL_NOCAST); + } else { // shouldn't do that with float, array or structure + werror(E_INCOMPAT_TYPES); + } + } + } + } /* if they are the same size create an assignment */ if (getSize (type) == getSize (optype) && diff --git a/support/Util/SDCCerr.c b/support/Util/SDCCerr.c index c46aa3ce..15e2618b 100644 --- a/support/Util/SDCCerr.c +++ b/support/Util/SDCCerr.c @@ -207,8 +207,8 @@ struct "Pre-Processor %s" }, { E_DUP_FAILED, ERROR_LEVEL_ERROR, "_dup call failed" }, -{ W_INCOMPAT_CAST, ERROR_LEVEL_WARNING, - "pointer being cast to incompatible type " }, +{ E_INCOMPAT_TYPES, ERROR_LEVEL_ERROR, + "incompatible types" }, { W_LOOP_ELIMINATE, ERROR_LEVEL_WARNING, "'while' loop with 'zero' constant.loop eliminated" }, { W_NO_SIDE_EFFECTS, ERROR_LEVEL_WARNING, @@ -227,8 +227,9 @@ struct "unknown size for operand" }, { W_LONG_UNSUPPORTED, ERROR_LEVEL_WARNING, "'%s' 'long' not supported , declared as 'int' ." }, -{ W_LITERAL_GENERIC, ERROR_LEVEL_WARNING, - "illegal cast of LITERAL value to 'generic' pointer: assuming 'xdata' pointer" }, +{ E_LITERAL_GENERIC, ERROR_LEVEL_ERROR, + //"illegal cast of LITERAL value to 'generic' pointer: assuming 'xdata' pointer" }, + "illegal cast of LITERAL value to 'generic' pointer" }, { E_SFR_ADDR_RANGE, ERROR_LEVEL_ERROR, "%s '%s' address out of range" }, { E_BITVAR_STORAGE, ERROR_LEVEL_ERROR, @@ -305,12 +306,12 @@ struct "constant is out of range %s" }, { W_CODE_UNREACH, ERROR_LEVEL_PEDANTIC, "unreachable code %s(%d)" }, -{ W_NONPTR2_GENPTR, ERROR_LEVEL_WARNING, - "non-pointer type cast to _generic pointer" }, +{ E_NONPTR2_GENPTR, ERROR_LEVEL_ERROR, + "non-pointer type cast to generic pointer" }, { W_POSSBUG, ERROR_LEVEL_WARNING, "possible code generation error at line %d,\n" " send source to sandeep.dutta@usa.net" }, -{ W_PTR_ASSIGN, ERROR_LEVEL_WARNING, +{ E_INCOMPAT_PTYPES, ERROR_LEVEL_WARNING, "pointer types incompatible " }, { W_UNKNOWN_MODEL, ERROR_LEVEL_WARNING, "unknown memory model at %s : %d" }, @@ -361,6 +362,10 @@ struct "Only one short option can be specified at a time. Rest of %s ignored." }, { E_VOID_VALUE_USED, ERROR_LEVEL_ERROR, "void value not ignored as it ought to be" }, +{ W_INTEGRAL2PTR_NOCAST, ERROR_LEVEL_WARNING, + "converting integral to pointer without a cast" }, +{ W_PTR2INTEGRAL_NOCAST, ERROR_LEVEL_WARNING, + "converting pointer to integral without a cast" }, }; /* diff --git a/support/Util/SDCCerr.h b/support/Util/SDCCerr.h index f2e91666..75b17f90 100644 --- a/support/Util/SDCCerr.h +++ b/support/Util/SDCCerr.h @@ -93,7 +93,7 @@ SDCCERR - SDCC Standard error handler #define E_EXTERN_INIT 75 /* extern variable initialised */ #define E_PRE_PROC_FAILED 76 /* preprocessor failed */ #define E_DUP_FAILED 77 /* file DUP failed */ -#define W_INCOMPAT_CAST 78 /* incompatible pointer casting */ +#define E_INCOMPAT_TYPES 78 /* incompatible types casting */ #define W_LOOP_ELIMINATE 79 /* loop eliminated */ #define W_NO_SIDE_EFFECTS 80 /* expression has no side effects */ #define W_CONST_TOO_LARGE 81 /* constant out of range */ @@ -103,7 +103,7 @@ SDCCERR - SDCC Standard error handler #define W_NO_REFERENCE 85 /* no reference to local variable */ #define E_OP_UNKNOWN_SIZE 86 /* unknown size for operand */ #define W_LONG_UNSUPPORTED 87 /* 'long' not supported yet */ -#define W_LITERAL_GENERIC 88 /* literal being cast to generic pointer */ +#define E_LITERAL_GENERIC 88 /* literal being cast to generic pointer */ #define E_SFR_ADDR_RANGE 89 /* sfr address out of range */ #define E_BITVAR_STORAGE 90 /* storage given for 'bit' variable */ #define E_EXTERN_MISMATCH 91 /* extern declaration mismatches */ @@ -142,9 +142,9 @@ SDCCERR - SDCC Standard error handler #define E_CAST_ZERO 124 /* casting to from size zero */ #define W_CONST_RANGE 125 /* constant too large */ #define W_CODE_UNREACH 126 /* unreachable code */ -#define W_NONPTR2_GENPTR 127 /* non pointer cast to generic pointer */ +#define E_NONPTR2_GENPTR 127 /* non pointer cast to generic pointer */ #define W_POSSBUG 128 /* possible code generation error */ -#define W_PTR_ASSIGN 129 /* incampatible pointer assignment */ +#define E_INCOMPAT_PTYPES 129 /* incampatible pointer assignment */ #define W_UNKNOWN_MODEL 130 /* Unknown memory model */ #define E_UNKNOWN_TARGET 131 /* target not defined */ #define W_INDIR_BANKED 132 /* Indirect call to a banked fun */ @@ -169,6 +169,8 @@ SDCCERR - SDCC Standard error handler #define E_CANNOT_USE_GENERIC_POINTER 151 #define W_EXCESS_SHORT_OPTIONS 152 #define E_VOID_VALUE_USED 153 +#define W_INTEGRAL2PTR_NOCAST 154 +#define W_PTR2INTEGRAL_NOCAST 155 /** Describes the maximum error level that will be logged. Any level * includes all of the levels listed after it. -- 2.47.2