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