aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/plugin/diagnostic-test-string-literals-1.c
blob: d5be021896ec8802732186f5a9eabad33fc5022d (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* { dg-do compile } */
/* { dg-options "-O -fdiagnostics-show-caret" } */

/* This is a collection of unittests for ranges within string literals,
   using diagnostic_plugin_test_string_literals, which handles
   "__emit_string_literal_range" by generating a warning at the given
   subset of a string literal.

   The indices are 0-based.  It's easiest to verify things using string
   literals that are runs of 0-based digits (to avoid having to count
   characters).

   LITERAL is a const void * to allow testing the various kinds of wide
   string literal, rather than just const char *.  */

extern void __emit_string_literal_range (const void *literal, int caret_idx,
					 int start_idx, int end_idx);

void
test_simple_string_literal (void)
{
  __emit_string_literal_range ("0123456789", /* { dg-warning "range" } */
			       6, 6, 7);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range ("0123456789",
                                       ^~
   { dg-end-multiline-output "" } */
}

void
test_concatenated_string_literal (void)
{
  __emit_string_literal_range ("01234" "56789", /* { dg-warning "range" } */
			       4, 3, 6);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range ("01234" "56789",
                                    ~^~~~~~
   { dg-end-multiline-output "" } */
}

void
test_multiline_string_literal (void)
{
  __emit_string_literal_range ("01234" /* { dg-warning "range" } */
                               "56789",
                               4, 3, 6);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range ("01234"
                                    ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                "56789",
                                ~~~   
   { dg-end-multiline-output "" } */
  /* FIXME: why does the above need three trailing spaces?  */
}

/* Tests of various unicode encodings.

   Digits 0 through 9 are unicode code points:
      U+0030 DIGIT ZERO
      ...
      U+0039 DIGIT NINE
   However, these are not always valid as UCN (see the comment in
   libcpp/charset.c:_cpp_valid_ucn).

   Hence we need to test UCN using an alternative unicode
   representation of numbers; let's use Roman numerals,
   (though these start at one, not zero):
      U+2170 SMALL ROMAN NUMERAL ONE
      ...
      U+2174 SMALL ROMAN NUMERAL FIVE  ("v")
      U+2175 SMALL ROMAN NUMERAL SIX   ("vi")
      ...
      U+2178 SMALL ROMAN NUMERAL NINE.  */

void
test_hex (void)
{
  /* Digits 0-9, expressing digit 5 in ASCII as "\x35"
     and with a space in place of digit 6, to terminate the escaped
     hex code.  */
  __emit_string_literal_range ("01234\x35 789", /* { dg-warning "range" } */
			       4, 3, 7);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range ("01234\x35 789"
                                    ~^~~~~~~
   { dg-end-multiline-output "" } */
}

void
test_oct (void)
{
  /* Digits 0-9, expressing digit 5 in ASCII as "\065"
     and with a space in place of digit 6, to terminate the escaped
     octal code.  */
  __emit_string_literal_range ("01234\065 789", /* { dg-warning "range" } */
			       4, 3, 7);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range ("01234\065 789"
                                    ~^~~~~~~
   { dg-end-multiline-output "" } */
}

void
test_multiple (void)
{
  /* Digits 0-9, expressing digit 5 in ASCII as hex "\x35"
     digit 6 in ASCII as octal "\066", concatenating multiple strings.  */
  __emit_string_literal_range ("01234"  "\x35"  "\066"  "789", /* { dg-warning "range" } */
			       5, 3, 8);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range ("01234"  "\x35"  "\066"  "789",
                                    ~~~~~~^~~~~~~~~~~~~~~~~~
   { dg-end-multiline-output "" } */
}

void
test_ucn4 (void)
{
  /* Digits 0-9, expressing digits 5 and 6 as Roman numerals expressed
     as UCN 4.
     The resulting string is encoded as UTF-8.  Most of the digits are 1 byte
     each, but digits 5 and 6 are encoded with 3 bytes each.
     Hence to underline digits 4-7 we need to underling using bytes 4-11 in
     the UTF-8 encoding.  */
  __emit_string_literal_range ("01234\u2174\u2175789", /* { dg-warning "range" } */
			       5, 4, 11);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range ("01234\u2174\u2175789",
                                     ~^~~~~~~~~~~~~
   { dg-end-multiline-output "" } */
}

void
test_ucn8 (void)
{
  /* Digits 0-9, expressing digits 5 and 6 as Roman numerals as UCN 8.
     The resulting string is the same as as in test_ucn4 above, and hence
     has the same UTF-8 encoding, and so we again need to underline bytes
     4-11 in the UTF-8 encoding in order to underline digits 4-7.  */
  __emit_string_literal_range ("01234\U00002174\U00002175789", /* { dg-warning "range" } */
			       5, 4, 11);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range ("01234\U00002174\U00002175789",
                                     ~^~~~~~~~~~~~~~~~~~~~~
   { dg-end-multiline-output "" } */
}

void
test_u8 (void)
{
  /* Digits 0-9.  */
  __emit_string_literal_range (u8"0123456789", /* { dg-warning "range" } */
			       6, 4, 7);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range (u8"0123456789",
                                       ~~^~
   { dg-end-multiline-output "" } */
}

void
test_u (void)
{
  /* Digits 0-9.  */
  __emit_string_literal_range (u"0123456789", /* { dg-error "unable to read substring location: execution character set != source character set" } */
			       6, 4, 7);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range (u"0123456789",
                                ^~~~~~~~~~~~~
   { dg-end-multiline-output "" } */
}

void
test_U (void)
{
  /* Digits 0-9.  */
  __emit_string_literal_range (U"0123456789", /* { dg-error "unable to read substring location: execution character set != source character set" } */
			       6, 4, 7);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range (U"0123456789",
                                ^~~~~~~~~~~~~
   { dg-end-multiline-output "" } */
}

void
test_L (void)
{
  /* Digits 0-9.  */
  __emit_string_literal_range (L"0123456789", /* { dg-error "unable to read substring location: execution character set != source character set" } */
			       6, 4, 7);
/* { dg-begin-multiline-output "" }
   __emit_string_literal_range (L"0123456789",
                                ^~~~~~~~~~~~~
   { dg-end-multiline-output "" } */
}

void
test_macro (void)
{
#define START "01234"  /* { dg-warning "range" } */
  __emit_string_literal_range (START
                               "56789",
                               4, 3, 6);
/* { dg-begin-multiline-output "" }
 #define START "01234"
                   ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   __emit_string_literal_range (START
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                "56789",
                                ~~~
   { dg-end-multiline-output "" } */
}