clang 22.0.0git
HTMLPrint.cpp
Go to the documentation of this file.
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
15#include "clang/AST/Decl.h"
22#include "llvm/ADT/RewriteBuffer.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25using llvm::RewriteBuffer;
26
27//===----------------------------------------------------------------------===//
28// Functional HTML pretty-printing.
29//===----------------------------------------------------------------------===//
30
31namespace {
32 class HTMLPrinter : public ASTConsumer {
33 Rewriter R;
34 std::unique_ptr<raw_ostream> Out;
35 Preprocessor &PP;
37
38 public:
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) {}
43
44 void Initialize(ASTContext &context) override;
45 void HandleTranslationUnit(ASTContext &Ctx) override;
46 };
47}
48
49std::unique_ptr<ASTConsumer>
50clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
51 bool SyntaxHighlight, bool HighlightMacros) {
52 return std::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
53 HighlightMacros);
54}
55
56void HTMLPrinter::Initialize(ASTContext &context) {
57 R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
58}
59
60void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
61 if (PP.getDiagnostics().hasErrorOccurred())
62 return;
63
64 // Format the file.
65 FileID FID = R.getSourceMgr().getMainFileID();
66 OptionalFileEntryRef Entry = R.getSourceMgr().getFileEntryRefForID(FID);
67 StringRef Name;
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
70 // cases.
71 if (Entry)
72 Name = Entry->getName();
73 else
74 Name = R.getSourceMgr().getBufferOrFake(FID).getBufferIdentifier();
75
78
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,
81 // for example.
82
83 if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
84 if (HighlightMacros) html::HighlightMacros(R, FID, PP);
85 html::EscapeText(R, FID, false, true);
86
87 // Emit the HTML.
88 const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
89 std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);
90 std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
91 Out->write(Buffer.get(), RewriteBuf.size());
92}
Defines the clang::ASTContext interface.
Defines the Diagnostic-related interfaces.
Defines the clang::Preprocessor interface.
Defines the SourceManager interface.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition: ASTConsumer.h:34
virtual void HandleTranslationUnit(ASTContext &Ctx)
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
Definition: ASTConsumer.h:67
virtual void Initialize(ASTContext &Context)
Initialize - This is called to initialize the consumer, providing the ASTContext.
Definition: ASTConsumer.h:48
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
SourceManager & getSourceManager()
Definition: ASTContext.h:801
const LangOptions & getLangOpts() const
Definition: ASTContext.h:894
StringRef getName() const
The name of this FileEntry.
Definition: FileEntry.h:61
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:145
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:32
void AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID, StringRef title)
void AddLineNumbers(Rewriter &R, FileID FID)
void SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP, RelexRewriteCacheRef Cache=nullptr)
SyntaxHighlight - Relex the specified FileID and annotate the HTML with information about keywords,...
void HighlightMacros(Rewriter &R, FileID FID, const Preprocessor &PP, RelexRewriteCacheRef Cache=nullptr)
HighlightMacros - This uses the macro table state from the end of the file, to reexpand macros and in...
void EscapeText(Rewriter &R, FileID FID, bool EscapeSpaces=false, bool ReplaceTabs=false)
EscapeText - HTMLize a specified file so that special characters are are translated so that they are ...
The JSON file list parser is used to communicate input to InstallAPI.
std::unique_ptr< ASTConsumer > CreateHTMLPrinter(std::unique_ptr< raw_ostream > OS, Preprocessor &PP, bool SyntaxHighlight=true, bool HighlightMacros=true)
CreateHTMLPrinter - Create an AST consumer which rewrites source code to HTML with syntax highlightin...
Definition: HTMLPrint.cpp:50