xref: /llvm-project/clang/test/Preprocessor/embed_parameter_offset.c (revision 41c6e4379204ffc00948edd33d59ba5ebbceaba2)
1 // RUN: %clang_cc1 -std=c23 %s --embed-dir=%S/Inputs -fsyntax-only -verify
2 
3 const char data[] = {
4 #embed <jk.txt>
5 };
6 const char offset_data[] = {
7 #embed <jk.txt> clang::offset(1)
8 };
9 static_assert(sizeof(data) == 2);
10 static_assert('j' == data[0]);
11 static_assert('k' == data[1]);
12 static_assert(sizeof(offset_data) == 1);
13 static_assert('k' == offset_data[0]);
14 static_assert(offset_data[0] == data[1]);
15 
16 // Cannot have a negative offset.
17 #embed <jk.txt> clang::offset(-1)
18 // expected-error@-1 {{invalid value '-1'; must be positive}}
19 
20 // If the offset is past the end of the file, the file should be considered
21 // empty.
22 #if __has_embed(<jk.txt> clang::offset(3)) != __STDC_EMBED_EMPTY__
23 #error "__has_embed should return false when there's no data"
24 #endif
25 
26 // When the offset is past the end of the file, the resource is empty, so if_empty kicks in.
27 const unsigned char buffer[] = {
28 #embed <jk.txt> clang::offset(3) if_empty(1)
29 };
30 static_assert(sizeof(buffer) == 1);
31 static_assert(buffer[0] == 1);
32 
33 // However, prefix and suffix do not kick in.
34 const unsigned char other_buffer[] = {
35   1,
36 #embed <jk.txt> clang::offset(3) prefix(2,) suffix(3)
37 };
38 static_assert(sizeof(other_buffer) == 1);
39 static_assert(other_buffer[0] == 1);
40 
41 // Ensure we can offset to zero (that's the default behavior)
42 const unsigned char third_buffer[] = {
43 #embed <jk.txt> clang::offset(0)
44 };
45 static_assert(sizeof(third_buffer) == 2);
46 static_assert('j' == third_buffer[0]);
47 static_assert('k' == third_buffer[1]);
48 
49 // Test the offsets of a file with more than one character in it.
50 const unsigned char fourth_buffer[] = {
51 #embed <media/art.txt> clang::offset(24) limit(4)
52 };
53 static_assert(sizeof(fourth_buffer) == 4);
54 static_assert('.' == fourth_buffer[0]);
55 static_assert('-' == fourth_buffer[1]);
56 static_assert('.' == fourth_buffer[2]);
57 static_assert('\'' == fourth_buffer[3]);
58 
59 // Ensure that an offset larger than what can fit into a 64-bit value is
60 // rejected. This offset is fine because it fits in a 64-bit value.
61 const unsigned char fifth_buffer[] = {
62   1,
63 #embed <jk.txt> clang::offset(0xFFFF'FFFF'FFFF'FFFF)
64 };
65 static_assert(sizeof(fifth_buffer) == 1);
66 static_assert(1 == fifth_buffer[0]);
67 
68 // But this one is not fine because it does not fit into a 64-bit value.
69 const unsigned char sixth_buffer[] = {
70 #embed <jk.txt> clang::offset(0xFFFF'FFFF'FFFF'FFFF'1)
71 };
72 // expected-error@-2 {{integer literal is too large to be represented in any integer type}}
73 
74 // Ensure we diagnose duplicate parameters even if they're the same value.
75 const unsigned char a[] = {
76 #embed <jk.txt> clang::offset(1) prefix() clang::offset(1)
77 // expected-error@-1 {{cannot specify parameter 'clang::offset' twice in the same '#embed' directive}}
78 ,
79 #embed <jk.txt> clang::offset(1) if_empty() clang::offset(2)
80 // expected-error@-1 {{cannot specify parameter 'clang::offset' twice in the same '#embed' directive}}
81 };
82 
83 // Matches with C23 6.10.3.2p2, is documented as part of our extension.
84 static_assert(
85 #embed <jk.txt> clang::offset(defined(FOO))
86   == 0); // expected-error {{expected expression}}
87  /* expected-error@-2 {{'defined' cannot appear within this context}}
88     pedantic-warning@-2 {{'clang::offset' is a Clang extension}}
89   */
90