我正在尝试从头开始为嵌入式x86_64环境编译OpenJDK 12.0.1,而gcc 7抛出错误。
src/hotspot/share/utilities/globalDefinitions.hpp:969:39: warning: integer overflow in expression [-Woverflow]
#define right_n_bits(n) (nth_bit(n) - 1)
^
src/hotspot/share/oops/markOop.hpp:142:37: note: in expansion of macro ‘right_n_bits’
,hash_mask = right_n_bits(hash_bits),
^~~~~~~~~~~~
src/hotspot/share/utilities/globalDefinitions.hpp:969:42: error: overflow in constant expression [-fpermissive]
#define right_n_bits(n) (nth_bit(n) - 1)
代码如下:
//----------------------------------------------------------------------------------------------------
// Utility functions for bitfield manipulations
const intptr_t AllBits = ~0; // all bits set in a word
const intptr_t NoBits = 0; // no bits set in a word
const jlong NoLongBits = 0; // no bits set in a long
const intptr_t OneBit = 1; // only right_most bit set in a word
// get a word with the n.th or the right-most or left-most n bits set
// (note: #define used only so that they can be used in enum constant definitions)
#define nth_bit(n) (((n) >= BitsPerWord) ? 0 : (OneBit << (n)))
#define right_n_bits(n) (nth_bit(n) - 1)
#define left_n_bits(n) (right_n_bits(n) << (((n) >= BitsPerWord) ? 0 : (BitsPerWord - (n))))
代码参考here。该代码已经存在多个版本,并且在即将发布的OpenJDK 13版本中也存在。
我了解:
#define right_n_bits(n) (nth_bit(n) - 1)
是错误,因为'-1'可能溢出特定类型的边界。通常,解决方案是在宏中包含强制转换。
但是,程序员显然在这里做了一些特殊的事情,以便他们可以在同一组宏中同时处理单词和long。
什么是适当的处理方式?有没有简单的解决方案,还是我需要告诉gcc忽略此类错误?