2
0

has_include.c 5.7 KB

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