Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74487
  • 博文数量: 29
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 337
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-24 20:02
文章分类

全部博文(29)

文章存档

2011年(1)

2009年(1)

2008年(27)

我的朋友

分类: C/C++

2008-12-22 14:18:35

[-Wsequence-point]
关于顺序点(sequence point),在C标准中有解释,不过很晦涩。我们在平时编码中尽量避免写出与实现相关、受实现影响的代码便是了。而-Wsequence-point选项恰恰可以帮我们这个忙,它可以帮我们查出这样的代码来,并给出其警告。

e.g.
/*
* test_sequence_point.c
* gcc -Wsequence-point test_sequence_point.c
*/

#i nclude

int main() {
         int      i = 12;
         i = i--;
         printf("the i is %d\n", i);
         return 0;
}

gcc -Wsequence-point test_sequence_point.c
test_sequence_point.c: In function `main':
test_sequence_point.c:10: warning: operation on `i' may be undefined

在两个平台上给出的编译警告都是一致的,但是输出结果却大相径庭。

Solaris输出:
the i is 11

Windows输出:
the i is 12

类似的像这种与顺序点相关的代码例子有:
i = i++;
a[i] = b[i++]
a[i++] = i
等等...

[-Wswitch]
这个选项的功能浅显易懂,通过文字描述也可以清晰的说明。当以一个枚举类型(enum)作为switch语句的索引时但却没有处理default情况,或者没有处理所有枚举类型定义范围内的情况时,该选项会给处警告。

e.g.
/*
* test_switch1.c
*/
enum week {
         SUNDAY,
         MONDAY,
         TUESDAY /* only an example , we omitted the others */
};

int test1() {
         enum week        w        = SUNDAY;
         switch(w) {
                 case SUNDAY:
                         break; // without default or the other case handlings
         };

         return 0;
}

int test2() { // Ok, won't invoke even a warning
         enum week        w        = SUNDAY;
         switch(w) {
                 case SUNDAY:
                         break;
                 default:
                         break;                
         };

         return 0;
}

int test3() { // Ok, won't invoke even a warning
         enum week        w        = SUNDAY;
         switch(w) {
                 case SUNDAY:
                         break;
                 case MONDAY:
                         break;
                 case TUESDAY:
                         break;             
         };

         return 0;
}

gcc -Wswitch -c test_switch.c
test_switch.c: In function `test1':
test_switch.c:16: warning: enumeration value `MONDAY' not handled in switch
test_switch.c:16: warning: enumeration value `TUESDAY' not handled in switch

[-Wunused]
-Wunused是-Wunused-function、-Wunused-label、-Wunused-variable、-Wunused-value选项的集合,-Wunused-parameter需单独使用。
(1) -Wunused-function用来警告存在一个未使用的static函数的定义或者存在一个只声明却未定义的static函数,参见下面例子中的func1和func2;
(2) -Wunused-label用来警告存在一个使用了却未定义或者存在一个定义了却未使用的label,参加下面例子中的func3和func7;
(3) -Wunused-variable用来警告存在一个定义了却未使用的局部变量或者非常量static变量;参见下面例子中func5和var1;
(4) -Wunused-value用来警告一个显式计算表达式的结果未被使用;参见下面例子中func6
(5) -Wunused-parameter用来警告一个函数的参数在函数的实现中并未被用到,参见下面例子中func4。

下面是一个综合的例子
e.g.
/*
* test_unused.c
*/
static void func1(); //to prove function used but never defined
static void func2(); //to prove function defined but not used
static void func3(); //to prove label used but never defined
static void func7(); //to prove label defined but never used
static void func4(int a); //to prove parameter declared but not used
static void func5(); //to prove local variable defined but not used
static void func6(); //to prove value evaluated but not used

static int var1;

void test() {
         func1();
         func3();
         func4(4);
         func5();
         func6();
}

static void func2() {
         ; // do nothing
}

static void func3() {
         goto over;
}

static void func4(int a) {
         ; // do nothing
}

static void func5() {
         int      a = 0;
}

static void func6() {
         int      a = 0;
         int      b = 6;
         a + b;
}

gcc -Wunused-parameter -c test_unused.c //如果不是用-Wunused-parameter,则func4函数将不被警告。
test_unused.c: In function `func3':
test_unused.c:30: label `over' used but not defined
test_unused.c: In function `func7':
test_unused.c:35: warning: deprecated use of label at end of compound statement
test_unused.c:34: warning: label `over' defined but not used
test_unused.c: In function `func4':
test_unused.c:37: warning: unused parameter `a'
test_unused.c: In function `func5':
test_unused.c:42: warning: unused variable `a'
test_unused.c: In function `func6':
test_unused.c:48: warning: statement with no effect
test_unused.c: At top level:
test_unused.c:6: warning: `func1' used but never defined
test_unused.c:25: warning: `func2' defined but not used
test_unused.c:14: warning: `var1' defined but not used

[-Wuninitialized]
该警告选项用于检查一个局部自动变量在使用之前是否已经初始化了或者在一个longjmp调用可能修改 一个non-volatile automatic variable时给出警告。目前编译器还不是那么smart,所以对有些可以正确按照程序员的意思运行的代码还是给出警告。而且该警告选项需要和'- O'选项一起使用,否则你得不到任何uinitialized的警告。

e.g.
/*
* test_uninitialized.c
*/
int test(int y) {
         int      x;

         switch (y) {
                 case 1:
                         x = 11;
                         break;
                 case 2:
                         x = 22;
                         break;
                 case 3:
                         x = 33;
                         break;
         }

         return x;
}

gcc -Wuninitialized -O -c test_uninitialized.c
test_uninitialized.c: In function `test':
test_uninitialized.c:6: warning: `x' might be used uninitialized in this function

2、非-Wall集合警告选项
以下讨论的这些警告选项并不包含在-Wall中,需要程序员显式添加。

[-Wfloat-equal]
该项用来检查浮点值是否出现在相等比较的表达式中。

e.g.
/*
* test_float_equal.c
*/

void test(int i) {
         double d = 1.5;
         if (d == i) {
                 ;
         }
}

gcc -Wfloat-equal -c test_float_equal.c
test_float_equal.c: In function `test':
test_float_equal.c:8: warning: comparing floating point with == or != is unsafe

[-Wshadow]
当局部变量遮蔽(shadow)了参数、全局变量或者是其他局部变量时,该警告选项会给我们以警告信息。

e.g.
/*
* test_shadow.c
*/
int      g;

void test(int i) {
         short    i;
         double g;
}

gcc -Wshadow -c test_shadow.c
test_shadow.c: In function `test':
test_shadow.c:9: warning: declaration of `i' shadows a parameter
test_shadow.c:10: warning: declaration of `g' shadows a global declaration
test_shadow.c:6: warning: shadowed declaration is here

[-Wbad-function-cast]
当函数(准确地说应该是函数返回类型)被转换为非匹配类型时,均产生警告。

阅读(3537) | 评论(0) | 转发(1) |
0

上一篇:汉字转拼音问题

下一篇:简介

给主人留下些什么吧!~~