HP C/aC++ Version 6 Features to Improve Developer Productivity Version 1.6 Introduction ......................................................................................................................................................2 What’s New......................................................................................................................................................2 Compile Time Diagnostics ....................................................................................
Introduction The most time consuming and expensive task in software development is to find and fix defects. The later a defect is found in the software development process, the costlier it gets. A bug found and fixed at the coding or testing stages is better and less expensive than the same found by a user after the product release. The HP compilers for Integrity provide a number of features to help software developers detect potential problems in their programs earlier in the process.
Example $ cat bitfield.c struct { int bit:1; } s; void test() { s.bit = 1; } $ cc -c +wlint bitfield.c "bitfield.c", line 1: warning #2108-D: signed bit field of length 1 struct { int bit:1; } s; ^ "bitfield.c", line 5: warning #4251-D: the assignment has an excessive size for a bit field s.
+wendian This option enables diagnostics that identify areas in the source code that might have porting issues between little-endian and big-endian. For example • de-reference which could cause endian-dependent behavior • Union definition that is endian dependent Example $ cat endian.c union u1 { char c[4]; int v; }; $ cc -c +wendian endian.c "endian.
"lock.c", line 12: warning #20223-D: Trying to unlock a lock held conditionally +wperfadvice This option enables performance advisory messages. It offers both integrity-specific and architecture-independent performance advice. The advice emitted is dependent on the optimization options used for compilation. Example $ cat large.c struct X{ int i; int arr[1000]; } x; int foo( struct X); int main() { foo (x); } $ cc –c +wperfadvice large.c "large.
ipo or +O4 options are not specified. This may lead to a significant increase in the compile time in comparison to a build without the +wsecurity option. Using this option may cause the compiler to invoke optimizations other than those which are part of the user-specified optimization level. If +wsecurity is used in addition to -ipo or +O4, the generated code is not affected and the compile time does not increase much.
Customization of Compiler Diagnostics Compiler Diagnostics have the following conflicting goals: • • • Emit all possible messages from the compiler to ensure that the user gets the maximum information about potential problems detected by the compiler. Reduce the number of messages so that the user is not overwhelmed by the sheer magnitude of the warnings. Eliminate or reduce the number of benign or misleading messages The right balance for these conflicting goals is different for each specific situation.
# disable warning: statement is unreachable +W2111 $ cc +opts warnings_config +wlint -c uninit.c "uninit.c", line 6: warning #3348-D: declaration hides variable "i" (declared at line 3) int i; ^ "uninit.c", line 9: error #2549-D: variable "i" is used before its value is set if (i) j = 3; ^ 1 error detected in the compilation of "uninit.c".
• • • 0 - A failed runtime check will abort the program immediately after the error message is emitted. 1 - The default setting, which will abort the program at the end of execution upon failure. 2 - A failed runtime check will not enable the end of execution abort. The +check options need to be specified at both compile time and link time since they may require additional libraries to be linked into the user program.
You can combine +check=bounds:[pointer|all] with all other +check options, except for +check=globals (which would be ignored in this case). Also see the +check=malloc and the +check=stack options for related runtime checks for heap and stack objects. +check=globals This option enables runtime checks to detect corruption of global variables, by introducing and checking "guards" between them, at the time of program exit.
sleep(10); return 0; } cc +check=lock simple_race.c -lpthread ./a.out Runtime Error: locking discipline violation: in file simple_race.c line 16 address 40010658 (0) 0x0000000004072ca0 _rtc_raise_fault + 0x2c0 at rtc_utils.c:382 [./a.out] (1) 0x0000000004028650 _ZN11DRD_Runtime15HandleMemAccessEybPcjS0_ + 0x590 at lock_check.C:438 [./a.out] (2) 0x0000000004029840 _ZN11DRD_Runtime17HandleStoreAccessEyPcjS0_ + 0x60 at lock_check.C:145 [./a.
+check=stack:variables This option enables runtime checks for illegal writes to the stack just before or after some variables on the stack. This includes array, struct/class/union and variables whose address is taken. It also includes the overflow check for the stack frame (+check=stack:frame). In addition to the above checks, this option causes the whole stack to be initialized to a "poison" value, which can help detect the use of uninitialized variables on the stack.
Programs might contain intentional truncation at runtime, such as when obtaining a hash value from a pointer or integer. To avoid runtime failures on these truncations, you can explicitly mask off the value: ch = (int_val & 0xff); Note that the +check=all option does not imply +check=truncate. To enable +check=truncate, you must explicitly specify it. +check=truncate:explicit This option turns on runtime checks for truncation on explicit user casts of integral values, such as (char)int_val.
memory checking. The librtc.so library is shipped as part of the wdb product. Install the HP WDB bundled with the compiler or a more recent version of wdb to get full functionality. The default behavior of the +check=malloc option can be changed by users providing their own rtcconfig file. The user specified rtcconfig file can be in the current directory OR in a directory specified by the GDBRTC_CONFIG environment variable.
+pathtrace=none This option disables generation of both the global and local path tables. The values can be combined by joining them with a colon. For example: +pathtrace=global:local. The global_fixed_size and global values are mutually exclusive. If more than one of them is specified on the command line, the last one takes precedence. The same is true for the none value.