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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/* XML support for diagnostics.
Copyright (C) 2024-2025 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#define INCLUDE_MAP
#define INCLUDE_STRING
#define INCLUDE_VECTOR
#include "system.h"
#include "coretypes.h"
#include "xml.h"
#include "xml-printer.h"
#include "pretty-print.h"
#include "selftest.h"
namespace xml {
/* Disable warnings about quoting issues in the pp_xxx calls below
that (intentionally) don't follow GCC diagnostic conventions. */
#if __GNUC__ >= 10
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wformat-diag"
#endif
/* Implementation. */
static void
write_escaped_text (pretty_printer *pp, const char *text)
{
gcc_assert (text);
for (const char *p = text; *p; ++p)
{
char ch = *p;
switch (ch)
{
default:
pp_character (pp, ch);
break;
case '\'':
pp_string (pp, "'");
break;
case '"':
pp_string (pp, """);
break;
case '&':
pp_string (pp, "&");
break;
case '<':
pp_string (pp, "<");
break;
case '>':
pp_string (pp, ">");
break;
}
}
}
/* struct node. */
void
node::dump (FILE *out) const
{
pretty_printer pp;
pp.set_output_stream (out);
write_as_xml (&pp, 0, true);
pp_flush (&pp);
}
/* struct text : public node. */
void
text::write_as_xml (pretty_printer *pp, int depth, bool indent) const
{
if (indent)
{
for (int i = 0; i < depth; ++i)
pp_string (pp, " ");
}
write_escaped_text (pp, m_str.c_str ());
if (indent)
pp_newline (pp);
}
/* struct node_with_children : public node. */
void
node_with_children::add_child (std::unique_ptr<node> node)
{
gcc_assert (node.get ());
m_children.push_back (std::move (node));
}
void
node_with_children::add_text (std::string str)
{
// Consolidate runs of text
if (!m_children.empty ())
if (text *t = m_children.back ()->dyn_cast_text ())
{
t->m_str += std::move (str);
return;
}
add_child (std::make_unique <text> (std::move (str)));
}
/* struct document : public node_with_children. */
void
document::write_as_xml (pretty_printer *pp, int depth, bool indent) const
{
pp_string (pp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
if (m_doctypedecl)
m_doctypedecl->write_as_xml (pp, depth, indent);
for (auto &iter : m_children)
iter->write_as_xml (pp, depth, indent);
}
/* struct element : public node_with_children. */
void
element::write_as_xml (pretty_printer *pp, int depth, bool indent) const
{
if (indent)
{
for (int i = 0; i < depth; ++i)
pp_string (pp, " ");
}
pp_printf (pp, "<%s", m_kind.c_str ());
for (auto &key : m_key_insertion_order)
{
auto iter = m_attributes.find (key);
if (iter != m_attributes.end ())
{
pp_printf (pp, " %s=\"", key.c_str ());
write_escaped_text (pp, iter->second.c_str ());
pp_string (pp, "\"");
}
}
if (m_children.empty ())
pp_string (pp, "/>");
else
{
const bool indent_children = m_preserve_whitespace ? false : indent;
pp_string (pp, ">");
if (indent_children)
pp_newline (pp);
for (auto &child : m_children)
child->write_as_xml (pp, depth + 1, indent_children);
if (indent_children)
{
for (int i = 0; i < depth; ++i)
pp_string (pp, " ");
}
pp_printf (pp, "</%s>", m_kind.c_str ());
}
if (indent)
pp_newline (pp);
}
void
element::set_attr (const char *name, std::string value)
{
auto iter = m_attributes.find (name);
if (iter == m_attributes.end ())
m_key_insertion_order.push_back (name);
m_attributes[name] = std::move (value);
}
// struct raw : public node
void
raw::write_as_xml (pretty_printer *pp,
int /*depth*/, bool /*indent*/) const
{
pp_string (pp, m_xml_src.c_str ());
}
#if __GNUC__ >= 10
# pragma GCC diagnostic pop
#endif
// class printer
printer::printer (element &insertion_point)
{
m_open_tags.push_back (&insertion_point);
}
void
printer::push_tag (std::string name,
bool preserve_whitespace)
{
push_element
(std::make_unique<element> (std::move (name),
preserve_whitespace));
}
void
printer::push_tag_with_class (std::string name, std::string class_,
bool preserve_whitespace)
{
auto new_element
= std::make_unique<element> (std::move (name),
preserve_whitespace);
new_element->set_attr ("class", class_);
push_element (std::move (new_element));
}
void
printer::pop_tag ()
{
m_open_tags.pop_back ();
}
void
printer::set_attr (const char *name, std::string value)
{
m_open_tags.back ()->set_attr (name, value);
}
void
printer::add_text (std::string text)
{
element *parent = m_open_tags.back ();
parent->add_text (std::move (text));
}
void
printer::add_raw (std::string text)
{
element *parent = m_open_tags.back ();
parent->add_child (std::make_unique<xml::raw> (std::move (text)));
}
void
printer::push_element (std::unique_ptr<element> new_element)
{
element *parent = m_open_tags.back ();
m_open_tags.push_back (new_element.get ());
parent->add_child (std::move (new_element));
}
void
printer::append (std::unique_ptr<node> new_node)
{
element *parent = m_open_tags.back ();
parent->add_child (std::move (new_node));
}
element *
printer::get_insertion_point () const
{
return m_open_tags.back ();
}
} // namespace xml
#if CHECKING_P
namespace selftest {
static void
test_no_dtd ()
{
xml::document doc;
pretty_printer pp;
doc.write_as_xml (&pp, 0, true);
ASSERT_STREQ
(pp_formatted_text (&pp),
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
}
static void
test_printer ()
{
xml::element top ("top", false);
xml::printer xp (top);
xp.push_tag ("foo");
xp.add_text ("hello");
xp.push_tag ("bar");
xp.set_attr ("size", "3");
xp.set_attr ("color", "red");
xp.add_text ("world");
xp.push_tag ("baz");
xp.pop_tag ();
xp.pop_tag ();
xp.pop_tag ();
pretty_printer pp;
top.write_as_xml (&pp, 0, true);
ASSERT_STREQ
(pp_formatted_text (&pp),
"<top>\n"
" <foo>\n"
" hello\n"
" <bar size=\"3\" color=\"red\">\n"
" world\n"
" <baz/>\n"
" </bar>\n"
" </foo>\n"
"</top>\n");
}
// Verify that element attributes preserve insertion order.
static void
test_attribute_ordering ()
{
xml::element top ("top", false);
xml::printer xp (top);
xp.push_tag ("chronological");
xp.set_attr ("maldon", "991");
xp.set_attr ("hastings", "1066");
xp.set_attr ("edgehill", "1642");
xp.set_attr ("naseby", "1645");
xp.pop_tag ();
xp.push_tag ("alphabetical");
xp.set_attr ("edgehill", "1642");
xp.set_attr ("hastings", "1066");
xp.set_attr ("maldon", "991");
xp.set_attr ("naseby", "1645");
xp.pop_tag ();
pretty_printer pp;
top.write_as_xml (&pp, 0, true);
ASSERT_STREQ
(pp_formatted_text (&pp),
"<top>\n"
" <chronological maldon=\"991\" hastings=\"1066\" edgehill=\"1642\" naseby=\"1645\"/>\n"
" <alphabetical edgehill=\"1642\" hastings=\"1066\" maldon=\"991\" naseby=\"1645\"/>\n"
"</top>\n");
}
/* Run all of the selftests within this file. */
void
xml_cc_tests ()
{
test_no_dtd ();
test_printer ();
test_attribute_ordering ();
}
} // namespace selftest
#endif /* CHECKING_P */
|