1 //===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
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 // Pretty-printing of source code to HTML.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTConsumer.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "clang/Rewrite/Core/HTMLRewrite.h"
22 #include "clang/Rewrite/Core/Rewriter.h"
23 #include "clang/Rewrite/Frontend/ASTConsumers.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
27 //===----------------------------------------------------------------------===//
28 // Functional HTML pretty-printing.
29 //===----------------------------------------------------------------------===//
32 class HTMLPrinter : public ASTConsumer {
34 std::unique_ptr<raw_ostream> Out;
36 bool SyntaxHighlight, HighlightMacros;
39 HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,
40 bool _SyntaxHighlight, bool _HighlightMacros)
41 : Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),
42 HighlightMacros(_HighlightMacros) {}
44 void Initialize(ASTContext &context) override;
45 void HandleTranslationUnit(ASTContext &Ctx) override;
49 std::unique_ptr<ASTConsumer>
50 clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
51 bool SyntaxHighlight, bool HighlightMacros) {
52 return llvm::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
56 void HTMLPrinter::Initialize(ASTContext &context) {
57 R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
60 void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
61 if (PP.getDiagnostics().hasErrorOccurred())
65 FileID FID = R.getSourceMgr().getMainFileID();
66 const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
68 // In some cases, in particular the case where the input is from stdin,
69 // there is no entry. Fall back to the memory buffer for a name in those
72 Name = Entry->getName();
74 Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
76 html::AddLineNumbers(R, FID);
77 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
79 // If we have a preprocessor, relex the file and syntax highlight.
80 // We might not have a preprocessor if we come from a deserialized AST file,
83 if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
84 if (HighlightMacros) html::HighlightMacros(R, FID, PP);
85 html::EscapeText(R, FID, false, true);
88 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
89 char *Buffer = (char*)malloc(RewriteBuf.size());
90 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
91 Out->write(Buffer, RewriteBuf.size());