2222#include " swift/Basic/LLVM.h"
2323#include " swift/Basic/SourceManager.h"
2424#include " swift/Bridging/ASTGen.h"
25- #include " swift/Markup/Markup.h"
2625#include " llvm/ADT/SmallString.h"
2726#include " llvm/ADT/StringRef.h"
2827#include " llvm/ADT/Twine.h"
2928#include " llvm/Support/MemoryBuffer.h"
3029#include " llvm/Support/raw_ostream.h"
3130
3231using namespace swift ;
33- using namespace swift ::markup;
34-
35- namespace {
36- // MARK: Markdown Printing
37- class TerminalMarkupPrinter : public MarkupASTVisitor <TerminalMarkupPrinter> {
38- llvm::raw_ostream &OS;
39- unsigned Indent;
40- unsigned ShouldBold;
41-
42- void indent (unsigned Amount = 2 ) { Indent += Amount; }
43-
44- void dedent (unsigned Amount = 2 ) {
45- assert (Indent >= Amount && " dedent without matching indent" );
46- Indent -= Amount;
47- }
48-
49- void bold () {
50- ++ShouldBold;
51- updateFormatting ();
52- }
53-
54- void unbold () {
55- assert (ShouldBold > 0 && " unbolded without matching bold" );
56- --ShouldBold;
57- updateFormatting ();
58- }
59-
60- void updateFormatting () {
61- OS.resetColor ();
62- if (ShouldBold > 0 )
63- OS.changeColor (raw_ostream::Colors::SAVEDCOLOR, true );
64- }
65-
66- void print (StringRef Str) {
67- for (auto c : Str) {
68- OS << c;
69- if (c == ' \n ' )
70- for (unsigned i = 0 ; i < Indent; ++i)
71- OS << ' ' ;
72- }
73- }
74-
75- public:
76- TerminalMarkupPrinter (llvm::raw_ostream &OS)
77- : OS(OS), Indent(0 ), ShouldBold(0 ) {}
78-
79- void printNewline () { print (" \n " ); }
80-
81- void visitDocument (const Document *D) {
82- for (const auto *Child : D->getChildren ()) {
83- if (Child->getKind () == markup::ASTNodeKind::Paragraph) {
84- // Add a newline before top-level paragraphs
85- printNewline ();
86- }
87- visit (Child);
88- }
89- }
90-
91- void visitInlineAttributes (const InlineAttributes *A) {
92- print (" ^[" );
93- for (const auto *Child : A->getChildren ())
94- visit (Child);
95- print (" ](" );
96- print (A->getAttributes ());
97- print (" )" );
98- }
99-
100- void visitBlockQuote (const BlockQuote *BQ) {
101- indent ();
102- printNewline ();
103- for (const auto *Child : BQ->getChildren ())
104- visit (Child);
105- dedent ();
106- }
107-
108- void visitList (const List *BL) {
109- indent ();
110- printNewline ();
111- for (const auto *Child : BL->getChildren ())
112- visit (Child);
113- dedent ();
114- }
115-
116- void visitItem (const Item *I) {
117- print (" - " );
118- for (const auto *N : I->getChildren ())
119- visit (N);
120- }
121-
122- void visitCodeBlock (const CodeBlock *CB) {
123- indent ();
124- printNewline ();
125- print (CB->getLiteralContent ());
126- dedent ();
127- }
128-
129- void visitCode (const Code *C) {
130- print (" '" );
131- print (C->getLiteralContent ());
132- print (" '" );
133- }
134-
135- void visitHTML (const HTML *H) { print (H->getLiteralContent ()); }
136-
137- void visitInlineHTML (const InlineHTML *IH) {
138- print (IH->getLiteralContent ());
139- }
140-
141- void visitSoftBreak (const SoftBreak *SB) { printNewline (); }
142-
143- void visitLineBreak (const LineBreak *LB) {
144- printNewline ();
145- printNewline ();
146- }
147-
148- void visitLink (const Link *L) {
149- print (" [" );
150- for (const auto *Child : L->getChildren ())
151- visit (Child);
152- print (" ](" );
153- print (L->getDestination ());
154- print (" )" );
155- }
156-
157- void visitImage (const Image *I) { llvm_unreachable (" unsupported" ); }
158-
159- void visitParagraph (const Paragraph *P) {
160- for (const auto *Child : P->getChildren ())
161- visit (Child);
162- printNewline ();
163- }
164-
165- // TODO: add raw_ostream support for italics ANSI codes in LLVM.
166- void visitEmphasis (const Emphasis *E) {
167- for (const auto *Child : E->getChildren ())
168- visit (Child);
169- }
170-
171- void visitStrong (const Strong *E) {
172- bold ();
173- for (const auto *Child : E->getChildren ())
174- visit (Child);
175- unbold ();
176- }
177-
178- void visitHRule (const HRule *HR) {
179- print (" --------------" );
180- printNewline ();
181- }
182-
183- void visitHeader (const Header *H) {
184- bold ();
185- for (const auto *Child : H->getChildren ())
186- visit (Child);
187- unbold ();
188- printNewline ();
189- }
190-
191- void visitText (const Text *T) { print (T->getLiteralContent ()); }
192-
193- void visitPrivateExtension (const PrivateExtension *PE) {
194- llvm_unreachable (" unsupported" );
195- }
196-
197- void visitParamField (const ParamField *PF) {
198- llvm_unreachable (" unsupported" );
199- }
200-
201- void visitReturnField (const ReturnsField *RF) {
202- llvm_unreachable (" unsupported" );
203- }
204-
205- void visitThrowField (const ThrowsField *TF) {
206- llvm_unreachable (" unsupported" );
207- }
208-
209- #define MARKUP_SIMPLE_FIELD (Id, Keyword, XMLKind ) \
210- void visit##Id(const Id *Field) { llvm_unreachable (" unsupported" ); }
211- #include " swift/Markup/SimpleFields.def"
212- };
213-
214- static void printMarkdown (StringRef Content, raw_ostream &Out,
215- bool UseColor) {
216- markup::MarkupContext ctx;
217- auto document = markup::parseDocument (ctx, Content);
218- if (UseColor) {
219- ColoredStream stream{Out};
220- TerminalMarkupPrinter printer (stream);
221- printer.visit (document);
222- } else {
223- NoColorStream stream{Out};
224- TerminalMarkupPrinter printer (stream);
225- printer.visit (document);
226- }
227- }
228- } // end anonymous namespace
22932
23033// MARK: Main DiagnosticConsumer entrypoint.
23134void PrintingDiagnosticConsumer::handleDiagnostic (SourceManager &SM,
@@ -261,16 +64,6 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
26164
26265 case DiagnosticOptions::FormattingStyle::LLVM:
26366 printDiagnostic (SM, Info);
264-
265- if (PrintEducationalNotes) {
266- for (auto path : Info.EducationalNotePaths ) {
267- if (auto buffer = SM.getFileSystem ()->getBufferForFile (path)) {
268- printMarkdown (buffer->get ()->getBuffer (), Stream, ForceColors);
269- Stream << " \n " ;
270- }
271- }
272- }
273-
27467 for (auto ChildInfo : Info.ChildDiagnosticInfo ) {
27568 printDiagnostic (SM, *ChildInfo);
27669 }
@@ -283,13 +76,6 @@ void PrintingDiagnosticConsumer::flush(bool includeTrailingBreak) {
28376 DiagBridge.flush (Stream, includeTrailingBreak,
28477 /* forceColors=*/ ForceColors);
28578#endif
286-
287- for (auto note : BufferedEducationalNotes) {
288- printMarkdown (note, Stream, ForceColors);
289- Stream << " \n " ;
290- }
291-
292- BufferedEducationalNotes.clear ();
29379}
29480
29581bool PrintingDiagnosticConsumer::finishProcessing () {
0 commit comments