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/Frontend/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/Core/HTMLRewrite.h"
00023 #include "clang/Rewrite/Core/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) override;
00046     void HandleTranslationUnit(ASTContext &Ctx) override;
00047   };
00048 }
00049 
00050 std::unique_ptr<ASTConsumer> clang::CreateHTMLPrinter(raw_ostream *OS,
00051                                                       Preprocessor &PP,
00052                                                       bool SyntaxHighlight,
00053                                                       bool HighlightMacros) {
00054   return llvm::make_unique<HTMLPrinter>(OS, PP, SyntaxHighlight,
00055                                         HighlightMacros);
00056 }
00057 
00058 void HTMLPrinter::Initialize(ASTContext &context) {
00059   R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
00060 }
00061 
00062 void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
00063   if (PP.getDiagnostics().hasErrorOccurred())
00064     return;
00065 
00066   // Format the file.
00067   FileID FID = R.getSourceMgr().getMainFileID();
00068   const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
00069   const char* Name;
00070   // In some cases, in particular the case where the input is from stdin,
00071   // there is no entry.  Fall back to the memory buffer for a name in those
00072   // cases.
00073   if (Entry)
00074     Name = Entry->getName();
00075   else
00076     Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
00077 
00078   html::AddLineNumbers(R, FID);
00079   html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
00080 
00081   // If we have a preprocessor, relex the file and syntax highlight.
00082   // We might not have a preprocessor if we come from a deserialized AST file,
00083   // for example.
00084 
00085   if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
00086   if (HighlightMacros) html::HighlightMacros(R, FID, PP);
00087   html::EscapeText(R, FID, false, true);
00088 
00089   // Emit the HTML.
00090   const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
00091   char *Buffer = (char*)malloc(RewriteBuf.size());
00092   std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
00093   Out->write(Buffer, RewriteBuf.size());
00094   free(Buffer);
00095 }