Oracle Solaris Studio 12.2:C++ 用户指南

A.2.166.1 值

下表列出了v 的有效值:

表 A–42 -xport64

值  

含义  

no

将代码从 32 位环境移植到 64 位环境时,不会生成与该代码移植有关的任何警告。 

implicit

只生成隐式转换的警告。显式强制类型转换出现时不生成警告。 

full

将代码从 32 位环境移植到 64 位环境时,生成了与该代码移植有关的所有警告。其中包括对 64 位值的截断警告、根据 ISO 值的保存规则对 64 位的符号扩展,以及对位字段包装的更改。 

缺省值

如果未指定 -xport64,则缺省值为 -xport64=no。如果指定了 -xport64 但未指定标志,则缺省值为 -xport64=full

示例

本节提供了可以导致类型截断、符号扩展和对位包装更改的代码示例。

检查 64 位值的截断

在移植到诸如 V9 的 64 位架构时,数据可能会被截断。截断可能会因赋值(初始化时)或显式强制类型转换而隐式地发生。两个指针的差异在于 typedef ptrdiff_t,它在 32 位模式下是 32 位整型,而在 64 位模式下是 64 位整型。将较长的整型截断为较小的整型会生成警告,如下示例所示。


example% cat test1.c
int x[10];

int diff = &x[10] - &x[5]; //warn

example% CC -c -xarch=v9 -Qoption ccfe -xport64=full test1.c
"test1.c", line 3: Warning: Conversion of 64-bit type value to "int" causes truncation.
1 Warning(s) detected.
example%

可使用 -xport64=implicit 禁用 64 位编译模式下显式强制类型转换导致数据截断时出现截断警告。


example% CC -c -xarch=v9 -Qoption ccfe -xport64=implicit test1.c
"test1.c", line 3: Warning: Conversion of 64-bit type value to "int" causes truncation.
1 Warning(s) detected.
example%

在移植到 64 位架构过程中出现的另一个常见问题是指针的截断。该问题在 C++ 中始终是错误。如果指定 -xport64,导致此类截断的操作(如将指针强制转换为整型)可能会导致在 V9 中出现错误诊断。


example% cat test2.c
char* p;
int main() {
  p =(char*) (((unsigned int)p) & 0xFF); // -xarch=v9 error
  return 0;
}
example% CC -c -xarch=v9 -Qoption ccfe -xport64=full test2.c
"test2.c", line 3: Error: Cannot cast from char* to unsigned.
1 Error(s) detected.
example%

检查符号扩展

还可以使用 -xport64 选项来检查这种情况:标准 ISO C 值保留规则允许在无符号整型的表达式中进行带符号整数值的符号扩展。这种符号扩展会产生细微的运行时错误。


example% cat test3.c
int i= -1;
void promo(unsigned long l) {}

int main() {
    unsigned long l;
    l = i;  // warn
    promo(i);       // warn
}
example% CC -c -xarch=v9 -Qoption ccfe -xport64=full test3.c
"test3.c", line 6: Warning: Sign extension from "int" to 64-bit integer.
"test3.c", line 7: Warning: Sign extension from "int" to 64-bit integer.
2 Warning(s) detected.

检查位字段包装的更改

可使用 -xport64 生成对长位字段的警告。出现这种位字段时,位字段的包装可能会显著更改。在成功移植到 64 位架构之前,依赖于假定的任何程序都需要重新检查,该假定与包装位字段的方法有关。


example% cat test4.c
#include <stdio.h>

union U {
   struct S {
       unsigned long b1:20;
       unsigned long b2:20;
   } s;

   long buf[2];
} u;

int main() {
   u.s.b1 = 0XFFFFF;
   u.s.b2 = 0XFFFFF;
   printf(" u.buf[0] = %lx u.buf[1] = %lx\n", u.buf[0], u.buf[1]);
   return 0;
}
example%

V9 中的输出:


example% u.buf[0] = ffffffffff000000 u.buf[1] = 0

警告

请注意,仅当通过指定 -m64 等选项以 64 位模式编译时,才会生成警告。

另请参见

A.2.50 -m32|-m64