[PATCH] D44559: [Sema] Wrong width of result of mul operation

Andrew V. Tischenko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 22 00:02:12 PDT 2018


avt77 added a comment.

> That's an interesting question.  In general, these warnings do try to ignore the effects of implicit promotion.  We would not want -Wsign-conversion to fire on `unsigned short x = an_unsigned_short + 1;` (or `- 1`, for that matter), even though formally this coerces a `signed int` to `unsigned short`.  Similarly, -Wsign-conversion *should* warn on `signed short x = an_unsigned_short + 1;`, even though formally the promotion from `unsigned short` to `signed int` is not problematic and the final conversion from `signed int` to `signed short` is not a signedness change.  (This second example should also generate a -Wconversion warning, but the questions are independent.)  Applying that strictly here would say that the user is entitled to think of this as an operation on `unsigned char` that then gets losslessly promoted to `signed short`, even though arithmetically that's not what happens.  On the other hand, I do think there's some room for -Wsign-conversion to be more aggressive than -Wconversion about this sort of thing; -Wsign-conversion should generally fire for any changes in signedness from the original operand types (with the usual complexities around constant values), and there's just an exception for computations whose value is known to fit within the expressible range of the result type, which is not true of this multiplication.  So I think it would be acceptable to warn on this.
> 
> John.

OK, is it LGTM?


https://reviews.llvm.org/D44559





More information about the cfe-commits mailing list