HTMLPrint.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include "clang/Rewrite/Frontend/ASTConsumers.h"
  14. #include "clang/AST/ASTConsumer.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/Decl.h"
  17. #include "clang/Basic/Diagnostic.h"
  18. #include "clang/Basic/FileManager.h"
  19. #include "clang/Basic/SourceManager.h"
  20. #include "clang/Lex/Preprocessor.h"
  21. #include "clang/Rewrite/Core/HTMLRewrite.h"
  22. #include "clang/Rewrite/Core/Rewriter.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. using namespace clang;
  26. //===----------------------------------------------------------------------===//
  27. // Functional HTML pretty-printing.
  28. //===----------------------------------------------------------------------===//
  29. namespace {
  30. class HTMLPrinter : public ASTConsumer {
  31. Rewriter R;
  32. raw_ostream *Out;
  33. Preprocessor &PP;
  34. bool SyntaxHighlight, HighlightMacros;
  35. public:
  36. HTMLPrinter(raw_ostream *OS, Preprocessor &pp,
  37. bool _SyntaxHighlight, bool _HighlightMacros)
  38. : Out(OS), PP(pp), SyntaxHighlight(_SyntaxHighlight),
  39. HighlightMacros(_HighlightMacros) {}
  40. void Initialize(ASTContext &context) override;
  41. void HandleTranslationUnit(ASTContext &Ctx) override;
  42. };
  43. }
  44. std::unique_ptr<ASTConsumer> clang::CreateHTMLPrinter(raw_ostream *OS,
  45. Preprocessor &PP,
  46. bool SyntaxHighlight,
  47. bool HighlightMacros) {
  48. return llvm::make_unique<HTMLPrinter>(OS, PP, SyntaxHighlight,
  49. HighlightMacros);
  50. }
  51. void HTMLPrinter::Initialize(ASTContext &context) {
  52. R.setSourceMgr(context.getSourceManager(), context.getLangOpts());
  53. }
  54. void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
  55. if (PP.getDiagnostics().hasErrorOccurred())
  56. return;
  57. // Format the file.
  58. FileID FID = R.getSourceMgr().getMainFileID();
  59. const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
  60. const char* Name;
  61. // In some cases, in particular the case where the input is from stdin,
  62. // there is no entry. Fall back to the memory buffer for a name in those
  63. // cases.
  64. if (Entry)
  65. Name = Entry->getName();
  66. else
  67. Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier();
  68. html::AddLineNumbers(R, FID);
  69. html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name);
  70. // If we have a preprocessor, relex the file and syntax highlight.
  71. // We might not have a preprocessor if we come from a deserialized AST file,
  72. // for example.
  73. if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP);
  74. if (HighlightMacros) html::HighlightMacros(R, FID, PP);
  75. html::EscapeText(R, FID, false, true);
  76. // Emit the HTML.
  77. const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID);
  78. char *Buffer = new char[RewriteBuf.size()]; // HLSL Change: Use overridable operator new
  79. std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer);
  80. Out->write(Buffer, RewriteBuf.size());
  81. delete[] Buffer; // HLSL Change: Use overridable operator delete
  82. }