xref: /aosp_15_r20/external/clang/test/Preprocessor/has_include.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -ffreestanding -Eonly -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li // Try different path permutations of __has_include with existing file.
4*67e74705SXin Li #if __has_include("stdint.h")
5*67e74705SXin Li #else
6*67e74705SXin Li   #error "__has_include failed (1)."
7*67e74705SXin Li #endif
8*67e74705SXin Li 
9*67e74705SXin Li #if __has_include(<stdint.h>)
10*67e74705SXin Li #else
11*67e74705SXin Li   #error "__has_include failed (2)."
12*67e74705SXin Li #endif
13*67e74705SXin Li 
14*67e74705SXin Li // Try unary expression.
15*67e74705SXin Li #if !__has_include("stdint.h")
16*67e74705SXin Li   #error "__has_include failed (5)."
17*67e74705SXin Li #endif
18*67e74705SXin Li 
19*67e74705SXin Li // Try binary expression.
20*67e74705SXin Li #if __has_include("stdint.h") && __has_include("stddef.h")
21*67e74705SXin Li #else
22*67e74705SXin Li   #error "__has_include failed (6)."
23*67e74705SXin Li #endif
24*67e74705SXin Li 
25*67e74705SXin Li // Try non-existing file.
26*67e74705SXin Li #if __has_include("blahblah.h")
27*67e74705SXin Li   #error "__has_include failed (7)."
28*67e74705SXin Li #endif
29*67e74705SXin Li 
30*67e74705SXin Li // Try defined.
31*67e74705SXin Li #if !defined(__has_include)
32*67e74705SXin Li   #error "defined(__has_include) failed (8)."
33*67e74705SXin Li #endif
34*67e74705SXin Li 
35*67e74705SXin Li // Try different path permutations of __has_include_next with existing file.
36*67e74705SXin Li #if __has_include_next("stddef.h") // expected-warning {{#include_next in primary source file}}
37*67e74705SXin Li #else
38*67e74705SXin Li   #error "__has_include failed (1)."
39*67e74705SXin Li #endif
40*67e74705SXin Li 
41*67e74705SXin Li #if __has_include_next(<stddef.h>) // expected-warning {{#include_next in primary source file}}
42*67e74705SXin Li #else
43*67e74705SXin Li   #error "__has_include failed (2)."
44*67e74705SXin Li #endif
45*67e74705SXin Li 
46*67e74705SXin Li // Try unary expression.
47*67e74705SXin Li #if !__has_include_next("stdint.h") // expected-warning {{#include_next in primary source file}}
48*67e74705SXin Li   #error "__has_include_next failed (5)."
49*67e74705SXin Li #endif
50*67e74705SXin Li 
51*67e74705SXin Li // Try binary expression.
52*67e74705SXin Li #if __has_include_next("stdint.h") && __has_include("stddef.h") // expected-warning {{#include_next in primary source file}}
53*67e74705SXin Li #else
54*67e74705SXin Li   #error "__has_include_next failed (6)."
55*67e74705SXin Li #endif
56*67e74705SXin Li 
57*67e74705SXin Li // Try non-existing file.
58*67e74705SXin Li #if __has_include_next("blahblah.h") // expected-warning {{#include_next in primary source file}}
59*67e74705SXin Li   #error "__has_include_next failed (7)."
60*67e74705SXin Li #endif
61*67e74705SXin Li 
62*67e74705SXin Li // Try defined.
63*67e74705SXin Li #if !defined(__has_include_next)
64*67e74705SXin Li   #error "defined(__has_include_next) failed (8)."
65*67e74705SXin Li #endif
66*67e74705SXin Li 
67*67e74705SXin Li // Fun with macros
68*67e74705SXin Li #define MACRO1 __has_include(<stdint.h>)
69*67e74705SXin Li #define MACRO2 ("stdint.h")
70*67e74705SXin Li #define MACRO3 ("blahblah.h")
71*67e74705SXin Li #define MACRO4 blahblah.h>)
72*67e74705SXin Li #define MACRO5 <stdint.h>
73*67e74705SXin Li 
74*67e74705SXin Li #if !MACRO1
75*67e74705SXin Li   #error "__has_include with macro failed (1)."
76*67e74705SXin Li #endif
77*67e74705SXin Li 
78*67e74705SXin Li #if !__has_include MACRO2
79*67e74705SXin Li   #error "__has_include with macro failed (2)."
80*67e74705SXin Li #endif
81*67e74705SXin Li 
82*67e74705SXin Li #if __has_include MACRO3
83*67e74705SXin Li   #error "__has_include with macro failed (3)."
84*67e74705SXin Li #endif
85*67e74705SXin Li 
86*67e74705SXin Li #if __has_include(<MACRO4
87*67e74705SXin Li   #error "__has_include with macro failed (4)."
88*67e74705SXin Li #endif
89*67e74705SXin Li 
90*67e74705SXin Li #if !__has_include(MACRO5)
91*67e74705SXin Li   #error "__has_include with macro failed (2)."
92*67e74705SXin Li #endif
93*67e74705SXin Li 
94*67e74705SXin Li // Try as non-preprocessor directives
foo(void)95*67e74705SXin Li void foo( void ) {
96*67e74705SXin Li   __has_include_next("stdint.h")  // expected-warning {{#include_next in primary source file}} expected-error {{__has_include_next must be used within a preprocessing directive}}
97*67e74705SXin Li   __has_include("stdint.h")  // expected-error {{__has_include must be used within a preprocessing directive}}
98*67e74705SXin Li }
99*67e74705SXin Li 
100*67e74705SXin Li MACRO1  // expected-error {{__has_include must be used within a preprocessing directive}}
101*67e74705SXin Li 
102*67e74705SXin Li #if 1
103*67e74705SXin Li MACRO1  // expected-error {{__has_include must be used within a preprocessing directive}}
104*67e74705SXin Li #endif
105*67e74705SXin Li 
106*67e74705SXin Li #if 0
107*67e74705SXin Li #elif 1
108*67e74705SXin Li MACRO1  // expected-error {{__has_include must be used within a preprocessing directive}}
109*67e74705SXin Li #endif
110*67e74705SXin Li 
111*67e74705SXin Li #if 0
112*67e74705SXin Li MACRO1  // This should be fine because it is never actually reached
113*67e74705SXin Li #endif
114*67e74705SXin Li 
115*67e74705SXin Li 
116*67e74705SXin Li // Try badly formed expressions.
117*67e74705SXin Li // FIXME: We can recover better in almost all of these cases. (PR13335)
118*67e74705SXin Li 
119*67e74705SXin Li // expected-error@+1 {{missing '(' after '__has_include'}}
120*67e74705SXin Li #if __has_include "stdint.h")
121*67e74705SXin Li #endif
122*67e74705SXin Li 
123*67e74705SXin Li // expected-error@+1 {{expected "FILENAME" or <FILENAME>}} expected-error@+1 {{token is not a valid binary operator in a preprocessor subexpression}}
124*67e74705SXin Li #if __has_include(stdint.h)
125*67e74705SXin Li #endif
126*67e74705SXin Li 
127*67e74705SXin Li // expected-error@+1 {{expected "FILENAME" or <FILENAME>}}
128*67e74705SXin Li #if __has_include()
129*67e74705SXin Li #endif
130*67e74705SXin Li 
131*67e74705SXin Li // expected-error@+1 {{missing '(' after '__has_include'}}
132*67e74705SXin Li #if __has_include)
133*67e74705SXin Li #endif
134*67e74705SXin Li 
135*67e74705SXin Li // expected-error@+1 {{missing '(' after '__has_include'}}
136*67e74705SXin Li #if __has_include<stdint.h>)
137*67e74705SXin Li #endif
138*67e74705SXin Li 
139*67e74705SXin Li // expected-error@+1 {{expected "FILENAME" or <FILENAME>}} expected-warning@+1 {{missing terminating '"' character}}  expected-error@+1 {{invalid token at start of a preprocessor expression}}
140*67e74705SXin Li #if __has_include("stdint.h)
141*67e74705SXin Li #endif
142*67e74705SXin Li 
143*67e74705SXin Li // expected-error@+1 {{expected "FILENAME" or <FILENAME>}} expected-warning@+1 {{missing terminating '"' character}} expected-error@+1 {{token is not a valid binary operator in a preprocessor subexpression}}
144*67e74705SXin Li #if __has_include(stdint.h")
145*67e74705SXin Li #endif
146*67e74705SXin Li 
147*67e74705SXin Li // expected-error@+1 {{expected "FILENAME" or <FILENAME>}} expected-error@+1 {{token is not a valid binary operator in a preprocessor subexpression}}
148*67e74705SXin Li #if __has_include(stdint.h>)
149*67e74705SXin Li #endif
150*67e74705SXin Li 
151*67e74705SXin Li // expected-error@+1 {{__has_include must be used within a preprocessing directive}}
152*67e74705SXin Li __has_include
153*67e74705SXin Li 
154*67e74705SXin Li // expected-error@+1 {{missing ')' after '__has_include'}} // expected-error@+1 {{expected value in expression}}  // expected-note@+1 {{to match this '('}}
155*67e74705SXin Li #if __has_include("stdint.h"
156*67e74705SXin Li #endif
157*67e74705SXin Li 
158*67e74705SXin Li // expected-error@+1 {{expected "FILENAME" or <FILENAME>}} // expected-error@+1 {{expected value in expression}}
159*67e74705SXin Li #if __has_include(
160*67e74705SXin Li #endif
161*67e74705SXin Li 
162*67e74705SXin Li // expected-error@+1 {{missing '(' after '__has_include'}} // expected-error@+1 {{expected value in expression}}
163*67e74705SXin Li #if __has_include
164*67e74705SXin Li #endif
165*67e74705SXin Li 
166*67e74705SXin Li // expected-error@+1 {{missing '(' after '__has_include'}}
167*67e74705SXin Li #if __has_include'x'
168*67e74705SXin Li #endif
169*67e74705SXin Li 
170*67e74705SXin Li // expected-error@+1 {{expected "FILENAME" or <FILENAME>}}
171*67e74705SXin Li #if __has_include('x'
172*67e74705SXin Li #endif
173*67e74705SXin Li 
174*67e74705SXin Li // expected-error@+1 {{expected "FILENAME" or <FILENAME}} expected-error@+1 {{expected end of line in preprocessor expression}}
175*67e74705SXin Li #if __has_include('x')
176*67e74705SXin Li #endif
177*67e74705SXin Li 
178*67e74705SXin Li // expected-error@+1 {{missing ')' after '__has_include'}}  // expected-error@+1 {{expected value in expression}}  // expected-note@+1 {{to match this '('}}
179*67e74705SXin Li #if __has_include(<stdint.h>
180*67e74705SXin Li #endif
181*67e74705SXin Li 
182*67e74705SXin Li // expected-error@+1 {{expected "FILENAME" or <FILENAME>}} // expected-error@+1 {{expected value in expression}}
183*67e74705SXin Li #if __has_include(<stdint.h)
184*67e74705SXin Li #endif
185*67e74705SXin Li 
186*67e74705SXin Li #define HAS_INCLUDE(header) __has_include(header)
187*67e74705SXin Li #if HAS_INCLUDE(<stdint.h>)
188*67e74705SXin Li #else
189*67e74705SXin Li   #error "__has_include failed (9)."
190*67e74705SXin Li #endif
191*67e74705SXin Li 
192*67e74705SXin Li #if FOO
193*67e74705SXin Li #elif __has_include(<foo>)
194*67e74705SXin Li #endif
195*67e74705SXin Li 
196*67e74705SXin Li // PR15539
197*67e74705SXin Li #ifdef FOO
198*67e74705SXin Li #elif __has_include(<foo>)
199*67e74705SXin Li #endif
200