This is an archive of the discontinued LLVM Phabricator instance.

[clang][Diagnostics] Provide source range to integer-overflow warnings
ClosedPublic

Authored by hazohelet on Aug 8 2023, 4:37 AM.

Details

Summary

BEFORE:

overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    1 | int x = __INT_MAX__ + 1 + 3;
      |                     ^
overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    2 | int a = -(1 << 31) + 1;
      |         ^

AFTER:

overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    1 | int x = __INT_MAX__ + 1 + 3;
      |         ~~~~~~~~~~~~^~~
overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    2 | int a = -(1 << 31) + 1;
      |         ^~~~~~~~~~

Diff Detail

Event Timeline

hazohelet created this revision.Aug 8 2023, 4:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 8 2023, 4:37 AM
hazohelet requested review of this revision.Aug 8 2023, 4:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 8 2023, 4:37 AM
hazohelet added inline comments.Aug 8 2023, 4:41 AM
clang/lib/AST/Interp/Interp.h
532–538

I'm not sure whether this branch takes effect.
I could not find codes that takes this block, so I haven't added tests for this.

FWIW, the old interpreter does not have the corresponding warn_integer_constant_overflow generated against overflowing increments.

tbaeder added inline comments.Aug 8 2023, 5:17 AM
clang/lib/AST/Interp/Interp.h
532–538
hazohelet added inline comments.Aug 8 2023, 5:22 AM
clang/lib/AST/Interp/Interp.h
532–538

That note is emitted from S.CCEDiag at L539.
This warning looks like it is intended to be emitted when the function is not constexpr, but it does not appear.

tbaeder accepted this revision.Aug 16 2023, 4:46 AM
tbaeder added inline comments.
clang/lib/AST/Interp/Interp.h
532–538

From reading the code, it makes sense to me do emit this diagnostic here, but I can't get it to trigger either because the increment operator is always evaluated standalone and not as part of a function.

For this patch, let's ignore that. The changes here are fine.

This revision is now accepted and ready to land.Aug 16 2023, 4:46 AM