From dc12b2fa3dab01b4dad1902128af60b03d903dbb Mon Sep 17 00:00:00 2001 From: eck Date: Wed, 25 Apr 1990 15:09:51 +0000 Subject: [PATCH] warn about ptrdiff_t passed as long on 2-4 machines --- lang/cem/cemcom.ansi/LLlex.c | 2 +- lang/cem/cemcom.ansi/arith.c | 30 ++++++++++++++++++++++++++++++ lang/cem/cemcom.ansi/ch3bin.c | 2 ++ lang/cem/cemcom.ansi/domacro.c | 2 +- lang/cem/cemcom.ansi/expr.str | 1 + lang/cem/cemcom.ansi/expression.g | 2 ++ lang/cem/cemcom.ansi/idf.c | 2 +- lang/cem/cemcom.ansi/replace.c | 4 ++-- 8 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lang/cem/cemcom.ansi/LLlex.c b/lang/cem/cemcom.ansi/LLlex.c index 4ba1edff8..a0fe5e780 100644 --- a/lang/cem/cemcom.ansi/LLlex.c +++ b/lang/cem/cemcom.ansi/LLlex.c @@ -327,7 +327,7 @@ garbage: if (ch != EOI) UnGetChar(); *tg++ = '\0'; /* mark the end of the identifier */ - idef = ptok->tk_idf = idf_hashed(buf, tg - buf, hash); + idef = ptok->tk_idf = idf_hashed(buf, (int) (tg - buf), hash); idef->id_file = ptok->tk_file; idef->id_line = ptok->tk_line; #ifndef NOPP diff --git a/lang/cem/cemcom.ansi/arith.c b/lang/cem/cemcom.ansi/arith.c index 5f9aa60c5..81309bc29 100644 --- a/lang/cem/cemcom.ansi/arith.c +++ b/lang/cem/cemcom.ansi/arith.c @@ -38,13 +38,33 @@ arithbalance(e1p, oper, e2p) /* 3.1.2.5 */ { /* The expressions *e1p and *e2p are balanced to be operands of the arithmetic operator oper. + We check here if the EX_PTRDIFF flag should be retained. + They are set to zero in because one of the opreands might + have a floating type, in which case the flags shouldn't + travel upward in the expression tree. */ register int t1, t2, u1, u2; int shifting = (oper == LEFT || oper == RIGHT || oper == LEFTAB || oper == RIGHTAB); + int ptrdiff = 0; t1 = any2arith(e1p, oper); t2 = any2arith(e2p, oper); + + if (int_size != pointer_size) { + if (ptrdiff = ((*e1p)->ex_flags & EX_PTRDIFF) + || ((*e2p)->ex_flags & EX_PTRDIFF)) { + if (!((*e1p)->ex_flags & EX_PTRDIFF) && t1 == LONG) + ptrdiff = 0; + if (!((*e2p)->ex_flags & EX_PTRDIFF) && t2 == LONG + && !shifting) + ptrdiff = 0; + } + /* Now turn off ptrdiff flags */ + (*e1p)->ex_flags &= ~EX_PTRDIFF; + (*e2p)->ex_flags &= ~EX_PTRDIFF; + } + /* Now t1 and t2 are either INT, LONG, FLOAT, DOUBLE, or LNGDBL */ /* If any operand has the type long double, the other operand @@ -132,6 +152,13 @@ arithbalance(e1p, oper, e2p) /* 3.1.2.5 */ else if (!u1 && u2 && !shifting) t1 = int2int(e1p, (t2 == LONG) ? ulong_type : uint_type); + + if (int_size != pointer_size) { + if (ptrdiff) { + (*e1p)->ex_flags |= EX_PTRDIFF; + (*e2p)->ex_flags |= EX_PTRDIFF; + } + } } relbalance(e1p, oper, e2p) @@ -549,6 +576,9 @@ any2parameter(expp) /* To handle default argument promotions */ any2opnd(expp, '('); + if (int_size != pointer_size) + if ((*expp)->ex_flags & EX_PTRDIFF) + expr_warning(*expp, "pointer difference caused long expression"); if ((*expp)->ex_type->tp_fund == FLOAT) float2float(expp, double_type); } diff --git a/lang/cem/cemcom.ansi/ch3bin.c b/lang/cem/cemcom.ansi/ch3bin.c index af93c154c..247ec27ae 100644 --- a/lang/cem/cemcom.ansi/ch3bin.c +++ b/lang/cem/cemcom.ansi/ch3bin.c @@ -17,6 +17,7 @@ #include "label.h" #include "expr.h" #include "Lpars.h" +#include "sizes.h" extern char options[]; extern char *symbol2str(); @@ -297,6 +298,7 @@ pntminuspnt(expp, oper, expr) , pa_type->tp_fund)); ch3cast(expp, CAST, pa_type); /* result will be an integral expr */ /* cast necessary ??? */ + if (int_size != pointer_size) (*expp)->ex_flags |= EX_PTRDIFF; } mk_binop(expp, oper, expr, commutative) diff --git a/lang/cem/cemcom.ansi/domacro.c b/lang/cem/cemcom.ansi/domacro.c index ffd3fb29c..b60aa1669 100644 --- a/lang/cem/cemcom.ansi/domacro.c +++ b/lang/cem/cemcom.ansi/domacro.c @@ -682,7 +682,7 @@ get_text(formals, length) blank = 0; } *length = repl->r_ptr - repl->r_text; - return Realloc(repl->r_text, repl->r_ptr - repl->r_text + 1); + return Realloc(repl->r_text, (unsigned)(repl->r_ptr - repl->r_text +1)); } /* macroeq() decides whether two macro replacement texts are diff --git a/lang/cem/cemcom.ansi/expr.str b/lang/cem/cemcom.ansi/expr.str index 197028236..a9071b1c9 100644 --- a/lang/cem/cemcom.ansi/expr.str +++ b/lang/cem/cemcom.ansi/expr.str @@ -95,6 +95,7 @@ struct expr { #define EX_VOLATILE 0x080 /* volatile variabele */ #define EX_ILVALUE 0x100 /* an illegal lvalue e.g. f().x */ #define EX_ERROR 0x200 /* the expression is wrong */ +#define EX_PTRDIFF 0x400 /* subtracting 2 pointers in expr. */ #define NILEXPR ((struct expr *)0) diff --git a/lang/cem/cemcom.ansi/expression.g b/lang/cem/cemcom.ansi/expression.g index ec110ebc5..667bc3051 100644 --- a/lang/cem/cemcom.ansi/expression.g +++ b/lang/cem/cemcom.ansi/expression.g @@ -127,6 +127,8 @@ unary(register struct expr **expp;) cast(&tp) unary(expp) { ch3cast(expp, CAST, tp); (*expp)->ex_flags |= EX_CAST; + if (int_size != pointer_size) + (*expp)->ex_flags &= ~EX_PTRDIFF; } | postfix_expression(expp) diff --git a/lang/cem/cemcom.ansi/idf.c b/lang/cem/cemcom.ansi/idf.c index 5d4b64a01..0681ddadd 100644 --- a/lang/cem/cemcom.ansi/idf.c +++ b/lang/cem/cemcom.ansi/idf.c @@ -141,7 +141,7 @@ str2idf(tg) } hash = STOPHASH(hash); *ncp++ = '\0'; - return idf_hashed(ntg, ncp - ntg, hash); + return idf_hashed(ntg, (int) (ncp - ntg), hash); } struct idf * diff --git a/lang/cem/cemcom.ansi/replace.c b/lang/cem/cemcom.ansi/replace.c index 6c01b13c4..3d62f3601 100644 --- a/lang/cem/cemcom.ansi/replace.c +++ b/lang/cem/cemcom.ansi/replace.c @@ -51,7 +51,7 @@ replace(idf) if (!expand_macro(repl, idf)) return 0; InputLevel++; - InsertText(repl->r_text, repl->r_ptr - repl->r_text); + InsertText(repl->r_text, (int)(repl->r_ptr - repl->r_text)); idf->id_macro->mc_flag |= NOREPLACE; repl->r_level = InputLevel; repl->next = ReplaceList; @@ -347,7 +347,7 @@ actual(repl) /* When the identifier has an associated macro replacement list, it's expanded. */ - idef = idf_hashed(buf, p - buf, hash); + idef = idf_hashed(buf, (int) (p - buf), hash); if (NoExpandMacro || !replace(idef)) { if ((idef->id_macro && (idef->id_macro->mc_flag & NOREPLACE)) -- 2.34.1