xref: /llvm-project/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp (revision e4f3735d5f600b17b8f86956162d41ce82096685)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST1
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST2
3 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST3
4 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST4
5 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++14 -DTEST5
6 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++14 -DTEST6
7 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST7
8 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST8
9 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST9
10 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST10 -ffreestanding
11 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s -DTEST11
12 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s -DTEST12
13 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s -DTEST13
14 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s -DTEST14
15 // RUN: %clang_cc1 -triple x86_64-linux -emit-llvm-only -verify -pedantic %s -DTEST15
16 
17 #if TEST1
18 int main; // expected-error{{main cannot be declared as a variable in the global scope}}
19 
20 #elif TEST2
21 // expected-no-diagnostics
22 int f () {
23   int main;
24   return main;
25 }
26 
27 #elif TEST3
28 // expected-no-diagnostics
29 void x(int main) {};
30 int y(int main);
31 
32 #elif TEST4
33 // expected-no-diagnostics
34 class A {
35   static int main;
36 };
37 
38 #elif TEST5
39 // expected-no-diagnostics
40 template<class T> constexpr T main;
41 
42 #elif TEST6
43 extern template<class T> constexpr T main; //expected-error{{expected unqualified-id}}
44 
45 #elif TEST7
46 // expected-no-diagnostics
47 namespace foo {
48   int main;
49 }
50 
51 #elif TEST8
52 void z(void)
53 {
54   extern int main;  // expected-error{{main cannot be declared as a variable in the global scope}}}
55 }
56 
57 #elif TEST9
58 // expected-no-diagnostics
59 int q(void)
60 {
61   static int main;
62   return main;
63 }
64 
65 #elif TEST10
66 // expected-no-diagnostics
67 int main;
68 
69 #elif TEST11
70 extern "C" {
71   namespace Y {
72     int main; // expected-error {{main cannot be declared as a variable with C language linkage}}}
73   }
74 }
75 namespace ns {
76   extern "C" int main; // expected-error {{main cannot be declared as a variable with C language linkage}}
77 }
78 
79 #elif TEST12
80 extern "C" struct A { int main(); }; // ok
81 
82 namespace c {
83   extern "C" void main(); // expected-error {{'main' must return 'int'}} \
84                           // expected-warning {{'main' should not be 'extern "C"'}}
85 }
86 
87 extern "C" {
88   namespace Z {
89     void main(); // expected-error {{'main' must return 'int'}} \
90                  // expected-warning {{'main' should not be 'extern "C"'}}
91   }
92 }
93 
94 namespace ns {
95   extern "C" struct A {
96     int main; // ok
97   };
98 
99   extern "C" struct B {
100     int main(); // ok
101   };
102 }
103 
104 #elif TEST13
105 extern "C++" {
106   int main(); // expected-warning {{'main' should not be 'extern "C++"'}}
107 }
108 
109 extern "C++" int main(); // expected-warning {{'main' should not be 'extern "C++"'}}
110 
111 namespace ns1 {
112   extern "C++" int main(); // ok
113   extern "C" {
114     extern "C++" {
115       int main(void *); // ok
116     }
117   }
118 }
119 
120 namespace ns2 {
121   extern "C++" void main() {} // ok
122 }
123 
124 #elif TEST14
125 extern "C" {
126   int main(); // expected-warning {{'main' should not be 'extern "C"'}}
127 }
128 
129 extern "C" int main(); // expected-warning {{'main' should not be 'extern "C"'}}
130 
131 #elif TEST15
132 extern "C" __attribute__((visibility("default"))) __attribute__((weak))
133 int main(); // expected-warning {{'main' should not be 'extern "C"'}}
134 
135 unsigned long g() {
136   return reinterpret_cast<unsigned long>(&main); // expected-warning {{referring to 'main' within an expression is a Clang extension}}
137 }
138 
139 #else
140 #error Unknown Test
141 #endif
142