Skip to contentSkip to conversation

fix clang negative signed bit shift warning

PATCH Applied Stefan Hajnoczi 6 participants 18 messages Nov 10, 2015 – Nov 17, 2015

lore
Share & tools
lore
#0 lore raw

cover fix clang negative signed bit shift warning

LLVM's clang 3.7.0 compile warns about bit shifting negative numbers because the result is undefined. This series includes 3 small fixes to appease clang.

Stefan Hajnoczi (3):
monitor: avoid clang shifting negative signed warning
tpm: avoid clang shifting negative signed warning disas/arm: avoid clang shifting negative signed warning

 disas/arm.c           | 2 +-
 hw/tpm/tpm_tis.c      | 2 +-
 target-i386/monitor.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.5.0
#1 lore raw

PATCH 1/3 monitor: avoid clang shifting negative signed warning

clang 3.7.0 on x86_64 warns about the following:

target-i386/monitor.c:38:22: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

        addr |= -1LL << 48;
                ~~~~ ^
---
 target-i386/monitor.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target-i386/monitor.c +1 -1
index aac6b1b..6f5c280 100644
@@ -35,7 +35,7 @@ static void print_pte(Monitor *mon, hwaddr addr,
 {
 #ifdef TARGET_X86_64
     if (addr & (1ULL << 47)) {
-        addr |= -1LL << 48;
+        addr |= ~0ULL << 48;
     }
 #endif
     monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
Signature · 1 line
2.5.0
Reviewed-by on v1 1/3 Applied
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] monitor: avoid clang shifting negative signed warning

Quoted 7 lines · Stefan Hajnoczi

clang 3.7.0 on x86_64 warns about the following:

target-i386/monitor.c:38:22: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

        addr |= -1LL << 48;
                ~~~~ ^

Applied to x86 tree. Thanks!

Signature · 1 line
Eduardo
#3 lore raw

PATCH 2/3 tpm: avoid clang shifting negative signed warning

In reply to Stefan Hajnoczi

clang 3.7.0 on x86_64 warns about the following:

hw/tpm/tpm_tis.c:1000:36: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

            tis->loc[c].iface_id = TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3;
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  hw/tpm/tpm_tis.c:144:10: note: expanded from macro 'TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3'
     (~0 << 4)/* all of it is don't care */)
      ~~ ^
---
 hw/tpm/tpm_tis.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/tpm/tpm_tis.c +1 -1
index 0806b5f..ff073d5 100644
@@ -141,7 +141,7 @@
 
 #define TPM_TIS_IFACE_ID_SUPPORTED_FLAGS1_3 \
     (TPM_TIS_IFACE_ID_INTERFACE_TIS1_3 | \
-     (~0 << 4)/* all of it is don't care */)
+     (~0u << 4)/* all of it is don't care */)
 
 /* if backend was a TPM 2.0: */
 #define TPM_TIS_IFACE_ID_SUPPORTED_FLAGS2_0 \
Signature · 1 line
2.5.0
#4 lore raw

PATCH 3/3 disas/arm: avoid clang shifting negative signed warning

In reply to Stefan Hajnoczi

clang 3.7.0 on x86_64 warns about the following:

disas/arm.c:1782:17: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

    imm |= (-1 << 7);
            ~~ ^

Note that this patch preserves the tab indent in this source file because the surrounding code still uses tabs.

---
 disas/arm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
disas/arm.c +1 -1
index 6165246..7a7354b 100644
@@ -1779,7 +1779,7 @@ print_insn_coprocessor (bfd_vma pc, struct disassemble_info *info, long given,
 
 			/* Is ``imm'' a negative number?  */
 			if (imm & 0x40)
-			  imm |= (-1 << 7);
+			  imm |= (~0u << 7);
 
 			func (stream, "%d", imm);
 		      }
Signature · 1 line
2.5.0
#5 lore raw

Re: [Qemu-devel] [PATCH 3/3] disas/arm: avoid clang shifting negative signed warning

Quoted 10 lines · Stefan Hajnoczi

clang 3.7.0 on x86_64 warns about the following:

