Debian Codegraph @

clang/test/Preprocessor/embed_weird.cpp

raw
1 // RUN: rm -rf %t && mkdir -p %t/media
2 // RUN: cp %S/Inputs/single_byte.txt %S/Inputs/jk.txt %S/Inputs/numbers.txt %t/
3 // RUN: cp %S/Inputs/media/empty %t/media/
4 // RUN: printf "\0" > %t/null_byte.bin
5 // RUN: %clang_cc1 %s -fsyntax-only --embed-dir=%t -verify=expected,cxx -Wno-c23-extensions
6 // RUN: %clang_cc1 -x c -std=c23 %s -fsyntax-only --embed-dir=%t -verify=expected,c
7 #embed <media/empty>
8 ;
9
10 void f (unsigned char x) { (void)x;}
11 void g () {}
12 void h (unsigned char x, int y) {(void)x; (void)y;}
13 int i () {
14 return
15 #embed <single_byte.txt>
16 ;
17 }
18
19 _Static_assert(
20 #embed <single_byte.txt> suffix(,)
21 ""
22 );
23 _Static_assert(
24 #embed <single_byte.txt>
25 , ""
26 );
27 _Static_assert(sizeof(
28 #embed <single_byte.txt>
29 ) ==
30 sizeof(int)
31 , ""
32 );
33 _Static_assert(sizeof
34 #embed <single_byte.txt>
35 , ""
36 );
37 _Static_assert(sizeof(
38 #embed <jk.txt>
39 ) ==
40 sizeof(int)
41 , ""
42 );
43
44 #ifdef __cplusplus
45 template <int First, int Second>
46 void j() {
47 static_assert(First == 'j', "");
48 static_assert(Second == 'k', "");
49 }
50 #endif
51
52 void do_stuff() {
53 f(
54 #embed <single_byte.txt>
55 );
56 g(
57 #embed <media/empty>
58 );
59 h(
60 #embed <jk.txt>
61 );
62 int r = i();
63 (void)r;
64 #ifdef __cplusplus
65 j<
66 #embed <jk.txt>
67 >(
68 #embed <media/empty>
69 );
70 #endif
71 }
72
73 // Ensure that we don't accidentally allow you to initialize an unsigned char *
74 // from embedded data; the data is modeled as a string literal internally, but
75 // is not actually a string literal.
76 const unsigned char *ptr = (
77 #embed <jk.txt> // expected-warning {{left operand of comma operator has no effect}}
78 ); // c-error@-2 {{incompatible integer to pointer conversion initializing 'const unsigned char *' with an expression of type 'int'}} \
79 cxx-error@-2 {{cannot initialize a variable of type 'const unsigned char *' with an rvalue of type 'int'}}
80
81 // However, there are some cases where this is fine and should work.
82 const unsigned char *null_ptr_1 =
83 #embed <media/empty> if_empty(0)
84 ;
85
86 const unsigned char *null_ptr_2 =
87 #embed <null_byte.bin>
88 ;
89
90 const unsigned char *null_ptr_3 = {
91 #embed <null_byte.bin>
92 };
93
94 #define FILE_NAME <null_byte.bin>
95 #define LIMIT 1
96 #define OFFSET 0
97 #define EMPTY_SUFFIX suffix()
98
99 constexpr unsigned char ch =
100 #embed FILE_NAME limit(LIMIT) clang::offset(OFFSET) EMPTY_SUFFIX
101 ;
102 static_assert(ch == 0);
103
104 void foobar(float x, char y, char z);
105 void g1() { foobar((float)
106 #embed "numbers.txt" limit(3)
107 );
108 }
109
110 #if __cplusplus
111 struct S { S(char x); ~S(); };
112 void f1() {
113 S s[] = {
114 #embed "null_byte.bin"
115 };
116 }
117 #endif
118
119 static_assert(_Generic(
120 #embed __FILE__ limit(1)
121 , int : 1, default : 0));
122
123 static_assert(alignof(typeof(
124 #embed __FILE__ limit(1)
125 )) == alignof(int));
126
127 struct HasChar {
128 signed char ch;
129 };
130
131 constexpr struct HasChar c = {
132 #embed "Inputs/big_char.txt" // cxx-error {{constant expression evaluates to 255 which cannot be narrowed to type 'signed char'}} \
133 cxx-note {{insert an explicit cast to silence this issue}} \
134 c-error {{constexpr initializer evaluates to 255 which is not exactly representable in type 'signed char'}}
135
136 };