Debian Codegraph @

gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_xhtml_format.cc

raw
1 /* Verify that we can write a non-trivial diagnostic output format
2 as a plugin (XHTML).
3 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 Contributed by David Malcolm <[email protected]>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22
23 #include "config.h"
24 #define INCLUDE_LIST
25 #define INCLUDE_MAP
26 #define INCLUDE_MEMORY
27 #define INCLUDE_VECTOR
28 #include "system.h"
29 #include "coretypes.h"
30 #include "diagnostic.h"
31 #include "diagnostic-metadata.h"
32 #include "diagnostic-path.h"
33 #include "cpplib.h"
34 #include "logical-location.h"
35 #include "diagnostic-client-data-hooks.h"
36 #include "diagnostic-diagram.h"
37 #include "text-art/canvas.h"
38 #include "diagnostic-format.h"
39 #include "diagnostic-buffer.h"
40 #include "ordered-hash-map.h"
41 #include "sbitmap.h"
42 #include "make-unique.h"
43 #include "selftest.h"
44 #include "selftest-diagnostic.h"
45 #include "selftest-diagnostic-show-locus.h"
46 #include "text-range-label.h"
47 #include "pretty-print-format-impl.h"
48 #include "pretty-print-urlifier.h"
49 #include "intl.h"
50 #include "gcc-plugin.h"
51 #include "plugin-version.h"
52
53 namespace xml {
54
55 /* Disable warnings about quoting issues in the pp_xxx calls below
56 that (intentionally) don't follow GCC diagnostic conventions. */
57 #if __GNUC__ >= 10
58 # pragma GCC diagnostic push
59 # pragma GCC diagnostic ignored "-Wformat-diag"
60 #endif
61
62 static void write_escaped_text (const char *text);
63
64 struct node
65 {
66 virtual ~node () {}
67 virtual void write_as_xml (pretty_printer *pp,
68 int depth, bool indent) const = 0;
69 void dump (FILE *out) const;
70 void DEBUG_FUNCTION dump () const { dump (stderr); }
71 };
72
73 struct text : public node
74 {
75 text (label_text str)
76 : m_str (std::move (str))
77 {}
78
79 void write_as_xml (pretty_printer *pp,
80 int depth, bool indent) const final override;
81
82 label_text m_str;
83 };
84
85 struct node_with_children : public node
86 {
87 void add_child (std::unique_ptr<node> node);
88 void add_text (label_text str);
89
90 std::vector<std::unique_ptr<node>> m_children;
91 };
92
93 struct document : public node_with_children
94 {
95 void write_as_xml (pretty_printer *pp,
96 int depth, bool indent) const final override;
97 };
98
99 struct element : public node_with_children
100 {
101 element (const char *kind, bool preserve_whitespace)
102 : m_kind (kind),
103 m_preserve_whitespace (preserve_whitespace)
104 {}
105
106 void write_as_xml (pretty_printer *pp,
107 int depth, bool indent) const final override;
108
109 void set_attr (const char *name, label_text value);
110
111 const char *m_kind;
112 bool m_preserve_whitespace;
113 std::map<const char *, label_text> m_attributes;
114 };
115
116 /* Implementation. */
117
118 static void
119 write_escaped_text (pretty_printer *pp, const char *text)
120 {
121 gcc_assert (text);
122
123 for (const char *p = text; *p; ++p)
124 {
125 char ch = *p;
126 switch (ch)
127 {
128 default:
129 pp_character (pp, ch);
130 break;
131 case '\'':
132 pp_string (pp, "&apos;");
133 break;
134 case '"':
135 pp_string (pp, "&quot;");
136 break;
137 case '&':
138 pp_string (pp, "&amp;");
139 break;
140 case '<':
141 pp_string (pp, "&lt;");
142 break;
143 case '>':
144 pp_string (pp, "&gt;");
145 break;
146 }
147 }
148 }
149
150 /* struct node. */
151
152 void
153 node::dump (FILE *out) const
154 {
155 pretty_printer pp;
156 pp.set_output_stream (out);
157 write_as_xml (&pp, 0, true);
158 pp_flush (&pp);
159 }
160
161 /* struct text : public node. */
162
163 void
164 text::write_as_xml (pretty_printer *pp, int /*depth*/, bool /*indent*/) const
165 {
166 write_escaped_text (pp, m_str.get ());
167 }
168
169 /* struct node_with_children : public node. */
170
171 void
172 node_with_children::add_child (std::unique_ptr<node> node)
173 {
174 gcc_assert (node.get ());
175 m_children.push_back (std::move (node));
176 }
177
178 void
179 node_with_children::add_text (label_text str)
180 {
181 gcc_assert (str.get ());
182 add_child (::make_unique <text> (std::move (str)));
183 }
184
185
186 /* struct document : public node_with_children. */
187
188 void
189 document::write_as_xml (pretty_printer *pp, int depth, bool indent) const
190 {
191 pp_string (pp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
192 pp_string (pp, "<!DOCTYPE html\n"
193 " PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
194 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
195 for (auto &iter : m_children)
196 iter->write_as_xml (pp, depth, indent);
197 }
198
199 /* struct element : public node_with_children. */
200
201 void
202 element::write_as_xml (pretty_printer *pp, int depth, bool indent) const
203 {
204 if (indent)
205 {
206 pp_newline (pp);
207 for (int i = 0; i < depth; ++i)
208 pp_string (pp, " ");
209 }
210
211 if (m_preserve_whitespace)
212 indent = false;
213
214 pp_printf (pp, "<%s", m_kind);
215 for (auto &attr : m_attributes)
216 {
217 pp_printf (pp, " %s=\"", attr.first);
218 write_escaped_text (pp, attr.second.get ());
219 pp_string (pp, "\"");
220 }
221 if (m_children.empty ())
222 pp_string (pp, " />");
223 else
224 {
225 pp_string (pp, ">");
226 for (auto &child : m_children)
227 child->write_as_xml (pp, depth + 1, indent);
228 if (indent)
229 {
230 pp_newline (pp);
231 for (int i = 0; i < depth; ++i)
232 pp_string (pp, " ");
233 }
234 pp_printf (pp, "</%s>", m_kind);
235 }
236 }
237
238 void
239 element::set_attr (const char *name, label_text value)
240 {
241 m_attributes[name] = std::move (value);
242 }
243
244 #if __GNUC__ >= 10
245 # pragma GCC diagnostic pop
246 #endif
247
248 } // namespace xml
249
250 class xhtml_builder;
251
252 /* Concrete buffering implementation subclass for HTML output. */
253
254 class diagnostic_xhtml_format_buffer : public diagnostic_per_format_buffer
255 {
256 public:
257 friend class xhtml_builder;
258 friend class xhtml_output_format;
259
260 diagnostic_xhtml_format_buffer (xhtml_builder &builder)
261 : m_builder (builder)
262 {}
263
264 void dump (FILE *out, int indent) const final override;
265 bool empty_p () const final override;
266 void move_to (diagnostic_per_format_buffer &dest) final override;
267 void clear () final override;
268 void flush () final override;
269
270 void add_result (std::unique_ptr<xml::element> result)
271 {
272 m_results.push_back (std::move (result));
273 }
274
275 private:
276 xhtml_builder &m_builder;
277 std::vector<std::unique_ptr<xml::element>> m_results;
278 };
279
280 /* A class for managing XHTML output of diagnostics.
281
282 Implemented:
283 - message text
284
285 Known limitations/missing functionality:
286 - title for page
287 - file/line/column
288 - error vs warning
289 - CWEs
290 - rules
291 - fix-it hints
292 - paths
293 */
294
295 class xhtml_builder
296 {
297 public:
298 friend class diagnostic_xhtml_format_buffer;
299
300 xhtml_builder (diagnostic_context &context,
301 pretty_printer &pp,
302 const line_maps *line_maps);
303
304 void on_report_diagnostic (const diagnostic_info &diagnostic,
305 diagnostic_t orig_diag_kind,
306 diagnostic_xhtml_format_buffer *buffer);
307 void emit_diagram (const diagnostic_diagram &diagram);
308 void end_group ();
309
310 std::unique_ptr<xml::element> take_current_diagnostic ()
311 {
312 return std::move (m_cur_diagnostic_element);
313 }
314
315 void flush_to_file (FILE *outf);
316
317 const xml::document &get_document () const { return *m_document; }
318
319 void set_printer (pretty_printer &pp)
320 {
321 m_printer = &pp;
322 }
323
324 private:
325 std::unique_ptr<xml::element>
326 make_element_for_diagnostic (const diagnostic_info &diagnostic,
327 diagnostic_t orig_diag_kind);
328
329 diagnostic_context &m_context;
330 pretty_printer *m_printer;
331 const line_maps *m_line_maps;
332
333 std::unique_ptr<xml::document> m_document;
334 xml::element *m_diagnostics_element;
335 std::unique_ptr<xml::element> m_cur_diagnostic_element;
336 };
337
338 static std::unique_ptr<xml::element>
339 make_div (label_text class_)
340 {
341 auto div = ::make_unique<xml::element> ("div", false);
342 div->set_attr ("class", std::move (class_));
343 return div;
344 }
345
346 static std::unique_ptr<xml::element>
347 make_span (label_text class_)
348 {
349 auto span = ::make_unique<xml::element> ("span", true);
350 span->set_attr ("class", std::move (class_));
351 return span;
352 }
353
354 /* class diagnostic_xhtml_format_buffer : public diagnostic_per_format_buffer. */
355
356 void
357 diagnostic_xhtml_format_buffer::dump (FILE *out, int indent) const
358 {
359 fprintf (out, "%*sdiagnostic_xhtml_format_buffer:\n", indent, "");
360 int idx = 0;
361 for (auto &result : m_results)
362 {
363 fprintf (out, "%*sresult[%i]:\n", indent + 2, "", idx);
364 result->dump (out);
365 fprintf (out, "\n");
366 ++idx;
367 }
368 }
369
370 bool
371 diagnostic_xhtml_format_buffer::empty_p () const
372 {
373 return m_results.empty ();
374 }
375
376 void
377 diagnostic_xhtml_format_buffer::move_to (diagnostic_per_format_buffer &base)
378 {
379 diagnostic_xhtml_format_buffer &dest
380 = static_cast<diagnostic_xhtml_format_buffer &> (base);
381 for (auto &&result : m_results)
382 dest.m_results.push_back (std::move (result));
383 m_results.clear ();
384 }
385
386 void
387 diagnostic_xhtml_format_buffer::clear ()
388 {
389 m_results.clear ();
390 }
391
392 void
393 diagnostic_xhtml_format_buffer::flush ()
394 {
395 for (auto &&result : m_results)
396 m_builder.m_diagnostics_element->add_child (std::move (result));
397 m_results.clear ();
398 }
399
400 /* class xhtml_builder. */
401
402 /* xhtml_builder's ctor. */
403
404 xhtml_builder::xhtml_builder (diagnostic_context &context,
405 pretty_printer &pp,
406 const line_maps *line_maps)
407 : m_context (context),
408 m_printer (&pp),
409 m_line_maps (line_maps)
410 {
411 gcc_assert (m_line_maps);
412
413 m_document = ::make_unique<xml::document> ();
414 {
415 auto html_element = ::make_unique<xml::element> ("html", false);
416 html_element->set_attr
417 ("xmlns",
418 label_text::borrow ("http://www.w3.org/1999/xhtml"));
419 {
420 {
421 auto head_element = ::make_unique<xml::element> ("head", false);
422 {
423 auto title_element = ::make_unique<xml::element> ("title", true);
424 label_text title (label_text::borrow ("Title goes here")); // TODO
425 title_element->add_text (std::move (title));
426 head_element->add_child (std::move (title_element));
427 }
428 html_element->add_child (std::move (head_element));
429
430 auto body_element = ::make_unique<xml::element> ("body", false);
431 {
432 auto diagnostics_element
433 = make_div (label_text::borrow ("gcc-diagnostic-list"));
434 m_diagnostics_element = diagnostics_element.get ();
435 body_element->add_child (std::move (diagnostics_element));
436 }
437 html_element->add_child (std::move (body_element));
438 }
439 }
440 m_document->add_child (std::move (html_element));
441 }
442 }
443
444 /* Implementation of "on_report_diagnostic" for XHTML output. */
445
446 void
447 xhtml_builder::on_report_diagnostic (const diagnostic_info &diagnostic,
448 diagnostic_t orig_diag_kind,
449 diagnostic_xhtml_format_buffer *buffer)
450 {
451 if (diagnostic.kind == DK_ICE || diagnostic.kind == DK_ICE_NOBT)
452 {
453 /* Print a header for the remaining output to stderr, and
454 return, attempting to print the usual ICE messages to
455 stderr. Hopefully this will be helpful to the user in
456 indicating what's gone wrong (also for DejaGnu, for pruning
457 those messages). */
458 fnotice (stderr, "Internal compiler error:\n");
459 }
460
461 auto diag_element
462 = make_element_for_diagnostic (diagnostic, orig_diag_kind);
463 if (buffer)
464 {
465 gcc_assert (!m_cur_diagnostic_element);
466 buffer->m_results.push_back (std::move (diag_element));
467 }
468 else
469 {
470 if (m_cur_diagnostic_element)
471 /* Nested diagnostic. */
472 m_cur_diagnostic_element->add_child (std::move (diag_element));
473 else
474 /* Top-level diagnostic. */
475 m_cur_diagnostic_element = std::move (diag_element);
476 }
477 }
478
479 std::unique_ptr<xml::element>
480 xhtml_builder::make_element_for_diagnostic (const diagnostic_info &diagnostic,
481 diagnostic_t orig_diag_kind)
482 {
483 class xhtml_token_printer : public token_printer
484 {
485 public:
486 xhtml_token_printer (xhtml_builder &builder,
487 xml::element &parent_element)
488 : m_builder (builder)
489 {
490 m_open_elements.push_back (&parent_element);
491 }
492 void print_tokens (pretty_printer */*pp*/,
493 const pp_token_list &tokens) final override
494 {
495 /* Implement print_tokens by adding child elements to
496 m_parent_element. */
497 for (auto iter = tokens.m_first; iter; iter = iter->m_next)
498 switch (iter->m_kind)
499 {
500 default:
501 gcc_unreachable ();
502
503 case pp_token::kind::text:
504 {
505 pp_token_text *sub = as_a <pp_token_text *> (iter);
506 /* The value might be in the obstack, so we may need to
507 copy it. */
508 insertion_element ().add_text
509 (label_text::take (xstrdup (sub->m_value.get ())));
510 }
511 break;
512
513 case pp_token::kind::begin_color:
514 case pp_token::kind::end_color:
515 /* These are no-ops. */
516 break;
517
518 case pp_token::kind::begin_quote:
519 {
520 insertion_element ().add_text (label_text::borrow (open_quote));
521 push_element (make_span (label_text::borrow ("gcc-quoted-text")));
522 }
523 break;
524 case pp_token::kind::end_quote:
525 {
526 pop_element ();
527 insertion_element ().add_text (label_text::borrow (close_quote));
528 }
529 break;
530
531 case pp_token::kind::begin_url:
532 {
533 pp_token_begin_url *sub = as_a <pp_token_begin_url *> (iter);
534 auto anchor = ::make_unique<xml::element> ("a", true);
535 anchor->set_attr ("href", std::move (sub->m_value));
536 push_element (std::move (anchor));
537 }
538 break;
539 case pp_token::kind::end_url:
540 pop_element ();
541 break;
542 }
543 }
544
545 private:
546 xml::element &insertion_element () const
547 {
548 return *m_open_elements.back ();
549 }
550 void push_element (std::unique_ptr<xml::element> new_element)
551 {
552 xml::element &current_top = insertion_element ();
553 m_open_elements.push_back (new_element.get ());
554 current_top.add_child (std::move (new_element));
555 }
556 void pop_element ()
557 {
558 m_open_elements.pop_back ();
559 }
560
561 xhtml_builder &m_builder;
562 /* We maintain a stack of currently "open" elements.
563 Children are added to the topmost open element. */
564 std::vector<xml::element *> m_open_elements;
565 };
566
567 auto diag_element = make_div (label_text::borrow ("gcc-diagnostic"));
568
569 // TODO: might be nice to emulate the text output format, but colorize it
570
571 auto message_span = make_span (label_text::borrow ("gcc-message"));
572 xhtml_token_printer tok_printer (*this, *message_span.get ());
573 m_printer->set_token_printer (&tok_printer);
574 pp_output_formatted_text (m_printer, m_context.get_urlifier ());
575 m_printer->set_token_printer (nullptr);
576 pp_clear_output_area (m_printer);
577 diag_element->add_child (std::move (message_span));
578
579 if (diagnostic.metadata)
580 {
581 int cwe = diagnostic.metadata->get_cwe ();
582 if (cwe)
583 {
584 diag_element->add_text (label_text::borrow (" "));
585 auto cwe_span = make_span (label_text::borrow ("gcc-cwe-metadata"));
586 cwe_span->add_text (label_text::borrow ("["));
587 {
588 auto anchor = ::make_unique<xml::element> ("a", true);
589 anchor->set_attr ("href", label_text::take (get_cwe_url (cwe)));
590 pretty_printer pp;
591 pp_printf (&pp, "CWE-%i", cwe);
592 anchor->add_text
593 (label_text::take (xstrdup (pp_formatted_text (&pp))));
594 cwe_span->add_child (std::move (anchor));
595 }
596 cwe_span->add_text (label_text::borrow ("]"));
597 diag_element->add_child (std::move (cwe_span));
598 }
599 }
600
601 // TODO: show any rules
602
603 label_text option_text = label_text::take
604 (m_context.make_option_name (diagnostic.option_id,
605 orig_diag_kind, diagnostic.kind));
606 if (option_text.get ())
607 {
608 label_text option_url = label_text::take
609 (m_context.make_option_url (diagnostic.option_id));
610
611 diag_element->add_text (label_text::borrow (" "));
612 auto option_span = make_span (label_text::borrow ("gcc-option"));
613 option_span->add_text (label_text::borrow ("["));
614 {
615 if (option_url.get ())
616 {
617 auto anchor = ::make_unique<xml::element> ("a", true);
618 anchor->set_attr ("href", std::move (option_url));
619 anchor->add_text (std::move (option_text));
620 option_span->add_child (std::move (anchor));
621 }
622 else
623 option_span->add_text (std::move (option_text));
624 option_span->add_text (label_text::borrow ("]"));
625 }
626 diag_element->add_child (std::move (option_span));
627 }
628
629 {
630 auto pre = ::make_unique<xml::element> ("pre", true);
631 pre->set_attr ("class", label_text::borrow ("gcc-annotated-source"));
632 // TODO: ideally we'd like to capture elements within the following:
633 diagnostic_show_locus (&m_context, m_context.m_source_printing,
634 diagnostic.richloc, diagnostic.kind,
635 m_printer);
636 pre->add_text
637 (label_text::take (xstrdup (pp_formatted_text (m_printer))));
638 pp_clear_output_area (m_printer);
639 diag_element->add_child (std::move (pre));
640 }
641
642 return diag_element;
643 }
644
645 /* Implementation of diagnostic_context::m_diagrams.m_emission_cb
646 for XHTML output. */
647
648 void
649 xhtml_builder::emit_diagram (const diagnostic_diagram &/*diagram*/)
650 {
651 /* We must be within the emission of a top-level diagnostic. */
652 gcc_assert (m_cur_diagnostic_element);
653
654 // TODO
655 }
656
657 /* Implementation of "end_group_cb" for XHTML output. */
658
659 void
660 xhtml_builder::end_group ()
661 {
662 if (m_cur_diagnostic_element)
663 m_diagnostics_element->add_child (std::move (m_cur_diagnostic_element));
664 }
665
666 /* Create a top-level object, and add it to all the results
667 (and other entities) we've seen so far.
668
669 Flush it all to OUTF. */
670
671 void
672 xhtml_builder::flush_to_file (FILE *outf)
673 {
674 auto top = m_document.get ();
675 top->dump (outf);
676 fprintf (outf, "\n");
677 }
678
679 class xhtml_output_format : public diagnostic_output_format
680 {
681 public:
682 ~xhtml_output_format ()
683 {
684 /* Any diagnostics should have been handled by now.
685 If not, then something's gone wrong with diagnostic
686 groupings. */
687 std::unique_ptr<xml::element> pending_diag
688 = m_builder.take_current_diagnostic ();
689 gcc_assert (!pending_diag);
690 }
691
692 void dump (FILE *out, int indent) const override
693 {
694 fprintf (out, "%*sxhtml_output_format\n", indent, "");
695 diagnostic_output_format::dump (out, indent);
696 }
697
698 std::unique_ptr<diagnostic_per_format_buffer>
699 make_per_format_buffer () final override
700 {
701 return ::make_unique<diagnostic_xhtml_format_buffer> (m_builder);
702 }
703 void set_buffer (diagnostic_per_format_buffer *base_buffer) final override
704 {
705 diagnostic_xhtml_format_buffer *buffer
706 = static_cast<diagnostic_xhtml_format_buffer *> (base_buffer);
707 m_buffer = buffer;
708 }
709
710 void on_begin_group () final override
711 {
712 /* No-op, */
713 }
714 void on_end_group () final override
715 {
716 m_builder.end_group ();
717 }
718 void
719 on_report_diagnostic (const diagnostic_info &diagnostic,
720 diagnostic_t orig_diag_kind) final override
721 {
722 m_builder.on_report_diagnostic (diagnostic, orig_diag_kind, m_buffer);
723 }
724 void on_diagram (const diagnostic_diagram &diagram) final override
725 {
726 m_builder.emit_diagram (diagram);
727 }
728 void after_diagnostic (const diagnostic_info &)
729 {
730 /* No-op, but perhaps could show paths here. */
731 }
732 bool follows_reference_printer_p () const final override
733 {
734 return false;
735 }
736 void update_printer () final override
737 {
738 m_printer = m_context.clone_printer ();
739
740 /* Don't colorize the text. */
741 pp_show_color (m_printer.get ()) = false;
742
743 /* No textual URLs. */
744 m_printer->set_url_format (URL_FORMAT_NONE);
745
746 /* Update the builder to use the new printer. */
747 m_builder.set_printer (*get_printer ());
748 }
749
750 const xml::document &get_document () const
751 {
752 return m_builder.get_document ();
753 }
754
755 protected:
756 xhtml_output_format (diagnostic_context &context,
757 const line_maps *line_maps)
758 : diagnostic_output_format (context),
759 m_builder (context, *get_printer (), line_maps),
760 m_buffer (nullptr)
761 {}
762
763 xhtml_builder m_builder;
764 diagnostic_xhtml_format_buffer *m_buffer;
765 };
766
767 class xhtml_stream_output_format : public xhtml_output_format
768 {
769 public:
770 xhtml_stream_output_format (diagnostic_context &context,
771 const line_maps *line_maps,
772 FILE *stream)
773 : xhtml_output_format (context, line_maps),
774 m_stream (stream)
775 {
776 }
777 ~xhtml_stream_output_format ()
778 {
779 m_builder.flush_to_file (m_stream);
780 }
781 bool machine_readable_stderr_p () const final override
782 {
783 return m_stream == stderr;
784 }
785 private:
786 FILE *m_stream;
787 };
788
789 class xhtml_file_output_format : public xhtml_output_format
790 {
791 public:
792 xhtml_file_output_format (diagnostic_context &context,
793 const line_maps *line_maps,
794 const char *base_file_name)
795 : xhtml_output_format (context, line_maps),
796 m_base_file_name (xstrdup (base_file_name))
797 {
798 }
799 ~xhtml_file_output_format ()
800 {
801 char *filename = concat (m_base_file_name, ".xhtml", nullptr);
802 free (m_base_file_name);
803 m_base_file_name = nullptr;
804 FILE *outf = fopen (filename, "w");
805 if (!outf)
806 {
807 const char *errstr = xstrerror (errno);
808 fnotice (stderr, "error: unable to open '%s' for writing: %s\n",
809 filename, errstr);
810 free (filename);
811 return;
812 }
813 m_builder.flush_to_file (outf);
814 fclose (outf);
815 free (filename);
816 }
817 bool machine_readable_stderr_p () const final override
818 {
819 return false;
820 }
821
822 private:
823 char *m_base_file_name;
824 };
825
826 /* Populate CONTEXT in preparation for XHTML output (either to stderr, or
827 to a file). */
828
829 static void
830 diagnostic_output_format_init_xhtml (diagnostic_context &context,
831 std::unique_ptr<xhtml_output_format> fmt)
832 {
833 /* Don't colorize the text. */
834 pp_show_color (fmt->get_printer ()) = false;
835 context.set_show_highlight_colors (false);
836
837 context.set_output_format (std::move (fmt));
838 }
839
840 /* Populate CONTEXT in preparation for XHTML output to stderr. */
841
842 void
843 diagnostic_output_format_init_xhtml_stderr (diagnostic_context &context,
844 const line_maps *line_maps)
845 {
846 gcc_assert (line_maps);
847 auto format = ::make_unique<xhtml_stream_output_format> (context,
848 line_maps,
849 stderr);
850 diagnostic_output_format_init_xhtml (context, std::move (format));
851 }
852
853 /* Populate CONTEXT in preparation for XHTML output to a file named
854 BASE_FILE_NAME.xhtml. */
855
856 void
857 diagnostic_output_format_init_xhtml_file (diagnostic_context &context,
858 const line_maps *line_maps,
859 const char *base_file_name)
860 {
861 gcc_assert (line_maps);
862 auto format = ::make_unique<xhtml_file_output_format> (context,
863 line_maps,
864 base_file_name);
865 diagnostic_output_format_init_xhtml (context, std::move (format));
866 }
867
868 #if CHECKING_P
869
870 namespace selftest {
871
872 /* A subclass of xhtml_output_format for writing selftests.
873 The XML output is cached internally, rather than written
874 out to a file. */
875
876 class test_xhtml_diagnostic_context : public test_diagnostic_context
877 {
878 public:
879 test_xhtml_diagnostic_context ()
880 {
881 auto format = ::make_unique<xhtml_buffered_output_format> (*this,
882 line_table);
883 m_format = format.get (); // borrowed
884 diagnostic_output_format_init_xhtml (*this, std::move (format));
885 }
886
887 const xml::document &get_document () const
888 {
889 return m_format->get_document ();
890 }
891
892 private:
893 class xhtml_buffered_output_format : public xhtml_output_format
894 {
895 public:
896 xhtml_buffered_output_format (diagnostic_context &context,
897 const line_maps *line_maps)
898 : xhtml_output_format (context, line_maps)
899 {
900 }
901 bool machine_readable_stderr_p () const final override
902 {
903 return true;
904 }
905 };
906
907 xhtml_output_format *m_format; // borrowed
908 };
909
910 /* Test of reporting a diagnostic at UNKNOWN_LOCATION to a
911 diagnostic_context and examining the generated XML document.
912 Verify various basic properties. */
913
914 static void
915 test_simple_log ()
916 {
917 test_xhtml_diagnostic_context dc;
918
919 rich_location richloc (line_table, UNKNOWN_LOCATION);
920 dc.report (DK_ERROR, richloc, nullptr, 0, "this is a test: %i", 42);
921
922 const xml::document &doc = dc.get_document ();
923
924 pretty_printer pp;
925 doc.write_as_xml (&pp, 0, true);
926 ASSERT_STREQ
927 (pp_formatted_text (&pp),
928 ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
929 "<!DOCTYPE html\n"
930 " PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
931 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
932 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
933 " <head>\n"
934 " <title>Title goes here</title>\n"
935 " </head>\n"
936 " <body>\n"
937 " <div class=\"gcc-diagnostic-list\">\n"
938 " <div class=\"gcc-diagnostic\">\n"
939 " <span class=\"gcc-message\">this is a test: 42</span>\n"
940 " <pre class=\"gcc-annotated-source\"></pre>\n"
941 " </div>\n"
942 " </div>\n"
943 " </body>\n"
944 "</html>"));
945 }
946
947 /* Run all of the selftests within this file. */
948
949 static void
950 xhtml_format_selftests ()
951 {
952 test_simple_log ();
953 }
954
955 } // namespace selftest
956
957 #endif /* CHECKING_P */
958
959 /* Plugin hooks. */
960
961 int plugin_is_GPL_compatible;
962
963 /* Entrypoint for the plugin. */
964
965 int
966 plugin_init (struct plugin_name_args *plugin_info,
967 struct plugin_gcc_version *version)
968 {
969 const char *plugin_name = plugin_info->base_name;
970 int argc = plugin_info->argc;
971 struct plugin_argument *argv = plugin_info->argv;
972
973 if (!plugin_default_version_check (version, &gcc_version))
974 return 1;
975
976 global_dc->set_output_format
977 (::make_unique<xhtml_stream_output_format> (*global_dc,
978 line_table,
979 stderr));
980
981 #if CHECKING_P
982 selftest::xhtml_format_selftests ();
983 #endif
984
985 return 0;
986 }