clang  3.7.0
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 
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Lex/Preprocessor.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace clang;
27 
28 //===----------------------------------------------------------------------===//
29 // Functional HTML pretty-printing.
30 //===----------------------------------------------------------------------===//
31 
32 namespace {
33  class HTMLPrinter : public ASTConsumer {
34  Rewriter R;
35  raw_ostream *Out;
36  Preprocessor &PP;
38 
39  public:
40  HTMLPrinter(raw_ostream *OS, Preprocessor &pp,
41  bool _SyntaxHighlight, bool _HighlightMacros)
42  : Out(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> clang::CreateHTMLPrinter(raw_ostream *OS,
51  Preprocessor &PP,
52  bool SyntaxHighlight,
53  bool HighlightMacros) {
54  return llvm::make_unique<HTMLPrinter>(OS, PP, SyntaxHighlight,
56 }
57 
58 void HTMLPrinter::Initialize(ASTContext &context) {
59  R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
60 }
61 
62 void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
63  if (PP.getDiagnostics().hasErrorOccurred())
64  return;
65 
66  // Format the file.
67  FileID FID = R.getSourceMgr().getMainFileID();
68  const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
69  const char* Name;
70  // In some cases, in particular the case where the input is from stdin,
71  // there is no entry. Fall back to the memory buffer for a name in those
72  // cases.
73  if (Entry)
74  Name = Entry->getName();
75  else
76  Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
77 
78  html::AddLineNumbers(R, FID);
80 
81  // If we have a preprocessor, relex the file and syntax highlight.
82  // We might not have a preprocessor if we come from a deserialized AST file,
83  // for example.
84 
85  if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
86  if (HighlightMacros) html::HighlightMacros(R, FID, PP);
87  html::EscapeText(R, FID, false, true);
88 
89  // Emit the HTML.
90  const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
91  char *Buffer = (char*)malloc(RewriteBuf.size());
92  std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
93  Out->write(Buffer, RewriteBuf.size());
94  free(Buffer);
95 }
Defines the clang::ASTContext interface.
Defines the clang::FileManager interface and associated types.
Defines the SourceManager interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:89
void HighlightMacros(Rewriter &R, FileID FID, const Preprocessor &PP)
void AddLineNumbers(Rewriter &R, FileID FID)
iterator end() const
Definition: RewriteBuffer.h:36
const LangOptions & getLangOpts() const
Definition: ASTContext.h:533
void SyntaxHighlight(Rewriter &R, FileID FID, const Preprocessor &PP)
iterator begin() const
Definition: RewriteBuffer.h:35
Defines the clang::Preprocessor interface.
void AddHeaderFooterInternalBuiltinCSS(Rewriter &R, FileID FID, const char *title=nullptr)
const char * getName() const
Definition: FileManager.h:84
void EscapeText(Rewriter &R, FileID FID, bool EscapeSpaces=false, bool ReplaceTabs=false)
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:53
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Defines the Diagnostic-related interfaces.
std::unique_ptr< ASTConsumer > CreateHTMLPrinter(raw_ostream *OS, Preprocessor &PP, bool SyntaxHighlight=true, bool HighlightMacros=true)
Definition: HTMLPrint.cpp:50
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer. Note that it isn't safe to...
Definition: Rewriter.cpp:27
SourceManager & getSourceManager()
Definition: ASTContext.h:494
unsigned size() const
Definition: RewriteBuffer.h:37
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:96