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"
35 const char *StartTag,
const char *EndTag,
38 B =
SM.getExpansionLoc(B);
39 E =
SM.getExpansionLoc(
E);
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);
51 const char *BufferStart =
SM.getBufferData(FID, &
Invalid).data();
56 BufferStart, StartTag, EndTag);
62 const char *BufferStart,
63 const char *StartTag,
const char *EndTag) {
65 RB.InsertTextAfter(B, StartTag);
66 RB.InsertTextBefore(
E, EndTag);
70 bool HadOpenTag =
true;
72 unsigned LastNonWhiteSpace = B;
73 for (
unsigned i = B; i !=
E; ++i) {
74 switch (BufferStart[i]) {
80 RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
99 RB.InsertTextAfter(i, StartTag);
104 LastNonWhiteSpace = i;
133 return std::make_shared<RelexRewriteCache>();
137 bool EscapeSpaces,
bool ReplaceTabs) {
140 const char*
C = Buf.getBufferStart();
141 const char* FileEnd = Buf.getBufferEnd();
143 assert (
C <= FileEnd);
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,
"&");
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;
239 unsigned B,
unsigned E) {
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\">";
249 RB.InsertTextBefore(B, OS.str());
251 RB.InsertTextBefore(B, OS.str());
252 RB.InsertTextBefore(
E,
"</td></tr>");
259 const char* FileBeg = Buf.getBufferStart();
260 const char* FileEnd = Buf.getBufferEnd();
261 const char*
C = FileBeg;
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);
280 while (
C != FileEnd) {
285 LineEndPos = FilePos++;
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>");
307 const char* FileStart = Buf.getBufferStart();
308 const char* FileEnd = Buf.getBufferEnd();
314 llvm::raw_string_ostream os(
s);
315 os <<
"<!doctype html>\n"
322<style type="text/css">
323body { color:#000000; background-color:#ffffff }
324body { font-family:Helvetica, sans-serif; font-size:10pt }
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;
441input.spoilerhider + label {
443 text-decoration: underline;
449input.spoilerhider ~ .spoiler {
455input.spoilerhider:checked + label + .spoiler{
476 llvm::function_ref<
void(RewriteBuffer &,
unsigned,
unsigned,
const char *,
477 const char *,
const char *)>
478 HighlightRangeCallback) {
482 llvm::MemoryBufferRef FromFile =
SM.getBufferOrFake(FID);
483 const char *BufferStart = FromFile.getBuffer().data();
496 while (Tok.
isNot(tok::eof)) {
503 case tok::identifier:
504 llvm_unreachable(
"tok::identifier in raw lexing mode!");
505 case tok::raw_identifier: {
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:
526 case tok::wide_string_literal:
527 case tok::utf16_string_literal:
528 case tok::utf32_string_literal:
533 case tok::string_literal:
535 HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart,
536 "<span class='string_literal'>",
"</span>");
545 unsigned TokEnd = TokOffs+TokLen;
553 HighlightRangeCallback(RB, TokOffs, TokEnd, BufferStart,
554 "<span class='directive'>",
"</span>");
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()) {
583 auto HighlightRangeCallback = [&](RewriteBuffer &RB,
unsigned B,
unsigned E,
584 const char *BufferStart,
585 const char *StartTag,
const char *EndTag) {
589 Cache->SyntaxHighlights[FID].push_back({B,
E, StartTag, EndTag});
598 const char *,
const char *,
bool)>
599 HighlightRangeCallback) {
603 std::vector<Token> TokenStream;
605 llvm::MemoryBufferRef FromFile =
SM.getBufferOrFake(FID);
612 L.LexFromRawLexer(Tok);
622 if (Tok.
is(tok::hashhash))
628 if (Tok.
is(tok::raw_identifier))
631 TokenStream.push_back(Tok);
633 if (Tok.
is(tok::eof))
break;
659 TmpPP.EnterTokenStream(TokenStream,
false,
false);
666 while (Tok.
isNot(tok::eof)) {
684 assert(
SM.getFileID(LLoc.
getEnd()) == FID &&
685 "Start and end of expansion must be in the same ultimate file!");
688 unsigned LineLen = Expansion.size();
698 while (!Tok.
is(tok::eof) &&
706 LineLen -= Expansion.size();
711 ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
716 LineLen += Expansion.size();
718 PrevPrevTok = PrevTok;
725 Expansion =
"<span class='macro_popup'>" + Expansion +
"</span></span>";
728 "<span class='macro'>", Expansion.c_str(),
744 auto CacheIt =
Cache->MacroHighlights.find(FID);
745 if (CacheIt !=
Cache->MacroHighlights.end()) {
757 const char *EndTag,
bool isTokenRange) {
761 Cache->MacroHighlights[FID].push_back(
762 {B,
E, StartTag, EndTag, isTokenRange});
static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo, unsigned B, unsigned E)
static void HighlightMacrosImpl(Rewriter &R, FileID FID, const Preprocessor &PP, llvm::function_ref< void(Rewriter &, SourceLocation, SourceLocation, const char *, const char *, bool)> HighlightRangeCallback)
static void SyntaxHighlightImpl(Rewriter &R, FileID FID, const Preprocessor &PP, llvm::function_ref< void(RewriteBuffer &, unsigned, unsigned, const char *, const char *, const char *)> HighlightRangeCallback)
SyntaxHighlight - Relex the specified FileID and annotate the HTML with information about keywords,...
Defines the clang::Preprocessor interface.
Defines the SourceManager interface.
__device__ __2f16 float __ockl_bool s
__device__ __2f16 float c
Represents a character-granular source range.
bool isTokenRange() const
Return true if the end of this range specifies the start of the last token.
SourceLocation getEnd() const
SourceLocation getBegin() const
Concrete class used by the front-end to report problems and issues.
DiagnosticOptions & getDiagnosticOptions() const
Retrieve the diagnostic options.
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
unsigned getHashValue() const
A diagnostic client that ignores all diagnostics.
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens.
bool LexFromRawLexer(Token &Result)
LexFromRawLexer - Lex a token from a designated raw lexer (one with no associated preprocessor object...
void SetCommentRetentionState(bool Mode)
SetCommentRetentionMode - Change the comment retention mode of the lexer to the specified mode.
static unsigned MeasureTokenLength(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
MeasureTokenLength - Relex the token at the specified location and return its length in bytes in the ...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
IdentifierInfo * LookUpIdentifierInfo(Token &Identifier) const
Given a tok::raw_identifier token, look up the identifier information for the token and install it in...
void setDiagnostics(DiagnosticsEngine &D)
void Lex(Token &Result)
Lex the next token for this preprocessor.
SourceManager & getSourceManager() const
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
const LangOptions & getLangOpts() const
void setPragmasEnabled(bool Enabled)
void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments)
Control whether the preprocessor retains comments in output.
bool getPragmasEnabled() const
DiagnosticsEngine & getDiagnostics() const
Rewriter - This is the main interface to the rewrite buffers.
bool InsertTextBefore(SourceLocation Loc, StringRef Str)
InsertText - Insert the specified string at the specified location in the original buffer.
SourceManager & getSourceMgr() const
const LangOptions & getLangOpts() const
bool InsertTextAfter(SourceLocation Loc, StringRef Str)
InsertTextAfter - Insert the specified string at the specified location in the original buffer.
llvm::RewriteBuffer & getEditBuffer(FileID FID)
getEditBuffer - This is like getRewriteBufferFor, but always returns a buffer, and allows you to writ...
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
llvm::MemoryBufferRef getBufferOrFake(FileID FID, SourceLocation Loc=SourceLocation()) const
Return the buffer for the specified FileID.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file.
TokenConcatenation class, which answers the question of "Is it safe to emit two tokens without a whit...
Token - This structure provides full information about a lexed token.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
unsigned getLength() const
void setKind(tok::TokenKind K)
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
tok::TokenKind getKind() const
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
bool isNot(tok::TokenKind K) const
void AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID, StringRef title)
void HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E, const char *StartTag, const char *EndTag, bool IsTokenRange=true)
HighlightRange - Highlight a range in the source code with the specified start/end tags.
RelexRewriteCacheRef instantiateRelexRewriteCache()
If you need to rewrite the same file multiple times, you can instantiate a RelexRewriteCache and refe...
void AddLineNumbers(Rewriter &R, FileID FID)
void SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP, RelexRewriteCacheRef Cache=nullptr)
SyntaxHighlight - Relex the specified FileID and annotate the HTML with information about keywords,...
void HighlightMacros(Rewriter &R, FileID FID, const Preprocessor &PP, RelexRewriteCacheRef Cache=nullptr)
HighlightMacros - This uses the macro table state from the end of the file, to reexpand macros and in...
void EscapeText(Rewriter &R, FileID FID, bool EscapeSpaces=false, bool ReplaceTabs=false)
EscapeText - HTMLize a specified file so that special characters are are translated so that they are ...
std::shared_ptr< RelexRewriteCache > RelexRewriteCacheRef
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.
std::vector< RawHighlight > RawHighlightList
DenseMap< FileID, RawHighlightList > SyntaxHighlights
std::vector< Highlight > HighlightList
DenseMap< FileID, HighlightList > MacroHighlights