disas/arm.c:1782:17: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

    imm |= (-1 << 7);
            ~~ ^

Note that this patch preserves the tab indent in this source file because the surrounding code still uses tabs.

I would like to know a case where (except with ubsan) clang actually uses the optimization.

If not, this is just error message theatre (which is not news for clang) and shouldn't have been part of -Wall.

Paolo

#6 lore raw

Re: [Qemu-devel] [PATCH 3/3] disas/arm: avoid clang shifting negative signed warning

Quoted 12 lines · Paolo Bonzini
Quoted 9 lines · Stefan Hajnoczi

clang 3.7.0 on x86_64 warns about the following:

disas/arm.c:1782:17: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

    imm |= (-1 << 7);
            ~~ ^

Note that this patch preserves the tab indent in this source file because the surrounding code still uses tabs.

Quoted 7 lines
Quoted 1 line

I would like to know a case where (except with ubsan) clang actually uses the optimization.

If not, this is just error message theatre (which is not news for clang) and shouldn't have been part of -Wall.

It could be they're attempting to warn us now about the possibility that in a future version of clang they will start using this UB to optimize with.

http://stackoverflow.com/questions/22883790/left-shift-of-negative-values
reports that Intel's ICC will use this in dead-code-elimination optimization. One day clang might do that too.

thanks
-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH 3/3] disas/arm: avoid clang shifting negative signed warning

Quoted 3 lines · Peter Maydell

It could be they're attempting to warn us now about the possibility that in a future version of clang they will start using this UB to optimize with.

Good luck to them.

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH 3/3] disas/arm: avoid clang shifting negative signed warning

In reply to Peter Maydell
Quoted 21 lines · Peter Maydell
Quoted 12 lines · Paolo Bonzini
Quoted 9 lines · Stefan Hajnoczi

clang 3.7.0 on x86_64 warns about the following:

disas/arm.c:1782:17: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

    imm |= (-1 << 7);
            ~~ ^

Note that this patch preserves the tab indent in this source file because the surrounding code still uses tabs.

Quoted 7 lines
Quoted 1 line

I would like to know a case where (except with ubsan) clang actually uses the optimization.

If not, this is just error message theatre (which is not news for clang) and shouldn't have been part of -Wall.

Quoted 7 lines

It could be they're attempting to warn us now about the possibility that in a future version of clang they will start using this UB to optimize with.

http://stackoverflow.com/questions/22883790/left-shift-of-negative-values
reports that Intel's ICC will use this in dead-code-elimination optimization. One day clang might do that too.

Nice example of a compiler being gratuitously nasty.

#9 lore raw

Re: [Qemu-devel] [PATCH 3/3] disas/arm: avoid clang shifting negative signed warning

Quoted 27 lines · Markus Armbruster
Quoted 25 lines · Peter Maydell
Quoted 12 lines · Paolo Bonzini
Quoted 9 lines · Stefan Hajnoczi

clang 3.7.0 on x86_64 warns about the following:

disas/arm.c:1782:17: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

    imm |= (-1 << 7);
            ~~ ^

Note that this patch preserves the tab indent in this source file because the surrounding code still uses tabs.

Quoted 7 lines
Quoted 1 line

I would like to know a case where (except with ubsan) clang actually uses the optimization.

If not, this is just error message theatre (which is not news for clang) and shouldn't have been part of -Wall.

It could be they're attempting to warn us now about the possibility that in a future version of clang they will start using this UB to optimize with.

Quoted 6 lines
Quoted 3 lines

http://stackoverflow.com/questions/22883790/left-shift-of-negative-values
reports that Intel's ICC will use this in dead-code-elimination optimization. One day clang might do that too.

Nice example of a compiler being gratuitously nasty.

I don't read this warning as "clang will do crazy things with your code eventually". Clang has always been very verbose when it comes to undefined behavior, and I don't think that's really a bad thing to do. Even if clang does emit sane code for it, all bets are off for other compilers -- so it's more of a portability warning. And I know some other compilers *won't* warn before doing crazy things in the name of undefined behavior. The ICC example is a fine one...

