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