19#include "llvm/ADT/SmallString.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/raw_ostream.h"
34 const char *StartTag,
const char *EndTag,
37 B =
SM.getExpansionLoc(B);
38 E =
SM.getExpansionLoc(
E);
40 assert(
SM.getFileID(
E) == FID &&
"B/E not in the same file!");
42 unsigned BOffset =
SM.getFileOffset(B);
43 unsigned EOffset =
SM.getFileOffset(
E);
50 const char *BufferStart =
SM.getBufferData(FID, &
Invalid).data();
55 BufferStart, StartTag, EndTag);
61 const char *BufferStart,
62 const char *StartTag,
const char *EndTag) {
69 bool HadOpenTag =
true;
71 unsigned LastNonWhiteSpace = B;
72 for (
unsigned i = B; i !=
E; ++i) {
73 switch (BufferStart[i]) {
103 LastNonWhiteSpace = i;
132 return std::make_shared<RelexRewriteCache>();
136 bool EscapeSpaces,
bool ReplaceTabs) {
139 const char*
C = Buf.getBufferStart();
140 const char* FileEnd = Buf.getBufferEnd();
142 assert (
C <= FileEnd);
147 for (
unsigned FilePos = 0;
C != FileEnd ; ++
C, ++FilePos) {
149 default: ++ColNo;
break;
168 unsigned NumSpaces = 8-(ColNo&7);
171 StringRef(
" "
172 " ", 6*NumSpaces));
174 RB.
ReplaceText(FilePos, 1, StringRef(
" ", NumSpaces));
198 unsigned len =
s.size();
200 llvm::raw_string_ostream os(Str);
202 for (
unsigned i = 0 ; i < len; ++i) {
210 if (EscapeSpaces) os <<
" ";
217 for (
unsigned i = 0; i < 4; ++i)
220 for (
unsigned i = 0; i < 4; ++i)
228 case '<': os <<
"<";
break;
229 case '>': os <<
">";
break;
230 case '&': os <<
"&";
break;
238 unsigned B,
unsigned E) {
240 llvm::raw_svector_ostream OS(Str);
242 OS <<
"<tr class=\"codeline\" data-linenumber=\"" << LineNo <<
"\">"
243 <<
"<td class=\"num\" id=\"LN" << LineNo <<
"\">" << LineNo
244 <<
"</td><td class=\"line\">";
258 const char* FileBeg = Buf.getBufferStart();
259 const char* FileEnd = Buf.getBufferEnd();
260 const char*
C = FileBeg;
263 assert (
C <= FileEnd);
266 unsigned FilePos = 0;
268 while (
C != FileEnd) {
271 unsigned LineStartPos = FilePos;
272 unsigned LineEndPos = FileEnd - FileBeg;
274 assert (FilePos <= LineEndPos);
275 assert (
C < FileEnd);
279 while (
C != FileEnd) {
284 LineEndPos = FilePos++;
296 llvm::raw_string_ostream os(
s);
297 os <<
"<table class=\"code\" data-fileid=\"" << FID.
getHashValue() <<
"\">\n";
306 const char* FileStart = Buf.getBufferStart();
307 const char* FileEnd = Buf.getBufferEnd();
313 llvm::raw_string_ostream os(
s);
314 os <<
"<!doctype html>\n"
321<style type="text/css">
322body { color:#000000; background-color:#ffffff }
323body { font-family:Helvetica, sans-serif; font-size:10pt }
325.FileName { margin-top: 5px; margin-bottom: 5px; display: inline; }
326.FileNav { margin-left: 5px; margin-right: 5px; display: inline; }
327.FileNav a { text-decoration:none; font-size: larger; }
328.divider { margin-top: 30px; margin-bottom: 30px; height: 15px; }
329.divider { background-color: gray; }
330.code { border-collapse:collapse; width:100%; }
331.code { font-family: "Monospace", monospace; font-size:10pt }
332.code { line-height: 1.2em }
333.comment { color: green; font-style: oblique }
334.keyword { color: blue }
335.string_literal { color: red }
336.directive { color: darkmagenta }
338/* Macros and variables could have pop-up notes hidden by default.
339 - Macro pop-up: expansion of the macro
340 - Variable pop-up: value (table) of the variable */
341.macro_popup, .variable_popup { display: none; }
343/* Pop-up appears on mouse-hover event. */
344.macro:hover .macro_popup, .variable:hover .variable_popup {
347 -webkit-border-radius:5px;
348 -webkit-box-shadow:1px 1px 7px #000;
350 box-shadow:1px 1px 7px #000;
358 border: 2px solid red;
359 background-color:#FFF0F0;
364 border: 2px solid blue;
365 background-color:#F0F0FF;
367 font-family: Helvetica, sans-serif;
371/* Pop-up notes needs a relative position as a base where they pops up. */
373 background-color: PaleGoldenRod;
376.macro { color: DarkMagenta; }
384 border: 1px solid #b0b0b0;
386 box-shadow: 1px 1px 7px black;
387 background-color: #c0c0c0;
391.num { width:2.5em; padding-right:2ex; background-color:#eeeeee }
392.num { text-align:right; font-size:8pt }
393.num { color:#444444 }
394.line { padding-left: 1ex; border-left: 3px solid #ccc }
395.line { white-space: pre }
396.msg { -webkit-box-shadow:1px 1px 7px #000 }
397.msg { box-shadow:1px 1px 7px #000 }
398.msg { -webkit-border-radius:5px }
399.msg { border-radius:5px }
400.msg { font-family:Helvetica, sans-serif; font-size:8pt }
402.msg { position:relative }
403.msg { padding:0.25em 1ex 0.25em 1ex }
404.msg { margin-top:10px; margin-bottom:10px }
405.msg { font-weight:bold }
406.msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }
407.msgT { padding:0x; spacing:0x }
408.msgEvent { background-color:#fff8b4; color:#000000 }
409.msgControl { background-color:#bbbbbb; color:#000000 }
410.msgNote { background-color:#ddeeff; color:#000000 }
411.mrange { background-color:#dfddf3 }
412.mrange { border-bottom:1px solid #6F9DBE }
413.PathIndex { font-weight: bold; padding:0px 5px; margin-right:5px; }
414.PathIndex { -webkit-border-radius:8px }
415.PathIndex { border-radius:8px }
416.PathIndexEvent { background-color:#bfba87 }
417.PathIndexControl { background-color:#8c8c8c }
418.PathIndexPopUp { background-color: #879abc; }
419.PathNav a { text-decoration:none; font-size: larger }
420.CodeInsertionHint { font-weight: bold; background-color: #10dd10 }
421.CodeRemovalHint { background-color:#de1010 }
422.CodeRemovalHint { border-bottom:1px solid #6F9DBE }
423.msg.selected{ background-color:orange !important; }
429 border-collapse: collapse; border-spacing: 0px;
440input.spoilerhider + label {
442 text-decoration: underline;
448input.spoilerhider ~ .spoiler {
454input.spoilerhider:checked + label + .spoiler{
475 llvm::function_ref<
void(
RewriteBuffer &,
unsigned,
unsigned,
const char *,
476 const char *,
const char *)>
477 HighlightRangeCallback) {
481 llvm::MemoryBufferRef FromFile =
SM.getBufferOrFake(FID);
482 const char *BufferStart = FromFile.getBuffer().data();
495 while (Tok.
isNot(tok::eof)) {
502 case tok::identifier:
503 llvm_unreachable(
"tok::identifier in raw lexing mode!");
504 case tok::raw_identifier: {
510 if (Tok.
isNot(tok::identifier))
511 HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart,
512 "<span class='keyword'>",
"</span>");
516 HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart,
517 "<span class='comment'>",
"</span>");
519 case tok::utf8_string_literal:
525 case tok::wide_string_literal:
526 case tok::utf16_string_literal:
527 case tok::utf32_string_literal:
532 case tok::string_literal:
534 HighlightRangeCallback(RB, TokOffs, TokOffs + TokLen, BufferStart,
535 "<span class='string_literal'>",
"</span>");
544 unsigned TokEnd = TokOffs+TokLen;
552 HighlightRangeCallback(RB, TokOffs, TokEnd, BufferStart,
553 "<span class='directive'>",
"</span>");
567 llvm::MemoryBufferRef FromFile =
SM.getBufferOrFake(FID);
568 const char *BufferStart = FromFile.getBuffer().data();
571 auto CacheIt =
Cache->SyntaxHighlights.find(FID);
572 if (CacheIt !=
Cache->SyntaxHighlights.end()) {
582 auto HighlightRangeCallback = [&](
RewriteBuffer &RB,
unsigned B,
unsigned E,
583 const char *BufferStart,
584 const char *StartTag,
const char *EndTag) {
588 Cache->SyntaxHighlights[FID].push_back({B,
E, StartTag, EndTag});
597 const char *,
const char *,
bool)>
598 HighlightRangeCallback) {
602 std::vector<Token> TokenStream;
604 llvm::MemoryBufferRef FromFile =
SM.getBufferOrFake(FID);
611 L.LexFromRawLexer(Tok);
621 if (Tok.
is(tok::hashhash))
627 if (Tok.
is(tok::raw_identifier))
630 TokenStream.push_back(Tok);
632 if (Tok.
is(tok::eof))
break;
658 TmpPP.EnterTokenStream(TokenStream,
false,
false);
665 while (Tok.
isNot(tok::eof)) {
683 assert(
SM.getFileID(LLoc.
getEnd()) == FID &&
684 "Start and end of expansion must be in the same ultimate file!");
687 unsigned LineLen = Expansion.size();
697 while (!Tok.
is(tok::eof) &&
705 LineLen -= Expansion.size();
710 ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
715 LineLen += Expansion.size();
717 PrevPrevTok = PrevTok;
724 Expansion =
"<span class='macro_popup'>" + Expansion +
"</span></span>";
727 "<span class='macro'>", Expansion.c_str(),
743 auto CacheIt =
Cache->MacroHighlights.find(FID);
744 if (CacheIt !=
Cache->MacroHighlights.end()) {
756 const char *EndTag,
bool isTokenRange) {
760 Cache->MacroHighlights[FID].push_back(
761 {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
RewriteBuffer - As code is rewritten, SourceBuffer's from the original input with modifications get a...
void InsertTextAfter(unsigned OrigOffset, StringRef Str)
InsertTextAfter - Insert some text at the specified point, where the offset in the buffer is specifie...
void InsertTextBefore(unsigned OrigOffset, StringRef Str)
InsertTextBefore - Insert some text before the specified point, where the offset in the buffer is spe...
void ReplaceText(unsigned OrigOffset, unsigned OrigLength, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string.
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.
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