In my experience fixing the warnings produced by clang has actually eliminated bugs that were present but undiscovered on other platforms.

#10 lore raw

Re: [Qemu-devel] [PATCH 3/3] disas/arm: avoid clang shifting negative signed warning

Quoted 32 lines · Steven Noonan
Quoted 31 lines · Markus Armbruster
Quoted 28 lines · Peter Maydell
Quoted 12 lines · Paolo Bonzini
Quoted 9 lines · Stefan Hajnoczi

clang 3.7.0 on x86_64 warns about the following:

disas/arm.c:1782:17: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

    imm |= (-1 << 7);
            ~~ ^

Note that this patch preserves the tab indent in this source file because the surrounding code still uses tabs.

Quoted 7 lines
Quoted 1 line

I would like to know a case where (except with ubsan) clang actually uses the optimization.

If not, this is just error message theatre (which is not news for clang) and shouldn't have been part of -Wall.

It could be they're attempting to warn us now about the possibility that in a future version of clang they will start using this UB to optimize with.

http://stackoverflow.com/questions/22883790/left-shift-of-negative-values
reports that Intel's ICC will use this in dead-code-elimination optimization. One day clang might do that too.

Quoted 6 lines
Quoted 2 lines

Nice example of a compiler being gratuitously nasty.

I don't read this warning as "clang will do crazy things with your code eventually". Clang has always been very verbose when it comes to undefined behavior, and I don't think that's really a bad thing to do.

[...]

Misunderstanding?

Clang's warning is at worst annoying, but nowhere near nasty.

ICC concluding that code executing a left shift of a negative value must be unreachable is gratuitously nasty.

#11 lore raw

Re: [Qemu-devel] [PATCH 3/3] disas/arm: avoid clang shifting negative signed warning

Quoted 37 lines · Markus Armbruster
Quoted 35 lines · Steven Noonan
Quoted 33 lines · Markus Armbruster
Quoted 21 lines · Peter Maydell
Quoted 12 lines · Paolo Bonzini
Quoted 9 lines · Stefan Hajnoczi

clang 3.7.0 on x86_64 warns about the following:

disas/arm.c:1782:17: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

    imm |= (-1 << 7);
            ~~ ^

Note that this patch preserves the tab indent in this source file because the surrounding code still uses tabs.

Quoted 7 lines
Quoted 1 line

I would like to know a case where (except with ubsan) clang actually uses the optimization.

If not, this is just error message theatre (which is not news for clang) and shouldn't have been part of -Wall.

Quoted 7 lines

It could be they're attempting to warn us now about the possibility that in a future version of clang they will start using this UB to optimize with.

http://stackoverflow.com/questions/22883790/left-shift-of-negative-values
reports that Intel's ICC will use this in dead-code-elimination optimization. One day clang might do that too.

Nice example of a compiler being gratuitously nasty.

Quoted 8 lines
Quoted 3 lines

I don't read this warning as "clang will do crazy things with your code eventually". Clang has always been very verbose when it comes to undefined behavior, and I don't think that's really a bad thing to do.

[...]

Misunderstanding?

Clang's warning is at worst annoying, but nowhere near nasty.

Sure, it's merely annoying if you only compile your code with Clang. But my point is that a lot of Clang's aggressive undefined behavior warnings are there to get people to write portable code so it doesn't break on other compilers where the "undefined behavior" depends on the compiler implementation.

Quoted 2 lines

ICC concluding that code executing a left shift of a negative value must be unreachable is gratuitously nasty.

I agree, that's completely insane.

#12 lore raw

Re: [Qemu-devel] [PATCH 3/3] disas/arm: avoid clang shifting negative signed warning

In reply to Steven Noonan
Quoted 3 lines · Steven Noonan

I don't read this warning as "clang will do crazy things with your code eventually". Clang has always been very verbose when it comes to undefined behavior, and I don't think that's really a bad thing to do.

Sure, but it doesn't belong in -Wall. It's what -Wextra is for.

Paolo

