Sun Studio 12 Update 1: C++ User's Guide

A.2.166 -xport64[=(v)]

Use this option to help you debug code you are porting to a 64-bit environment. Specifically, this option warns against problems such as truncation of types (including pointers), sign extension, and changes to bit-packing that are common when code is ported from a 32-bit architecture such as V8 to a 64-bit architecture such as V9.

A.2.166.1 Values

The following table lists the valid values for v:

Table A–42 The -xport64 Values

Values  

Meaning  

no

Generate no warnings related to the porting of code from a 32 bit environment to a 64 bit environment. 

implicit

Generate warning only for implicit conversions. Do not generate warnings when an explicit cast is present. 

full

Generate all warnings related to the porting of code from a 32 bit environment to a 64 bit environment. This includes warnings for truncation of 64-bit values, sign-extension to 64 bits under ISO value-preserving rules, and changes to packing of bitfields. 

Defaults

If you do not specify -xport64, the default is -xport64=no. If you specify -xport64, but do not specify a flag, the default is -xport64=full.

Examples

This section provides examples of code that can cause truncation of type, sign extension and changes to bit-packing.

Checking for the Truncation of 64-bit Values

When you port to a 64-bit architecture such as V9, your data may be truncated. The truncation could happen implicitly, by assignment, at initialization, or by an explicit cast. The difference of two pointers is the typedef ptrdiff_t, which is a 32-bit integer type in 32-bit mode, and a 64-bit integer type in 64-bit mode. The truncation of a long to a smaller size integral type generates a warning as in the following example.


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%

Use -xport64=implicit to disable truncation warnings in 64bit compilation mode when an explicit cast is the cause of data truncation.


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%

Another common issue that arises from porting to a 64-bit architecture is the truncation of a pointer. This is always an error in C++. An operation such as casting a pointer to an int which causes such a truncation results in an error diagnostic in V9 when you specify -xport64.


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%

Checking for Sign Extension

You can also use the -xport64 option to check for situations in which the normal ISO C value-preserving rules allow for the extension of the sign of a signed-integral value in an expression of unsigned-integral type. Such sign extensions can cause subtle run-time bugs.


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.

Checking for Changes to Packing of Bitfields

Use -xport64 to generate warnings against long bitfields. In the presence of such bitfields, packing of the bitfields might drastically change. Any program which relies on assumptions regarding the way bitfields are packed needs to be reviewed before a successful port can take place to a 64-bit architecture.


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%

Output in V9:


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

Warnings

Note that warnings are generated only when you compile in 64-bit mode by specifying options such as -m64.

See Also

A.2.51 -m32|-m64