1fdbe6799cae249c8b29cb7f7d4032d2f2468df06Ted Kremenek//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
25e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek//
35e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek//                     The LLVM Compiler Infrastructure
45e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek//
55e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek// This file is distributed under the University of Illinois Open Source
65e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek// License. See LICENSE.TXT for details.
75e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek//
85e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek//===----------------------------------------------------------------------===//
95e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek//
10fdbe6799cae249c8b29cb7f7d4032d2f2468df06Ted Kremenek// Pretty-printing of source code to HTML.
115e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek//
125e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek//===----------------------------------------------------------------------===//
135e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek
14305c613af6cfc40e519c75d9d2c84c6fa9a841c0Ted Kremenek#include "clang/Rewrite/Frontend/ASTConsumers.h"
155e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek#include "clang/AST/ASTConsumer.h"
1690b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar#include "clang/AST/ASTContext.h"
17e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Decl.h"
188ac661c3c5ffaeedfb3268994ad864ade77b3ba0Chris Lattner#include "clang/Basic/Diagnostic.h"
19f6f593fae2f0531b4bc06891941f7fbba5217618Ted Kremenek#include "clang/Basic/FileManager.h"
2090b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar#include "clang/Basic/SourceManager.h"
2190b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar#include "clang/Lex/Preprocessor.h"
22305c613af6cfc40e519c75d9d2c84c6fa9a841c0Ted Kremenek#include "clang/Rewrite/Core/HTMLRewrite.h"
23305c613af6cfc40e519c75d9d2c84c6fa9a841c0Ted Kremenek#include "clang/Rewrite/Core/Rewriter.h"
2424200917df7f66afed031e6f4f1672158d28d377Eli Friedman#include "llvm/Support/MemoryBuffer.h"
250fa0daafdfc631681cc42a26a99618e9f9e63f97Chris Lattner#include "llvm/Support/raw_ostream.h"
265e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenekusing namespace clang;
275e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek
2813e479b2b74771738a5e5c5dc1c270416924b126Ted Kremenek//===----------------------------------------------------------------------===//
2913e479b2b74771738a5e5c5dc1c270416924b126Ted Kremenek// Functional HTML pretty-printing.
301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump//===----------------------------------------------------------------------===//
3113e479b2b74771738a5e5c5dc1c270416924b126Ted Kremenek
325e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremeneknamespace {
335e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek  class HTMLPrinter : public ASTConsumer {
345e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek    Rewriter R;
355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    raw_ostream *Out;
3690b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar    Preprocessor &PP;
3790b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar    bool SyntaxHighlight, HighlightMacros;
3890b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar
395e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek  public:
405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    HTMLPrinter(raw_ostream *OS, Preprocessor &pp,
4190b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar                bool _SyntaxHighlight, bool _HighlightMacros)
4290b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar      : Out(OS), PP(pp), SyntaxHighlight(_SyntaxHighlight),
4390b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar        HighlightMacros(_HighlightMacros) {}
441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    void Initialize(ASTContext &context) override;
46651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    void HandleTranslationUnit(ASTContext &Ctx) override;
475e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek  };
485e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek}
495e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek
505f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerASTConsumer* clang::CreateHTMLPrinter(raw_ostream *OS,
5190b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar                                      Preprocessor &PP,
5290b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar                                      bool SyntaxHighlight,
5390b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar                                      bool HighlightMacros) {
5490b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar  return new HTMLPrinter(OS, PP, SyntaxHighlight, HighlightMacros);
558ac661c3c5ffaeedfb3268994ad864ade77b3ba0Chris Lattner}
565e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek
575e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenekvoid HTMLPrinter::Initialize(ASTContext &context) {
584e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
595e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek}
605e0020ee6182f63cd5f4a53e240dc08bd02d7728Ted Kremenek
61de2baa7576f6638c31168960557d30eac1766eb9Eli Friedmanvoid HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
6290b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar  if (PP.getDiagnostics().hasErrorOccurred())
638ac661c3c5ffaeedfb3268994ad864ade77b3ba0Chris Lattner    return;
648ac661c3c5ffaeedfb3268994ad864ade77b3ba0Chris Lattner
658ac661c3c5ffaeedfb3268994ad864ade77b3ba0Chris Lattner  // Format the file.
662b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  FileID FID = R.getSourceMgr().getMainFileID();
672b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
6824200917df7f66afed031e6f4f1672158d28d377Eli Friedman  const char* Name;
6924200917df7f66afed031e6f4f1672158d28d377Eli Friedman  // In some cases, in particular the case where the input is from stdin,
7024200917df7f66afed031e6f4f1672158d28d377Eli Friedman  // there is no entry.  Fall back to the memory buffer for a name in those
7124200917df7f66afed031e6f4f1672158d28d377Eli Friedman  // cases.
7224200917df7f66afed031e6f4f1672158d28d377Eli Friedman  if (Entry)
7324200917df7f66afed031e6f4f1672158d28d377Eli Friedman    Name = Entry->getName();
7424200917df7f66afed031e6f4f1672158d28d377Eli Friedman  else
7524200917df7f66afed031e6f4f1672158d28d377Eli Friedman    Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
7624200917df7f66afed031e6f4f1672158d28d377Eli Friedman
772b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  html::AddLineNumbers(R, FID);
7824200917df7f66afed031e6f4f1672158d28d377Eli Friedman  html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
798ac661c3c5ffaeedfb3268994ad864ade77b3ba0Chris Lattner
8047abe7690491f28da5978225801ae146810bd388Ted Kremenek  // If we have a preprocessor, relex the file and syntax highlight.
8147abe7690491f28da5978225801ae146810bd388Ted Kremenek  // We might not have a preprocessor if we come from a deserialized AST file,
8247abe7690491f28da5978225801ae146810bd388Ted Kremenek  // for example.
831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8490b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar  if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
8590b1827c1c1cf075266b96b416eefcf37924333bDaniel Dunbar  if (HighlightMacros) html::HighlightMacros(R, FID, PP);
862b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  html::EscapeText(R, FID, false, true);
8766d6f048471aad6daf67b3ea804b54fbea72a076Eli Friedman
8813e479b2b74771738a5e5c5dc1c270416924b126Ted Kremenek  // Emit the HTML.
892b2453a7d8fe732561795431f39ceb2b2a832d84Chris Lattner  const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
908ac661c3c5ffaeedfb3268994ad864ade77b3ba0Chris Lattner  char *Buffer = (char*)malloc(RewriteBuf.size());
918ac661c3c5ffaeedfb3268994ad864ade77b3ba0Chris Lattner  std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
9266d6f048471aad6daf67b3ea804b54fbea72a076Eli Friedman  Out->write(Buffer, RewriteBuf.size());
938ac661c3c5ffaeedfb3268994ad864ade77b3ba0Chris Lattner  free(Buffer);
9413e479b2b74771738a5e5c5dc1c270416924b126Ted Kremenek}
95