1 //== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the HTMLRewriter class, which is used to translate the
11 // text of a source file into prettified HTML.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Rewrite/Core/HTMLRewrite.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Lex/TokenConcatenation.h"
19 #include "clang/Rewrite/Core/Rewriter.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"
25 using namespace clang;
28 /// HighlightRange - Highlight a range in the source code with the specified
29 /// start/end tags. B/E must be in the same file. This ensures that
30 /// start/end tags are placed at the start/end of each line if the range is
32 void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E,
33 const char *StartTag, const char *EndTag) {
34 SourceManager &SM = R.getSourceMgr();
35 B = SM.getExpansionLoc(B);
36 E = SM.getExpansionLoc(E);
37 FileID FID = SM.getFileID(B);
38 assert(SM.getFileID(E) == FID && "B/E not in the same file!");
40 unsigned BOffset = SM.getFileOffset(B);
41 unsigned EOffset = SM.getFileOffset(E);
43 // Include the whole end token in the range.
44 EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts());
47 const char *BufferStart = SM.getBufferData(FID, &Invalid).data();
51 HighlightRange(R.getEditBuffer(FID), BOffset, EOffset,
52 BufferStart, StartTag, EndTag);
55 /// HighlightRange - This is the same as the above method, but takes
56 /// decomposed file locations.
57 void html::HighlightRange(RewriteBuffer &RB, unsigned B, unsigned E,
58 const char *BufferStart,
59 const char *StartTag, const char *EndTag) {
60 // Insert the tag at the absolute start/end of the range.
61 RB.InsertTextAfter(B, StartTag);
62 RB.InsertTextBefore(E, EndTag);
64 // Scan the range to see if there is a \r or \n. If so, and if the line is
65 // not blank, insert tags on that line as well.
66 bool HadOpenTag = true;
68 unsigned LastNonWhiteSpace = B;
69 for (unsigned i = B; i != E; ++i) {
70 switch (BufferStart[i]) {
73 // Okay, we found a newline in the range. If we have an open tag, we need
74 // to insert a close tag at the first non-whitespace before the newline.
76 RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
78 // Instead of inserting an open tag immediately after the newline, we
79 // wait until we see a non-whitespace character. This prevents us from
80 // inserting tags around blank lines, and also allows the open tag to
81 // be put *after* whitespace on a non-blank line.
93 // If there is no tag open, do it now.
95 RB.InsertTextAfter(i, StartTag);
99 // Remember this character.
100 LastNonWhiteSpace = i;
106 void html::EscapeText(Rewriter &R, FileID FID,
107 bool EscapeSpaces, bool ReplaceTabs) {
109 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
110 const char* C = Buf->getBufferStart();
111 const char* FileEnd = Buf->getBufferEnd();
113 assert (C <= FileEnd);
115 RewriteBuffer &RB = R.getEditBuffer(FID);
118 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
120 default: ++ColNo; break;
128 RB.ReplaceText(FilePos, 1, " ");
132 RB.ReplaceText(FilePos, 1, "<hr>");
139 unsigned NumSpaces = 8-(ColNo&7);
141 RB.ReplaceText(FilePos, 1,
142 StringRef(" "
143 " ", 6*NumSpaces));
145 RB.ReplaceText(FilePos, 1, StringRef(" ", NumSpaces));
150 RB.ReplaceText(FilePos, 1, "<");
155 RB.ReplaceText(FilePos, 1, ">");
160 RB.ReplaceText(FilePos, 1, "&");
167 std::string html::EscapeText(StringRef s, bool EscapeSpaces, bool ReplaceTabs) {
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;
208 static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
209 unsigned B, unsigned E) {
210 SmallString<256> Str;
211 llvm::raw_svector_ostream OS(Str);
213 OS << "<tr><td class=\"num\" id=\"LN"
215 << LineNo << "</td><td class=\"line\">";
217 if (B == E) { // Handle empty lines.
219 RB.InsertTextBefore(B, OS.str());
221 RB.InsertTextBefore(B, OS.str());
222 RB.InsertTextBefore(E, "</td></tr>");
226 void html::AddLineNumbers(Rewriter& R, FileID FID) {
228 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
229 const char* FileBeg = Buf->getBufferStart();
230 const char* FileEnd = Buf->getBufferEnd();
231 const char* C = FileBeg;
232 RewriteBuffer &RB = R.getEditBuffer(FID);
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);
248 // Scan until the newline (or end-of-file).
250 while (C != FileEnd) {
255 LineEndPos = FilePos++;
262 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
265 // Add one big table tag that surrounds all of the code.
266 RB.InsertTextBefore(0, "<table class=\"code\">\n");
267 RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
270 void html::AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID,
273 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FID);
274 const char* FileStart = Buf->getBufferStart();
275 const char* FileEnd = Buf->getBufferEnd();
277 SourceLocation StartLoc = R.getSourceMgr().getLocForStartOfFile(FID);
278 SourceLocation EndLoc = StartLoc.getLocWithOffset(FileEnd-FileStart);
281 llvm::raw_string_ostream os(s);
282 os << "<!doctype html>\n" // Use HTML 5 doctype
286 os << "<title>" << html::EscapeText(title) << "</title>\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;"
312 // Macros are position: relative to provide base for expansions.
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>";
362 R.InsertTextBefore(StartLoc, os.str());
365 R.InsertTextAfter(EndLoc, "</body></html>\n");
368 /// SyntaxHighlight - Relex the specified FileID and annotate the HTML with
369 /// information about keywords, macro expansions etc. This uses the macro
370 /// table state from the end of the file, so it won't be perfectly perfect,
371 /// but it will be reasonably close.
372 void html::SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP) {
373 RewriteBuffer &RB = R.getEditBuffer(FID);
375 const SourceManager &SM = PP.getSourceManager();
376 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
377 Lexer L(FID, FromFile, SM, PP.getLangOpts());
378 const char *BufferStart = L.getBuffer().data();
380 // Inform the preprocessor that we want to retain comments as tokens, so we
381 // can highlight them.
382 L.SetCommentRetentionState(true);
384 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
387 L.LexFromRawLexer(Tok);
389 while (Tok.isNot(tok::eof)) {
390 // Since we are lexing unexpanded tokens, all tokens are from the main
392 unsigned TokOffs = SM.getFileOffset(Tok.getLocation());
393 unsigned TokLen = Tok.getLength();
394 switch (Tok.getKind()) {
396 case tok::identifier:
397 llvm_unreachable("tok::identifier in raw lexing mode!");
398 case tok::raw_identifier: {
399 // Fill in Result.IdentifierInfo and update the token kind,
400 // looking up the identifier in the identifier table.
401 PP.LookUpIdentifierInfo(Tok);
403 // If this is a pp-identifier, for a keyword, highlight it as such.
404 if (Tok.isNot(tok::identifier))
405 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
406 "<span class='keyword'>", "</span>");
410 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
411 "<span class='comment'>", "</span>");
413 case tok::utf8_string_literal:
414 // Chop off the u part of u8 prefix
417 // FALL THROUGH to chop the 8
419 case tok::wide_string_literal:
420 case tok::utf16_string_literal:
421 case tok::utf32_string_literal:
422 // Chop off the L, u, U or 8 prefix
426 case tok::string_literal:
427 // FIXME: Exclude the optional ud-suffix from the highlighted range.
428 HighlightRange(RB, TokOffs, TokOffs+TokLen, BufferStart,
429 "<span class='string_literal'>", "</span>");
432 // If this is a preprocessor directive, all tokens to end of line are too.
433 if (!Tok.isAtStartOfLine())
436 // Eat all of the tokens until we get to the next one at the start of
438 unsigned TokEnd = TokOffs+TokLen;
439 L.LexFromRawLexer(Tok);
440 while (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {
441 TokEnd = SM.getFileOffset(Tok.getLocation())+Tok.getLength();
442 L.LexFromRawLexer(Tok);
445 // Find end of line. This is a hack.
446 HighlightRange(RB, TokOffs, TokEnd, BufferStart,
447 "<span class='directive'>", "</span>");
449 // Don't skip the next token.
454 L.LexFromRawLexer(Tok);
458 /// HighlightMacros - This uses the macro table state from the end of the
459 /// file, to re-expand macros and insert (into the HTML) information about the
460 /// macro expansions. This won't be perfectly perfect, but it will be
461 /// reasonably close.
462 void html::HighlightMacros(Rewriter &R, FileID FID, const Preprocessor& PP) {
463 // Re-lex the raw token stream into a token buffer.
464 const SourceManager &SM = PP.getSourceManager();
465 std::vector<Token> TokenStream;
467 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
468 Lexer L(FID, FromFile, SM, PP.getLangOpts());
470 // Lex all the tokens in raw mode, to avoid entering #includes or expanding
474 L.LexFromRawLexer(Tok);
476 // If this is a # at the start of a line, discard it from the token stream.
477 // We don't want the re-preprocess step to see #defines, #includes or other
478 // preprocessor directives.
479 if (Tok.is(tok::hash) && Tok.isAtStartOfLine())
482 // If this is a ## token, change its kind to unknown so that repreprocessing
483 // it will not produce an error.
484 if (Tok.is(tok::hashhash))
485 Tok.setKind(tok::unknown);
487 // If this raw token is an identifier, the raw lexer won't have looked up
488 // the corresponding identifier info for it. Do this now so that it will be
489 // macro expanded when we re-preprocess it.
490 if (Tok.is(tok::raw_identifier))
491 PP.LookUpIdentifierInfo(Tok);
493 TokenStream.push_back(Tok);
495 if (Tok.is(tok::eof)) break;
498 // Temporarily change the diagnostics object so that we ignore any generated
499 // diagnostics from this pass.
500 DiagnosticsEngine TmpDiags(PP.getDiagnostics().getDiagnosticIDs(),
501 &PP.getDiagnostics().getDiagnosticOptions(),
502 new IgnoringDiagConsumer);
504 // FIXME: This is a huge hack; we reuse the input preprocessor because we want
505 // its state, but we aren't actually changing it (we hope). This should really
506 // construct a copy of the preprocessor.
507 Preprocessor &TmpPP = const_cast<Preprocessor&>(PP);
508 DiagnosticsEngine *OldDiags = &TmpPP.getDiagnostics();
509 TmpPP.setDiagnostics(TmpDiags);
511 // Inform the preprocessor that we don't want comments.
512 TmpPP.SetCommentRetentionState(false, false);
514 // We don't want pragmas either. Although we filtered out #pragma, removing
515 // _Pragma and __pragma is much harder.
516 bool PragmasPreviouslyEnabled = TmpPP.getPragmasEnabled();
517 TmpPP.setPragmasEnabled(false);
519 // Enter the tokens we just lexed. This will cause them to be macro expanded
520 // but won't enter sub-files (because we removed #'s).
521 TmpPP.EnterTokenStream(TokenStream, false);
523 TokenConcatenation ConcatInfo(TmpPP);
525 // Lex all the tokens.
528 while (Tok.isNot(tok::eof)) {
529 // Ignore non-macro tokens.
530 if (!Tok.getLocation().isMacroID()) {
535 // Okay, we have the first token of a macro expansion: highlight the
536 // expansion by inserting a start tag before the macro expansion and
538 std::pair<SourceLocation, SourceLocation> LLoc =
539 SM.getExpansionRange(Tok.getLocation());
541 // Ignore tokens whose instantiation location was not the main file.
542 if (SM.getFileID(LLoc.first) != FID) {
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();
555 // Okay, eat this token, getting the next one.
558 // Skip all the rest of the tokens that are part of this macro
559 // instantiation. It would be really nice to pop up a window with all the
560 // spelling of the tokens or something.
561 while (!Tok.is(tok::eof) &&
562 SM.getExpansionLoc(Tok.getLocation()) == LLoc.first) {
563 // Insert a newline if the macro expansion is getting large.
569 LineLen -= Expansion.size();
571 // If the tokens were already space separated, or if they must be to avoid
572 // them being implicitly pasted, add a space between them.
573 if (Tok.hasLeadingSpace() ||
574 ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok))
577 // Escape any special characters in the token text.
578 Expansion += EscapeText(TmpPP.getSpelling(Tok));
579 LineLen += Expansion.size();
581 PrevPrevTok = PrevTok;
587 // Insert the expansion as the end tag, so that multi-line macros all get
589 Expansion = "<span class='expansion'>" + Expansion + "</span></span>";
591 HighlightRange(R, LLoc.first, LLoc.second,
592 "<span class='macro'>", Expansion.c_str());
595 // Restore the preprocessor's old state.
596 TmpPP.setDiagnostics(*OldDiags);
597 TmpPP.setPragmasEnabled(PragmasPreviouslyEnabled);