1 //===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Pretty-printing of source code to HTML.
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/ASTConsumer.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Rewrite/Core/HTMLRewrite.h"
21 #include "clang/Rewrite/Core/Rewriter.h"
22 #include "clang/Rewrite/Frontend/ASTConsumers.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
26 //===----------------------------------------------------------------------===//
27 // Functional HTML pretty-printing.
28 //===----------------------------------------------------------------------===//
31 class HTMLPrinter : public ASTConsumer {
33 std::unique_ptr<raw_ostream> Out;
35 bool SyntaxHighlight, HighlightMacros;
38 HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,
39 bool _SyntaxHighlight, bool _HighlightMacros)
40 : Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),
41 HighlightMacros(_HighlightMacros) {}
43 void Initialize(ASTContext &context) override;
44 void HandleTranslationUnit(ASTContext &Ctx) override;
48 std::unique_ptr<ASTConsumer>
49 clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
50 bool SyntaxHighlight, bool HighlightMacros) {
51 return llvm::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
55 void HTMLPrinter::Initialize(ASTContext &context) {
56 R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
59 void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
60 if (PP.getDiagnostics().hasErrorOccurred())
64 FileID FID = R.getSourceMgr().getMainFileID();
65 const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
67 // In some cases, in particular the case where the input is from stdin,
68 // there is no entry. Fall back to the memory buffer for a name in those
71 Name = Entry->getName();
73 Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
75 html::AddLineNumbers(R, FID);
76 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
78 // If we have a preprocessor, relex the file and syntax highlight.
79 // We might not have a preprocessor if we come from a deserialized AST file,
82 if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
83 if (HighlightMacros) html::HighlightMacros(R, FID, PP);
84 html::EscapeText(R, FID, false, true);
87 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
88 std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);
89 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
90 Out->write(Buffer.get(), RewriteBuf.size());