xref: /llvm-project/clang/test/Sema/implicit-decl.c (revision 9a92f2f742347d9b31470349f3b777ecab580ac1)
1 // RUN: %clang_cc1 %s -verify=expected,both -fsyntax-only -Werror=implicit-function-declaration -std=c99
2 // RUN: %clang_cc1 %s -verify=expected,both -fsyntax-only -std=c11
3 // RUN: %clang_cc1 %s -verify=c2x,both -fsyntax-only -std=c2x
4 
5 /// -Werror-implicit-function-declaration is a deprecated alias used by many projects.
6 // RUN: %clang_cc1 %s -verify=expected,both -fsyntax-only -Werror-implicit-function-declaration
7 
8 // c2x-note@*:* {{'__builtin_va_list' declared here}}
9 
10 typedef int int32_t;
11 typedef unsigned char Boolean;
12 
13 extern int printf(__const char *__restrict __format, ...); // both-note{{'printf' declared here}}
func(void)14 void func(void) {
15    int32_t *vector[16];
16    const char compDesc[16 + 1];
17    int32_t compCount = 0;
18    if (_CFCalendarDecomposeAbsoluteTimeV(compDesc, vector, compCount)) { // expected-error {{call to undeclared function '_CFCalendarDecomposeAbsoluteTimeV'; ISO C99 and later do not support implicit function declarations}} \
19                                                                             expected-note {{previous implicit declaration}} \
20                                                                             c2x-error {{use of undeclared identifier '_CFCalendarDecomposeAbsoluteTimeV'}}
21    }
22 
23    printg("Hello, World!\n"); // expected-error{{call to undeclared function 'printg'; ISO C99 and later do not support implicit function declarations}} \
24                                  expected-note{{did you mean 'printf'?}} \
25                                  c2x-error {{use of undeclared identifier 'printg'; did you mean 'printf'?}}
26 
27   __builtin_is_les(1, 3); // expected-error{{use of unknown builtin '__builtin_is_les'}} \
28                              c2x-error {{unknown type name '__builtin_is_les'; did you mean '__builtin_va_list'?}} \
29                              c2x-error {{expected identifier or '('}} \
30                              c2x-error {{expected ')'}} \
31                              c2x-note {{to match this '('}}
32 }
_CFCalendarDecomposeAbsoluteTimeV(const char * componentDesc,int32_t ** vector,int32_t count)33 Boolean _CFCalendarDecomposeAbsoluteTimeV(const char *componentDesc, int32_t **vector, int32_t count) { // expected-error {{conflicting types}}
34  return 0;
35 }
36 
37 
38 // Test the typo-correction callback in Sema::ImplicitlyDefineFunction
39 extern int sformatf(char *str, __const char *__restrict __format, ...); // expected-note{{'sformatf' declared here}}
test_implicit(void)40 void test_implicit(void) {
41   int formats = 0; // c2x-note {{'formats' declared here}}
42   formatd("Hello, World!\n"); // expected-error{{call to undeclared function 'formatd'; ISO C99 and later do not support implicit function declarations}} \
43                                  expected-note{{did you mean 'sformatf'?}} \
44                                  c2x-error {{use of undeclared identifier 'formatd'; did you mean 'formats'?}} \
45                                  c2x-error {{called object type 'int' is not a function or function pointer}}
46 }
47 
test_suggestion(void)48 void test_suggestion(void) {
49   bark(); // expected-error {{call to undeclared function 'bark'; ISO C99 and later do not support implicit function declarations}} \
50              c2x-error {{use of undeclared identifier 'bark'}}
51   bork(); // expected-error {{call to undeclared function 'bork'; ISO C99 and later do not support implicit function declarations}} \
52              c2x-error {{use of undeclared identifier 'bork'}}
53 }
54 
55 // The following used to cause an assertion from trying to define the implicit
56 // function within a structure scope as opposed to within function or global
57 // scoped.
GH48579_1(void)58 int GH48579_1(void) {
59   struct {
60     int x __attribute__((aligned(({ a(); })))); // expected-error {{call to undeclared function 'a'; ISO C99 and later do not support implicit function declarations}} \
61                                                    c2x-error {{use of undeclared identifier 'a'}} \
62                                                    both-error {{'aligned' attribute requires integer constant}}
63   } x;
64 }
65 
GH48579_2(void)66 void GH48579_2(void) {
67   struct S {
68     int array[({ a(); })]; // expected-error {{call to undeclared function 'a'; ISO C99 and later do not support implicit function declarations}} \
69                               c2x-error {{use of undeclared identifier 'a'}} \
70                               expected-error {{fields must have a constant size: 'variable length array in structure' extension will never be supported}} \
71                               c2x-error {{size of array has non-integer type 'void'}}
72   };
73 }
74 
75 int GH48579_3 = ({a();});              // both-error {{statement expression not allowed at file scope}}
76 void GH48579_4(int array[({ a(); })]); // both-error {{statement expression not allowed at file scope}}
77 
pragma_warning(void)78 void pragma_warning(void) {
79 #pragma clang diagnostic warning "-Wimplicit-function-declaration"
80   bark(); // expected-warning {{call to undeclared function 'bark'; ISO C99 and later do not support implicit function declarations}} \
81              c2x-error {{use of undeclared identifier 'bark'}}
82 }
83 
pragma_error(void)84 void pragma_error(void) {
85 #pragma clang diagnostic error "-Wimplicit-function-declaration"
86   bark(); // expected-error {{call to undeclared function 'bark'; ISO C99 and later do not support implicit function declarations}} \
87              c2x-error {{use of undeclared identifier 'bark'}}
88 }
89 
pragma_ignored(void)90 void pragma_ignored(void) {
91 #pragma clang diagnostic ignored "-Wimplicit-function-declaration"
92   bark(); // c2x-error {{use of undeclared identifier 'bark'}}
93 }
94