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