clang  20.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"
19 #include "clang/Lex/Preprocessor.h"
23 #include "llvm/ADT/RewriteBuffer.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
26 using llvm::RewriteBuffer;
27 
28 //===----------------------------------------------------------------------===//
29 // Functional HTML pretty-printing.
30 //===----------------------------------------------------------------------===//
31 
32 namespace {
33  class HTMLPrinter : public ASTConsumer {
34  Rewriter R;
35  std::unique_ptr<raw_ostream> Out;
36  Preprocessor &PP;
38 
39  public:
40  HTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &pp,
41  bool _SyntaxHighlight, bool _HighlightMacros)
42  : Out(std::move(OS)), PP(pp), SyntaxHighlight(_SyntaxHighlight),
43  HighlightMacros(_HighlightMacros) {}
44 
45  void Initialize(ASTContext &context) override;
46  void HandleTranslationUnit(ASTContext &Ctx) override;
47  };
48 }
49 
50 std::unique_ptr<ASTConsumer>
51 clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
52  bool SyntaxHighlight, bool HighlightMacros) {
53  return std::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
55 }
56 
57 void HTMLPrinter::Initialize(ASTContext &context) {
58  R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
59 }
60 
61 void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
62  if (PP.getDiagnostics().hasErrorOccurred())
63  return;
64 
65  // Format the file.
66  FileID FID = R.getSourceMgr().getMainFileID();
67  OptionalFileEntryRef Entry = R.getSourceMgr().getFileEntryRefForID(FID);
68  StringRef Name;
69  // In some cases, in particular the case where the input is from stdin,
70  // there is no entry. Fall back to the memory buffer for a name in those
71  // cases.
72  if (Entry)
73  Name = Entry->getName();
74  else
75  Name = R.getSourceMgr().getBufferOrFake(FID).getBufferIdentifier();
76 
77  html::AddLineNumbers(R, FID);
79 
80  // If we have a preprocessor, relex the file and syntax highlight.
81  // We might not have a preprocessor if we come from a deserialized AST file,
82  // for example.
83 
84  if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
85  if (HighlightMacros) html::HighlightMacros(R, FID, PP);
86  html::EscapeText(R, FID, false, true);
87 
88  // Emit the HTML.
89  const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
90  std::unique_ptr<char[]> Buffer(new char[RewriteBuf.size()]);
91  std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer.get());
92  Out->write(Buffer.get(), RewriteBuf.size());
93 }
Defines the clang::ASTContext interface.
Defines the Diagnostic-related interfaces.
Defines the clang::FileManager interface and associated types.
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
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
SourceManager & getSourceManager()
Definition: ASTContext.h:721
const LangOptions & getLangOpts() const
Definition: ASTContext.h:797
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:137
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:51