1 //== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the HTMLRewriter class, which is used to translate the
10 // text of a source file into prettified HTML.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Rewrite/Core/HTMLRewrite.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "clang/Lex/TokenConcatenation.h"
18 #include "clang/Rewrite/Core/Rewriter.h"
19 #include "llvm/ADT/RewriteBuffer.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/raw_ostream.h"
26 using namespace clang
;
30 /// HighlightRange - Highlight a range in the source code with the specified
31 /// start/end tags. B/E must be in the same file. This ensures that
32 /// start/end tags are placed at the start/end of each line if the range is
34 void html::HighlightRange(Rewriter
&R
, SourceLocation B
, SourceLocation E
,
35 const char *StartTag
, const char *EndTag
,
37 SourceManager
&SM
= R
.getSourceMgr();
38 B
= SM
.getExpansionLoc(B
);
39 E
= SM
.getExpansionLoc(E
);
40 FileID FID
= SM
.getFileID(B
);
41 assert(SM
.getFileID(E
) == FID
&& "B/E not in the same file!");
43 unsigned BOffset
= SM
.getFileOffset(B
);
44 unsigned EOffset
= SM
.getFileOffset(E
);
46 // Include the whole end token in the range.
48 EOffset
+= Lexer::MeasureTokenLength(E
, R
.getSourceMgr(), R
.getLangOpts());
51 const char *BufferStart
= SM
.getBufferData(FID
, &Invalid
).data();
55 HighlightRange(R
.getEditBuffer(FID
), BOffset
, EOffset
,
56 BufferStart
, StartTag
, EndTag
);
59 /// HighlightRange - This is the same as the above method, but takes
60 /// decomposed file locations.
61 void html::HighlightRange(RewriteBuffer
&RB
, unsigned B
, unsigned E
,
62 const char *BufferStart
,
63 const char *StartTag
, const char *EndTag
) {
64 // Insert the tag at the absolute start/end of the range.
65 RB
.InsertTextAfter(B
, StartTag
);
66 RB
.InsertTextBefore(E
, EndTag
);
68 // Scan the range to see if there is a \r or \n. If so, and if the line is
69 // not blank, insert tags on that line as well.
70 bool HadOpenTag
= true;
72 unsigned LastNonWhiteSpace
= B
;
73 for (unsigned i
= B
; i
!= E
; ++i
) {
74 switch (BufferStart
[i
]) {
77 // Okay, we found a newline in the range. If we have an open tag, we need
78 // to insert a close tag at the first non-whitespace before the newline.
80 RB
.InsertTextBefore(LastNonWhiteSpace
+1, EndTag
);
82 // Instead of inserting an open tag immediately after the newline, we
83 // wait until we see a non-whitespace character. This prevents us from
84 // inserting tags around blank lines, and also allows the open tag to
85 // be put *after* whitespace on a non-blank line.
97 // If there is no tag open, do it now.
99 RB
.InsertTextAfter(i
, StartTag
);
103 // Remember this character.
104 LastNonWhiteSpace
= i
;
110 namespace clang::html
{
111 struct RelexRewriteCache
{
112 // These structs mimic input arguments of HighlightRange().
115 std::string StartTag
, EndTag
;
118 struct RawHighlight
{
120 std::string StartTag
, EndTag
;
123 // SmallVector isn't appropriate because these vectors are almost never small.
124 using HighlightList
= std::vector
<Highlight
>;
125 using RawHighlightList
= std::vector
<RawHighlight
>;
127 DenseMap
<FileID
, RawHighlightList
> SyntaxHighlights
;
128 DenseMap
<FileID
, HighlightList
> MacroHighlights
;
130 } // namespace clang::html
132 html::RelexRewriteCacheRef
html::instantiateRelexRewriteCache() {
133 return std::make_shared
<RelexRewriteCache
>();
136 void html::EscapeText(Rewriter
&R
, FileID FID
,
137 bool EscapeSpaces
, bool ReplaceTabs
) {
139 llvm::MemoryBufferRef Buf
= R
.getSourceMgr().getBufferOrFake(FID
);
140 const char* C
= Buf
.getBufferStart();
141 const char* FileEnd
= Buf
.getBufferEnd();
143 assert (C
<= FileEnd
);
145 RewriteBuffer
&RB
= R
.getEditBuffer(FID
);
148 for (unsigned FilePos
= 0; C
!= FileEnd
; ++C
, ++FilePos
) {
150 default: ++ColNo
; break;
158 RB
.ReplaceText(FilePos
, 1, " ");
162 RB
.ReplaceText(FilePos
, 1, "<hr>");
169 unsigned NumSpaces
= 8-(ColNo
&7);
171 RB
.ReplaceText(FilePos
, 1,
172 StringRef(" "
173 " ", 6*NumSpaces
));
175 RB
.ReplaceText(FilePos
, 1, StringRef(" ", NumSpaces
));
180 RB
.ReplaceText(FilePos
, 1, "<");
185 RB
.ReplaceText(FilePos
, 1, ">");
190 RB
.ReplaceText(FilePos
, 1, "&");
197 std::string
html::EscapeText(StringRef s
, bool EscapeSpaces
, bool ReplaceTabs
) {
199 unsigned len
= s
.size();
201 llvm::raw_string_ostream
os(Str
);
203 for (unsigned i
= 0 ; i
< len
; ++i
) {
211 if (EscapeSpaces
) os
<< " ";
218 for (unsigned i
= 0; i
< 4; ++i
)
221 for (unsigned i
= 0; i
< 4; ++i
)
229 case '<': os
<< "<"; break;
230 case '>': os
<< ">"; break;
231 case '&': os
<< "&"; break;
238 static void AddLineNumber(RewriteBuffer
&RB
, unsigned LineNo
,
239 unsigned B
, unsigned E
) {
240 SmallString
<256> Str
;
241 llvm::raw_svector_ostream
OS(Str
);
243 OS
<< "<tr class=\"codeline\" data-linenumber=\"" << LineNo
<< "\">"
244 << "<td class=\"num\" id=\"LN" << LineNo
<< "\">" << LineNo
245 << "</td><td class=\"line\">";
247 if (B
== E
) { // Handle empty lines.
249 RB
.InsertTextBefore(B
, OS
.str());
251 RB
.InsertTextBefore(B
, OS
.str());
252 RB
.InsertTextBefore(E
, "</td></tr>");
256 void html::AddLineNumbers(Rewriter
& R
, FileID FID
) {
258 llvm::MemoryBufferRef Buf
= R
.getSourceMgr().getBufferOrFake(FID
);
259 const char* FileBeg
= Buf
.getBufferStart();
260 const char* FileEnd
= Buf
.getBufferEnd();
261 const char* C
= FileBeg
;
262 RewriteBuffer
&RB
= R
.getEditBuffer(FID
);
264 assert (C
<= FileEnd
);
267 unsigned FilePos
= 0;
269 while (C
!= FileEnd
) {
272 unsigned LineStartPos
= FilePos
;
273 unsigned LineEndPos
= FileEnd
- FileBeg
;
275 assert (FilePos
<= LineEndPos
);
276 assert (C
< FileEnd
);
278 // Scan until the newline (or end-of-file).
280 while (C
!= FileEnd
) {
285 LineEndPos
= FilePos
++;
292 AddLineNumber(RB
, LineNo
, LineStartPos
, LineEndPos
);
295 // Add one big table tag that surrounds all of the code.
297 llvm::raw_string_ostream
os(s
);
298 os
<< "<table class=\"code\" data-fileid=\"" << FID
.getHashValue() << "\">\n";
299 RB
.InsertTextBefore(0, os
.str());
300 RB
.InsertTextAfter(FileEnd
- FileBeg
, "</table>");
303 void html::AddHeaderFooterInternalBuiltinCSS(Rewriter
&R
, FileID FID
,
306 llvm::MemoryBufferRef Buf
= R
.getSourceMgr().getBufferOrFake(FID
);
307 const char* FileStart
= Buf
.getBufferStart();
308 const char* FileEnd
= Buf
.getBufferEnd();
310 SourceLocation StartLoc
= R
.getSourceMgr().getLocForStartOfFile(FID
);
311 SourceLocation EndLoc
= StartLoc
.getLocWithOffset(FileEnd
-FileStart
);
314 llvm::raw_string_ostream
os(s
);
315 os
<< "<!doctype html>\n" // Use HTML 5 doctype
319 os
<< "<title>" << html::EscapeText(title
) << "</title>\n";
322 <style type="text
/css
">
323 body { color:#000000; background-color:#ffffff }
324 body { font-family:Helvetica, sans-serif; font-size:10pt }
325 h1 { font-size:14pt }
326 .FileName { margin-top: 5px; margin-bottom: 5px; display: inline; }
327 .FileNav { margin-left: 5px; margin-right: 5px; display: inline; }
328 .FileNav a { text-decoration:none; font-size: larger; }
329 .divider { margin-top: 30px; margin-bottom: 30px; height: 15px; }
330 .divider { background-color: gray; }
331 .code { border-collapse:collapse; width:100%; }
332 .code { font-family: "Monospace
", monospace; font-size:10pt }
333 .code { line-height: 1.2em }
334 .comment { color: green; font-style: oblique }
335 .keyword { color: blue }
336 .string_literal { color: red }
337 .directive { color: darkmagenta }
339 /* Macros and variables could have pop-up notes hidden by default.
340 - Macro pop-up: expansion of the macro
341 - Variable pop-up: value (table) of the variable */
342 .macro_popup, .variable_popup { display: none; }
344 /* Pop-up appears on mouse-hover event. */
345 .macro:hover .macro_popup, .variable:hover .variable_popup {
348 -webkit-border-radius:5px;
349 -webkit-box-shadow:1px 1px 7px #000;
351 box-shadow:1px 1px 7px #000;
359 border: 2px solid red;
360 background-color:#FFF0F0;
365 border: 2px solid blue;
366 background-color:#F0F0FF;
368 font-family: Helvetica, sans-serif;
372 /* Pop-up notes needs a relative position as a base where they pops up. */
374 background-color: PaleGoldenRod;
377 .macro { color: DarkMagenta; }
385 border: 1px solid #b0b0b0;
387 box-shadow: 1px 1px 7px black;
388 background-color: #c0c0c0;
392 .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }
393 .num { text-align:right; font-size:8pt }
394 .num { color:#444444 }
395 .line { padding-left: 1ex; border-left: 3px solid #ccc }
396 .line { white-space: pre }
397 .msg { -webkit-box-shadow:1px 1px 7px #000 }
398 .msg { box-shadow:1px 1px 7px #000 }
399 .msg { -webkit-border-radius:5px }
400 .msg { border-radius:5px }
401 .msg { font-family:Helvetica, sans-serif; font-size:8pt }
403 .msg { position:relative }
404 .msg { padding:0.25em 1ex 0.25em 1ex }
405 .msg { margin-top:10px; margin-bottom:10px }
406 .msg { font-weight:bold }
407 .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }
408 .msgT { padding:0x; spacing:0x }
409 .msgEvent { background-color:#fff8b4; color:#000000 }
410 .msgControl { background-color:#bbbbbb; color:#000000 }
411 .msgNote { background-color:#ddeeff; color:#000000 }
412 .mrange { background-color:#dfddf3 }
413 .mrange { border-bottom:1px solid #6F9DBE }
414 .PathIndex { font-weight: bold; padding:0px 5px; margin-right:5px; }
415 .PathIndex { -webkit-border-radius:8px }
416 .PathIndex { border-radius:8px }
417 .PathIndexEvent { background-color:#bfba87 }
418 .PathIndexControl { background-color:#8c8c8c }
419 .PathIndexPopUp { background-color: #879abc; }
420 .PathNav a { text-decoration:none; font-size: larger }
421 .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }
422 .CodeRemovalHint { background-color:#de1010 }
423 .CodeRemovalHint { border-bottom:1px solid #6F9DBE }
424 .msg.selected{ background-color:orange !important; }
430 border-collapse: collapse; border-spacing: 0px;
441 input.spoilerhider + label {
443 text-decoration: underline;
449 input.spoilerhider ~ .spoiler {
455 input.spoilerhider:checked + label + .spoiler{
464 R
.InsertTextBefore(StartLoc
, os
.str());
467 R
.InsertTextAfter(EndLoc
, "</body></html>\n");
470 /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
471 /// information about keywords, macro expansions etc. This uses the macro
472 /// table state from the end of the file, so it won't be perfectly perfect,
473 /// but it will be reasonably close.
474 static void SyntaxHighlightImpl(
475 Rewriter
&R
, FileID FID
, const Preprocessor
&PP
,
476 llvm::function_ref
<void(RewriteBuffer
&, unsigned, unsigned, const char *,
477 const char *, const char *)>
478 HighlightRangeCallback
) {
480 RewriteBuffer
&RB
= R
.getEditBuffer(FID
);
481 const SourceManager
&SM
= PP
.getSourceManager();
482 llvm::MemoryBufferRef FromFile
= SM
.getBufferOrFake(FID
);
483 const char *BufferStart
= FromFile
.getBuffer().data();
485 Lexer
L(FID
, FromFile
, SM
, PP
.getLangOpts());
487 // Inform the preprocessor that we want to retain comments as tokens, so we
488 // can highlight them.
489 L
.SetCommentRetentionState(true);
491 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
494 L
.LexFromRawLexer(Tok
);
496 while (Tok
.isNot(tok::eof
)) {
497 // Since we are lexing unexpanded tokens, all tokens are from the main
499 unsigned TokOffs
= SM
.getFileOffset(Tok
.getLocation());
500 unsigned TokLen
= Tok
.getLength();
501 switch (Tok
.getKind()) {
503 case tok::identifier
:
504 llvm_unreachable("tok::identifier in raw lexing mode!");
505 case tok::raw_identifier
: {
506 // Fill in Result.IdentifierInfo and update the token kind,
507 // looking up the identifier in the identifier table.
508 PP
.LookUpIdentifierInfo(Tok
);
510 // If this is a pp-identifier, for a keyword, highlight it as such.
511 if (Tok
.isNot(tok::identifier
))
512 HighlightRangeCallback(RB
, TokOffs
, TokOffs
+ TokLen
, BufferStart
,
513 "<span class='keyword'>", "</span>");
517 HighlightRangeCallback(RB
, TokOffs
, TokOffs
+ TokLen
, BufferStart
,
518 "<span class='comment'>", "</span>");
520 case tok::utf8_string_literal
:
521 // Chop off the u part of u8 prefix
524 // FALL THROUGH to chop the 8
526 case tok::wide_string_literal
:
527 case tok::utf16_string_literal
:
528 case tok::utf32_string_literal
:
529 // Chop off the L, u, U or 8 prefix
533 case tok::string_literal
:
534 // FIXME: Exclude the optional ud-suffix from the highlighted range.
535 HighlightRangeCallback(RB
, TokOffs
, TokOffs
+ TokLen
, BufferStart
,
536 "<span class='string_literal'>", "</span>");
539 // If this is a preprocessor directive, all tokens to end of line are too.
540 if (!Tok
.isAtStartOfLine())
543 // Eat all of the tokens until we get to the next one at the start of
545 unsigned TokEnd
= TokOffs
+TokLen
;
546 L
.LexFromRawLexer(Tok
);
547 while (!Tok
.isAtStartOfLine() && Tok
.isNot(tok::eof
)) {
548 TokEnd
= SM
.getFileOffset(Tok
.getLocation())+Tok
.getLength();
549 L
.LexFromRawLexer(Tok
);
552 // Find end of line. This is a hack.
553 HighlightRangeCallback(RB
, TokOffs
, TokEnd
, BufferStart
,
554 "<span class='directive'>", "</span>");
556 // Don't skip the next token.
561 L
.LexFromRawLexer(Tok
);
564 void html::SyntaxHighlight(Rewriter
&R
, FileID FID
, const Preprocessor
&PP
,
565 RelexRewriteCacheRef Cache
) {
566 RewriteBuffer
&RB
= R
.getEditBuffer(FID
);
567 const SourceManager
&SM
= PP
.getSourceManager();
568 llvm::MemoryBufferRef FromFile
= SM
.getBufferOrFake(FID
);
569 const char *BufferStart
= FromFile
.getBuffer().data();
572 auto CacheIt
= Cache
->SyntaxHighlights
.find(FID
);
573 if (CacheIt
!= Cache
->SyntaxHighlights
.end()) {
574 for (const RelexRewriteCache::RawHighlight
&H
: CacheIt
->second
) {
575 HighlightRange(RB
, H
.B
, H
.E
, BufferStart
, H
.StartTag
.data(),
582 // "Every time you would call HighlightRange, cache the inputs as well."
583 auto HighlightRangeCallback
= [&](RewriteBuffer
&RB
, unsigned B
, unsigned E
,
584 const char *BufferStart
,
585 const char *StartTag
, const char *EndTag
) {
586 HighlightRange(RB
, B
, E
, BufferStart
, StartTag
, EndTag
);
589 Cache
->SyntaxHighlights
[FID
].push_back({B
, E
, StartTag
, EndTag
});
592 SyntaxHighlightImpl(R
, FID
, PP
, HighlightRangeCallback
);
595 static void HighlightMacrosImpl(
596 Rewriter
&R
, FileID FID
, const Preprocessor
&PP
,
597 llvm::function_ref
<void(Rewriter
&, SourceLocation
, SourceLocation
,
598 const char *, const char *, bool)>
599 HighlightRangeCallback
) {
601 // Re-lex the raw token stream into a token buffer.
602 const SourceManager
&SM
= PP
.getSourceManager();
603 std::vector
<Token
> TokenStream
;
605 llvm::MemoryBufferRef FromFile
= SM
.getBufferOrFake(FID
);
606 Lexer
L(FID
, FromFile
, SM
, PP
.getLangOpts());
608 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
612 L
.LexFromRawLexer(Tok
);
614 // If this is a # at the start of a line, discard it from the token stream.
615 // We don't want the re-preprocess step to see #defines, #includes or other
616 // preprocessor directives.
617 if (Tok
.is(tok::hash
) && Tok
.isAtStartOfLine())
620 // If this is a ## token, change its kind to unknown so that repreprocessing
621 // it will not produce an error.
622 if (Tok
.is(tok::hashhash
))
623 Tok
.setKind(tok::unknown
);
625 // If this raw token is an identifier, the raw lexer won't have looked up
626 // the corresponding identifier info for it. Do this now so that it will be
627 // macro expanded when we re-preprocess it.
628 if (Tok
.is(tok::raw_identifier
))
629 PP
.LookUpIdentifierInfo(Tok
);
631 TokenStream
.push_back(Tok
);
633 if (Tok
.is(tok::eof
)) break;
636 // Temporarily change the diagnostics object so that we ignore any generated
637 // diagnostics from this pass.
638 DiagnosticsEngine
TmpDiags(PP
.getDiagnostics().getDiagnosticIDs(),
639 &PP
.getDiagnostics().getDiagnosticOptions(),
640 new IgnoringDiagConsumer
);
642 // FIXME: This is a huge hack; we reuse the input preprocessor because we want
643 // its state, but we aren't actually changing it (we hope). This should really
644 // construct a copy of the preprocessor.
645 Preprocessor
&TmpPP
= const_cast<Preprocessor
&>(PP
);
646 DiagnosticsEngine
*OldDiags
= &TmpPP
.getDiagnostics();
647 TmpPP
.setDiagnostics(TmpDiags
);
649 // Inform the preprocessor that we don't want comments.
650 TmpPP
.SetCommentRetentionState(false, false);
652 // We don't want pragmas either. Although we filtered out #pragma, removing
653 // _Pragma and __pragma is much harder.
654 bool PragmasPreviouslyEnabled
= TmpPP
.getPragmasEnabled();
655 TmpPP
.setPragmasEnabled(false);
657 // Enter the tokens we just lexed. This will cause them to be macro expanded
658 // but won't enter sub-files (because we removed #'s).
659 TmpPP
.EnterTokenStream(TokenStream
, false, /*IsReinject=*/false);
661 TokenConcatenation
ConcatInfo(TmpPP
);
663 // Lex all the tokens.
666 while (Tok
.isNot(tok::eof
)) {
667 // Ignore non-macro tokens.
668 if (!Tok
.getLocation().isMacroID()) {
673 // Okay, we have the first token of a macro expansion: highlight the
674 // expansion by inserting a start tag before the macro expansion and
676 CharSourceRange LLoc
= SM
.getExpansionRange(Tok
.getLocation());
678 // Ignore tokens whose instantiation location was not the main file.
679 if (SM
.getFileID(LLoc
.getBegin()) != FID
) {
684 assert(SM
.getFileID(LLoc
.getEnd()) == FID
&&
685 "Start and end of expansion must be in the same ultimate file!");
687 std::string Expansion
= EscapeText(TmpPP
.getSpelling(Tok
));
688 unsigned LineLen
= Expansion
.size();
692 // Okay, eat this token, getting the next one.
695 // Skip all the rest of the tokens that are part of this macro
696 // instantiation. It would be really nice to pop up a window with all the
697 // spelling of the tokens or something.
698 while (!Tok
.is(tok::eof
) &&
699 SM
.getExpansionLoc(Tok
.getLocation()) == LLoc
.getBegin()) {
700 // Insert a newline if the macro expansion is getting large.
706 LineLen
-= Expansion
.size();
708 // If the tokens were already space separated, or if they must be to avoid
709 // them being implicitly pasted, add a space between them.
710 if (Tok
.hasLeadingSpace() ||
711 ConcatInfo
.AvoidConcat(PrevPrevTok
, PrevTok
, Tok
))
714 // Escape any special characters in the token text.
715 Expansion
+= EscapeText(TmpPP
.getSpelling(Tok
));
716 LineLen
+= Expansion
.size();
718 PrevPrevTok
= PrevTok
;
723 // Insert the 'macro_popup' as the end tag, so that multi-line macros all
725 Expansion
= "<span class='macro_popup'>" + Expansion
+ "</span></span>";
727 HighlightRangeCallback(R
, LLoc
.getBegin(), LLoc
.getEnd(),
728 "<span class='macro'>", Expansion
.c_str(),
729 LLoc
.isTokenRange());
732 // Restore the preprocessor's old state.
733 TmpPP
.setDiagnostics(*OldDiags
);
734 TmpPP
.setPragmasEnabled(PragmasPreviouslyEnabled
);
737 /// HighlightMacros - This uses the macro table state from the end of the
738 /// file, to re-expand macros and insert (into the HTML) information about the
739 /// macro expansions. This won't be perfectly perfect, but it will be
740 /// reasonably close.
741 void html::HighlightMacros(Rewriter
&R
, FileID FID
, const Preprocessor
&PP
,
742 RelexRewriteCacheRef Cache
) {
744 auto CacheIt
= Cache
->MacroHighlights
.find(FID
);
745 if (CacheIt
!= Cache
->MacroHighlights
.end()) {
746 for (const RelexRewriteCache::Highlight
&H
: CacheIt
->second
) {
747 HighlightRange(R
, H
.B
, H
.E
, H
.StartTag
.data(), H
.EndTag
.data(),
754 // "Every time you would call HighlightRange, cache the inputs as well."
755 auto HighlightRangeCallback
= [&](Rewriter
&R
, SourceLocation B
,
756 SourceLocation E
, const char *StartTag
,
757 const char *EndTag
, bool isTokenRange
) {
758 HighlightRange(R
, B
, E
, StartTag
, EndTag
, isTokenRange
);
761 Cache
->MacroHighlights
[FID
].push_back(
762 {B
, E
, StartTag
, EndTag
, isTokenRange
});
766 HighlightMacrosImpl(R
, FID
, PP
, HighlightRangeCallback
);