blob: 5e4947b8cdd6962233ca3fe1b0f124b49b2793c4 [file] [log] [blame]
Richard Smith85b29a42012-02-17 01:35:32 +00001// RUN: %clang_cc1 -pedantic -Wunused-label -verify -x c %s
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +00002// RUN: cp %s %t
Richard Smith23153182011-09-06 03:01:15 +00003// RUN: not %clang_cc1 -pedantic -Wunused-label -fixit -x c %t
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +00004// RUN: grep -v CHECK %t > %t2
Anna Zaksd5612a22011-07-28 20:52:06 +00005// RUN: %clang_cc1 -pedantic -Wunused-label -Werror -x c %t
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +00006// RUN: FileCheck -input-file=%t2 %t
Douglas Gregor9b3064b2009-04-01 22:41:11 +00007
8/* This is a test of the various code modification hints that are
Douglas Gregorfe057ac2009-04-02 03:20:30 +00009 provided as part of warning or extension diagnostics. All of the
10 warnings will be fixed by -fixit, and the resulting file should
11 compile cleanly with -Werror -pedantic. */
Daniel Dunbard8aefab2009-11-17 22:25:16 +000012
13// FIXME: FIX-IT should add #include <string.h>?
14int strcmp(const char *s1, const char *s2);
Douglas Gregor9b3064b2009-04-01 22:41:11 +000015
Richard Smith85b29a42012-02-17 01:35:32 +000016void f0(void) { }; // expected-warning {{';'}}
Douglas Gregor9b3064b2009-04-01 22:41:11 +000017
18struct s {
Richard Smith85b29a42012-02-17 01:35:32 +000019 int x, y;; // expected-warning {{extra ';'}}
Douglas Gregor9b3064b2009-04-01 22:41:11 +000020};
21
Daniel Dunbar266cc532009-11-14 19:25:21 +000022// CHECK: _Complex double cd;
Richard Smith85b29a42012-02-17 01:35:32 +000023_Complex cd; // expected-warning {{assuming '_Complex double'}}
Douglas Gregor9b3064b2009-04-01 22:41:11 +000024
Daniel Dunbar266cc532009-11-14 19:25:21 +000025// CHECK: struct s s0 = { .y = 5 };
Richard Smith85b29a42012-02-17 01:35:32 +000026struct s s0 = { y: 5 }; // expected-warning {{GNU old-style}}
Daniel Dunbar266cc532009-11-14 19:25:21 +000027
28// CHECK: int array0[5] = { [3] = 3 };
Richard Smith85b29a42012-02-17 01:35:32 +000029int array0[5] = { [3] 3 }; // expected-warning {{GNU 'missing ='}}
Douglas Gregora3a83512009-04-01 23:51:29 +000030
Richard Smith85b29a42012-02-17 01:35:32 +000031// CHECK: int x
32// CHECK: int y
33void f1(x, y) // expected-warning 2{{defaulting to type 'int'}}
Douglas Gregora3a83512009-04-01 23:51:29 +000034{
35}
36
37int i0 = { 17 };
38
Argyrios Kyrtzidisb5303aa2011-06-24 17:28:29 +000039#define ONE 1
40#define TWO 2
41
Richard Smith85b29a42012-02-17 01:35:32 +000042int test_cond(int y, int fooBar) { // expected-note {{here}}
Chris Lattnere5deae92010-04-20 21:33:39 +000043// CHECK: int x = y ? 1 : 4+fooBar;
Richard Smith85b29a42012-02-17 01:35:32 +000044 int x = y ? 1 4+foobar; // expected-error {{expected ':'}} expected-error {{undeclared identifier}} expected-note {{to match}}
Argyrios Kyrtzidisb5303aa2011-06-24 17:28:29 +000045// CHECK: x = y ? ONE : TWO;
Richard Smith85b29a42012-02-17 01:35:32 +000046 x = y ? ONE TWO; // expected-error {{':'}} expected-note {{to match}}
Ted Kremenek987aa872010-04-12 22:10:35 +000047 return x;
48}
Douglas Gregorae2fb142010-08-23 14:34:43 +000049
Richard Smith85b29a42012-02-17 01:35:32 +000050// CHECK: const typedef int int_t;
51const typedef typedef int int_t; // expected-warning {{duplicate 'typedef'}}
Douglas Gregor751f6922010-09-07 14:51:08 +000052
53// <rdar://problem/7159693>
54enum Color {
55 Red // expected-error{{missing ',' between enumerators}}
56 Green = 17 // expected-error{{missing ',' between enumerators}}
57 Blue,
58};
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +000059
60// rdar://9295072
61struct test_struct {
62 // CHECK: struct test_struct *struct_ptr;
63 test_struct *struct_ptr; // expected-error {{must use 'struct' tag to refer to type 'test_struct'}}
64};
Anna Zaksd5612a22011-07-28 20:52:06 +000065
66void removeUnusedLabels(char c) {
Richard Smith85b29a42012-02-17 01:35:32 +000067 L0 /*removed comment*/: c++; // expected-warning {{unused label}}
Anna Zaksd5612a22011-07-28 20:52:06 +000068 removeUnusedLabels(c);
Richard Smith85b29a42012-02-17 01:35:32 +000069 L1: // expected-warning {{unused label}}
Anna Zaksd5612a22011-07-28 20:52:06 +000070 c++;
Richard Smith85b29a42012-02-17 01:35:32 +000071 /*preserved comment*/ L2 : c++; // expected-warning {{unused label}}
72 LL // expected-warning {{unused label}}
Anna Zaksd5612a22011-07-28 20:52:06 +000073 : c++;
Richard Smith85b29a42012-02-17 01:35:32 +000074 c = c + 3; L4: return; // expected-warning {{unused label}}
Anna Zaksd5612a22011-07-28 20:52:06 +000075}
Richard Smith0706df42011-10-19 21:33:05 +000076
Richard Smith85b29a42012-02-17 01:35:32 +000077int oopsAComma = 0, // expected-error {{';'}}
Richard Smith0706df42011-10-19 21:33:05 +000078void oopsMoreCommas() {
Richard Smith85b29a42012-02-17 01:35:32 +000079 static int a[] = { 0, 1, 2 }, // expected-error {{';'}}
80 static int b[] = { 3, 4, 5 }, // expected-error {{';'}}
Richard Smith0706df42011-10-19 21:33:05 +000081 &a == &b ? oopsMoreCommas() : removeUnusedLabels(a[0]);
82}
Richard Smith85b29a42012-02-17 01:35:32 +000083
84int noSemiAfterLabel(int n) {
85 switch (n) {
86 default:
87 return n % 4;
88 case 0:
89 case 1:
90 case 2:
91 // CHECK: /*FOO*/ case 3: ;
92 /*FOO*/ case 3: // expected-error {{expected statement}}
93 }
94 switch (n) {
95 case 1:
96 case 2:
97 return 0;
98 // CHECK: /*BAR*/ default: ;
99 /*BAR*/ default: // expected-error {{expected statement}}
100 }
101 return 1;
102}