xref: /llvm-project/clang/test/Sema/extern-redecl.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-strict-prototypes %s
2 
3 static int a16[];  // expected-warning {{tentative array definition assumed to have one element}}
4 
f16(void)5 void f16(void) {
6     extern int a16[];
7 }
8 
9 
10 // PR10013: Scope of extern declarations extend past enclosing block
11 extern int PR10013_x;
PR10013(void)12 int PR10013(void) {
13   int *PR10013_x = 0;
14   {
15     extern int PR10013_x;
16     extern int PR10013_x;
17   }
18 
19   return PR10013_x; // expected-error{{incompatible pointer to integer conversion}}
20 }
21 
22 static int test1_a[]; // expected-warning {{tentative array definition assumed to have one element}}
23 extern int test1_a[];
24 
test2declarer(void)25 void test2declarer(void) { extern int test2_array[100]; }
26 extern int test2_array[];
27 int test2v = sizeof(test2_array); // expected-error {{invalid application of 'sizeof' to an incomplete type 'int[]'}}
28 
test3declarer(void)29 void test3declarer(void) {
30   { extern int test3_array[100]; }
31   extern int test3_array[];
32   int x = sizeof(test3_array); // expected-error {{invalid application of 'sizeof' to an incomplete type 'int[]'}}
33 }
34 
test4(void)35 void test4(void) {
36   extern int test4_array[];
37   {
38     extern int test4_array[100];
39     int x = sizeof(test4_array); // fine
40   }
41   int x = sizeof(test4_array); // expected-error {{invalid application of 'sizeof' to an incomplete type 'int[]'}}
42 }
43 
44 // Test that invalid local extern declarations of library
45 // builtins behave reasonably.
46 extern void abort(void); // expected-note 2 {{previous declaration is here}}
47 extern float *calloc(void); // expected-warning {{incompatible redeclaration of library function}} expected-note {{is a builtin}} expected-note 2 {{previous declaration is here}}
test5a(void)48 void test5a(void) {
49   int abort(void); // expected-error {{conflicting types}}
50   float *malloc(void); // expected-warning {{incompatible redeclaration of library function}} expected-note 2 {{is a builtin}}
51   int *calloc(void); // expected-error {{conflicting types}}
52 }
test5b(void)53 void test5b(void) {
54   int abort(void); // expected-error {{conflicting types}}
55   float *malloc(void); // expected-warning {{incompatible redeclaration of library function}}
56   int *calloc(void); // expected-error {{conflicting types}}
57 }
test5c(void)58 void test5c(void) {
59   void (*_abort)(void) = &abort;
60   void *(*_malloc)() = &malloc;
61   float *(*_calloc)() = &calloc;
62 }
63 
test6(void)64 void test6(void) {
65   extern int test6_array1[100];
66   extern int test6_array2[100];
67   void test6_fn1(int*);
68   void test6_fn2(int*);
69   {
70     // Types are only merged from visible declarations.
71     char test6_array2;
72     char test6_fn2;
73     {
74       extern int test6_array1[];
75       extern int test6_array2[];
76       (void)sizeof(test6_array1); // ok
77       (void)sizeof(test6_array2); // expected-error {{incomplete type}}
78 
79       void test6_fn1();
80       void test6_fn2();
81       test6_fn1(1.2); // expected-error {{passing 'double' to parameter of incompatible type 'int *'}}
82       // FIXME: This is valid, but we should warn on it.
83       test6_fn2(1.2);
84     }
85   }
86 }
87