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" 25 using namespace clang;
33 const char *StartTag,
const char *EndTag) {
38 assert(SM.
getFileID(E) == FID &&
"B/E not in the same file!");
47 const char *BufferStart = SM.
getBufferData(FID, &Invalid).data();
52 BufferStart, StartTag, EndTag);
58 const char *BufferStart,
59 const char *StartTag,
const char *EndTag) {
66 bool HadOpenTag =
true;
68 unsigned LastNonWhiteSpace = B;
69 for (
unsigned i = B; i != E; ++i) {
70 switch (BufferStart[i]) {
100 LastNonWhiteSpace = i;
107 bool EscapeSpaces,
bool ReplaceTabs) {
110 const char*
C = Buf->getBufferStart();
111 const char* FileEnd = Buf->getBufferEnd();
113 assert (C <= FileEnd);
118 for (
unsigned FilePos = 0; C != FileEnd ; ++
C, ++FilePos) {
120 default: ++ColNo;
break;
139 unsigned NumSpaces = 8-(ColNo&7);
142 StringRef(
" " 143 " ", 6*NumSpaces));
145 RB.
ReplaceText(FilePos, 1, StringRef(
" ", NumSpaces));
169 unsigned len = s.size();
171 llvm::raw_string_ostream os(Str);
173 for (
unsigned i = 0 ; i < len; ++i) {
181 if (EscapeSpaces) os <<
" ";
188 for (
unsigned i = 0; i < 4; ++i)
191 for (
unsigned i = 0; i < 4; ++i)
199 case '<': os <<
"<";
break;
200 case '>': os <<
">";
break;
201 case '&': os <<
"&";
break;
209 unsigned B,
unsigned E) {
211 llvm::raw_svector_ostream OS(Str);
213 OS <<
"<tr><td class=\"num\" id=\"LN" 215 << LineNo <<
"</td><td class=\"line\">";
229 const char* FileBeg = Buf->getBufferStart();
230 const char* FileEnd = Buf->getBufferEnd();
231 const char*
C = FileBeg;
234 assert (C <= FileEnd);
237 unsigned FilePos = 0;
239 while (C != FileEnd) {
242 unsigned LineStartPos = FilePos;
243 unsigned LineEndPos = FileEnd - FileBeg;
245 assert (FilePos <= LineEndPos);
246 assert (C < FileEnd);
250 while (C != FileEnd) {
255 LineEndPos = FilePos++;
274 const char* FileStart = Buf->getBufferStart();
275 const char* FileEnd = Buf->getBufferEnd();
281 llvm::raw_string_ostream os(s);
282 os <<
"<!doctype html>\n" 288 os <<
"<style type=\"text/css\">\n" 289 " body { color:#000000; background-color:#ffffff }\n" 290 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n" 291 " h1 { font-size:14pt }\n" 292 " .FileName { margin-top: 5px; margin-bottom: 5px; display: inline; }\n" 293 " .FileNav { margin-left: 5px; margin-right: 5px; display: inline; }\n" 294 " .FileNav a { text-decoration:none; font-size: larger; }\n" 295 " .divider { margin-top: 30px; margin-bottom: 30px; height: 15px; }\n" 296 " .divider { background-color: gray; }\n" 297 " .code { border-collapse:collapse; width:100%; }\n" 298 " .code { font-family: \"Monospace\", monospace; font-size:10pt }\n" 299 " .code { line-height: 1.2em }\n" 300 " .comment { color: green; font-style: oblique }\n" 301 " .keyword { color: blue }\n" 302 " .string_literal { color: red }\n" 303 " .directive { color: darkmagenta }\n" 305 " .expansion { display: none; }\n" 306 " .macro:hover .expansion { display: block; border: 2px solid #FF0000; " 307 "padding: 2px; background-color:#FFF0F0; font-weight: normal; " 308 " -webkit-border-radius:5px; -webkit-box-shadow:1px 1px 7px #000; " 309 " border-radius:5px; box-shadow:1px 1px 7px #000; " 310 "position: absolute; top: -1em; left:10em; z-index: 1 } \n" 311 " .macro { color: darkmagenta; background-color:LemonChiffon;" 313 " position: relative }\n" 314 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n" 315 " .num { text-align:right; font-size:8pt }\n" 316 " .num { color:#444444 }\n" 317 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n" 318 " .line { white-space: pre }\n" 319 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n" 320 " .msg { box-shadow:1px 1px 7px #000 }\n" 321 " .msg { -webkit-border-radius:5px }\n" 322 " .msg { border-radius:5px }\n" 323 " .msg { font-family:Helvetica, sans-serif; font-size:8pt }\n" 324 " .msg { float:left }\n" 325 " .msg { padding:0.25em 1ex 0.25em 1ex }\n" 326 " .msg { margin-top:10px; margin-bottom:10px }\n" 327 " .msg { font-weight:bold }\n" 328 " .msg { max-width:60em; word-wrap: break-word; white-space: pre-wrap }\n" 329 " .msgT { padding:0x; spacing:0x }\n" 330 " .msgEvent { background-color:#fff8b4; color:#000000 }\n" 331 " .msgControl { background-color:#bbbbbb; color:#000000 }\n" 332 " .msgNote { background-color:#ddeeff; color:#000000 }\n" 333 " .mrange { background-color:#dfddf3 }\n" 334 " .mrange { border-bottom:1px solid #6F9DBE }\n" 335 " .PathIndex { font-weight: bold; padding:0px 5px; " 336 "margin-right:5px; }\n" 337 " .PathIndex { -webkit-border-radius:8px }\n" 338 " .PathIndex { border-radius:8px }\n" 339 " .PathIndexEvent { background-color:#bfba87 }\n" 340 " .PathIndexControl { background-color:#8c8c8c }\n" 341 " .PathNav a { text-decoration:none; font-size: larger }\n" 342 " .CodeInsertionHint { font-weight: bold; background-color: #10dd10 }\n" 343 " .CodeRemovalHint { background-color:#de1010 }\n" 344 " .CodeRemovalHint { border-bottom:1px solid #6F9DBE }\n" 345 " .selected{ background-color:orange !important; }\n" 346 " table.simpletable {\n" 350 " border-collapse: collapse; border-spacing: 0px;\n" 353 " text-align: right;\n" 354 " vertical-align: top;\n" 355 " font-weight: bold;\n" 357 " padding-right:2ex;\n" 359 "</style>\n</head>\n<body>";
376 const llvm::MemoryBuffer *FromFile = SM.
getBuffer(FID);
378 const char *BufferStart = L.
getBuffer().data();
382 L.SetCommentRetentionState(
true);
387 L.LexFromRawLexer(Tok);
396 case tok::identifier:
397 llvm_unreachable(
"tok::identifier in raw lexing mode!");
398 case tok::raw_identifier: {
404 if (Tok.
isNot(tok::identifier))
406 "<span class='keyword'>",
"</span>");
411 "<span class='comment'>",
"</span>");
413 case tok::utf8_string_literal:
419 case tok::wide_string_literal:
420 case tok::utf16_string_literal:
421 case tok::utf32_string_literal:
426 case tok::string_literal:
429 "<span class='string_literal'>",
"</span>");
438 unsigned TokEnd = TokOffs+TokLen;
439 L.LexFromRawLexer(Tok);
442 L.LexFromRawLexer(Tok);
447 "<span class='directive'>",
"</span>");
454 L.LexFromRawLexer(Tok);
465 std::vector<Token> TokenStream;
467 const llvm::MemoryBuffer *FromFile = SM.
getBuffer(FID);
474 L.LexFromRawLexer(Tok);
484 if (Tok.
is(tok::hashhash))
490 if (Tok.
is(tok::raw_identifier))
493 TokenStream.push_back(Tok);
509 TmpPP.setDiagnostics(TmpDiags);
512 TmpPP.SetCommentRetentionState(
false,
false);
516 bool PragmasPreviouslyEnabled = TmpPP.getPragmasEnabled();
517 TmpPP.setPragmasEnabled(
false);
521 TmpPP.EnterTokenStream(TokenStream,
false);
538 std::pair<SourceLocation, SourceLocation> LLoc =
547 assert(SM.
getFileID(LLoc.second) == FID &&
548 "Start and end of expansion must be in the same ultimate file!");
550 std::string Expansion =
EscapeText(TmpPP.getSpelling(Tok));
551 unsigned LineLen = Expansion.size();
569 LineLen -= Expansion.size();
578 Expansion +=
EscapeText(TmpPP.getSpelling(Tok));
579 LineLen += Expansion.size();
581 PrevPrevTok = PrevTok;
589 Expansion =
"<span class='expansion'>" + Expansion +
"</span></span>";
592 "<span class='macro'>", Expansion.c_str());
596 TmpPP.setDiagnostics(*OldDiags);
597 TmpPP.setPragmasEnabled(PragmasPreviouslyEnabled);
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens...
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
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)) {...
Defines the SourceManager interface.
TokenConcatenation class, which answers the question of "Is it safe to emit two tokens without a whit...
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
RewriteBuffer & getEditBuffer(FileID FID)
getEditBuffer - This is like getRewriteBufferFor, but always returns a buffer, and allows you to writ...
RewriteBuffer - As code is rewritten, SourceBuffer's from the original input with modifications get a...
tok::TokenKind getKind() const
static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo, unsigned B, unsigned E)
void HighlightMacros(Rewriter &R, FileID FID, const Preprocessor &PP)
HighlightMacros - This uses the macro table state from the end of the file, to reexpand macros and in...
void AddLineNumbers(Rewriter &R, FileID FID)
Token - This structure provides full information about a lexed token.
void setKind(tok::TokenKind K)
const LangOptions & getLangOpts() const
SourceManager & getSourceMgr() const
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
Concrete class used by the front-end to report problems and issues.
StringRef getBuffer() const
Gets source code buffer.
void SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP)
SyntaxHighlight - Relex the specified FileID and annotate the HTML with information about keywords...
IdentifierInfo * LookUpIdentifierInfo(Token &Identifier) const
Given a tok::raw_identifier token, look up the identifier information for the token and install it in...
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 ...
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Defines the clang::Preprocessor interface.
void InsertTextAfter(unsigned OrigOffset, StringRef Str)
InsertTextAfter - Insert some text at the specified point, where the offset in the buffer is specifie...
SourceManager & getSourceManager() const
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
llvm::MemoryBuffer * getBuffer(FileID FID, SourceLocation Loc, bool *Invalid=nullptr) const
Return the buffer for the specified FileID.
void ReplaceText(unsigned OrigOffset, unsigned OrigLength, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string...
Encodes a location in the source.
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 ...
bool InsertTextAfter(SourceLocation Loc, StringRef Str)
InsertTextAfter - Insert the specified string at the specified location in the original buffer...
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
bool InsertTextBefore(SourceLocation Loc, StringRef Str)
InsertText - Insert the specified string at the specified location in the original buffer...
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool isNot(tok::TokenKind K) const
void InsertTextBefore(unsigned OrigOffset, StringRef Str)
InsertTextBefore - Insert some text before the specified point, where the offset in the buffer is spe...
Dataflow Directional Tag Classes.
A diagnostic client that ignores all diagnostics.
std::pair< SourceLocation, SourceLocation > getExpansionRange(SourceLocation Loc) const
Given a SourceLocation object, return the range of tokens covered by the expansion in the ultimate fi...
DiagnosticOptions & getDiagnosticOptions() const
Retrieve the diagnostic options.
void AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID, StringRef title)
unsigned getLength() const
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
const LangOptions & getLangOpts() const
bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok, const Token &Tok) const
AvoidConcat - If printing PrevTok immediately followed by Tok would cause the two individual tokens t...
Rewriter - This is the main interface to the rewrite buffers.
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
DiagnosticsEngine & getDiagnostics() const
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
void HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E, const char *StartTag, const char *EndTag)
HighlightRange - Highlight a range in the source code with the specified start/end tags...