Reviewed-by on the v1 series
#13 lore raw
In reply to Stefan Hajnoczi
Quoted 8 lines · Stefan Hajnoczi

LLVM's clang 3.7.0 compile warns about bit shifting negative numbers because the result is undefined. This series includes 3 small fixes to appease clang.

Stefan Hajnoczi (3):
monitor: avoid clang shifting negative signed warning
tpm: avoid clang shifting negative signed warning disas/arm: avoid clang shifting negative signed warning

Quoted 4 lines
 disas/arm.c           | 2 +-
 hw/tpm/tpm_tis.c      | 2 +-
 target-i386/monitor.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

Whole series

thanks
-- PMM

#14 lore raw
In reply to Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi

LLVM's clang 3.7.0 compile warns about bit shifting negative numbers because the result is undefined. This series includes 3 small fixes to appease clang.

Stefan Hajnoczi (3):
monitor: avoid clang shifting negative signed warning
tpm: avoid clang shifting negative signed warning disas/arm: avoid clang shifting negative signed warning

 disas/arm.c           | 2 +-
 hw/tpm/tpm_tis.c      | 2 +-
 target-i386/monitor.c | 2 +-

My clang-3.7 build (it's part of fbinfer) also complains about:

/Users/pm215/src/qemu/hw/audio/fmopl.c:1085:39: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

                data = Limit( outd[0] , OPL_MAXOUT, OPL_MINOUT );
                                                    ^~~~~~~~~~
/Users/pm215/src/qemu/hw/audio/fmopl.c:75:28: note: expanded from
macro 'OPL_MINOUT'
#define OPL_MINOUT (-0x8000<<OPL_OUTSB)
                    ~~~~~~~^

and a lot of shift-of-negative uses in target-mips code. Did you also have patches planned for those or are these three the only ones you have in progress?

thanks
-- PMM

#15 lore raw
Quoted 10 lines · Peter Maydell

My clang-3.7 build (it's part of fbinfer) also complains about:

/Users/pm215/src/qemu/hw/audio/fmopl.c:1085:39: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

                data = Limit( outd[0] , OPL_MAXOUT, OPL_MINOUT );
                                                    ^~~~~~~~~~
/Users/pm215/src/qemu/hw/audio/fmopl.c:75:28: note: expanded from
macro 'OPL_MINOUT'
#define OPL_MINOUT (-0x8000<<OPL_OUTSB)
                    ~~~~~~~^

I sent a patch for that one. The MIPS stuff I'll leave for somebody else at least for now...

thanks
-- PMM

#16 lore raw
Quoted 12 lines · Peter Maydell
Quoted 10 lines · Peter Maydell

My clang-3.7 build (it's part of fbinfer) also complains about:

/Users/pm215/src/qemu/hw/audio/fmopl.c:1085:39: warning: shifting a negative signed value is undefined [-Wshift-negative-value]

                data = Limit( outd[0] , OPL_MAXOUT, OPL_MINOUT );
                                                    ^~~~~~~~~~
/Users/pm215/src/qemu/hw/audio/fmopl.c:75:28: note: expanded from
macro 'OPL_MINOUT'
#define OPL_MINOUT (-0x8000<<OPL_OUTSB)
                    ~~~~~~~^
Quoted 2 lines

I sent a patch for that one. The MIPS stuff I'll leave for somebody else at least for now...

Thanks. I don't plan to investigate the audio one soon.

Stefan

author Applied
#17 lore raw
In reply to Stefan Hajnoczi
Quoted 8 lines · Stefan Hajnoczi

LLVM's clang 3.7.0 compile warns about bit shifting negative numbers because the result is undefined. This series includes 3 small fixes to appease clang.

Stefan Hajnoczi (3):
monitor: avoid clang shifting negative signed warning
tpm: avoid clang shifting negative signed warning disas/arm: avoid clang shifting negative signed warning

Quoted 8 lines
 disas/arm.c           | 2 +-
 hw/tpm/tpm_tis.c      | 2 +-
 target-i386/monitor.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.5.0

Thanks, applied Patches 2 & 3 to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan