17330f729Sjoerg //===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // Pretty-printing of source code to HTML.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/AST/ASTConsumer.h"
147330f729Sjoerg #include "clang/AST/ASTContext.h"
157330f729Sjoerg #include "clang/AST/Decl.h"
167330f729Sjoerg #include "clang/Basic/Diagnostic.h"
177330f729Sjoerg #include "clang/Basic/FileManager.h"
187330f729Sjoerg #include "clang/Basic/SourceManager.h"
197330f729Sjoerg #include "clang/Lex/Preprocessor.h"
207330f729Sjoerg #include "clang/Rewrite/Core/HTMLRewrite.h"
217330f729Sjoerg #include "clang/Rewrite/Core/Rewriter.h"
227330f729Sjoerg #include "clang/Rewrite/Frontend/ASTConsumers.h"
237330f729Sjoerg #include "llvm/Support/raw_ostream.h"
247330f729Sjoerg using namespace clang;
257330f729Sjoerg
267330f729Sjoerg //===----------------------------------------------------------------------===//
277330f729Sjoerg // Functional HTML pretty-printing.
287330f729Sjoerg //===----------------------------------------------------------------------===//
297330f729Sjoerg
307330f729Sjoerg namespace {
317330f729Sjoerg class HTMLPrinter : public ASTConsumer {
327330f729Sjoerg Rewriter R;
337330f729Sjoerg std::unique_ptr<raw_ostream> Out;
347330f729Sjoerg Preprocessor &PP;
357330f729Sjoerg bool SyntaxHighlight, HighlightMacros;
367330f729Sjoerg
377330f729Sjoerg public:
HTMLPrinter(std::unique_ptr<raw_ostream> OS,Preprocessor & pp,bool _SyntaxHighlight,bool _HighlightMacros)387330f729Sjoerg HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,
397330f729Sjoerg bool _SyntaxHighlight, bool _HighlightMacros)
407330f729Sjoerg : Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),
417330f729Sjoerg HighlightMacros(_HighlightMacros) {}
427330f729Sjoerg
437330f729Sjoerg void Initialize(ASTContext &context) override;
447330f729Sjoerg void HandleTranslationUnit(ASTContext &Ctx) override;
457330f729Sjoerg };
467330f729Sjoerg }
477330f729Sjoerg
487330f729Sjoerg std::unique_ptr<ASTConsumer>
CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS,Preprocessor & PP,bool SyntaxHighlight,bool HighlightMacros)497330f729Sjoerg clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
507330f729Sjoerg bool SyntaxHighlight, bool HighlightMacros) {
517330f729Sjoerg return std::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
527330f729Sjoerg HighlightMacros);
537330f729Sjoerg }
547330f729Sjoerg
Initialize(ASTContext & context)557330f729Sjoerg void HTMLPrinter::Initialize(ASTContext &context) {
567330f729Sjoerg R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
577330f729Sjoerg }
587330f729Sjoerg
HandleTranslationUnit(ASTContext & Ctx)597330f729Sjoerg void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
607330f729Sjoerg if (PP.getDiagnostics().hasErrorOccurred())
617330f729Sjoerg return;
627330f729Sjoerg
637330f729Sjoerg // Format the file.
647330f729Sjoerg FileID FID = R.getSourceMgr().getMainFileID();
657330f729Sjoerg const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
667330f729Sjoerg StringRef Name;
677330f729Sjoerg // In some cases, in particular the case where the input is from stdin,
687330f729Sjoerg // there is no entry. Fall back to the memory buffer for a name in those
697330f729Sjoerg // cases.
707330f729Sjoerg if (Entry)
717330f729Sjoerg Name = Entry->getName();
727330f729Sjoerg else
73*e038c9c4Sjoerg Name = R.getSourceMgr().getBufferOrFake(FID).getBufferIdentifier();
747330f729Sjoerg
757330f729Sjoerg html::AddLineNumbers(R, FID);
767330f729Sjoerg html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
777330f729Sjoerg
787330f729Sjoerg // If we have a preprocessor, relex the file and syntax highlight.
797330f729Sjoerg // We might not have a preprocessor if we come from a deserialized AST file,
807330f729Sjoerg // for example.
817330f729Sjoerg
827330f729Sjoerg if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
837330f729Sjoerg if (HighlightMacros) html::HighlightMacros(R, FID, PP);
847330f729Sjoerg html::EscapeText(R, FID, false, true);
857330f729Sjoerg
867330f729Sjoerg // Emit the HTML.
877330f729Sjoerg const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
887330f729Sjoerg std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);
897330f729Sjoerg std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
907330f729Sjoerg Out->write(Buffer.get(), RewriteBuf.size());
917330f729Sjoerg }
92