aboutsummaryrefslogtreecommitdiff
path: root/clang/test/Preprocessor/embed_parameter_limit.c
blob: da3e4fb877c1b9c71974cd9b5badac0254b06bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// RUN: %clang_cc1 -std=c23 %s --embed-dir=%S/Inputs -fsyntax-only -verify

const char data[] = {
#embed <jk.txt>
};
const char offset_data[] = {
#embed <jk.txt> limit(1)
};
static_assert(sizeof(data) == 2);
static_assert('j' == data[0]);
static_assert('k' == data[1]);
static_assert(sizeof(offset_data) == 1);
static_assert('j' == offset_data[0]);
static_assert(offset_data[0] == data[0]);

// Cannot have a negative limit.
#embed <jk.txt> limit(-1)
// expected-error@-1 {{invalid value '-1'; must be positive}}

// It can have a limit of 0, in which case the __has_embed should return false.
#if __has_embed(<jk.txt> limit(0)) != __STDC_EMBED_EMPTY__
#error "__has_embed should return false when there's no data"
#endif

// When the limit is zero, the resource is empty, so if_empty kicks in.
const unsigned char buffer[] = {
#embed <jk.txt> limit(0) if_empty(1)
};
static_assert(sizeof(buffer) == 1);
static_assert(buffer[0] == 1);

// However, prefix and suffix do not kick in.
const unsigned char other_buffer[] = {
  1,
#embed <jk.txt> limit(0) prefix(2,) suffix(3)
};
static_assert(sizeof(other_buffer) == 1);
static_assert(other_buffer[0] == 1);

// Ensure we can limit to something larger than the file size as well.
const unsigned char third_buffer[] = {
#embed <jk.txt> limit(100)
};
static_assert(sizeof(third_buffer) == 2);
static_assert('j' == third_buffer[0]);
static_assert('k' == third_buffer[1]);

// Test the limits of a file with more than one character in it.
const unsigned char fourth_buffer[] = {
#embed <media/art.txt> limit(10)
};
static_assert(sizeof(fourth_buffer) == 10);
static_assert(' ' == fourth_buffer[0]);
static_assert(' ' == fourth_buffer[1]);
static_assert(' ' == fourth_buffer[2]);
static_assert(' ' == fourth_buffer[3]);
static_assert(' ' == fourth_buffer[4]);
static_assert(' ' == fourth_buffer[5]);
static_assert(' ' == fourth_buffer[6]);
static_assert(' ' == fourth_buffer[7]);
static_assert(' ' == fourth_buffer[8]);
static_assert(' ' == fourth_buffer[9]);

// Ensure that a limit larger than what can fit into a 64-bit value is
// rejected. This limit is fine because it fits in a 64-bit value.
const unsigned char fifth_buffer[] = {
#embed <jk.txt> limit(0xFFFF'FFFF'FFFF'FFFF)
};
static_assert(sizeof(fifth_buffer) == 2);
static_assert('j' == fifth_buffer[0]);
static_assert('k' == fifth_buffer[1]);

// But this one is not fine because it does not fit into a 64-bit value.
const unsigned char sixth_buffer[] = {
#embed <jk.txt> limit(0xFFFF'FFFF'FFFF'FFFF'1)
};
// expected-error@-2 {{integer literal is too large to be represented in any integer type}}
// Note: the preprocessor will continue with the truncated value, so the parser
// will treat this case and the previous one identically in terms of what
// contents are retained from the embedded resource (which is the entire file).

// Ensure we diagnose duplicate parameters even if they're the same value.
const unsigned char a[] = {
#embed <jk.txt> limit(1) prefix() limit(1)
// expected-error@-1 {{cannot specify parameter 'limit' twice in the same '#embed' directive}}
,
#embed <jk.txt> limit(1) if_empty() limit(2)
// expected-error@-1 {{cannot specify parameter 'limit' twice in the same '#embed' directive}}
};

// C23 6.10.3.2p2
static_assert(
#embed <jk.txt> limit(defined(FOO)) // expected-error {{'defined' cannot appear within this context}}
  == 0); // expected-error {{expected expression}}