clang 10.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
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;
25
26 //===----------------------------------------------------------------------===//
27 // Functional HTML pretty-printing.
28 //===----------------------------------------------------------------------===//
29
30 namespace {
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
48 std::unique_ptr<ASTConsumer>
49 clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
50 bool SyntaxHighlight, bool HighlightMacros) {
51 return std::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
52 HighlightMacros);
53 }
54
55 void HTMLPrinter::Initialize(ASTContext &context) {
56 R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
57 }
58
59 void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
60 if (PP.getDiagnostics().hasErrorOccurred())
61 return;
62
63 // Format the file.
64 FileID FID = R.getSourceMgr().getMainFileID();
65 const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(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().getBuffer(FID)->getBufferIdentifier();
74
75 html::AddLineNumbers(R, FID);
76 html::AddHeaderFooterInternalBuiltinCSS(R, FID, 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, false, 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(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
90 Out->write(Buffer.get(), RewriteBuf.size());
91 }
ASTContext.h
Defines the clang::ASTContext interface.
Rewriter.h
clang::ASTConsumer
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:33
FileManager.h
Defines the clang::FileManager interface and associated types.
SourceManager.h
Defines the SourceManager interface.
clang::RewriteBuffer
RewriteBuffer - As code is rewritten, SourceBuffer&#39;s from the original input with modifications get a...
Definition: RewriteBuffer.h:25
clang::RewriteBuffer::end
iterator end() const
Definition: RewriteBuffer.h:38
clang::CreateHTMLPrinter
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:49
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
clang::html::HighlightMacros
void HighlightMacros(Rewriter &R, FileID FID, const Preprocessor &PP)
HighlightMacros - This uses the macro table state from the end of the file, to reexpand macros and in...
Definition: HTMLRewrite.cpp:534
clang::html::AddLineNumbers
void AddLineNumbers(Rewriter &R, FileID FID)
Definition: HTMLRewrite.cpp:227
std
Definition: Format.h:2445
Diagnostic.h
Defines the Diagnostic-related interfaces.
clang::html::SyntaxHighlight
void SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP)
SyntaxHighlight - Relex the specified FileID and annotate the HTML with information about keywords...
Definition: HTMLRewrite.cpp:444
ASTConsumers.h
HTMLRewrite.h
Preprocessor.h
Defines the clang::Preprocessor interface.
ASTConsumer.h
clang::RewriteBuffer::begin
iterator begin() const
Definition: RewriteBuffer.h:37
clang::RewriteBuffer::write
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer.
Definition: Rewriter.cpp:37
clang::FileEntry::getName
StringRef getName() const
Definition: FileManager.h:102
clang::html::EscapeText
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 ...
Definition: HTMLRewrite.cpp:107
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:78
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:38
Decl.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::html::AddHeaderFooterInternalBuiltinCSS
void AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID, StringRef title)
Definition: HTMLRewrite.cpp:274
clang::RewriteBuffer::size
unsigned size() const
Definition: RewriteBuffer.h:39
clang::ASTContext::getSourceManager
SourceManager & getSourceManager()
Definition: ASTContext.h:679
clang::Rewriter
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:32
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:724
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128