clang API Documentation

HTMLPrint.cpp
Go to the documentation of this file.
00001 //===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Pretty-printing of source code to HTML.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Rewrite/ASTConsumers.h"
00015 #include "clang/AST/ASTConsumer.h"
00016 #include "clang/AST/ASTContext.h"
00017 #include "clang/AST/Decl.h"
00018 #include "clang/Basic/Diagnostic.h"
00019 #include "clang/Basic/FileManager.h"
00020 #include "clang/Basic/SourceManager.h"
00021 #include "clang/Lex/Preprocessor.h"
00022 #include "clang/Rewrite/HTMLRewrite.h"
00023 #include "clang/Rewrite/Rewriter.h"
00024 #include "llvm/Support/MemoryBuffer.h"
00025 #include "llvm/Support/raw_ostream.h"
00026 using namespace clang;
00027 
00028 //===----------------------------------------------------------------------===//
00029 // Functional HTML pretty-printing.
00030 //===----------------------------------------------------------------------===//
00031 
00032 namespace {
00033   class HTMLPrinter : public ASTConsumer {
00034     Rewriter R;
00035     raw_ostream *Out;
00036     Preprocessor &PP;
00037     bool SyntaxHighlight, HighlightMacros;
00038 
00039   public:
00040     HTMLPrinter(raw_ostream *OS, Preprocessor &pp,
00041                 bool _SyntaxHighlight, bool _HighlightMacros)
00042       : Out(OS), PP(pp), SyntaxHighlight(_SyntaxHighlight),
00043         HighlightMacros(_HighlightMacros) {}
00044 
00045     void Initialize(ASTContext &context);
00046     void HandleTranslationUnit(ASTContext &Ctx);
00047   };
00048 }
00049 
00050 ASTConsumer* clang::CreateHTMLPrinter(raw_ostream *OS,
00051                                       Preprocessor &PP,
00052                                       bool SyntaxHighlight,
00053                                       bool HighlightMacros) {
00054   return new HTMLPrinter(OS, PP, SyntaxHighlight, HighlightMacros);
00055 }
00056 
00057 void HTMLPrinter::Initialize(ASTContext &context) {
00058   R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
00059 }
00060 
00061 void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
00062   if (PP.getDiagnostics().hasErrorOccurred())
00063     return;
00064 
00065   // Format the file.
00066   FileID FID = R.getSourceMgr().getMainFileID();
00067   const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
00068   const char* Name;
00069   // In some cases, in particular the case where the input is from stdin,
00070   // there is no entry.  Fall back to the memory buffer for a name in those
00071   // cases.
00072   if (Entry)
00073     Name = Entry->getName();
00074   else
00075     Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
00076 
00077   html::AddLineNumbers(R, FID);
00078   html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
00079 
00080   // If we have a preprocessor, relex the file and syntax highlight.
00081   // We might not have a preprocessor if we come from a deserialized AST file,
00082   // for example.
00083 
00084   if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
00085   if (HighlightMacros) html::HighlightMacros(R, FID, PP);
00086   html::EscapeText(R, FID, false, true);
00087 
00088   // Emit the HTML.
00089   const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
00090   char *Buffer = (char*)malloc(RewriteBuf.size());
00091   std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
00092   Out->write(Buffer, RewriteBuf.size());
00093   free(Buffer);
00094 }