xref: /llvm-project/clang/test/Preprocessor/has_c_attribute.c (revision 79f87be6888d13a94661be9d8908c83dd2229c9b)
1 // RUN: %clang_cc1 -std=c11 -E -P %s -o - | FileCheck %s
2 // RUN: %clang_cc1 -std=c2x -E -P %s -o - | FileCheck %s
3 
4 #define C2x(x) x: __has_c_attribute(x)
5 
6 // CHECK: fallthrough: 201910L
7 C2x(fallthrough)
8 
9 // CHECK: __nodiscard__: 202003L
10 C2x(__nodiscard__)
11 
12 // CHECK: warn_unused_result: 0
13 C2x(warn_unused_result)
14 
15 // CHECK: gnu::warn_unused_result: 1
16 C2x(gnu::warn_unused_result)
17 
18 // CHECK: clang::warn_unused_result: 0
19 C2x(clang::warn_unused_result)
20 
21 // CHECK: selectany: 0
22 C2x(selectany); // Known attribute not supported in C mode
23 
24 // CHECK: frobble: 0
25 C2x(frobble) // Unknown attribute
26 
27 // CHECK: frobble::frobble: 0
28 C2x(frobble::frobble) // Unknown vendor namespace
29 
30 // CHECK: clang::annotate: 1
31 C2x(clang::annotate)
32 
33 // CHECK: deprecated: 201904L
34 C2x(deprecated)
35 
36 // CHECK: maybe_unused: 202106L
37 C2x(maybe_unused)
38 
39 // CHECK: __gnu__::warn_unused_result: 1
40 C2x(__gnu__::warn_unused_result)
41 
42 // CHECK: gnu::__warn_unused_result__: 1
43 C2x(gnu::__warn_unused_result__)
44 
45 // Test that macro expansion of the builtin argument works.
46 #define C clang
47 #define L likely
48 #define CL clang::likely
49 #define N nodiscard
50 
51 #if __has_c_attribute(N)
52 int has_nodiscard;
53 #endif
54 // CHECK: int has_nodiscard;
55 
56 #if __has_c_attribute(C::L)
57 int has_clang_likely_1;
58 #endif
59 // CHECK: int has_clang_likely_1;
60 
61 #if __has_c_attribute(clang::L)
62 int has_clang_likely_2;
63 #endif
64 // CHECK: int has_clang_likely_2;
65 
66 #if __has_c_attribute(C::likely)
67 int has_clang_likely_3;
68 #endif
69 // CHECK: int has_clang_likely_3;
70 
71 #if __has_c_attribute(CL)
72 int has_clang_likely_4;
73 #endif
74 // CHECK: int has_clang_likely_4;
75 
76 #define FUNCLIKE1(x) clang::x
77 #if __has_c_attribute(FUNCLIKE1(likely))
78 int funclike_1;
79 #endif
80 // CHECK: int funclike_1;
81 
82 #define FUNCLIKE2(x) _Clang::x
83 #if __has_c_attribute(FUNCLIKE2(likely))
84 int funclike_2;
85 #endif
86 // CHECK: int funclike_2;
87