clang  6.0.0svn
HTMLPrint.cpp
Go to the documentation of this file.
1 //===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Pretty-printing of source code to HTML.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/ASTConsumer.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Lex/Preprocessor.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
26 
27 //===----------------------------------------------------------------------===//
28 // Functional HTML pretty-printing.
29 //===----------------------------------------------------------------------===//
30 
31 namespace {
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 
49 std::unique_ptr<ASTConsumer>
50 clang::CreateHTMLPrinter(std::unique_ptr<raw_ostream> OS, Preprocessor &PP,
51  bool SyntaxHighlight, bool HighlightMacros) {
52  return llvm::make_unique<HTMLPrinter>(std::move(OS), PP, SyntaxHighlight,
54 }
55 
56 void HTMLPrinter::Initialize(ASTContext &context) {
57  R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
58 }
59 
60 void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
61  if (PP.getDiagnostics().hasErrorOccurred())
62  return;
63 
64  // Format the file.
65  FileID FID = R.getSourceMgr().getMainFileID();
66  const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(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().getBuffer(FID)->getBufferIdentifier();
75 
76  html::AddLineNumbers(R, FID);
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  char *Buffer = (char*)malloc(RewriteBuf.size());
90  std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
91  Out->write(Buffer, RewriteBuf.size());
92  free(Buffer);
93 }
Defines the clang::ASTContext interface.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:34
Defines the clang::FileManager interface and associated types.
Defines the SourceManager interface.
RewriteBuffer - As code is rewritten, SourceBuffer&#39;s from the original input with modifications get a...
Definition: RewriteBuffer.h:27
iterator end() const
Definition: RewriteBuffer.h:36
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
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
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...
void AddLineNumbers(Rewriter &R, FileID FID)
Definition: Format.h:1900
Defines the Diagnostic-related interfaces.
void SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP)
SyntaxHighlight - Relex the specified FileID and annotate the HTML with information about keywords...
Defines the clang::Preprocessor interface.
iterator begin() const
Definition: RewriteBuffer.h:35
StringRef getName() const
Definition: FileManager.h:84
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 ...
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
void AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID, StringRef title)
unsigned size() const
Definition: RewriteBuffer.h:37
SourceManager & getSourceManager()
Definition: ASTContext.h:643
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:31
const LangOptions & getLangOpts() const
Definition: ASTContext.h:688
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:127