类似地,可以通过 codean 命令使用代码分析器中的所有功能。本部分是有关如何使用 codean 命令发现代码中的新静态代码问题的简短教程,采用了 SolarisStudioSampleApplications 中的同一 sample 程序。
本教程前面的各个部分未编译 sample4.c。可使用 cat 命令预览此文件。
$ cat sample_4.c int another_new_umr() { int i; if (i) return 0; else return 1; }
请注意,int i 未初始化。
编译源代码并生成静态报告。
在 Oracle Solaris 上:
$ cc -g -xprevise main.c previse_1.c previse_all.c sample1.c sample2.c sample3.c
在 Oracle Linux 上:
$ cc -xannotate -g -xprevise main.c previse_1.c previse_all.c sample1.c sample2.c sample3.c
使用 codean –-save 选项将静态报告保存到 a.out。
$ codean --save -s a.out
重新编译 sample 应用程序(这次包括 sample4.c)。
在 Oracle Solaris 上:
$ cc -g -xprevise *.c
在 Oracle Linux 上:
$ cc -g -xannotate -xprevise *.c
此新函数从来未从 main 调用过,但它将引入一个新的 UMR 错误。
使用 –-whatisnew 选项可获取有关新增加的静态问题的报告。
$ codean --whatisnew -s a.out STATIC report of a.out showing new issues: Compare the latest results against a.out.analyze/history/2014.8.4.14.49.56... ERROR 1 (UMR): accessing uninitialized data: i at: another_new_umr() <sample_4.c : 4> 1: int another_new_umr() 2: { 3: int i; 4:=> if (i) 5: return 0; PREVISE SUMMARY for a.out: 1 new error(s), 0 new warning(s), 0 new leak(s) in total
下图展示了有关 codean 生成的静态代码问题的 HTML 报告。
有关 codean 的更多信息,请参见Oracle Solaris Studio 12.4:代码分析器用户指南 中的代码分析器命令行界面和 codean(1) 手册页。