xref: /llvm-project/clang/test/Sema/reserved-identifier.c (revision 8c5edb59cf487373e545ccb87ffcd24cb7d0d0ec)
1 // RUN: %clang_cc1 -isystem %S/Inputs -fsyntax-only -verify -Wreserved-identifier -Wno-visibility %s
2 
3 #include <reserved-identifier.h>
4 
__I_AM_A_SYSTEM_MACRO()5 __I_AM_A_SYSTEM_MACRO() // no-warning
6 
7 void test_system_macro_expansion(void) {
8   SOME_SYSTEM_MACRO(); // no-warning
9 }
10 
11 #define __oof foo__ // expected-warning {{macro name is a reserved identifier}}
12 
foo__bar(void)13 int foo__bar(void) { return 0; }    // no-warning
_bar(void)14 static int _bar(void) { return 0; } // expected-warning {{identifier '_bar' is reserved because it starts with '_' at global scope}}
_Bar(void)15 static int _Bar(void) { return 0; } // expected-warning {{identifier '_Bar' is reserved because it starts with '_' followed by a capital letter}}
_foo(void)16 int _foo(void) { return 0; }        // expected-warning {{identifier '_foo' is reserved because it starts with '_' at global scope}}
17 
18 // This one is explicitly skipped by -Wreserved-identifier
19 void *_; // no-warning
20 
foo(unsigned int _Reserved)21 void foo(unsigned int _Reserved) { // expected-warning {{identifier '_Reserved' is reserved because it starts with '_' followed by a capital letter}}
22   unsigned int __1 =               // expected-warning {{identifier '__1' is reserved because it starts with '__'}}
23       _Reserved;                   // no-warning
24   goto __reserved;                 // expected-warning {{identifier '__reserved' is reserved because it starts with '__'}}
25 __reserved: // expected-warning {{identifier '__reserved' is reserved because it starts with '__'}}
26             ;
27   goto _not_reserved;
28 _not_reserved: ;
29 }
30 
foot(unsigned int _not_reserved)31 void foot(unsigned int _not_reserved) {} // no-warning
32 
33 enum __menu { // expected-warning {{identifier '__menu' is reserved because it starts with '__'}}
34   __some,     // expected-warning {{identifier '__some' is reserved because it starts with '__'}}
35   _Other,     // expected-warning {{identifier '_Other' is reserved because it starts with '_' followed by a capital letter}}
36   _other      // expected-warning {{identifier '_other' is reserved because it starts with '_' at global scope}}
37 };
38 
39 struct __babar { // expected-warning {{identifier '__babar' is reserved because it starts with '__'}}
40 };
41 
42 struct _Zebulon;   // expected-warning {{identifier '_Zebulon' is reserved because it starts with '_' followed by a capital letter}}
43 struct _Zebulon2 { // expected-warning {{identifier '_Zebulon2' is reserved because it starts with '_' followed by a capital letter}}
44 } * p;
45 struct _Zebulon3 *pp; // expected-warning {{identifier '_Zebulon3' is reserved because it starts with '_' followed by a capital letter}}
46 
47 typedef struct {
48   int _Field; // expected-warning {{identifier '_Field' is reserved because it starts with '_' followed by a capital letter}}
49   int _field; // no-warning
50 } _Typedef;   // expected-warning {{identifier '_Typedef' is reserved because it starts with '_' followed by a capital letter}}
51 
foobar(void)52 int foobar(void) {
53   return foo__bar(); // no-warning
54 }
55 
56 struct _reserved { // expected-warning {{identifier '_reserved' is reserved because it starts with '_' at global scope}}
57   int a;
cunf(void)58 } cunf(void) {
59   return (struct _reserved){1};
60 }
61 
62 // FIXME: According to clang declaration context layering, _preserved belongs to
63 // the translation unit, so we emit a warning. It's unclear that's what the
64 // standard mandate, but it's such a corner case we can live with it.
func(struct _preserved{ int a; } r)65 void func(struct _preserved { int a; } r) {} // expected-warning {{identifier '_preserved' is reserved because it starts with '_' at global scope}}
66 
67 extern char *_strdup(const char *); // expected-warning {{identifier '_strdup' is reserved because it starts with '_' at global scope}}
68 
69 // Don't warn on redeclaration
70 extern char *_strdup(const char *); // no-warning
71 
ok(void)72 void ok(void) {
73   void _ko(void);           // expected-warning {{identifier '_ko' is reserved because it starts with '_' at global scope}}
74   extern int _ko_again; // expected-warning {{identifier '_ko_again' is reserved because it starts with '_' at global scope}}
75 }
76