1//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Pretty-printing of source code to HTML.
10//
11//===----------------------------------------------------------------------===//
12
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"
24using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Functional HTML pretty-printing.
28//===----------------------------------------------------------------------===//
29
30namespace {
31 class HTMLPrinter : public ASTConsumer {
32 Rewriter R;
33 std::unique_ptr<raw_ostream> Out;
34 Preprocessor &PP;
35 bool SyntaxHighlight, HighlightMacros;
36
37 public:
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) {}
42
43 void Initialize(ASTContext &context) override;
44 void HandleTranslationUnit(ASTContext &Ctx) override;
45 };
46}
47
48std::unique_ptr<ASTConsumer>
49clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
50 bool SyntaxHighlight, bool HighlightMacros) {
51 return std::make_unique<HTMLPrinter>(args: std::move(OS), args&: PP, args&: SyntaxHighlight,
52 args&: HighlightMacros);
53}
54
55void HTMLPrinter::Initialize(ASTContext &context) {
56 R.setSourceMgr(SM&: context.getSourceManager(), LO: context.getLangOpts());
57}
58
59void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
60 if (PP.getDiagnostics().hasErrorOccurred())
61 return;
62
63 // Format the file.
64 FileID FID = R.getSourceMgr().getMainFileID();
65 OptionalFileEntryRef Entry = R.getSourceMgr().getFileEntryRefForID(FID);
66 StringRef Name;
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
69 // cases.
70 if (Entry)
71 Name = Entry->getName();
72 else
73 Name = R.getSourceMgr().getBufferOrFake(FID).getBufferIdentifier();
74
75 html::AddLineNumbers(R, FID);
76 html::AddHeaderFooterInternalBuiltinCSS(R, FID, title: Name);
77
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,
80 // for example.
81
82 if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
83 if (HighlightMacros) html::HighlightMacros(R, FID, PP);
84 html::EscapeText(R, FID, EscapeSpaces: false, ReplaceTabs: true);
85
86 // Emit the HTML.
87 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
88 std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);
89 std::copy(first: RewriteBuf.begin(), last: RewriteBuf.end(), result: Buffer.get());
90 Out->write(Ptr: Buffer.get(), Size: RewriteBuf.size());
91}
92

source code of clang/lib/Frontend/Rewrite/HTMLPrint.cpp